I’ll help with understanding the logic behind such game and give you the testing code but advice you to do it on your own.
First, you’ll need a flag to tell you if the game is finished.
When does the game finish? 2 possible answers :
- One of the players wins
- The cells are all full without any winner
That will be what you’ll be testing in your do{}while()
loop
After that, you’ll need to fill an array each time a player makes a move.
You’ll have to be attentive to two things here :
- Number entered is an index of the array
- The number entered has not been played before
First condition is easy but for the second you’ll need a way to “stock” the plays already made by both players.
At the end of your loop, you’ll have to check if the game is finished and change your boolean if so.
How does someone win tic tac toe? By having an horizontal, vertical or diagonal set of the same character. You’ll have to test all the possibilities and change your flag if filled.
Otherwise, if your board is entirely filled, you’ll have to check that and break from the loop.
import java.util.ArrayList;
import java.util.Scanner;
public class TicTacToe{
private static Scanner scan = new Scanner(System.in);
public static void main (String[] args){
boolean finished = false;
int plays = 0;
int position = 0;
ArrayList<Integer> alreadyPlayed = new ArrayList<>();
String[] board = {"_","_","_","_","_","_","_","_","_"};
String character;
do{
character = plays % 2 == 0 ? "x" : "o";
do{
System.out.println("Player " + character + ", it's your turn. (0-8)");
System.out.println("Already played :" + alreadyPlayed.toString());
position = scan.nextInt();
} while (position >= 9 || alreadyPlayed.contains(position));
alreadyPlayed.add(position);
board[position] = character;
if (alreadyPlayed.size()>=5){
finished = checkFinished(board);
}
plays++;
for (int i = 0 ; i < 9 ; i++){
System.out.print(board[i]);
if ((i+1) % 3 == 0) System.out.println();
}
} while (!finished && plays < 9);
if (checkFinished(board)) System.out.println("Players " + character + " wins !");
else System.out.println("No one wins.");
}
private static boolean checkFinished(String[] toCheck) {
// Horizontal checks
if (!toCheck[0].equals("_") && toCheck[0].equals(toCheck[1]) && toCheck[0].equals(toCheck[2])) return true;
if (!toCheck[3].equals("_") && toCheck[3].equals(toCheck[4]) && toCheck[3].equals(toCheck[5])) return true;
if (!toCheck[6].equals("_") && toCheck[6].equals(toCheck[7]) && toCheck[6].equals(toCheck[8])) return true;
// Vertical checks
if (!toCheck[0].equals("_") && toCheck[0].equals(toCheck[3]) && toCheck[0].equals(toCheck[6])) return true;
if (!toCheck[1].equals("_") && toCheck[1].equals(toCheck[4]) && toCheck[1].equals(toCheck[7])) return true;
if (!toCheck[2].equals("_") && toCheck[2].equals(toCheck[5]) && toCheck[2].equals(toCheck[8])) return true;
// Diagonal checks
if (!toCheck[0].equals("_") && toCheck[0].equals(toCheck[4]) && toCheck[0].equals(toCheck[8])) return true;
if (!toCheck[2].equals("_") && toCheck[2].equals(toCheck[4]) && toCheck[2].equals(toCheck[6])) return true;
return false;
}
}
6
solved Java tictactoe. Don’t know how to continue [closed]