我很抱歉请求帮助做家庭作业......但我被卡住了。赋值是TicTacToe java程序中的二维数组。
我的方法moveHuman和moveAI是无效的,但它并没有节省游戏进程的价值。所以我把它改成了char [] []方法,但是这导致程序在moveHuman方法被调用后挂起。我知道这一点,因为我已经输入了各种打印报表,所以我知道我的程序在进展中的位置,并打印出“moveHuman”,它甚至不会提示我输入我想移动的位置。如果你想帮助我解决这个问题,那是晚上到午夜时分。大周五晚上的活动!谢谢!!二维数组不能保存价值
// File: TicTacToe.java
//**************************************************************************
import java.util.Random;
import java.util.Scanner;
public class jkcrosby_TicTacToe
{
private static Scanner get;
public static void main (String[] args)
{
get = new Scanner (System.in);
char[][] board = new char[ 3 ][ 3 ];
// Initialize the board to all spaces
for (int r=0; r<board.length; r++){
for(int c=0; c<board[r].length; c++){
board[r][c]= ' ';
}
}
// Print the game board
printBoard(board);
// Keep playing while the game isn't finished
while (checkWinner(board) =='N'){
// Have the human player make a move
moveHuman(board);
// Print the board after the user plays
printBoard(board);
// Check to see if the game is finished ('X' wins or
// it is a tie). If it is, break out of the loop.
if (checkWinner(board) == 'X' || checkWinner(board) == 'O' || checkWinner(board) == 'T')
break;
// Have the AI make a move
moveAI(board);
// Print the board after the AI plays
printBoard(board);
// Check to see who the winner is checkWinner(board);
}
// If the winner is 'X' or 'O', print that, otherwise, it is a tie
char win=checkWinner(board);
switch (win) {
case 'X': case 'O': System.out.printf("%c wins!\n", win);
break;
case 'T': System.out.printf("It is a tie!\n");
break;
}
}// end of main method
/**
* Validate a move for the human player
*
* @param board The game board
*/
public static void moveHuman (char[][] board)
{
get= new Scanner (System.in);
int r;
int c;
System.out.println("X's turn!\r\n");
// Get the location from the user and validate it (within bounds
do{
System.out.println("Enter the row and column, separated by spaces: ");
r=get.nextInt();
c=get.nextInt();
}while((r<0 || r>2 ||c<0||c>2)|| board[r][c] !=' '|| board[r][c]=='\u0000'); // and is already occupied).
// Mark the position in the board with an 'X' according to the user's
// specified location
board[r][c]='X';
//return board[r][c];
} //end method moveHuman
/**
* Makes a move for the AI, and marks the board with an 'O'.
*
* @param board The game board
*/ public static void moveAI (char[][] board)
{
// Check that the random location generated is valid.
// Keep recalculating the location if the one generated is
// already occupied.
get = new Scanner (System.in);
int r;
int c;
Random rand = new Random();
System.out.println("O's turn!");
do{
System.out.println("Enter the row and column, separated by spaces: ");
r = rand.nextInt(3);
c = rand.nextInt(3);
} while((r<0|| r>2|| c<0||c>2) || board[r][c]!=' '|| board[r][c]=='\u0000');
board[r][c]='O';
// Be sure to mark the valid position in the board with an 'O'
}
/* ******************************************************************* *
* ANYTHING BELOW THIS LINE DOES NOT NEED TO BE MODIFIED BY YOU. YOU *
* CAN CERTAINLY LOOK AT THE CODE, THOUGH, TO SEE WHAT'S HAPPENING. *
* IN FACT, YOU'RE ENCOURAGED TO LOOK AT THIS CODE AT SOME POINT. *
* ******************************************************************* */
/**
* 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/
{
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/
{
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 == true)
return 'T';
else
return 'N';
}
}
[为什么“有人可以帮助我?”不是一个真正的问题?](http://meta.stackoverflow.com/q/284236) –
我还会补充说,你的截止日期不是问题的一部分。如果您有真正的理由,请申请延期。否则,你的截止日期不是我们关心的问题。 –
其实@Stephen C,截止日期是相对的,以反映紧迫性。我很高兴你编辑了你写过的无礼评论......两次你应该进一步编辑它。不允许扩展。不,我没有提交截止日期,但我的计划有效。 :) 感谢你的关心!我很高兴在这个社区有像你这样善良和体贴的人。 – jkcrosby3