I am currently learning Java; I could really use some help from more experienced programmers.
How can I clean up my Tic-Tac-Toe code? What mistakes have I made? Can I use more exception handling? I tried to use exception handling, in isValidStepCheck
and takenFieldCheck
, but I don't think I used it efficiently.
public class TicTacToe {
public static String[] bpos = {" 1 ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ", " 8 ", " 9 "};
public static int turn = 0;
public static int num;
public static String numS;
public static String winner = "n";
public static int r = 0;
public static void main(String[] args) {
System.out.println("Welcome to the Tic-Tac-Toe game.");
Board();
System.out.println("X has the first step, enter a number.");
try{TicTacToeGame();}
catch (Exception e){
System.out.println("Something went wrong. Are you sure you entered a number?");
}
}
static boolean isValidStepCheck() {
while (true) {
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
numToString();
if (num < 1 || num > 9) {
System.out.println("You have to enter a number between 1 and 9!");
} else {
return true;
}
}
}
static void isValidMove() {
while (true) {
if (isValidStepCheck() && !takenFieldCheck()) {
if (turn % 2 == 0) {
turn++;
bpos[num - 1] = " x ";
Board();
break;
} else if (turn % 2 == 1) {
turn++;
bpos[num - 1] = " o ";
Board();
break;
}
} else if (takenFieldCheck()) {
System.out.println("This field is already taken!");
}
}
}
static void Board() {
System.out.println(" ___________ ");
System.out.println("|" + bpos[0] + "|" + bpos[1] + "|" + bpos[2] + "|");
System.out.println("|-----------|");
System.out.println("|" + bpos[3] + "|" + bpos[4] + "|" + bpos[5] + "|");
System.out.println("|-----------|");
System.out.println("|" + bpos[6] + "|" + bpos[7] + "|" + bpos[8] + "|");
System.out.println(" ----------- ");
}
static boolean takenFieldCheck() {
if (bpos[num - 1].equals(numS)) {
return false;
} else {
return true;
}
}
static void numToString() {
switch (num) {
case 1:
numS = " 1 ";
break;
case 2:
numS = " 2 ";
break;
case 3:
numS = " 3 ";
break;
case 4:
numS = " 4 ";
break;
case 5:
numS = " 5 ";
break;
case 6:
numS = " 6 ";
break;
case 7:
numS = " 7 ";
break;
case 8:
numS = " 8 ";
break;
case 9:
numS = " 9 ";
break;
}
}
static void isWon() {
String line = null;
if (winner.equals("n")) {
for (int i = 0; i < 8; i++) {
switch (i) {
case 0:
line = bpos[0] + bpos[1] + bpos[2];
break;
case 1:
line = bpos[3] + bpos[4] + bpos[5];
break;
case 2:
line = bpos[6] + bpos[7] + bpos[8];
break;
case 3:
line = bpos[0] + bpos[3] + bpos[6];
break;
case 4:
line = bpos[1] + bpos[4] + bpos[7];
break;
case 5:
line = bpos[2] + bpos[5] + bpos[8];
break;
case 6:
line = bpos[0] + bpos[4] + bpos[8];
break;
case 7:
line = bpos[2] + bpos[4] + bpos[6];
break;
}
if (line.equals(" x x x ")) {
winner = "x";
} else if (line.equals(" o o o ")) {
winner = "o";
}
}
} else {
winner = "n";
}
}
static void TicTacToeGame() {
while (true) {
isValidMove();
isWon();
r++;
if (winner.equals("x")) {
System.out.println("Congrats to X, you won!");
break;
} else if (winner.equals("o")) {
System.out.println("Congrats to O, you won!");
break;
}
else {
if(r==9){
System.out.println("Its a draw!");
break;
}
}
}
}
}
```