質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

Q&A

1回答

404閲覧

Ai millisについて 「オセロ」

peperontino

総合スコア0

Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

0グッド

0クリップ

投稿2022/07/01 16:30

オセロを作成しています。
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> class Board{ int cellSize = 80; ArrayList<ArrayList> cells = new ArrayList();

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;
}
}

<Cell> class Cell{ static final int Size = 80; static final int Black = 1; static final int White = -1;

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> class Ai { Board board; int stone;

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;
}
}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2022/07/01 23:04

このままではコードが読みづらいので、質問を編集し、</>(コードの挿入)ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
guest

回答1

0

この方のコードですかね?
https://qiita.com/sawamur@github/items/7cd17a68d7db8a4a4ca0
https://github.com/sawamur/reversi-processing

というかせっかく2秒待ってから敵AIを動かすコードにもうすでになってるのに
その前にさらに敵AIを動かしてるから敵が2手打つ反則コードになってますね。

投稿2022/07/01 19:30

編集2022/07/02 00:50
RiaFeed

総合スコア2701

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問