オセロを作成しています。
millisを使い自分が打った後2秒後にAiが自動で打つ設定にしようとしています。
自分が打った後Aiが打つ動作まではできたのですが、2秒待ってくれません。
コードをのせておくのでわかる方教えてください。
<sketch>
Board board;
Ai ai;
int myStone = Cell.Black;
int aiStone = Cell.White;
int turn = Cell.Black;
int aiTime;
void setup(){
size(640,680);
board = new Board();
board.startPosition();
ai = new Ai(board,aiStone);
}
void draw(){
background(255);
board.display();
if(board.getOKCells().size() == 0){ return; }
if(MyTurns()){
showGhost();
}
if(!MyTurns()){
turnForAi();
if(aiTime == 0){
aiTime = millis();
}
if(millis() - aiTime > 2000){
turnForAi();
aiTime = 0;
}
}
}
void showGhost(){
Cell cell = board.getCellAtGhost(mouseX,mouseY);
if(cell != null){
cell.showGhost(myStone);
}
}
boolean MyTurns(){
return(myStone == turn);
}
void turnEnd(){
turn = (myStone == turn) ? aiStone : myStone;
}
void mouseClicked(){
if(!MyTurns()){
return;
}
Cell cell = board.getCellAtGhost(mouseX,mouseY);
ArrayList<Cell> cellsFlip = board.cellsFlipWith(cell,myStone);
if(cellsFlip.size() > 0){
cell.putStone(myStone);
for(Cell o: cellsFlip){ o.flip(); } turnEnd();
}else{
}
}
void turnForAi(){
Cell cell = ai.think();
if(cell == null){
turnEnd();
return;
}
ArrayList<Cell> cellsFlip = board.cellsFlipWith(cell,turn);
cell.putStone(turn);
for(Cell o: cellsFlip){
o.flip();打ち消し線
}
turnEnd();
}
Board(){
for(int i = 0; i < 8; i++){
ArrayList<Cell> row = new ArrayList();
for(int j = 0; j < 8; j++){
row.add(new Cell(j,i));
}
cells.add(row);
}
}
void startPosition(){
getCellAt(3,3).putStone(Cell.Black);
getCellAt(4,4).putStone(Cell.Black);
getCellAt(3,4).putStone(Cell.White);
getCellAt(4,3).putStone(Cell.White);
}
Cell getCellAtGhost(int x,int y){
int numCol = floor(x / cellSize);
int numRow = floor(y / cellSize);
return this.getCellAt(numCol,numRow);
}
Cell getCellAt(int col, int row){
if(col < 0 || col > 7 || row < 0 || row > 7){
return null;
}
return(Cell)cells.get(row).get(col);
}
ArrayList<Cell> cellsFlipWith(Cell cell,int stone){
ArrayList<Cell> cellsFlip = new ArrayList<Cell>();
if(cell.hasStone()){
return cellsFlip;
}
for(int dCol = -1; dCol < 2; dCol++){
for(int dRow = -1; dRow < 2; dRow++){
ArrayList<Cell> cellsInDir = this.getCellsInDirection(cell, dCol, dRow);
ArrayList<Cell> checked = new ArrayList<Cell>();
for(Cell o: cellsInDir){
if(o.stone == 0){
break;
}
if(o.stone != stone){
checked.add(o);
} if(o.stone == stone){ for(Object toFlip: checked){ cellsFlip.add((Cell) toFlip); } break; } } }
}
return cellsFlip;
}
ArrayList<Cell> getCellsInDirection(Cell cellAtStart,int directionCol, int directionRow ) {
ArrayList<Cell> cellsToReturn = new ArrayList<Cell>();
if(directionCol == 0 && directionRow == 0){
return cellsToReturn;
}
int col = cellAtStart.getCol() + directionCol;
int row = cellAtStart.getRow() + directionRow;
Cell nextCell = getCellAt(col,row);
while(nextCell != null){
cellsToReturn.add(nextCell);
col += directionCol;
row += directionRow;
nextCell = getCellAt(col,row);
}
return cellsToReturn;
}
void display() {
for(ArrayList row: cells){
for(Object o: row){
Cell cell = (Cell) o;
cell.display();
}
}
}
ArrayList<Cell> getOKCells() {
ArrayList<Cell> aCells = new ArrayList<Cell>();
for(ArrayList<Cell> row: cells){
for(Cell cell: row){
if(!cell.hasStone()){
aCells.add(cell);
}
}
}
return aCells;
}
}
int x;
int y;
int stone = 0;
int size = Size;
Cell(int x, int y){
this.x = x;
this.y = y;
}
int getCol(){
return this.x;
}
int getRow(){
return this.y;
}
boolean hasStone(){
return (this.stone != 0);
}
void showGhost(int stoneToShow){
if(this.hasStone()){
return;
}
noStroke();
if(stoneToShow == Black){
fill(0,255 * 0.2);
}else{
fill(255,255 * 0.2);
}
ellipse(this.x * size + (size / 2), this.y * size + (size / 2),size * 0.9, size *0.9);
}
void display(){
stroke(0);
fill(0,0.6 * 255,0);
rect(x * size, y * size,size,size);
if(this.stone != 0){
if(this.stone == Black){
fill(0);
}else{
fill(255);
}
noStroke();
ellipse(this.x * size + (size / 2), this.y * size + (size / 2),size * 0.9, size *0.9);
}
}
boolean putStone(int stoneColor){
this.stone = stoneColor;
return true;
}
void flip(){
this.stone *= -1;
}
}
Ai(Board board, int stone) {
this.board = board;
this.stone = stone;
}
Cell think() {
int max = 0;
Cell cellToPut = null;
ArrayList<Cell> candidates = board.getOKCells();
for(Cell cell: candidates) {
ArrayList<Cell> cellsFlip = board.cellsFlipWith(cell, Cell.White);
if(max < cellsFlip.size()){
max = cellsFlip.size();
cellToPut = cell;
}
}
return cellToPut;
}
}
