6歳の息子に負けた、げきよわ対戦型オセロを作ってみました。
置ける場所を複数の中から選んだのちに、ランダムにひとつ選んでいるので、弱いのです。。
また一瞬でコマをおくので戸惑います。
let choice = 0; let flipCount = 0; let pass = false; let roboCount = 0; let rC = 0; let timer=0; let timerStart=false; let board = [ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 2, 0, 0, 0], [0, 0, 0, 2, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0] ]; let turn = 1; let s = 50; let col; let row; let kururi = 0; let count = 60; let white = 0; let black = 0; let shiro; let kuro; let maxCount = 0; let button1, button2, button3, button4; let possible = false; function setup() { createCanvas(400, 400); shiro = createDiv("白の番です。"); kuro = createDiv("黒の番です。"); textSize(48); button1 = createButton("コンピューターと対戦"); button2 = createButton("友人同士で対戦"); button3 = createButton("スキップ"); button4 = createButton("最初からやり直す"); button1.position(100, 100); button2.position(100, 200); button1.mousePressed(mode1); button2.mousePressed(mode2); button3.mousePressed(skip); frameRate(6); } function draw() { background(0, 140, 0); if (choice == 0) { //対戦を選択する前 shiro.hide(); kuro.hide(); button3.hide(); button4.hide(); } if(timerStart){ timer++; } if (choice == 1 || choice == 2) { //選択したとき button1.hide(); button2.hide(); button3.show(); button4.show(); if (turn == 1) { //白の番です kuro.hide(); shiro.show(); } if (turn == 2) { //黒の番です kuro.show(); shiro.hide(); } for (let i = 0; i < 8; i++) { for (let j = 0; j < 8; j++) { line(j * 50, 0, j * 50, height); line(0, i * 50, width, i * 50); if (board[i][j] == 1) { fill(255); ellipse(i * 50 + 25, j * 50 + 25, 40, 40); } if (board[i][j] == 2) { fill(0); ellipse(i * 50 + 25, j * 50 + 25, 40, 40); } } } if (count == 0) { //カウントが0になったら結果を表示 result(); } if (turn == 2 && choice == 1) { //ターンが2(黒)で対戦ロボにした場合 let countR = []; //変更することができるrowの場所を保存する変数 let countC = []; //変更することができるcolの場所を保存する変数 let rC = 0; //選択可能な場所を保存していく変数 for (let i = 0; i < 8; i++) { for (let j = 0; j < 8; j++) { if (getpos(i, j) == 0) { //ボード上にコマが置いてないところで check(i, j); //チェック関数で置けるかをチェックしていく if (flipCount == 0) { // 駒が一つも変わらないところは board[row][col] = 0; //何も置かない } else { //もし駒がひっくり返る場所の時 rC++;//選択可能な場所の数を増やす countR[rC] = row; //変更することができるrowの場所を保存する countC[rC] = col; //変更することができるcolの場所を保存する board[countR[rC]][countC[rC]] = 0;//いったん駒は消しておく print(countR[1], countC[1]); } if(rC==0){ //置く場所が無ければスキップ turn = 1; } } } } let choice=int(random(1,rC)); //選択可能な場所からランダムに一つ選ぶ reverseC(countR[choice], countC[choice]);//ひっくり返す。 } } } function reverseC(ro, co) { board[ro][co] = 2; for (let i = -1; i < 2; i++) { for (let j = -1; j < 2; j++) { serachReverse(ro, co, i, j); } } count--; turn = 1; } function check(r, c) { row = r; col = c; kururi = 0; flipCount = 0; board[row][col] = 2; for (let i = -1; i < 2; i++) { for (let j = -1; j < 2; j++) { checkReverse(row, col, i, j); } } } function getpos(x, y) { let xin = x >= 0 && x < 8; let yin = y >= 0 && y < 8; if (xin && yin) { return board[x][y]; } else { return 0; } } function setpos(x, y, num) { let xin = x >= 0 && x < 8; let yin = y >= 0 && y < 8; if (xin && yin) { board[x][y] = num; } } function serachReverse(x, y, vx, vy) { let state = getpos(x, y); let Opponent; if (state == 1) { Opponent = 2; } else { Opponent = 1; } let hit = false; let step = 0; let stepx = x + vx; let stepy = y + vy; while (!hit) { if (getpos(stepx, stepy) == 0) { hit = true; } if (getpos(stepx, stepy) == Opponent) { stepx += vx; stepy += vy; step++; } if (step == 0) { if (getpos(stepx, stepy) == state) { hit = true; } } if (step >= 1) { if (getpos(stepx, stepy) == state) { hit = true; kururi++; let fillx = stepx; let filly = stepy; for (let i = 0; i < step; i++) { fillx -= vx; filly -= vy; setpos(fillx, filly, state); flipCount++; } } } } } function checkReverse(x, y, vx, vy) { let state = getpos(x, y); let Opponent; if (state == 1) { Opponent = 2; } else { Opponent = 1; } let hit = false; let step = 0; let stepx = x + vx; let stepy = y + vy; while (!hit) { if (getpos(stepx, stepy) == 0) { hit = true; } if (getpos(stepx, stepy) == Opponent) { stepx += vx; stepy += vy; step++; } if (step == 0) { if (getpos(stepx, stepy) == state) { hit = true; } } if (step >= 1) { if (getpos(stepx, stepy) == state) { hit = true; kururi++; let fillx = stepx; let filly = stepy; for (let i = 0; i < step; i++) { // fillx -= vx; // filly -= vy; // setpos(fillx, filly, state); flipCount++; } } } } } function mousePressed() { row = floor(mouseX / s); col = floor(mouseY / s); rCount = 0; if (getpos(row, col) == 0) { if (turn == 1) { kururi = 0; board[row][col] = 1; flipCount = 0; for (let i = -1; i < 2; i++) { for (let j = -1; j < 2; j++) { serachReverse(row, col, i, j); } } turn = 2; print("白がひっくり返した数:" + flipCount); if (kururi == 0) { board[row][col] = 0; turn = 1; count++; } count--; } else if (turn == 2 && choice == 2) { kururi = 0; flipCount = 0; board[row][col] = 2; for (let i = -1; i < 2; i++) { for (let j = -1; j < 2; j++) { serachReverse(row, col, i, j); } } turn = 1; print("黒がひっくり返した数:" + flipCount); if (kururi == 0) { board[row][col] = 0; turn = 2; count++; } count--; } } } function result() { for (let i = 0; i < 8; i++) { for (let j = 0; j < 8; j++) { let c = getpos(i, j); if (c == 1) { white++; } else if (c == 2) { black++; } } } if (white > black) { fill(255, 0, 0); text("white Win!", 100, 200); } else { fill(255, 0, 0); text("Black Win!", 100, 200); } } function keyPressed() { if (key == 's') { turn++; if (turn > 2) { turn = 1; } } } function mode1() { choice = 1; } function mode2() { choice = 2; } function skip(){ turn++; if (turn > 2) { turn = 1; } }