Just finished my first java class that taught me all the way up to oop and abstract classes. Decided to start some projects over the summer. Started with tictactoe since it sounded fairly easy to do. Lemme know if their are things that I need to improve or just general good advice so i dont make bad habits when coding. most of the code i thought of without help except when checking win conditions or if the board was full i got help from online so a lot of what i did seems kind of sloppy.
import java.util.Scanner;
import java.util.Random;
public class TicTacToe
{
Scanner scanner = new Scanner(System.in);
private String[][] gameBoard = {
{" ", "|", " ", "|", " "},
{"-", "+", "-", "+", "-"},
{" ", "|", " ", "|", " "},
{"-", "+", "-", "+", "-"},
{" ", "|", " ", "|", " "}
};
private boolean[] space = new boolean[9];
Random rand = new Random();
private String name = "default player";
private static final String CPU_NAME = "cpu";
private String playerSymbol = "";
private String cpuSymbol = "";
private static String turnName = "";
public TicTacToe (String name)
{
this.name = name;
}
public void printGameBoard ()
{
for (int i = 0; i < gameBoard.length; i++)
{
for (int j = 0; j < gameBoard[i].length; j++)
{
System.out.print(gameBoard[i][j]);
}
System.out.println();
}
}
public void whosTurn(String turnName)
{
if (turnName.equals(name))
{
turnName = name;
}
if (turnName.equals(CPU_NAME))
{
turnName = CPU_NAME;
}
}
public void whatSymbol()
{
System.out.println("What Symbol would you want? O or X?");
String choice = scanner.nextLine();
if(choice.toLowerCase().equals("o"))
{
playerSymbol = "O";
cpuSymbol = "X";
game();
}
else if (choice.toLowerCase().equals("x"))
{
playerSymbol = "X";
cpuSymbol = "O";
game();
}
else
{
System.out.println("Invalid input. try again");
whatSymbol();
}
}
public void playerTurn(int choice)
{
switch (choice)
{
case 1:
if(turnName.equals(name))
{
gameBoard[0][0] = playerSymbol;
break;
}
if(turnName.equals(CPU_NAME))
{
gameBoard[0][0] = cpuSymbol;
break;
}
case 2:
if(turnName.equals(name))
{
gameBoard[0][2] = playerSymbol;
break;
}
if(turnName.equals(CPU_NAME))
{
gameBoard[0][2] = cpuSymbol;
break;
}
case 3:
if(turnName.equals(name))
{
gameBoard[0][4] = playerSymbol;
break;
}
if(turnName.equals(CPU_NAME))
{
gameBoard[0][4] = cpuSymbol;
break;
}
case 4:
if(turnName.equals(name))
{
gameBoard[2][0] = playerSymbol;
break;
}
if(turnName.equals(CPU_NAME))
{
gameBoard[2][0] = cpuSymbol;
break;
}
case 5:
if(turnName.equals(name))
{
gameBoard[2][2] = playerSymbol;
break;
}
if(turnName.equals(CPU_NAME))
{
gameBoard[2][2] = cpuSymbol;
break;
}
case 6:
if(turnName.equals(name))
{
gameBoard[2][4] = playerSymbol;
break;
}
if(turnName.equals(CPU_NAME))
{
gameBoard[2][4] = cpuSymbol;
break;
}
case 7:
if(turnName.equals(name))
{
gameBoard[4][0] = playerSymbol;
break;
}
if(turnName.equals(CPU_NAME))
{
gameBoard[4][0] = cpuSymbol;
break;
}
case 8:
if(turnName.equals(name))
{
gameBoard[4][2] = playerSymbol;
break;
}
if(turnName.equals(CPU_NAME))
{
gameBoard[4][2] = cpuSymbol;
break;
}
case 9:
if(turnName.equals(name))
{
gameBoard[4][4] = playerSymbol;
break;
}
if(turnName.equals(CPU_NAME))
{
gameBoard[4][4] = cpuSymbol;
break;
}
default:
System.out.println("Invalid input. try again");
game();
}
}
public void cpuMove()
{
boolean run = true;
while(run)
{
int choice = rand.nextInt(9) + 1;
if (!space[choice - 1])
{
playerTurn(choice);
space[choice - 1] = true;
turnName = name;
System.out.println("---------------------------------------------------");
System.out.println("Cpu chose: " + choice);
run = false;
}
}
}
public void whoStarts ()
{
System.out.println("Who starts first? " + name + " or " + CPU_NAME);
String starter = scanner.nextLine();
if (starter.toLowerCase().equals(name.toLowerCase()))
{
turnName = name;
whatSymbol();
}
else if (starter.toLowerCase().equals(CPU_NAME.toLowerCase()))
{
turnName = CPU_NAME;
whatSymbol();
}
else
{
System.out.println("Invalid input. try again");
System.out.println("---------------------------------------------------");
whoStarts();
}
}
public void startGame()
{
System.out.println("Do you want to play the game?");
String answer = scanner.nextLine();
switch(answer.toLowerCase())
{
case "yes":
whoStarts();
break;
case "no":
System.out.println("thats too bad. See you next time !!");
break;
default:
System.out.println("Invalid input. try again");
System.out.println("---------------------------------------------------");
startGame();
}
}
public void game()
{
boolean gameWon = false;
while(!gameWon)
{
int choice = 0;
System.out.println("---------------------------------------------------");
printGameBoard();
System.out.println("Choices : 1 to 9");
System.out.println(name + "(" + playerSymbol + "): ");
choice = scanner.nextInt();
if(turnName.equals(name))
{
if(!space[choice - 1])
{
playerTurn(choice);
space[choice - 1] = true;
turnName = CPU_NAME;
gameWon = checkGameStatus();
}
else
{
System.out.println("---------------------------------------------------");
System.out.println("Occupied Space. Try again.");
}
}
if(turnName.equals(CPU_NAME))
{
cpuMove();
gameWon = checkGameStatus();
}
}
}
public boolean checkGameStatus ()
{
if(checkWin(playerSymbol))
{
System.out.println("---------------------------------------------------");
System.out.println("Player " + name + " has won!!");
System.out.println("---------------------------------------------------");
printGameBoard();
return true;
}
else if (checkWin(cpuSymbol))
{
System.out.println("---------------------------------------------------");
System.out.println("The cpu has won!! you lost");
System.out.println("---------------------------------------------------");
printGameBoard();
return true;
}
else if(checkFull())
{
System.out.println("---------------------------------------------------");
System.out.println("The game is tie");
System.out.println("---------------------------------------------------");
printGameBoard();
return true;
}
return false;
}
public boolean checkWin (String symbol)
{
for (int i = 0; i < 6; i += 2)
{
if(gameBoard[i][0].equals(symbol) && gameBoard[i][2].equals(symbol)
&& gameBoard[i][4].equals(symbol))
{
return true;
}
if(gameBoard[0][i].equals(symbol) && gameBoard[2][i].equals(symbol)
&& gameBoard[4][i].equals(symbol))
{
return true;
}
}
if(gameBoard[0][0].equals(symbol) && gameBoard[2][2].equals(symbol)
&& gameBoard[4][4].equals(symbol))
{
return true;
}
if(gameBoard[0][4].equals(symbol) && gameBoard[2][2].equals(symbol)
&& gameBoard[4][0].equals(symbol))
{
return true;
}
return false;
}
public boolean checkFull ()
{
for (boolean spaceFilled : space) {
if (!spaceFilled) {
return false;
}
}
return true;
}
}
public class Main {
public static void main(String[] args)
{
TicTacToe johnDoe = new TicTacToe("John Doe");
johnDoe.startGame();
}
}
New contributor
bruh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1