Привет, я пишу игру в крестики-нолики. Я изложил в комментариях в своем коде то, что мне нужно. Сейчас у меня проблемы с созданием метода getMove. Я предполагаю, что мне нужно будет вызвать метод getMove в моих операторах if / else после нажатия строки и столбца?
Я не уверен, как перейти от получения номеров строк / столбцов и помещения их на мою доску для того, что ввел пользователь.
Вот мой код:
import java.util.*;
public class TicTac{
//declare a constant variable
public static final int SIZE = 3; //size of each row and each column
public static void main(String[] args) {
//initialize the board
char[][] board = new char[3][3];
//display the board
displayBoard(board);
//prompt for the first player
//determine if X or O is pressed
System.out.println("Who wants to go first (X or O)? ");
Scanner xOrO = new Scanner(System.in);
String entOp = xOrO.nextLine();
char enterOp = entOp.charAt(0);
if (enterOp == 'X'){
System.out.println("Enter a row (0,1,2) for player X: ");
Scanner enterRow = new Scanner(System.in);
int fTurn = enterRow.nextInt();
System.out.println("Enter a column (0,1,2) for player X: ");
Scanner enterCol = new Scanner(System.in);
int fCol = enterCol.nextInt();
} else if (enterOp == 'O') {
System.out.println("Enter a row (0,1,2) for player O: ");
Scanner enterRow = new Scanner(System.in);
int fTurn = enterRow.nextInt();
System.out.println("Enter a column (0,1,2) for player X: ");
Scanner enterCol = new Scanner(System.in);
int fCol = enterCol.nextInt();
} else {
System.out.println("Must enter either X or O");
}
//and display the board
//displayBoard(board);
}
//initializeBoard method
//displayBoard method
public static void drawLine() {
for (int i = 0; i <= 9 * SIZE; i++) {
System.out.print("-");
}
System.out.println();
}
public static void displayBoard(char[][] board) {
drawLine();
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
System.out.print("| " + board[i][j] + " ");
}
System.out.println("|");
drawLine();
}
System.out.println();
}
//getMove method: to prompt the current player for target position. And place the mark in the position if the position is available.
// public static void getMove() {
//
//
//
//
//
// }
//findWinner method: after each move, check the board see if there is a winner
//hasEmptyCell method: check if there is still empty spot in the board
}
board[fCol][fTurn] = enterOp
) и перерисовать доску. - person MadProgrammer   schedule 23.10.2013