這個井字棋遊戲是基於用戶和計算機的。 對於這個任務,我應該填寫缺失的部分,這只是完成主要和moveAI方法(其他方法已經預設,我只需要打電話給他們);有評論提供了我需要添加代碼來完成這個tic tac toe遊戲的工作。我相信我所擁有的主要方法幾乎完成,除了評論中的兩條空行之外,我不確定它們是什麼意思,而且我目前仍然堅持讓我的moveAI方法工作。這是我應該隨機生成的地方,在我將X放在某處後,計算機將會移動。我不確定如何編寫一些內容,以檢查計算機移動的位置是否可用並且用戶尚未使用。先謝謝您的幫助!Java二維陣列 - 井字遊戲
import java.util.Random;
import java.util.Scanner;
public class TicTacToeGame
{
public static void main (String[] args)
{
Scanner in = new Scanner (System.in);
char[][] board = new char[ 3 ][ 3 ];
int x, y = -1;
char winner = 'N';
// Initialize the board to spaces
// boolean noWinner = true;
char player = 'X';
for (int r = 0; x < board.length(); r++)
{
for (int c = 0; c < board.length(); c++)
{
board[r][c] = ' ';
}
}
// Print the game board
printBoard(board);
// Keep playing while the game isn't finished
while (winner == 'N')
{
while (x < 0 && x >2 && y <0 && y > 2)
{
System.out.println("Enter the row and column, separated by spaces: ");
x = in.nextInt();
y = in.nextInt();
}
// Get the location from the user and validate it
// Mark the position in the board according to the user's specified location
// Print the board after the user plays
printBoard(board);
// Check to see if the game is finished. If it is, break out of the loop.
// Have the AI make a move
moveAI(board);
// Print the board after the AI plays
printBoard(board);
// Check to see who the winner is
winner = checkWinner(board);
}
// If the winner is 'X' or 'O', print that, otherwise, it is a tie
if (winner == 'X')
System.out.println("X is the winner!");
else if (winner == 'O')
System.out.println("O is the winner!");
else
System.out.println("Tie");
}
/**
* Makes a move for the AI, and marks the board with an 'O'.
*
* @param board The game board
*/
public static void moveAI (char[][] board)
{
int x,y = -1;
Random r = new Random();
x = r.nextInt(3);
y = r.nextInt(3);
boolean open = false;
// Validate that the random location generated is valid.
while (x < 0 && x >2 && y < 0 && y > 2)
{
x = r.nextInt(3);
y = r.nextInt(3);
}
while (open == false)
{
if (board[r][c] != ' ')
while (x < 0 && x >2 && y < 0 && y > 2)
{
x = r.nextInt(3);
y = r.nextInt(3);
}
}
}
// Keep recalculating the location if the one generated is not
// if (board[r][c] != ' ')
//{
// an empty space.
System.out.print(" ");
// Be sure to mark the position in the board with an 'O'
board[][]=in.nextInt(3) + 'O';
}
/**
* Prints out the tic-tac-toe board
*
* @param board The game board
*/
public static void printBoard (char[][] board)
{
// Box drawing unicode characters:
char a = '\u250c'; // U+250C : top-left
char b = '\u2510'; // U+2510 : top-right
char c = '\u2514'; // U+2514 : bottom-left
char d = '\u2518'; // U+2518 : bottom-right
char e = '\u252c'; // U+252C : top-vertical-connector
char f = '\u2534'; // U+2534 : bottom-vertical-connector
char g = '\u251c'; // U+251C : left-horizontal-connector
char h = '\u2524'; // U+2524 : right-horizontal-connector
char i = '\u253c'; // U+253C : center plus sign connector
char j = '\u2500'; // U+2500 : horizontal
char k = '\u2502'; // U+2502 : vertical
String l = j + "" + j + "" + j; // Three horizontals
// Print out the game board
System.out.printf ("\n 0 1 2\n" +
" %c%s%c%s%c%s%c\n" +
"0 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n" +
"1 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n" +
"2 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n\n",
a, l, e, l, e, l, b,
k, board[0][0], k, board[0][1], k, board[0][2], k,
g, l, i, l, i, l, h,
k, board[1][0], k, board[1][1], k, board[1][2], k,
g, l, i, l, i, l, h,
k, board[2][0], k, board[2][1], k, board[2][2], k,
c, l, f, l, f, l, d);
}
/**
* Checks the result of the game
*
* @param board The game board
* @return 'X' if 'X' is the winner
* 'O' if 'O' is the winner
* 'T' if the game is a tie
* 'N' if the game isn't finished
*/
public static char checkWinner(char[][] board)
{
if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' || // Check row 0
board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' || // Check row 1
board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' || // Check row 2
board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || // Check col 0
board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || // Check col 1
board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || // Check col 2
board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || // Check diag \
board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || // Check diag/
board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == 'x' || // Check row 0
board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == 'x' || // Check row 1
board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == 'x' || // Check row 2
board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == 'x' || // Check col 0
board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == 'x' || // Check col 1
board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == 'x' || // Check col 2
board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x' || // Check diag \
board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x') // Check diag/
{
return 'X';
}
else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' || // Check row 0
board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' || // Check row 1
board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' || // Check row 2
board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' || // Check col 0
board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' || // Check col 1
board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' || // Check col 2
board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' || // Check diag \
board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O' || // Check diag/
board[0][0] == 'o' && board[0][1] == 'o' && board[0][2] == 'o' || // Check row 0
board[1][0] == 'o' && board[1][1] == 'o' && board[1][2] == 'o' || // Check row 1
board[2][0] == 'o' && board[2][1] == 'o' && board[2][2] == 'o' || // Check row 2
board[0][0] == 'o' && board[1][0] == 'o' && board[2][0] == 'o' || // Check col 0
board[0][1] == 'o' && board[1][1] == 'o' && board[2][1] == 'o' || // Check col 1
board[0][2] == 'o' && board[1][2] == 'o' && board[2][2] == 'o' || // Check col 2
board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o' || // Check diag \
board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o') // Check diag/
{
return 'O';
}
boolean finished = true;
// If there is a blank space in the board, the game isn't finished yet
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[ i ].length; j++)
if (board[ i ][ j ] == ' ')
finished = false;
// If the board is finished and 'X' or 'O' wasn't returned, then it is a tie
// Otherwise, the game is not finished yet
if (finished)
return 'T';
else
return 'N';
}
}
看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(控制檯輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ]和[問]。 –