Это все для tic tac toe.I есть метод, который проверяет победителя, и он может туда попасть. Мне кажется, мне нужно искать номера на игровом поле, и если они останутся, это не галстук, и если там останутся, то это галстук.Как бы я включил галстук-счетчик в свой код tic tac toe? Я хотел бы поставить его в конце моей программы.
public class TicTacToe { //Beginning of TicTacToe
static String[][] board = new String[3][3];
public static void main (String [] args) { // Main routine
String move; //User input
String marker = "O"; //The character/marker starts at O and switches to X
// asigning variables
boolean computer = false;
boolean done = false;
boolean playAgain = true;
int wins[] = {0,0,0};//1st. p1 wins, 2nd. p2/comp wins, 3rd. ties
System.out.println("Do you want to play against computer (y/n)? Typing no will give you a 2 player board.");
computer = TextIO.getlnBoolean(); //Option and input that player 1 gets to choose if they want to play against computer or player2
do { //Beginning of play again loop
fillArrays();// fills the arry with values
printBoard(); // prints the board
do { //Beginning of loop that runs the board
marker = switchMarker(marker); // turns o into x
do { // error check loop
move = getUserMove(marker, computer);
} while (!isMoveValid(move));
insertMove(marker, move);
printBoard();
done = checkForWinner();
} while(!done);
printWinner(marker,wins);
playAgain = getPlayAgain(); //Sets the variable play again to the method getPlayAgain, which asks the user if he/she wants to play again
} while(playAgain); //Loops while play again is true
} //End of main routine
public static void fillArrays(){ //fills the game board
int number =1;
for(int a=0; a<3; a++) {
for (int b=0; b<3; b++) {
board[a][b]= String.valueOf(number++);
}
}
}
public static boolean getPlayAgain(){ //meathod to play again
System.out.println("Would you like to play again? (y/n)");
String playAgain = TextIO.getln();
if(playAgain.equals("y")){
return true;
}
else{
return false;
}
}
public static boolean isMoveValid(String mov) { //error checks numbers greater than 9 and less than 0
int numMove = Integer.parseInt(mov);
if (numMove < 1 || numMove > 9) {
System.out.println("Sorry but your move is not valid! Please re enter:");
return false;
}
else {
return true;
}
}
public static String switchMarker(String marker){ // replaces O with X
if(marker.equals("X")){
return "O";
}
else{
return "X";
}
}
public static String getComputerMove(){ //determins where the computer decides to play
boolean found = false;
String randString;
do{
int rand =(int)(Math.random()*9)+1;
randString = Integer.toString(rand);
for(int a=0;a<3; a++){
for(int b=0;b<3; b++){
if(randString.equals(board[a][b])){
found = true;
// try and code for
}
}
}
}while(!found);
return randString;
}
public static String getUserMove(String marker, boolean computer){ //chooses computer or player2
if(marker.equals("X")){
System.out.println("Player 1 please enter your move");
}
else if(computer){
return getComputerMove();
}
else{
System.out.println("Player 2 please enter your move");
}
return TextIO.getln();
}
public static void insertMove(String move, String spot){ // places character on the board
for(int a=0;a<3; a++){
for(int b=0;b<3; b++){
if(spot.equals(board[a][b])){
board[a][b] = move;
}
}
}
}
public static void printBoard(){ //prints the board
for(int a=0;a<3; a++){
for (int b = 0; b<3; b++){
System.out.print(board[a][b] + " ");
}
System.out.println();
}
System.out.println();
}
public static boolean checkForWinner(){ // checks all possible win combos
//combo 1 - horizontal line
for(int a=0;a<3; a++){ //horizontal checks
if((board[a][0].equals(board[a][1]))&&(board[a][1].equals(board[a][2]))){
return true;
}
}
//combo 2 - vertical lines
for(int a=0;a<3; a++){ //vertical checks
if((board[0][a].equals(board[1][a]))&&(board[1][a].equals(board[2][a]))){
return true;
}
}
//combo 3 - diagonal lines
if((board[0][0].equals(board[1][1]))&&(board[1][1].equals(board[2][2]))||(board[0][2].equals(board[1][1]))&&(board[1][1].equals(board[2][0]))){
return true;
}
//If the above haven't returned true, then we either haven't won yet.. or it's a tie.
//Cycle through looking for any number in the board. If there isn't one, the board is full, and because we know
//there isn't a winning move on the board.. it must be full! So, return true.
return false; //Return false. We don't have a winner yet.
}
public static void printWinner(String marker,int wins[]){ // counts wins, ties and losses
if(marker.equals("X")){
System.out.println("Player 1 you win!");
wins[0]++;
}
else{
System.out.println("Player 2 you win!");
wins[1]++;
}
// if (
System.out.println("Player 1 has " + wins[0] + " wins and Player 2 has " + wins[1] + " wins and there are " + wins[2] + " ties.");
return;
}
}
checkForWinner(), где я бы хотел его проверить на галстук –