[英]finding a winner in tic tac toe java(returning)
此井字游戲代碼適用於除確定獲勝者之外的所有內容。 它可以確定平局並很好地展示板子。 我覺得問題出在從“檢查”方法返回變量“完成”,但我不知道是什么。 我返回的語法很生疏,所以我不確定是否只是不記得如何訪問返回的變量或其他內容,但是我被卡住了。 任何幫助是極大的贊賞。 先感謝您!
新密碼
import javax.swing.JOptionPane;
public class tictactoe
{
public static void main(String args[])//coordinates = row:column
{
String grid[][] = new String [4][4];//creates 2D array
int w = 0;//initiates counter
int j = 0;//initiates counter
for(w = 1; w <= 3; w++)//counter
{
for(j = 1; j <= 3; j++)///counter
{
grid[w][j] = "[]";//fills the array with "[]" (empty symbol)
}
}
JOptionPane.showMessageDialog(null,"WELCOME to Matt's Fun World of TIC TAC TOE");
JOptionPane.showMessageDialog(null, "Find the number of the box " + "\n" + "that you would like to enter your coordinates.");
board_game(grid, w, j);//sends grid, w and j to the board game method
}
public static void board_game(String grid[][], int w, int j)
{
boolean test = false;//checks which player won the game
boolean done = false;//used to test if the game is done
int player = 1;//used to switch between player 1 and 2
String a = grid[1][1]; String b = grid[1][2]; String c = grid[1][3]; //makes playing board
String d = grid[2][1]; String e = grid[2][2]; String f = grid[2][3];//makes playing board
String g = grid[3][1]; String h = grid[3][2]; String i = grid[3][3];//makes playing board
String board =
(" "+a+" | "+b+" | "+c+" " + "\n" +//makes board
"-------------|---------------|------------- " + "\n" +
" "+d+" | "+e+" | "+f+" " + "\n" +
"-------------|---------------|------------- " + "\n" +
" "+g+" | "+h+" | "+i+" " );
while(!done)
{
a = grid[1][1]; b = grid[1][2]; c = grid[1][3];//makes board be able to use in loop
d = grid[2][1]; e = grid[2][2]; f = grid[2][3];
g = grid[3][1]; h = grid[3][2]; i = grid[3][3];
board =
(" "+a+" | "+b+" | "+c+" " + "\n" +
"-------------|---------------|------------- " + "\n" +
" "+d+" | "+e+" | "+f+" " + "\n" +
"-------------|---------------|------------- " + "\n" +
" "+g+" | "+h+" | "+i+" " );
check(done, grid, a, b, c, d, e, f, g, h, i);
if(player % 2 != 0)//player 1
{
player++;//moves it to next player
JOptionPane.showMessageDialog(null, board);//prints board
String r=JOptionPane.showInputDialog(" PLAYER 1" + "\n" +"Enter the row of your desired move.");
int row = Integer.parseInt(r);//parses which row in desired
String cm=JOptionPane.showInputDialog(" PLAYER 1" + "\n" +"Enter the column of your desired move.");
int column = Integer.parseInt(cm);//parses which column is desired
grid[row][column] = "X";//puts an X in the desired location
test = true;//says player 1 wins if a winner is determined
done = check(done, grid, a, b, c, d, e, f, g, h, i);//sends done, grid, a, b, c, d, e, f, g, h, i
}
else if(player % 2 == 0)//player 2
{
player++;//moves it to next player
JOptionPane.showMessageDialog(null, board);//prints board
String rr=JOptionPane.showInputDialog(" PLAYER 2" + "\n" +"Enter the row of your desired move.");
int row2 = Integer.parseInt(rr);//parses which row is desired
String cc=JOptionPane.showInputDialog(" PLAYER 2" + "\n" +"Enter the column of your desired move.");
int column2 = Integer.parseInt(cc);//parses which column is desired
grid[row2][column2] = "O";//puts an X in the desired location
done = check(done, grid, a, b, c, d, e, f, g, h, i);//sends done, grid, a, b, c, d, e, f, g, h, i
}
}
if(test == true)
{
JOptionPane.showMessageDialog(null, "Player 1 is the winner!");
JOptionPane.showMessageDialog(null, board);//prints the board
}
else if (test == false)
{
JOptionPane.showMessageDialog(null, "Player 2 is the winner!");
JOptionPane.showMessageDialog(null, board);//prints the board
}
}
public static boolean check(boolean done, String grid[][], String a, String b, String c,String d, String e, String f, String g, String h, String i)
{
if(grid[1][1].equals(grid[1][2]) && grid[1][2].equals(grid[1][3]))
{
done = true;//makes game over by making done true
}
else if(grid[2][1].equals(grid[2][2]) && grid[2][2].equals(grid[2][3]))
{
done = true;//makes game over by making done true
}
else if(grid[3][1].equals(grid[3][2]) && grid[3][2].equals(grid[3][3]))
{
done = true;//makes game over by making done true
}
else if(grid[1][1].equals(grid[2][1]) && grid[2][1].equals(grid[3][1]))
{
done = true;//makes game over by making done true
}
else if(grid[1][2].equals(grid[2][2]) && grid[2][2].equals(grid[3][2]))
{
done = true;//makes game over by making done true
}
else if(grid[1][3].equals(grid[2][3]) && grid[2][3].equals(grid[3][3]))
{
done = true;//makes game over by making done true
}
else if(grid[1][1].equals(grid[2][2]) && grid[2][2].equals(grid[3][3]))
{
done = true;//makes game over by making done true
}
else if(grid[1][3].equals(grid[2][2]) && grid[3][1].equals(grid[1][3]))
{
done = true;//makes game over by making done true
}
else
{
int y = 0;//initiates the empty space counter at zero
for(int x = 1; x <= grid.length-1; x++)//counter
{
for(int z = 1; z <= grid.length-1; z++)//counter
{
if(grid[x][z] .equals("[]"))//if space is equal to "[]"
{
y++;//adds one to y(counter)
}
}
}
if(y == 0)//if there are no empty spaces
{
done = true;//game over
JOptionPane.showMessageDialog(null, "IT'S A DRAW!");
}
else
{
done = false;//game isn't over
}
}
return done;//return done
}
}
顯示代碼的獲勝者永遠都無法達到,因為player
總是奇數或偶數-如果不奇數,則偶數。
您的方法check()無法正常工作。 就像我做的一樣,如果玩家獲勝,則逐步調試代碼並觀看check()方法。 那里的邏輯有些問題。
盡管如此,就像羅賓·格林(Robin Green)解釋的那樣:因為前兩個if語句始終為true,所以永遠不會達到您的else if(test ...)情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.