作品:オセロとCPU対戦
この回でできるようになること
相手の石を、はさんで、ひっくり返す。オセロを、作ります。そして、その相手をしてくれる、コンピュータの打ち手も、自分で作ります。コンピュータは、どうやって、次の一手を決めるのか。その、いちばん基本の考え方に、触れる回です。
let SIZE = 8;
let CELL = 50;
// まわりの8方向。行のずれと、列のずれ
let DR = [-1, -1, -1, 0, 0, 1, 1, 1];
let DC = [-1, 0, 1, -1, 1, -1, 0, 1];
// マスごとの、価値。角は高く、角の隣は低い
let WEIGHTS = [
[100, -20, 10, 5, 5, 10, -20, 100],
[-20, -40, 1, 1, 1, 1, -40, -20],
[10, 1, 5, 2, 2, 5, 1, 10],
[5, 1, 2, 1, 1, 2, 1, 5],
[5, 1, 2, 1, 1, 2, 1, 5],
[10, 1, 5, 2, 2, 5, 1, 10],
[-20, -40, 1, 1, 1, 1, -40, -20],
[100, -20, 10, 5, 5, 10, -20, 100]
];
let board = [];
let turn = 1;
let scene = 'play';
let message = '';
let cpuWait = 0;
function setup() {
createCanvas(400, 460);
startGame();
}
function startGame() {
for (let row = 0; row < SIZE; row++) {
board[row] = [];
for (let col = 0; col < SIZE; col++) {
board[row][col] = 0;
}
}
board[3][3] = 2;
board[3][4] = 1;
board[4][3] = 1;
board[4][4] = 2;
turn = 1;
scene = 'play';
message = 'あなたの番です(黒)';
}
// (row, col) に color の石を置いたとき、(dr, dc) の方向で、返せる石の数
function flipsInDirection(row, col, dr, dc, color) {
let other = 3 - color;
let r = row + dr;
let c = col + dc;
let count = 0;
for (let i = 0; i < SIZE; i++) {
if (r < 0 || r >= SIZE || c < 0 || c >= SIZE) {
return 0;
}
if (board[r][c] === other) {
count += 1;
r += dr;
c += dc;
} else if (board[r][c] === color) {
return count;
} else {
return 0;
}
}
return 0;
}
// (row, col) に置いたとき、全部で、いくつ返せるか。置けないときは 0
function countFlips(row, col, color) {
if (board[row][col] !== 0) {
return 0;
}
let total = 0;
for (let d = 0; d < 8; d++) {
total += flipsInDirection(row, col, DR[d], DC[d], color);
}
return total;
}
// 石を置いて、はさんだ石を返す
function placeStone(row, col, color) {
for (let d = 0; d < 8; d++) {
let n = flipsInDirection(row, col, DR[d], DC[d], color);
for (let i = 1; i <= n; i++) {
board[row + DR[d] * i][col + DC[d] * i] = color;
}
}
board[row][col] = color;
}
// color に、置ける場所が、1つでもあるか
function hasMove(color) {
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (countFlips(row, col, color) > 0) {
return true;
}
}
}
return false;
}
function countStones(color) {
let count = 0;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (board[row][col] === color) {
count += 1;
}
}
}
return count;
}
// 次の番へ進める。置けないときは、パス。どちらも置けなければ、終わり
function nextTurn() {
let other = 3 - turn;
if (hasMove(other)) {
turn = other;
if (turn === 1) {
message = 'あなたの番です(黒)';
} else {
message = 'コンピュータが、考えています';
}
} else if (hasMove(turn)) {
if (turn === 1) {
message = 'コンピュータは、パス。もう一度、あなたの番';
} else {
message = 'あなたは、パス。コンピュータの番';
}
} else {
scene = 'result';
let black = countStones(1);
let white = countStones(2);
if (black > white) {
message = 'あなたの勝ち! クリックで、もう一度';
} else if (black < white) {
message = 'コンピュータの勝ち。クリックで、もう一度';
} else {
message = '引き分け。クリックで、もう一度';
}
}
if (turn === 2) {
cpuWait = 40;
}
}
// コンピュータが、いちばん評価の高い場所に置く
function cpuMove() {
let bestValue = -9999;
let bestRow = -1;
let bestCol = -1;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
let n = countFlips(row, col, 2);
if (n > 0) {
let value = n + WEIGHTS[row][col];
if (value > bestValue) {
bestValue = value;
bestRow = row;
bestCol = col;
}
}
}
}
if (bestRow >= 0) {
placeStone(bestRow, bestCol, 2);
}
nextTurn();
}
function draw() {
background(30, 30, 60);
// コンピュータの番なら、少し待ってから、置く
if (scene === 'play' && turn === 2) {
cpuWait -= 1;
if (cpuWait <= 0) {
cpuMove();
}
}
// 盤面
stroke(20, 70, 40);
strokeWeight(2);
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
fill(40, 130, 80);
rect(col * CELL, row * CELL, CELL, CELL);
let x = col * CELL + CELL / 2;
let y = row * CELL + CELL / 2;
if (board[row][col] === 1) {
fill(20);
circle(x, y, CELL * 0.8);
} else if (board[row][col] === 2) {
fill(245);
circle(x, y, CELL * 0.8);
} else if (scene === 'play' && turn === 1 && countFlips(row, col, 1) > 0) {
// 置ける場所の、目印
noStroke();
fill(255, 255, 255, 90);
circle(x, y, CELL * 0.25);
stroke(20, 70, 40);
}
}
}
// 下の表示
noStroke();
fill(255);
textSize(16);
text(`黒: ${countStones(1)} 白: ${countStones(2)}`, 12, 425);
text(message, 12, 449);
}
function mousePressed() {
if (scene === 'result') {
startGame();
} else if (turn === 1) {
let col = floor(mouseX / CELL);
let row = floor(mouseY / CELL);
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
if (countFlips(row, col, 1) > 0) {
placeStone(row, col, 1);
nextTurn();
}
}
}
}
あなたは、黒です。小さな白い点が、置ける場所です。クリックすると、石が置かれ、はさんだ白い石が、黒に変わります。少し待つと、コンピュータ(白)が、石を置きます。置ける場所がなくなると、終わりです。石の多いほうが、勝ちです。
この回が終わると、次のことができるようになります。
- 「はさんで返す」という決まりを、関数に分けて、作る
- 8つの方向を、配列を使って、まとめて調べる
- 置ける場所を調べ、表示する
- 番の交代、パス、終了を、正しく進める
- 「すべての手を試して、点を付け、いちばんよい手を選ぶ」という方法で、コンピュータの打ち手を作る
ゲームを、設計する
盤面
盤面は、8 × 8 の、二次元配列です。
| 値 | 意味 |
|---|---|
| 0 | 何もない |
| 1 | 黒い石(あなた) |
| 2 | 白い石(コンピュータ) |
番(どちらが打つか)も、同じ数で、表します。turn が 1 なら、黒の番。2 なら、白の番です。
相手の色は、3 - 色 で、求められます。3 − 1 = 2、3 − 2 = 1 です。if で分けなくても、引き算1つで、相手が、わかります。
決まりを、関数に分ける
オセロの決まりは、こうです。
- 石は、相手の石を、はさめる場所にしか、置けない
- 置いたら、はさんだ相手の石を、すべて、自分の色に返す
- はさむ方向は、縦、横、斜めの、8方向
これを、小さな関数に、分けていきます。小さな問いに答える関数を作り、それを使って、大きな問いに答える関数を、作ります。
| 関数 | 答える問い | 使う関数 |
|---|---|---|
flipsInDirection |
ここに置くと、ある1方向で、いくつ返せるか | なし |
countFlips |
ここに置くと、全部で、いくつ返せるか | flipsInDirection |
placeStone |
石を置いて、返す | flipsInDirection |
hasMove |
その色に、置ける場所が、1つでもあるか | countFlips |
cpuMove |
コンピュータにとって、いちばんよい場所は、どこか | countFlips |
いちばん下にある、flipsInDirection が、土台です。これさえ、正しく作れば、あとは、それを組み合わせるだけです。
ステップ1:盤面と、石を描く
盤面を作り、最初の4つの石を置いて、描きます。クリックで、石を置けるようにもします。まだ、決まりは、ありません。
let SIZE = 8;
let CELL = 50;
let board = [];
let turn = 1;
function setup() {
createCanvas(400, 400);
for (let row = 0; row < SIZE; row++) {
board[row] = [];
for (let col = 0; col < SIZE; col++) {
board[row][col] = 0;
}
}
board[3][3] = 2;
board[3][4] = 1;
board[4][3] = 1;
board[4][4] = 2;
}
function draw() {
background(30, 30, 60);
stroke(20, 70, 40);
strokeWeight(2);
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
fill(40, 130, 80);
rect(col * CELL, row * CELL, CELL, CELL);
let x = col * CELL + CELL / 2;
let y = row * CELL + CELL / 2;
if (board[row][col] === 1) {
fill(20);
circle(x, y, CELL * 0.8);
} else if (board[row][col] === 2) {
fill(245);
circle(x, y, CELL * 0.8);
}
}
}
}
function mousePressed() {
let col = floor(mouseX / CELL);
let row = floor(mouseY / CELL);
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
if (board[row][col] === 0) {
board[row][col] = turn;
turn = 3 - turn;
}
}
}
緑の盤面の真ん中に、黒と白の石が、2つずつ、置かれています。空いたマスをクリックすると、黒、白、黒、と、交互に、石が置かれます。
クリックされた位置を、マスの番号に、直しています。
let col = floor(mouseX / CELL);
let row = floor(mouseY / CELL);
マスの大きさで割り、小数点以下を切り捨てます。そのあと、番号が、盤面の範囲の中にあるかを、確かめてから、配列を使います。
turn = 3 - turn; で、番を、交代しています。
ステップ2:1つの方向で、いくつ返せるか
決まりの土台になる、関数を作ります。
「(row, col) に、color の石を置いたとき、(dr, dc) の方向で、いくつ返せるか」
方向は、1歩進むときの、行のずれと、列のずれで、表します。
| 方向 | dr |
dc |
|---|---|---|
| 右 | 0 | 1 |
| 下 | 1 | 0 |
| 右下 | 1 | 1 |
| 左上 | −1 | −1 |
1歩ずつ、進みながら、数える
置く場所から、その方向へ、1マスずつ、進みます。進んだ先のマスに、何があるかで、することが、決まります。
| 進んだ先 | すること |
|---|---|
| 盤面の外 | はさめない。0 を返して、終わり |
| 相手の石 | 数を、1 増やして、もう1歩、進む |
| 自分の石 | はさめた。数えた数を返して、終わり |
| 何もない | はさめない。0 を返して、終わり |
相手の石が、続いている間は、数えながら、進みます。その先で、自分の石に、たどり着けば、間の石を、はさめたことになります。自分の石に、たどり着く前に、盤面の外に出たり、空のマスに当たったりしたら、はさめていません。
function flipsInDirection(row, col, dr, dc, color) {
let other = 3 - color;
let r = row + dr;
let c = col + dc;
let count = 0;
for (let i = 0; i < SIZE; i++) {
if (r < 0 || r >= SIZE || c < 0 || c >= SIZE) {
return 0;
}
if (board[r][c] === other) {
count += 1;
r += dr;
c += dc;
} else if (board[r][c] === color) {
return count;
} else {
return 0;
}
}
return 0;
}
r と c は、いま、調べているマスです。置く場所の、1歩先から、始めます。相手の石だったら、r += dr; c += dc; で、さらに、1歩、進めます。
ループは、最大で 8 回です。盤面は 8 マスなので、それ以上、進むことは、ありません。実際には、途中の return で、関数を、抜けます。
すぐ隣が、自分の石のとき
置く場所の、すぐ隣が、自分の石だったら、どうなるでしょう。
1歩目で、「自分の石」に当たり、return count; で、返ります。このとき、count は、まだ 0 です。間に、相手の石が、1つもないので、返せる数は、0 です。正しい答えが、自然に、出ます。
ステップ3:8方向を、まとめて調べる
8つの方向を、2つの配列に、まとめておきます。
let DR = [-1, -1, -1, 0, 0, 1, 1, 1];
let DC = [-1, 0, 1, -1, 1, -1, 0, 1];
同じ番号の要素を、組にして、1つの方向を、表します。
| 番号 | DR |
DC |
方向 |
|---|---|---|---|
| 0 | −1 | −1 | 左上 |
| 1 | −1 | 0 | 上 |
| 2 | −1 | 1 | 右上 |
| 3 | 0 | −1 | 左 |
| 4 | 0 | 1 | 右 |
| 5 | 1 | −1 | 左下 |
| 6 | 1 | 0 | 下 |
| 7 | 1 | 1 | 右下 |
これで、8方向を、ループ1つで、回れます。
function countFlips(row, col, color) {
if (board[row][col] !== 0) {
return 0;
}
let total = 0;
for (let d = 0; d < 8; d++) {
total += flipsInDirection(row, col, DR[d], DC[d], color);
}
return total;
}
すでに石があるマスには、置けないので、最初に、0 を返します。そうでなければ、8方向のそれぞれで、返せる数を求め、合計します。
方向を、配列にしていなければ、flipsInDirection を呼ぶ行を、8回、書くことに、なります。配列にしておくと、同じ処理を、8方向に、繰り返すことが、ループで、書けます。
置ける場所とは
オセロでは、「1つ以上、返せる場所」にしか、置けません。つまり、次の条件が、「置ける」の意味です。
countFlips(row, col, color) > 0
「置けるか」を調べる、別の関数は、いりません。countFlips が、その答えも、兼ねています。
置ける場所を、表示する
盤面を描くとき、空のマスについて、countFlips を調べ、0 より大きければ、小さな点を、描きます。
let SIZE = 8;
let CELL = 50;
let board = [];
let turn = 1;
// まわりの8方向。行のずれと、列のずれ
let DR = [-1, -1, -1, 0, 0, 1, 1, 1];
let DC = [-1, 0, 1, -1, 1, -1, 0, 1];
function setup() {
createCanvas(400, 400);
for (let row = 0; row < SIZE; row++) {
board[row] = [];
for (let col = 0; col < SIZE; col++) {
board[row][col] = 0;
}
}
board[3][3] = 2;
board[3][4] = 1;
board[4][3] = 1;
board[4][4] = 2;
}
function draw() {
background(30, 30, 60);
stroke(20, 70, 40);
strokeWeight(2);
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
fill(40, 130, 80);
rect(col * CELL, row * CELL, CELL, CELL);
let x = col * CELL + CELL / 2;
let y = row * CELL + CELL / 2;
if (board[row][col] === 1) {
fill(20);
circle(x, y, CELL * 0.8);
} else if (board[row][col] === 2) {
fill(245);
circle(x, y, CELL * 0.8);
} else if (countFlips(row, col, turn) > 0) {
noStroke();
fill(255, 255, 255, 90);
circle(x, y, CELL * 0.25);
stroke(20, 70, 40);
}
}
}
}
// (row, col) に color の石を置いたとき、(dr, dc) の方向で、返せる石の数
function flipsInDirection(row, col, dr, dc, color) {
let other = 3 - color;
let r = row + dr;
let c = col + dc;
let count = 0;
for (let i = 0; i < SIZE; i++) {
if (r < 0 || r >= SIZE || c < 0 || c >= SIZE) {
return 0;
}
if (board[r][c] === other) {
count += 1;
r += dr;
c += dc;
} else if (board[r][c] === color) {
return count;
} else {
return 0;
}
}
return 0;
}
// (row, col) に置いたとき、全部で、いくつ返せるか。置けないときは 0
function countFlips(row, col, color) {
if (board[row][col] !== 0) {
return 0;
}
let total = 0;
for (let d = 0; d < 8; d++) {
total += flipsInDirection(row, col, DR[d], DC[d], color);
}
return total;
}
最初の盤面で、黒が置ける場所が、4つ、白い点で、示されます。
まだ、クリックしても、石は、置けません。調べる関数だけを、先に作って、正しく動いているかを、目で、確かめています。
数でも、確かめられます。setup の最後に、print(countFlips(2, 3, 1)); と書くと、コンソールに 1 と表示されます。上から3行目、左から4列目に、黒を置くと、石を1つ返せる、という意味です。print(countFlips(0, 0, 1)); は、0 です。角には、まだ、置けません。関数が返す値を、いくつかのマスで表示して、盤面の白い点と合っているかを、見比べます。
ステップ4:石を置いて、返す
置ける場所が、わかるようになりました。次は、実際に置いて、返します。
function placeStone(row, col, color) {
for (let d = 0; d < 8; d++) {
let n = flipsInDirection(row, col, DR[d], DC[d], color);
for (let i = 1; i <= n; i++) {
board[row + DR[d] * i][col + DC[d] * i] = color;
}
}
board[row][col] = color;
}
8方向のそれぞれについて、まず、いくつ返せるかを、flipsInDirection で、求めます。それが、n です。
そして、その方向へ、1歩目から n 歩目までのマスを、自分の色に、書き換えます。
board[row + DR[d] * i][col + DC[d] * i] = color;
DR[d] * i は、その方向へ、i 歩進んだときの、行のずれです。i が 1 なら、1歩先。2 なら、2歩先です。
最後に、置いた場所そのものに、石を、置きます。
数える関数と、返す関数が、同じ土台を使う
countFlips と placeStone は、どちらも、flipsInDirection を、使っています。
「いくつ返せるか」を数えることと、「実際に返す」ことは、同じ決まりに、基づいています。その決まりを、1つの関数に、まとめておいたので、2つが、食い違うことは、ありません。「置けると表示されたのに、置いたら、返らない」という間違いが、起きません。
let SIZE = 8;
let CELL = 50;
let board = [];
let turn = 1;
// まわりの8方向。行のずれと、列のずれ
let DR = [-1, -1, -1, 0, 0, 1, 1, 1];
let DC = [-1, 0, 1, -1, 1, -1, 0, 1];
function setup() {
createCanvas(400, 400);
for (let row = 0; row < SIZE; row++) {
board[row] = [];
for (let col = 0; col < SIZE; col++) {
board[row][col] = 0;
}
}
board[3][3] = 2;
board[3][4] = 1;
board[4][3] = 1;
board[4][4] = 2;
}
function draw() {
background(30, 30, 60);
stroke(20, 70, 40);
strokeWeight(2);
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
fill(40, 130, 80);
rect(col * CELL, row * CELL, CELL, CELL);
let x = col * CELL + CELL / 2;
let y = row * CELL + CELL / 2;
if (board[row][col] === 1) {
fill(20);
circle(x, y, CELL * 0.8);
} else if (board[row][col] === 2) {
fill(245);
circle(x, y, CELL * 0.8);
} else if (countFlips(row, col, turn) > 0) {
noStroke();
fill(255, 255, 255, 90);
circle(x, y, CELL * 0.25);
stroke(20, 70, 40);
}
}
}
}
// (row, col) に color の石を置いたとき、(dr, dc) の方向で、返せる石の数
function flipsInDirection(row, col, dr, dc, color) {
let other = 3 - color;
let r = row + dr;
let c = col + dc;
let count = 0;
for (let i = 0; i < SIZE; i++) {
if (r < 0 || r >= SIZE || c < 0 || c >= SIZE) {
return 0;
}
if (board[r][c] === other) {
count += 1;
r += dr;
c += dc;
} else if (board[r][c] === color) {
return count;
} else {
return 0;
}
}
return 0;
}
// (row, col) に置いたとき、全部で、いくつ返せるか。置けないときは 0
function countFlips(row, col, color) {
if (board[row][col] !== 0) {
return 0;
}
let total = 0;
for (let d = 0; d < 8; d++) {
total += flipsInDirection(row, col, DR[d], DC[d], color);
}
return total;
}
// 石を置いて、はさんだ石を返す
function placeStone(row, col, color) {
for (let d = 0; d < 8; d++) {
let n = flipsInDirection(row, col, DR[d], DC[d], color);
for (let i = 1; i <= n; i++) {
board[row + DR[d] * i][col + DC[d] * i] = color;
}
}
board[row][col] = color;
}
function mousePressed() {
let col = floor(mouseX / CELL);
let row = floor(mouseY / CELL);
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
if (countFlips(row, col, turn) > 0) {
placeStone(row, col, turn);
turn = 3 - turn;
}
}
}
置ける場所をクリックすると、石が置かれ、はさんだ石が、返ります。番が、交代し、今度は、白が置ける場所が、示されます。2人で、交互に、遊べます。
クイズ
placeStone の最後の行、board[row][col] = color; を、関数の最初に、移したとします。何か、困ることは、起きるでしょうか?
▶ 答えを確認する
困りません。同じ結果に、なります。
flipsInDirection は、置く場所の1歩先から、調べ始めます。置く場所そのもの(board[row][col])の値は、見ていません。だから、先に、石を置いておいても、あとで置いても、返せる数は、変わりません。
ただし、countFlips は、違います。countFlips は、最初に、「そのマスが、空か」を、調べています。石を置いたあとで、同じ場所の countFlips を呼ぶと、0 が返ります。「どの関数が、どのマスを、見ているか」を、意識しておくと、こうした違いに、気づけます。
ステップ5:番を進める。パスと、終わり
ここまでの作りでは、番を、ただ、交代していました。オセロには、パスが、あります。
| 状況 | 起きること |
|---|---|
| 相手に、置ける場所がある | 相手の番になる |
| 相手には、置ける場所がないが、自分にはある | 相手はパス。もう一度、自分の番 |
| どちらにも、置ける場所がない | 終わり。石の数を、比べる |
盤面が、すべて埋まったときも、「どちらにも、置ける場所がない」に、含まれます。
置ける場所が、1つでもあるか
function hasMove(color) {
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (countFlips(row, col, color) > 0) {
return true;
}
}
}
return false;
}
すべてのマスを調べ、置ける場所が、1つでも見つかれば、その場で、true を返します。最後まで、見つからなければ、false です。
番を進める関数
function nextTurn() {
let other = 3 - turn;
if (hasMove(other)) {
turn = other;
} else if (hasMove(turn)) {
// 相手はパス。番は、変えない
} else {
scene = 'result';
}
}
表の3つの場合を、そのまま、if と else if に、しています。完成したプログラムでは、それぞれの場合に、画面に出す言葉(message)も、決めています。
石を置いたら、必ず、この nextTurn を、呼びます。人が置いたあとも、コンピュータが置いたあとも、同じです。
ステップ6:コンピュータの打ち手を作る
いよいよ、相手を、作ります。
コンピュータは、どう考えるか
コンピュータは、人のように、「この辺が、よさそうだ」と、ひらめくことは、できません。その代わり、速く、もれなく、調べることが、得意です。
そこで、次の方法を、とります。
| 順番 | すること |
|---|---|
| 1 | 置ける場所を、すべて、調べる |
| 2 | それぞれの場所に、「どのくらい、よい手か」という、点を付ける |
| 3 | 点が、いちばん高い場所に、置く |
すべての手を試し、点を付けて、比べる。これが、コンピュータに、ゲームをさせるときの、いちばん基本の考え方です。
問題は、点の付け方です。ここに、作る人の、知恵が、入ります。
最初の考え:たくさん返せる手が、よい手
いちばん素直な考えは、「たくさん返せる手ほど、よい」です。返せる数は、countFlips で、わかります。
let value = countFlips(row, col, 2);
これでも、コンピュータは、動きます。しかし、あまり、強くありません。
オセロでは、途中で、たくさん取っても、あとで、まとめて取り返されることが、よくあります。目先の数ばかりを追う打ち方は、弱いのです。
場所には、価値がある
オセロを、少し遊ぶと、気づくことが、あります。角は、とても強い場所です。角に置いた石は、もう、はさまれることが、ありません。二度と、返されません。
逆に、角の隣は、危ない場所です。そこに置くと、相手に、角を取られる、きっかけを、与えてしまいます。
この知識を、マスごとの、価値の表として、コンピュータに、教えます。
let WEIGHTS = [
[100, -20, 10, 5, 5, 10, -20, 100],
[-20, -40, 1, 1, 1, 1, -40, -20],
[10, 1, 5, 2, 2, 5, 1, 10],
[5, 1, 2, 1, 1, 2, 1, 5],
[5, 1, 2, 1, 1, 2, 1, 5],
[10, 1, 5, 2, 2, 5, 1, 10],
[-20, -40, 1, 1, 1, 1, -40, -20],
[100, -20, 10, 5, 5, 10, -20, 100]
];
| 場所 | 価値 | 理由 |
|---|---|---|
| 角 | 100 | 返されない。とても強い |
| 角の、縦横の隣 | −20 | 相手に、角を取られやすくなる |
| 角の、斜めの隣 | −40 | さらに、危ない |
| 辺 | 5 〜 10 | 返されにくい |
| 中のほう | 1 〜 5 | ふつう |
この表は、盤面と、同じ形の、二次元配列です。WEIGHTS[row][col] で、そのマスの価値が、取り出せます。
点を、付ける
手の点を、「返せる数 + 場所の価値」に、します。
let value = n + WEIGHTS[row][col];
角に置ける手は、1つしか返せなくても、1 + 100 = 101 点です。角の斜め隣は、10 個返せても、10 − 40 = −30 点です。コンピュータは、角を、何よりも優先し、危ない場所を、避けるようになります。
いちばん高い点の手を、探す
function cpuMove() {
let bestValue = -9999;
let bestRow = -1;
let bestCol = -1;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
let n = countFlips(row, col, 2);
if (n > 0) {
let value = n + WEIGHTS[row][col];
if (value > bestValue) {
bestValue = value;
bestRow = row;
bestCol = col;
}
}
}
}
if (bestRow >= 0) {
placeStone(bestRow, bestCol, 2);
}
nextTurn();
}
「これまでで、いちばんよい手」を、変数に、覚えておきながら、すべてのマスを、調べます。
| 変数 | 意味 |
|---|---|
bestValue |
これまでに見つけた、いちばん高い点 |
bestRow、bestCol |
その手の場所 |
置ける場所(n > 0)が見つかるたびに、点を計算し、bestValue より高ければ、記録を、塗り替えます。すべてを調べ終えたとき、変数に残っているのが、いちばんよい手です。
bestValue を、最初に、−9999 という、とても小さな数に、しているのが、大事です。点は、マイナスにも、なります。0 から始めると、マイナスの点の手しかないときに、どの手も、選ばれなくなります。どんな手の点よりも、小さな数から、始めます。
少し待ってから、置く
コンピュータは、一瞬で、手を決めます。人が置いた、その瞬間に、白い石も、置かれてしまうと、何が起きたのか、わかりません。
そこで、少し、間を、置きます。
if (scene === 'play' && turn === 2) {
cpuWait -= 1;
if (cpuWait <= 0) {
cpuMove();
}
}
コンピュータの番になったとき、cpuWait を 40 にしておきます。draw の中で、毎フレーム、1 ずつ減らし、0 になったら、手を打ちます。40 フレームは、約 0.7 秒です。「考えている」ように、見えます。
完成
let SIZE = 8;
let CELL = 50;
// まわりの8方向。行のずれと、列のずれ
let DR = [-1, -1, -1, 0, 0, 1, 1, 1];
let DC = [-1, 0, 1, -1, 1, -1, 0, 1];
// マスごとの、価値。角は高く、角の隣は低い
let WEIGHTS = [
[100, -20, 10, 5, 5, 10, -20, 100],
[-20, -40, 1, 1, 1, 1, -40, -20],
[10, 1, 5, 2, 2, 5, 1, 10],
[5, 1, 2, 1, 1, 2, 1, 5],
[5, 1, 2, 1, 1, 2, 1, 5],
[10, 1, 5, 2, 2, 5, 1, 10],
[-20, -40, 1, 1, 1, 1, -40, -20],
[100, -20, 10, 5, 5, 10, -20, 100]
];
let board = [];
let turn = 1;
let scene = 'play';
let message = '';
let cpuWait = 0;
function setup() {
createCanvas(400, 460);
startGame();
}
function startGame() {
for (let row = 0; row < SIZE; row++) {
board[row] = [];
for (let col = 0; col < SIZE; col++) {
board[row][col] = 0;
}
}
board[3][3] = 2;
board[3][4] = 1;
board[4][3] = 1;
board[4][4] = 2;
turn = 1;
scene = 'play';
message = 'あなたの番です(黒)';
}
// (row, col) に color の石を置いたとき、(dr, dc) の方向で、返せる石の数
function flipsInDirection(row, col, dr, dc, color) {
let other = 3 - color;
let r = row + dr;
let c = col + dc;
let count = 0;
for (let i = 0; i < SIZE; i++) {
if (r < 0 || r >= SIZE || c < 0 || c >= SIZE) {
return 0;
}
if (board[r][c] === other) {
count += 1;
r += dr;
c += dc;
} else if (board[r][c] === color) {
return count;
} else {
return 0;
}
}
return 0;
}
// (row, col) に置いたとき、全部で、いくつ返せるか。置けないときは 0
function countFlips(row, col, color) {
if (board[row][col] !== 0) {
return 0;
}
let total = 0;
for (let d = 0; d < 8; d++) {
total += flipsInDirection(row, col, DR[d], DC[d], color);
}
return total;
}
// 石を置いて、はさんだ石を返す
function placeStone(row, col, color) {
for (let d = 0; d < 8; d++) {
let n = flipsInDirection(row, col, DR[d], DC[d], color);
for (let i = 1; i <= n; i++) {
board[row + DR[d] * i][col + DC[d] * i] = color;
}
}
board[row][col] = color;
}
// color に、置ける場所が、1つでもあるか
function hasMove(color) {
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (countFlips(row, col, color) > 0) {
return true;
}
}
}
return false;
}
function countStones(color) {
let count = 0;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (board[row][col] === color) {
count += 1;
}
}
}
return count;
}
// 次の番へ進める。置けないときは、パス。どちらも置けなければ、終わり
function nextTurn() {
let other = 3 - turn;
if (hasMove(other)) {
turn = other;
if (turn === 1) {
message = 'あなたの番です(黒)';
} else {
message = 'コンピュータが、考えています';
}
} else if (hasMove(turn)) {
if (turn === 1) {
message = 'コンピュータは、パス。もう一度、あなたの番';
} else {
message = 'あなたは、パス。コンピュータの番';
}
} else {
scene = 'result';
let black = countStones(1);
let white = countStones(2);
if (black > white) {
message = 'あなたの勝ち! クリックで、もう一度';
} else if (black < white) {
message = 'コンピュータの勝ち。クリックで、もう一度';
} else {
message = '引き分け。クリックで、もう一度';
}
}
if (turn === 2) {
cpuWait = 40;
}
}
// コンピュータが、いちばん評価の高い場所に置く
function cpuMove() {
let bestValue = -9999;
let bestRow = -1;
let bestCol = -1;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
let n = countFlips(row, col, 2);
if (n > 0) {
let value = n + WEIGHTS[row][col];
if (value > bestValue) {
bestValue = value;
bestRow = row;
bestCol = col;
}
}
}
}
if (bestRow >= 0) {
placeStone(bestRow, bestCol, 2);
}
nextTurn();
}
function draw() {
background(30, 30, 60);
// コンピュータの番なら、少し待ってから、置く
if (scene === 'play' && turn === 2) {
cpuWait -= 1;
if (cpuWait <= 0) {
cpuMove();
}
}
// 盤面
stroke(20, 70, 40);
strokeWeight(2);
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
fill(40, 130, 80);
rect(col * CELL, row * CELL, CELL, CELL);
let x = col * CELL + CELL / 2;
let y = row * CELL + CELL / 2;
if (board[row][col] === 1) {
fill(20);
circle(x, y, CELL * 0.8);
} else if (board[row][col] === 2) {
fill(245);
circle(x, y, CELL * 0.8);
} else if (scene === 'play' && turn === 1 && countFlips(row, col, 1) > 0) {
// 置ける場所の、目印
noStroke();
fill(255, 255, 255, 90);
circle(x, y, CELL * 0.25);
stroke(20, 70, 40);
}
}
}
// 下の表示
noStroke();
fill(255);
textSize(16);
text(`黒: ${countStones(1)} 白: ${countStones(2)}`, 12, 425);
text(message, 12, 449);
}
function mousePressed() {
if (scene === 'result') {
startGame();
} else if (turn === 1) {
let col = floor(mouseX / CELL);
let row = floor(mouseY / CELL);
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
if (countFlips(row, col, 1) > 0) {
placeStone(row, col, 1);
nextTurn();
}
}
}
}
プログラムの流れを、まとめます。
| きっかけ | 起きること |
|---|---|
| 人が、置ける場所をクリック | placeStone で置いて返す。nextTurn で、番を進める |
| コンピュータの番で、待ち時間が切れた | cpuMove で、いちばんよい手を探し、placeStone で置く。nextTurn で、番を進める |
nextTurn |
相手が置けるなら交代。置けないなら、パス。どちらも置けないなら、終わり |
| 終わったあとのクリック | startGame で、最初から |
人の手も、コンピュータの手も、同じ placeStone と nextTurn を、通ります。決まりは、1か所にしか、書かれていません。違うのは、「どこに置くかを、だれが、どう決めるか」だけです。
コンピュータの強さは、点の付け方で決まる
cpuMove の中で、コンピュータの「考え」が、書かれているのは、この1行だけです。
let value = n + WEIGHTS[row][col];
ここを変えると、コンピュータの性格が、変わります。
| 点の付け方 | 性格 |
|---|---|
n |
目先の数を追う。弱い |
n + WEIGHTS[row][col] |
角を大事にし、危ない場所を避ける |
WEIGHTS[row][col] |
数は気にせず、場所だけで決める |
-n + WEIGHTS[row][col] |
あえて、少なく返す。相手の置ける場所を、増やさない打ち方 |
さらに強くするには、「この手を打ったら、相手は、どう打ち返してくるか」まで、先を読む方法が、あります。それも、土台は、同じです。手を試し、点を付け、比べる。点の付け方を、より賢くし、より先まで、試していくことで、コンピュータは、強くなっていきます。
やってみよう
次の3つを作ってください。
- 点の付け方を、返せる数だけに変えて、コンピュータの強さが、どう変わるかを、対戦して確かめる
- コンピュータが、最後に置いた場所に、小さな赤い印を付ける
- ゲームの前半(空のマスが 50 より多い間)は、コンピュータが、なるべく少なく返すようにする。空のマスの数は、
countStones(0)で、数えられる
▶ 1. 返せる数だけで、決める
変えるのは、1行だけです。
let SIZE = 8;
let CELL = 50;
// まわりの8方向。行のずれと、列のずれ
let DR = [-1, -1, -1, 0, 0, 1, 1, 1];
let DC = [-1, 0, 1, -1, 1, -1, 0, 1];
// マスごとの、価値。角は高く、角の隣は低い
let WEIGHTS = [
[100, -20, 10, 5, 5, 10, -20, 100],
[-20, -40, 1, 1, 1, 1, -40, -20],
[10, 1, 5, 2, 2, 5, 1, 10],
[5, 1, 2, 1, 1, 2, 1, 5],
[5, 1, 2, 1, 1, 2, 1, 5],
[10, 1, 5, 2, 2, 5, 1, 10],
[-20, -40, 1, 1, 1, 1, -40, -20],
[100, -20, 10, 5, 5, 10, -20, 100]
];
let board = [];
let turn = 1;
let scene = 'play';
let message = '';
let cpuWait = 0;
function setup() {
createCanvas(400, 460);
startGame();
}
function startGame() {
for (let row = 0; row < SIZE; row++) {
board[row] = [];
for (let col = 0; col < SIZE; col++) {
board[row][col] = 0;
}
}
board[3][3] = 2;
board[3][4] = 1;
board[4][3] = 1;
board[4][4] = 2;
turn = 1;
scene = 'play';
message = 'あなたの番です(黒)';
}
// (row, col) に color の石を置いたとき、(dr, dc) の方向で、返せる石の数
function flipsInDirection(row, col, dr, dc, color) {
let other = 3 - color;
let r = row + dr;
let c = col + dc;
let count = 0;
for (let i = 0; i < SIZE; i++) {
if (r < 0 || r >= SIZE || c < 0 || c >= SIZE) {
return 0;
}
if (board[r][c] === other) {
count += 1;
r += dr;
c += dc;
} else if (board[r][c] === color) {
return count;
} else {
return 0;
}
}
return 0;
}
// (row, col) に置いたとき、全部で、いくつ返せるか。置けないときは 0
function countFlips(row, col, color) {
if (board[row][col] !== 0) {
return 0;
}
let total = 0;
for (let d = 0; d < 8; d++) {
total += flipsInDirection(row, col, DR[d], DC[d], color);
}
return total;
}
// 石を置いて、はさんだ石を返す
function placeStone(row, col, color) {
for (let d = 0; d < 8; d++) {
let n = flipsInDirection(row, col, DR[d], DC[d], color);
for (let i = 1; i <= n; i++) {
board[row + DR[d] * i][col + DC[d] * i] = color;
}
}
board[row][col] = color;
}
// color に、置ける場所が、1つでもあるか
function hasMove(color) {
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (countFlips(row, col, color) > 0) {
return true;
}
}
}
return false;
}
function countStones(color) {
let count = 0;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (board[row][col] === color) {
count += 1;
}
}
}
return count;
}
// 次の番へ進める。置けないときは、パス。どちらも置けなければ、終わり
function nextTurn() {
let other = 3 - turn;
if (hasMove(other)) {
turn = other;
if (turn === 1) {
message = 'あなたの番です(黒)';
} else {
message = 'コンピュータが、考えています';
}
} else if (hasMove(turn)) {
if (turn === 1) {
message = 'コンピュータは、パス。もう一度、あなたの番';
} else {
message = 'あなたは、パス。コンピュータの番';
}
} else {
scene = 'result';
let black = countStones(1);
let white = countStones(2);
if (black > white) {
message = 'あなたの勝ち! クリックで、もう一度';
} else if (black < white) {
message = 'コンピュータの勝ち。クリックで、もう一度';
} else {
message = '引き分け。クリックで、もう一度';
}
}
if (turn === 2) {
cpuWait = 40;
}
}
// コンピュータが、いちばん評価の高い場所に置く
function cpuMove() {
let bestValue = -9999;
let bestRow = -1;
let bestCol = -1;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
let n = countFlips(row, col, 2);
if (n > 0) {
let value = n;
if (value > bestValue) {
bestValue = value;
bestRow = row;
bestCol = col;
}
}
}
}
if (bestRow >= 0) {
placeStone(bestRow, bestCol, 2);
}
nextTurn();
}
function draw() {
background(30, 30, 60);
// コンピュータの番なら、少し待ってから、置く
if (scene === 'play' && turn === 2) {
cpuWait -= 1;
if (cpuWait <= 0) {
cpuMove();
}
}
// 盤面
stroke(20, 70, 40);
strokeWeight(2);
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
fill(40, 130, 80);
rect(col * CELL, row * CELL, CELL, CELL);
let x = col * CELL + CELL / 2;
let y = row * CELL + CELL / 2;
if (board[row][col] === 1) {
fill(20);
circle(x, y, CELL * 0.8);
} else if (board[row][col] === 2) {
fill(245);
circle(x, y, CELL * 0.8);
} else if (scene === 'play' && turn === 1 && countFlips(row, col, 1) > 0) {
// 置ける場所の、目印
noStroke();
fill(255, 255, 255, 90);
circle(x, y, CELL * 0.25);
stroke(20, 70, 40);
}
}
}
// 下の表示
noStroke();
fill(255);
textSize(16);
text(`黒: ${countStones(1)} 白: ${countStones(2)}`, 12, 425);
text(message, 12, 449);
}
function mousePressed() {
if (scene === 'result') {
startGame();
} else if (turn === 1) {
let col = floor(mouseX / CELL);
let row = floor(mouseY / CELL);
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
if (countFlips(row, col, 1) > 0) {
placeStone(row, col, 1);
nextTurn();
}
}
}
}
コンピュータは、いつも、いちばん多く返せる場所に、置くようになります。途中までは、白い石が、どんどん増えて、強そうに見えます。しかし、角の隣にも、ためらわずに置くので、角を、簡単に、取れます。角を取れば、辺の石を、まとめて、返せます。
元の点の付け方に戻して、もう一度、対戦すると、違いが、よくわかります。
▶ 2. 最後に置いた場所に、印を付ける
コンピュータが置いた場所を、lastRow、lastCol に、覚えておきます。盤面を描いたあとに、その場所に、小さな赤い円を、描きます。
let SIZE = 8;
let CELL = 50;
// まわりの8方向。行のずれと、列のずれ
let DR = [-1, -1, -1, 0, 0, 1, 1, 1];
let DC = [-1, 0, 1, -1, 1, -1, 0, 1];
// マスごとの、価値。角は高く、角の隣は低い
let WEIGHTS = [
[100, -20, 10, 5, 5, 10, -20, 100],
[-20, -40, 1, 1, 1, 1, -40, -20],
[10, 1, 5, 2, 2, 5, 1, 10],
[5, 1, 2, 1, 1, 2, 1, 5],
[5, 1, 2, 1, 1, 2, 1, 5],
[10, 1, 5, 2, 2, 5, 1, 10],
[-20, -40, 1, 1, 1, 1, -40, -20],
[100, -20, 10, 5, 5, 10, -20, 100]
];
let board = [];
let turn = 1;
let scene = 'play';
let message = '';
let cpuWait = 0;
let lastRow = -1;
let lastCol = -1;
function setup() {
createCanvas(400, 460);
startGame();
}
function startGame() {
for (let row = 0; row < SIZE; row++) {
board[row] = [];
for (let col = 0; col < SIZE; col++) {
board[row][col] = 0;
}
}
board[3][3] = 2;
board[3][4] = 1;
board[4][3] = 1;
board[4][4] = 2;
turn = 1;
lastRow = -1;
lastCol = -1;
scene = 'play';
message = 'あなたの番です(黒)';
}
// (row, col) に color の石を置いたとき、(dr, dc) の方向で、返せる石の数
function flipsInDirection(row, col, dr, dc, color) {
let other = 3 - color;
let r = row + dr;
let c = col + dc;
let count = 0;
for (let i = 0; i < SIZE; i++) {
if (r < 0 || r >= SIZE || c < 0 || c >= SIZE) {
return 0;
}
if (board[r][c] === other) {
count += 1;
r += dr;
c += dc;
} else if (board[r][c] === color) {
return count;
} else {
return 0;
}
}
return 0;
}
// (row, col) に置いたとき、全部で、いくつ返せるか。置けないときは 0
function countFlips(row, col, color) {
if (board[row][col] !== 0) {
return 0;
}
let total = 0;
for (let d = 0; d < 8; d++) {
total += flipsInDirection(row, col, DR[d], DC[d], color);
}
return total;
}
// 石を置いて、はさんだ石を返す
function placeStone(row, col, color) {
for (let d = 0; d < 8; d++) {
let n = flipsInDirection(row, col, DR[d], DC[d], color);
for (let i = 1; i <= n; i++) {
board[row + DR[d] * i][col + DC[d] * i] = color;
}
}
board[row][col] = color;
}
// color に、置ける場所が、1つでもあるか
function hasMove(color) {
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (countFlips(row, col, color) > 0) {
return true;
}
}
}
return false;
}
function countStones(color) {
let count = 0;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (board[row][col] === color) {
count += 1;
}
}
}
return count;
}
// 次の番へ進める。置けないときは、パス。どちらも置けなければ、終わり
function nextTurn() {
let other = 3 - turn;
if (hasMove(other)) {
turn = other;
if (turn === 1) {
message = 'あなたの番です(黒)';
} else {
message = 'コンピュータが、考えています';
}
} else if (hasMove(turn)) {
if (turn === 1) {
message = 'コンピュータは、パス。もう一度、あなたの番';
} else {
message = 'あなたは、パス。コンピュータの番';
}
} else {
scene = 'result';
let black = countStones(1);
let white = countStones(2);
if (black > white) {
message = 'あなたの勝ち! クリックで、もう一度';
} else if (black < white) {
message = 'コンピュータの勝ち。クリックで、もう一度';
} else {
message = '引き分け。クリックで、もう一度';
}
}
if (turn === 2) {
cpuWait = 40;
}
}
// コンピュータが、いちばん評価の高い場所に置く
function cpuMove() {
let bestValue = -9999;
let bestRow = -1;
let bestCol = -1;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
let n = countFlips(row, col, 2);
if (n > 0) {
let value = n + WEIGHTS[row][col];
if (value > bestValue) {
bestValue = value;
bestRow = row;
bestCol = col;
}
}
}
}
if (bestRow >= 0) {
placeStone(bestRow, bestCol, 2);
lastRow = bestRow;
lastCol = bestCol;
}
nextTurn();
}
function draw() {
background(30, 30, 60);
// コンピュータの番なら、少し待ってから、置く
if (scene === 'play' && turn === 2) {
cpuWait -= 1;
if (cpuWait <= 0) {
cpuMove();
}
}
// 盤面
stroke(20, 70, 40);
strokeWeight(2);
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
fill(40, 130, 80);
rect(col * CELL, row * CELL, CELL, CELL);
let x = col * CELL + CELL / 2;
let y = row * CELL + CELL / 2;
if (board[row][col] === 1) {
fill(20);
circle(x, y, CELL * 0.8);
} else if (board[row][col] === 2) {
fill(245);
circle(x, y, CELL * 0.8);
} else if (scene === 'play' && turn === 1 && countFlips(row, col, 1) > 0) {
// 置ける場所の、目印
noStroke();
fill(255, 255, 255, 90);
circle(x, y, CELL * 0.25);
stroke(20, 70, 40);
}
}
}
// コンピュータが最後に置いた場所に、赤い印
if (lastRow >= 0) {
noStroke();
fill(230, 70, 70);
circle(lastCol * CELL + CELL / 2, lastRow * CELL + CELL / 2, 10);
}
// 下の表示
noStroke();
fill(255);
textSize(16);
text(`黒: ${countStones(1)} 白: ${countStones(2)}`, 12, 425);
text(message, 12, 449);
}
function mousePressed() {
if (scene === 'result') {
startGame();
} else if (turn === 1) {
let col = floor(mouseX / CELL);
let row = floor(mouseY / CELL);
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
if (countFlips(row, col, 1) > 0) {
placeStone(row, col, 1);
nextTurn();
}
}
}
}
コンピュータが置くと、一度に、たくさんの石が、返ります。どこに置いたのかが、わかりにくいことが、あります。印があると、相手の手を、追いやすくなります。
▶ 3. 前半は、少なく返す
空のマスが、50 より多い間は、返せる数に、マイナスを付けて、点にします。たくさん返す手ほど、点が、低くなります。
let SIZE = 8;
let CELL = 50;
// まわりの8方向。行のずれと、列のずれ
let DR = [-1, -1, -1, 0, 0, 1, 1, 1];
let DC = [-1, 0, 1, -1, 1, -1, 0, 1];
// マスごとの、価値。角は高く、角の隣は低い
let WEIGHTS = [
[100, -20, 10, 5, 5, 10, -20, 100],
[-20, -40, 1, 1, 1, 1, -40, -20],
[10, 1, 5, 2, 2, 5, 1, 10],
[5, 1, 2, 1, 1, 2, 1, 5],
[5, 1, 2, 1, 1, 2, 1, 5],
[10, 1, 5, 2, 2, 5, 1, 10],
[-20, -40, 1, 1, 1, 1, -40, -20],
[100, -20, 10, 5, 5, 10, -20, 100]
];
let board = [];
let turn = 1;
let scene = 'play';
let message = '';
let cpuWait = 0;
function setup() {
createCanvas(400, 460);
startGame();
}
function startGame() {
for (let row = 0; row < SIZE; row++) {
board[row] = [];
for (let col = 0; col < SIZE; col++) {
board[row][col] = 0;
}
}
board[3][3] = 2;
board[3][4] = 1;
board[4][3] = 1;
board[4][4] = 2;
turn = 1;
scene = 'play';
message = 'あなたの番です(黒)';
}
// (row, col) に color の石を置いたとき、(dr, dc) の方向で、返せる石の数
function flipsInDirection(row, col, dr, dc, color) {
let other = 3 - color;
let r = row + dr;
let c = col + dc;
let count = 0;
for (let i = 0; i < SIZE; i++) {
if (r < 0 || r >= SIZE || c < 0 || c >= SIZE) {
return 0;
}
if (board[r][c] === other) {
count += 1;
r += dr;
c += dc;
} else if (board[r][c] === color) {
return count;
} else {
return 0;
}
}
return 0;
}
// (row, col) に置いたとき、全部で、いくつ返せるか。置けないときは 0
function countFlips(row, col, color) {
if (board[row][col] !== 0) {
return 0;
}
let total = 0;
for (let d = 0; d < 8; d++) {
total += flipsInDirection(row, col, DR[d], DC[d], color);
}
return total;
}
// 石を置いて、はさんだ石を返す
function placeStone(row, col, color) {
for (let d = 0; d < 8; d++) {
let n = flipsInDirection(row, col, DR[d], DC[d], color);
for (let i = 1; i <= n; i++) {
board[row + DR[d] * i][col + DC[d] * i] = color;
}
}
board[row][col] = color;
}
// color に、置ける場所が、1つでもあるか
function hasMove(color) {
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (countFlips(row, col, color) > 0) {
return true;
}
}
}
return false;
}
function countStones(color) {
let count = 0;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
if (board[row][col] === color) {
count += 1;
}
}
}
return count;
}
// 次の番へ進める。置けないときは、パス。どちらも置けなければ、終わり
function nextTurn() {
let other = 3 - turn;
if (hasMove(other)) {
turn = other;
if (turn === 1) {
message = 'あなたの番です(黒)';
} else {
message = 'コンピュータが、考えています';
}
} else if (hasMove(turn)) {
if (turn === 1) {
message = 'コンピュータは、パス。もう一度、あなたの番';
} else {
message = 'あなたは、パス。コンピュータの番';
}
} else {
scene = 'result';
let black = countStones(1);
let white = countStones(2);
if (black > white) {
message = 'あなたの勝ち! クリックで、もう一度';
} else if (black < white) {
message = 'コンピュータの勝ち。クリックで、もう一度';
} else {
message = '引き分け。クリックで、もう一度';
}
}
if (turn === 2) {
cpuWait = 40;
}
}
// コンピュータが、いちばん評価の高い場所に置く
function cpuMove() {
let bestValue = -9999;
let bestRow = -1;
let bestCol = -1;
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
let n = countFlips(row, col, 2);
if (n > 0) {
let value = n + WEIGHTS[row][col];
if (countStones(0) > 50) {
value = -n + WEIGHTS[row][col];
}
if (value > bestValue) {
bestValue = value;
bestRow = row;
bestCol = col;
}
}
}
}
if (bestRow >= 0) {
placeStone(bestRow, bestCol, 2);
}
nextTurn();
}
function draw() {
background(30, 30, 60);
// コンピュータの番なら、少し待ってから、置く
if (scene === 'play' && turn === 2) {
cpuWait -= 1;
if (cpuWait <= 0) {
cpuMove();
}
}
// 盤面
stroke(20, 70, 40);
strokeWeight(2);
for (let row = 0; row < SIZE; row++) {
for (let col = 0; col < SIZE; col++) {
fill(40, 130, 80);
rect(col * CELL, row * CELL, CELL, CELL);
let x = col * CELL + CELL / 2;
let y = row * CELL + CELL / 2;
if (board[row][col] === 1) {
fill(20);
circle(x, y, CELL * 0.8);
} else if (board[row][col] === 2) {
fill(245);
circle(x, y, CELL * 0.8);
} else if (scene === 'play' && turn === 1 && countFlips(row, col, 1) > 0) {
// 置ける場所の、目印
noStroke();
fill(255, 255, 255, 90);
circle(x, y, CELL * 0.25);
stroke(20, 70, 40);
}
}
}
// 下の表示
noStroke();
fill(255);
textSize(16);
text(`黒: ${countStones(1)} 白: ${countStones(2)}`, 12, 425);
text(message, 12, 449);
}
function mousePressed() {
if (scene === 'result') {
startGame();
} else if (turn === 1) {
let col = floor(mouseX / CELL);
let row = floor(mouseY / CELL);
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
if (countFlips(row, col, 1) > 0) {
placeStone(row, col, 1);
nextTurn();
}
}
}
}
オセロでは、前半に、石を取りすぎると、自分の置ける場所が、減り、相手の置ける場所が、増えます。前半は、石を少なく保ち、後半で、まとめて取る。これは、オセロの、よく知られた考え方です。
countStones は、色を渡すと、その色の石の数を、数える関数です。0 を渡せば、空のマスの数を、数えてくれます。新しい関数を作らなくても、すでにある関数が、使えます。
まとめ
- 盤面も、番も、同じ数(1 と 2)で表すと、相手の色は、
3 - 色で求められる - 決まりを、小さな関数に分ける。「1方向で、いくつ返せるか」という土台の関数から、「全部でいくつ」「置けるか」「置いて返す」を、組み立てる
- 8方向は、行のずれと、列のずれの、2つの配列にしておくと、ループ1つで、回れる
- 「1つ以上、返せる」が、「置ける」の意味。数える関数が、置けるかの判定を、兼ねる
- 数える処理と、返す処理が、同じ関数を使っていれば、2つが、食い違わない
- 石を置いたあとは、相手が置けるか、自分が置けるかを調べて、交代、パス、終了を、決める
- コンピュータの打ち手は、「すべての手を試し、点を付け、いちばん高い手を選ぶ」で作れる
- いちばんよいものを探すときは、「これまでの最高」を変数に覚え、とても小さな値から、始める
- コンピュータの強さと性格は、点の付け方で、決まる