我是初学者试图在java中创建一个tictactoe游戏,我需要创建两个特定的方法,一个是对手随机玩,另一个是积极玩“智能”,试图在每个回合中积极获胜。登记/> 我的问题来自主动播放方法,因为我不知道如何从这个开始。
import javax.swing.JOptionPane;
public class Tictactoe
{
String table[][];
boolean winner;
public Tictactoe()
{
table = new String[3][3];
for(int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
table[x][y] = " ";
int xUser;
int yUser;
int xMac;
int yMac;
}
}
}
public void board()
{
System.out.println("-----------");
for(int x = 0; x < 3; x++)
{
System.out.print("| ");
for(int y = 0; y < 3; y++)
{
System.out.print(table[x][y] + " | ");
}
System.out.println();
}
System.out.println("-----------");
}
public void playRandom()
{
int x;
int y;
int xMaq;
int yMaq;
boolean winner = false;
while(!winner)
{
move();
}
if(winner = true)
{
new Tictactoe();
}
public void findWinnerMach()
{
if (table[0][0]== "O" && table[0][1]== "O" && table [0][2]== "O") {
JOptionPane.showMessageDialog(null, "You lose");
winner = true;
board();
System.exit(0);
}
if (table[1][0]== "O" && table[1][1]== "O" && table [1][2]== "O") {
JOptionPane.showMessageDialog(null, "You lose");
winner = true;
board();
System.exit(0);
}
if (table[2][0]== "O" && table[2][1]== "O" && table [2][2]== "O") {
JOptionPane.showMessageDialog(null, "You lose");
winner = true;
board();
System.exit(0);
}
if (tabla[0][0]== "O" && tabla[1][0]== "O" && tabla [2][0]== "O") {
JOptionPane.showMessageDialog(null, "You lose");
winner = true;
board();
System.exit(0);
}
if (table[0][1]== "O" && table[1][1]== "O" && table [2][1]== "O") {
JOptionPane.showMessageDialog(null, "You lose");
winner = true;
board();
System.exit(0);
}
if (table[0][2]== "O" && table[1][2]== "O" && table [2][2]== "O") {
JOptionPane.showMessageDialog(null, "You lose");
winner = true;
board();
System.exit(0);
}
if (table[0][0]== "O" && table[1][1]== "O" && table [2][2]== "O") {
JOptionPane.showMessageDialog(null, "You lose");
winner = true;
board();
System.exit(0);
}
if (table[0][2]== "O" && table[1][1]== "O" && table [2][0]== "O") {
JOptionPane.showMessageDialog(null, "You lose");
winner = true;
board();
System.exit(0);
}
}
public void findWinnerUser()
{
if (table[0][0]== "X" && table[0][1]== "X" && table [0][2]== "X") {
JOptionPane.showMessageDialog(null, "You win!");
winner = true;
board();
System.exit(0);
}
if (table[1][0]== "X" && table[1][1]== "X" && table [1][2]== "X") {
JOptionPane.showMessageDialog(null, "You win!");
winner = true;
board();
System.exit(0);
}
else if (table[2][0]== "X" && table[2][1]== "X" && table [2][2]== "X") {
JOptionPane.showMessageDialog(null, "You win!");
winner = true;
board();
System.exit(0);
}
if (table[0][0]== "X" && table[1][0]== "X" && table [2][0]== "X") {
JOptionPane.showMessageDialog(null, "You win!");
winner = true;
board();
System.exit(0);
}
if (table[0][1]== "X" && table[1][1]== "X" && table [2][1]== "X") {
JOptionPane.showMessageDialog(null, "You win!");
winner = true
board();
System.exit(0);
}
if (table[0][2]== "X" && table[1][2]== "X" && table [2][2]== "X") {
JOptionPane.showMessageDialog(null, "You win!");
winner = true;
board();
System.exit(0);
}
if (table[0][0]== "X" && table[1][1]== "X" && table [2][2]== "X") {
JOptionPane.showMessageDialog(null, "You win!");
winner = true;
board();
System.exit(0);
}
if (table[0][2]== "X" && table[1][1]== "X" && table [2][0]== "X") {
JOptionPane.showMessageDialog(null, "You win!");
winner = true;
board();
System.exit(0);
}
}
public void move()
{
int xUser;
int yUser;
int xMac;
int yMac;
xUser = Integer.parseInt(JOptionPane.showInputDialog("Type a row from 0 to 2"));
yUser = Integer.parseInt(JOptionPane.showInputDialog("Type a column from 0 to 2"));
if(table[xUser][yUser] == "X" || tabla[xUser][yUser] == "O" ) {
xUser = Integer.parseInt(JOptionPane.showInputDialog("Selected slot is taken, select a new row"));
yUser = Integer.parseInt(JOptionPane.showInputDialog("Selected slot is taken, select a new column"));
}
else
{
table[xUser][yUser] = "X";
}
table[xUser][yUser] = "X";
JOptionPane.showMessageDialog(null,"CPU moves");
xMac = (int)(Math.random()*3)+0;
yMac = (int)(Math.random()*3)+0;
if(table[xUser][yUser] == "O" || table[xUser][yUser] == "X" ) {
xUser = (int)(Math.random()*3)+0;
yUser = (int)(Math.random()*3)+0;
table[xUser][yUser] = "O";
}
findWinnerUser();
findWinnerMac();
board();
}
public void playSmart()
{
}
}
大多数情况下仍然有很多工作需要改进,需要修改一些东西,但是,我不想这样做,直到我有一套方法,想法或计划实施这种“智能”方法以防万一到目前为止,我非常错误,不胜感激。