在过去的几天里,我一直在尝试使用Java在tictactoe中实现minmax算法。到目前为止,我的尝试取得了一些成功,但这并不是无与伦比的,并且在放置令牌时会犯很多错误。我希望就如何修复我的代码提出一些建议:How do I accomplish an if/else in mustache.js?
图中蓝色是播放器,红色是AI,蓝色是第一个。在这种情况下,红色应将其令牌放在中间方格中
此处不是阻止玩家红色将其代币放在右上方。
某些东西导致AI非常具有侵略性,并且总是在不考虑其他玩家正在做什么的情况下排队但是我无法弄清楚导致这种情况的原因,因为在某些情况下计算机会阻止玩家。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import com.sun.prism.BasicStroke;
import javafx.scene.text.Font;
public class Main extends JFrame implements MouseListener{
int[][] board = new int[3][3];
boolean turn = true;
int movex = 0;
int currMoves=0;
int movey=0;
int score;
int[][] tempboard = new int[3][3];
int[][] minMax = new int[3][3];
ArrayList<Integer> posMovess = new ArrayList<Integer>();
ArrayList<Integer> posMovesx = new ArrayList<Integer>();
ArrayList<Integer> posMovesy = new ArrayList<Integer>();
public static void main(String[] args) {
new Main().init();
}
public void init(){
setSize(600,600);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setFocusable(true);
addMouseListener(this);
}
int max =0;
public int recttt(int[][] tempboard, int XO){
int score = 0;
posMovess.clear();
posMovesx.clear();
posMovesy.clear();
max= 0;
if(tempboard[0][0] == 1 && tempboard[0][1]==1 && tempboard[0][2]==1 ||
tempboard[1][0] == 1 && tempboard[1][1]==1 && tempboard[1][2]==1 ||
tempboard[2][0] == 1 && tempboard[2][1]==1 && tempboard[2][2]==1 ||
tempboard[0][0] == 1 && tempboard[1][0]==1 && tempboard[2][0]==1 ||
tempboard[0][1] == 1 && tempboard[1][1]==1 && tempboard[2][1]==1 ||
tempboard[0][2] == 1 && tempboard[1][2]==1 && tempboard[2][2]==1 ||
tempboard[0][0] == 1 && tempboard[1][1]==1 && tempboard[2][2]==1 ||
tempboard[0][2] == 1 && tempboard[1][1]==1 && tempboard[2][0]==1 ){
return -1; //if player wins return negative
}else if(tempboard[0][0] == 2 && tempboard[0][1]==2 && tempboard[0][2]==2 ||
tempboard[1][0] == 2 && tempboard[1][1]==2 && tempboard[1][2]==2 ||
tempboard[2][0] == 2 && tempboard[2][1]==2 && tempboard[2][2]==2 ||
tempboard[0][0] == 2 && tempboard[1][0]==2 && tempboard[2][0]==2 ||
tempboard[0][1] == 2 && tempboard[1][1]==2 && tempboard[2][1]==2 ||
tempboard[0][2] == 2 && tempboard[1][2]==2 && tempboard[2][2]==2 ||
tempboard[0][0] == 2 && tempboard[1][1]==2 && tempboard[2][2]==2 ||
tempboard[0][2] == 2 && tempboard[1][1]==2 && tempboard[2][0]==2){
return 1; // if computer wins return positive
}
int count = 0;
for(int x = 0;x<3;x++){
for(int y = 0;y<3;y++){
if(tempboard[x][y] == 0){
count++;
}
}
}
if(count==0){
return 0; //if tie return neutral
}
for(int x =0;x<3;x++){
for(int y = 0;y<3;y++){
if(tempboard[x][y] == 0){
if(XO % 2 == 0){
tempboard[x][y] = 1;
XO+= 1;
posMovess.add(recttt(tempboard,XO));
XO-=1;
posMovesx.add(x);
posMovesy.add(y);
tempboard[x][y] =0;
for(int i = 0;i<posMovess.size();i++){
if(posMovess.get(i) < posMovess.get(max)){
//calculate min for players turn
max = i;
}
}
}
else {
tempboard[x][y] = 2;
XO+=1;
posMovess.add(recttt(tempboard,XO));
XO-=1;
posMovesx.add(x);
posMovesy.add(y);
tempboard[x][y] =0;
for(int i = 0;i<posMovess.size();i++){
if(posMovess.get(i) >posMovess.get(max)){
//calculate max if computers turn
max = i;
}
}
}
}
}
}
return posMovess.get(max);
}
@Override
public void mouseClicked(MouseEvent e){
System.out.println("");
int best = -2;
int temp = 0;
currMoves++;
board[e.getX()/200][e.getY()/200] = 1;
turn = false;
repaint();
for(int x = 0;x < 3;x++){
for(int y = 0;y< 3;y++){
//call minmax algorithm for each available space
if(board[x][y] == 0){
board[x][y] = 2;
temp = recttt(board,1);
System.out.println(temp);
board[x][y] = 0;
if(temp > best){
best = temp;
movex = x;
movey = y;
}
}
}
}
best = 0;
currMoves++;
board[movex][movey] = 2;
turn = true;
repaint();
if(board[0][0] == 1 && board[0][1]==1 && board[0][2]==1 ||
board[1][0] == 1 && board[1][1]==1 && board[1][2]==1 ||
board[2][0] == 1 && board[2][1]==1 && board[2][2]==1 ||
board[0][0] == 1 && board[1][0]==1 && board[2][0]==1 ||
board[0][1] == 1 && board[1][1]==1 && board[2][1]==1 ||
board[0][2] == 1 && board[1][2]==1 && board[2][2]==1 ||
board[0][0] == 1 && board[1][1]==1 && board[2][2]==1 ||
board[0][2] == 1 && board[1][1]==1 && board[2][0]==1 ){
System.out.println("Blue Wins");
}else if(board[0][0] == 2 && board[0][1]==2 && board[0][2]==2 ||
board[1][0] == 2 && board[1][1]==2 && board[1][2]==2 ||
board[2][0] == 2 && board[2][1]==2 && board[2][2]==2 ||
board[0][0] == 2 && board[1][0]==2 && board[2][0]==2 ||
board[0][1] == 2 && board[1][1]==2 && board[2][1]==2 ||
board[0][2] == 2 && board[1][2]==2 && board[2][2]==2 ||
board[0][0] == 2 && board[1][1]==2 && board[2][2]==2 ||
board[0][2] == 2 && board[1][1]==2 && board[2][0]==2){
System.out.println("RedWins");
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
for(int i = 0;i< 3;i++){
g2.drawLine(200*i, 0, 200*i, 600);
}
for(int i = 0;i< 3;i++){
g2.drawLine(0, 200*i, 600, 200*i);
}
for(int x = 0;x<3;x++){
for(int y = 0;y<3;y++){
if(board[x][y] == 1){
g2.setColor(Color.blue);
g2.drawOval(x*200+50, y*200+50, 100, 100);
}
if(board[x][y] == 2){
g2.setColor(Color.red);
g2.drawOval(x*200+50, y*200+50, 100, 100);
}
}
}
}
}