This discussion is archived
1 Reply Latest reply: May 19, 2013 3:26 AM by rukbat RSS

I need help with my Tictactoe. can't  switch between  players Please Please

1009737 Newbie
Currently Being Moderated
/*
You will be building a Tic Tac Toe program, I have provided a program shell that has four functions:

printBoard- Prints the board as shows in the comments at the top of the file.
hasWon - Takes in a player's marker and tells us if that player has won
markTheBoard - marks the given location with the player's marker
isTie - Tells if there are no more spots and no winner

The play starts with X, the variable turn will keep track of who's turn it is.
At the beginning you will display the board with position numbers so the player knows how to place his/her marker.
The code will ask each player where they would like to place their marker until either
there are no more spots to play or one of the players wins.

The shell program is here


*/


import java.util.Scanner;

/**
* Represents a tic tac toe board on the console, positions are shown below:
* -------------
* | 1 | 2 | 3 |
* -------------
* | 4 | 5 | 6 |
* -------------
* | 7 | 8 | 9 |
* -------------
*
* An example board in mid play looks like this:
* -------------
* | X | | |
* -------------
* | | O | |
* -------------
* | O | | X |
* -------------
*
*
*
*/
public class TicTacToe {
public enum Marker
{
EMPTY,
X,
O
};

private static Marker position1 = Marker.EMPTY;
private static Marker position2 = Marker.EMPTY;
private static Marker position3 = Marker.EMPTY;
private static Marker position4 = Marker.EMPTY;
private static Marker position5 = Marker.EMPTY;
private static Marker position6 = Marker.EMPTY;
private static Marker position7 = Marker.EMPTY;
private static Marker position8 = Marker.EMPTY;
private static Marker position9 = Marker.EMPTY;

private static Marker turn = Marker.X;
     

public static void main(String[] args)
{
          printBoard();
          Scanner scan = new Scanner(System.in);           
          
     
          for (int j = 1; j < 9; j++) {
          boolean validInput = false; // for input validation          

while(!validInput) {
if (turn == Marker.X) {
System.out.print("Player 'X', enter your move: ");
                    
} else {
System.out.print("Player 'O', enter your move: ");

          }

int player = scan.nextInt();
          
if (j == 1 || j == 3 || j == 5 || j == 7 || j == 9) {
                    turn = Marker.X;
                    markTheBoard(turn, player);
               } else if (j == 2 || j == 4 || j == 6 || j == 8) {
                    turn = Marker.O;
                    markTheBoard(turn, player);
                    }else {
System.out.println("This move at (" + j + ") is not valid. Try again...");
}
}

               }

}

/**
* This method will print the board as shown in the above example.
*/

public static void printBoard()
{
          System.out.println("Welcome! Tic Tac Toe is a two player game.");
          System.out.println("Please Refer to the board below when asked to enter your values.");
               System.out.println();
          for (int x = 0; x < 1; x++) {
               for (int i = 0; i < 9; i += 3) {
                    System.out.println(" -------------");
                    System.out.println(" | " + (i + 1) + " | " + (i + 2) + " | "
                              + (i + 3) + " | ");

               }
               System.out.println(" -------------");
               System.out.println();
          }

     }



/**
* Checks if a particular player has won.
*
* @param m The player to check
* @return true if the player won, false if not
*/
public static boolean hasWon(Marker m)
{
          boolean hasWon = true;
          System.out.println("Player " + m + " has won this game");
          return hasWon;
}

/**
* Checks if the board is full with no winner
* @return true if the board is full with no winner, false otherwise
*/
public static boolean isTie()
{
          boolean isTie = true;
          System.out.println("This game is a tie!");
          return isTie;
}

/**
* Mark the given position with the given marker
* @param m The marker of the player given
* @param pos The position that we are marking
*/

public static void markTheBoard(Marker m, int pos){
     
          switch (pos) {
               case 1:
                    position1 = m;
                    break;
          
               case 2:
                    position2 = m;
                    break;
          
               case 3:
                    position3 = m;
                    break;
          
               case 4:
                    position4 = m;
                    break;
                    
               case 5:
                    position5 = m;
                    break;
                    
               case 6:
                    position6 = m;
                    break;
                    
               case 7:
                    position7 = m;
                    break;
                    
               case 8:
                    position8 = m;
                    break;
                    
               case 9:
                    position9 = m;
                    break;
                    
               default:
                    break;
                    }


     System.out.println(" -------------");
     System.out.println(" | " + position1 + " | " + position2 + " | "
               + position3 + " | ");
     System.out.println(" -------------");
     System.out.println(" | " + position4 + " | " + position5 + " | "
               + position6 + " | ");
     System.out.println(" -------------");
     System.out.println(" | " + position7 + " | " + position8 + " | "
               + position9 + " | ");
     System.out.println(" -------------");          
          
     

          if (position1 == Marker.X && position2 == Marker.X
                    && position3 == Marker.X || position4 == Marker.X
                    && position5 == Marker.X && position6 == Marker.X
                    || position7 == Marker.X && position8 == Marker.X
                    && position9 == Marker.X) {

               hasWon(Marker.X);
          } else if (position1 == Marker.O && position2 == Marker.O
                    && position3 == Marker.O || position4 == Marker.O
                    && position5 == Marker.O && position6 == Marker.O
                    || position7 == Marker.O && position8 == Marker.O
                    && position9 == Marker.O) {
               hasWon(Marker.O);
          } else if (position1 == Marker.O && position2 == Marker.O
                    && position3 == Marker.X || position4 == Marker.O
                    && position5 == Marker.O && position6 == Marker.X
                    || position7 == Marker.O && position8 == Marker.O
                    && position9 == Marker.X) {
               isTie();
          }

          if (position1 == Marker.X && position4 == Marker.X
                    && position7 == Marker.X || position2 == Marker.X
                    && position5 == Marker.X && position8 == Marker.X
                    || position3 == Marker.X && position6 == Marker.X
                    && position9 == Marker.X) {

               hasWon(Marker.X);
          } else if (position1 == Marker.O && position4 == Marker.O
                    && position7 == Marker.O || position2 == Marker.O
                    && position5 == Marker.O && position8 == Marker.O
                    || position3 == Marker.O && position6 == Marker.O
                    && position9 == Marker.O) {
               hasWon(Marker.O);
          }
          if (position1 == Marker.X && position5 == Marker.X
                    && position9 == Marker.X || position3 == Marker.X
                    && position5 == Marker.X && position7 == Marker.X) {
               hasWon(Marker.X);

          } else if (position1 == Marker.O && position5 == Marker.O
                    && position9 == Marker.O || position3 == Marker.O
                    && position5 == Marker.O && position7 == Marker.O) {
               hasWon(Marker.O);

          
          }
     }
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points