好吧,这是我第一次参加计算机课,而且我遇到了一些麻烦,我有一个任务,我必须做一个tic tac toe游戏,使用老师给我们的方法,我不允许改变主要方法。我想创建一个板,询问用户是否需要4x4 5x5或6x6并使用' - '显示它。我已经正确完成了这一部分,但我不知道如何用他们想要移动的行和列的用户输入替换该板。
我的主要问题在于getMove方法。我不知道如何获取输入以用x或o替换' - '。
import java.util.Scanner;
import java.util.Random;
public class Lab7 {
public static int currentPlayer;
public static void main(String[] args) {
char [] [] board; char win; char player;
Random randy = new Random();
int size = getBoardSize();
while(size >= 4) {
board = createEmptyBoard( size );
player = getStartingPlayer(randy);
do {
getMove(board, player);
player = player=='O'?'X':'O';
win = checkForWin(board);
} while(win == 'N');
if(win == 'F'){
System.out.printf("\nThe board is Full: no Winner\n");
} else {
System.out.printf("\nThe winner is %c\n", win);
}
displayBoard(board);
size = getBoardSize();
}
}
public static int getBoardSize() {
int retval;
Scanner keyboard = new Scanner(System.in);
do {
System.out.printf("Enter board size 4, 5, 6 or -1 to quit: ");
retval = keyboard.nextInt();
} while(retval != 4 && retval != 5
&& retval != 6 && retval != -1);
keyboard.nextLine();
return retval;
}
public static char getStartingPlayer(Random rangen){
char retval = 'X';
System.out.printf("\n\tNew Game. Starting player is %c\n", retval);
return retval;
}
public static char [][] createEmptyBoard(int size) {
char [] [] board = new char [size][size];
// two loops to fill cells with '-'
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++)
if ((board[i][j] == 'x') || (board[i][j] == 'o'))
System.out.print(" " + board[i][j]);
else
System.out.print(" -");
System.out.println();
} return board;
}
public static void getMove(char [][] board, char player){
int row;
int column;
System.out.printf("Player %c move\n", player);
System.out.print("Enter Row:");
System.out.println();
displayBoard(board);
}
public static void displayBoard(char [][] board) {
}
public static char checkForWin(char [][] board){
char retval = 'F';
return retval;
}
}
答案 0 :(得分:0)
如果您知道如何操作,请清除屏幕并重新绘制电路板。我怀疑在开始的计算机课上需要这个。只需在每个转弯处重新打印电路板。