I've been trying to create a tic-tac-toe game in Java to help me improve at creating Object Oriented projects from scratch. But I have ran into a problem and am looking for some guidance.
I'm only half way through the project at the moment so I'll just give you a quick update on what I've done so far.
The project currently has a Game class as the driver. It has a Board class which stores a map between the locations on the board (A1, A2, A3...) and their values ('X', 'O', null). And a Player class which holds fields for the team the player is on (Xs or Os) and the number of wins (functionality for winning has not been implemented yet).
The issue I'm having is with creating two instances of the Player class (player1 and player2) from Game.
I've defined them as fields in Game like so:
public static Player player1 = new Player();
public static Player player2 = new Player();
player1 is then asked to choose between Xs and Os and player2 is given the opposite.
player1.setTeam(Character.toUpperCase(teamChoice));
if(player1.getTeam() == 'X')
player2.setTeam('O');
else
player2.setTeam('X');
In my head this should work. However, when I print out the player choices it prints out whatever player2 has been set to be.
System.out.println("Player 1 you have selected " + player1.getTeam() + "\n");
System.out.println("Player 2 you will be " + player2.getTeam() + "\n");
The contents of the Player class are seen below:
public class Player {
public static int wins = 0;
public static Character team;
public Player(){
}
public int getWins(){return wins;}
public Character getTeam(){return team;}
public void setTeam(Character team){
Player.team = team;
}
}