好的,这是我第一次上计算机课,我遇到了一些麻烦,我有一个作业,我必须使用老师给我们的方法制作井字游戏,并且我不允许更改主要方法. 我想创建一个板,询问用户是否需要 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;
}
}