title: Java - Tic Tac Toe - Why won't my "switch player" method work?
body: <p>Why does my switchPlayer() method only work once?  The method should work independently just fine, and when I put it inside my playGame() method after placeMark(), it works continuously, but that messes up my currentPlayer variable, which messes up other methods.  I do not want to make a player object, so I assume I am just missing something simple, like where to place the switchPlayer() method.</p>
  
  <p>My result goes like this:</p>
  
  <ul>
  <li><em>currentplayer 1</em>, place your mark.</li>
  <li>[places mark, all good]</li>
  <li><em>currentplayer 2</em>, place your mark. </li>
  <li>[places mark, all good]</li>
  <li><em>currentplayer 2</em>, place your mark.  // problem line.. doesn't switch</li>
  <li>[places mark, all good]</li>
  </ul>
  
  <p>Relevant code for sub class:</p>
  
  <pre><code>public void playGame(){
  
      assignNames();
  
      assignMarks();
  
      explainGame();
  
      while(checkWin() == false &amp;&amp; fullBoard() == false){
  
          getNum();
  
          placeMark();
  
          printBoard(); 
  
      }
  
      gameResult();
  
  }
  
  
  public void placeMark(){
  
      if(validNumber() == true){
          if(num == 1){
              board[0][0] = currentMark;
              switchPlayer();
          }
          if(num == 2){
              board[0][1] = currentMark;
              switchPlayer();
          }
          if(num == 3){
              board[0][2] = currentMark;
              switchPlayer();
          }
          if(num == 4){
              board[1][0] = currentMark;
              switchPlayer();
          }
          if(num == 5){
              board[1][1] = currentMark;
              switchPlayer();
          }
          if(num == 6){
              board[1][2] = currentMark;
              switchPlayer();
          }
          if(num == 7){
              board[2][0] = currentMark;    
              switchPlayer();
          }
          if(num == 8){
              board[2][1] = currentMark;
              switchPlayer();
          }
          if(num == 9){
              board[2][2] = currentMark;
              switchPlayer();
          }
  
  
      }
  }
  
  
  
  
  public void switchPlayer(){
  
      if(currentMark == 'X')
          currentMark = 'O';
          else{
              currentMark = 'X';
          }
  
  
      if(currentMark == 'O' &amp;&amp; playerMark1 == 'O'){
          currentPlayer = playerName1;
      }
          else{
              currentPlayer = playerName2;
          }
  } 
  </code></pre>
  
  <p>Relevant code for main class:</p>
  
  <pre><code>    TicTacToe game = new TicTacToe();
  
      game.playGame();
  </code></pre>
  
  <p>It seems as though I should just post the whole code... so here it is.</p>
  
  <pre><code>public class TicTacToe {
          private static char[][] board;
          Scanner input = new Scanner(System.in);
          int num;
          String entry;
          String playerName1;
          char playerMark1;
          String playerName2;
          char playerMark2;
          char currentMark;
          String currentPlayer;
  
  
  public void playGame(){
  
      assignNames();
  
      assignMarks();
  
      explainGame();
  
      while(checkWin() == false &amp;&amp; fullBoard() == false){
  
          getNum();
  
          placeMark();
  
          printBoard(); 
  
      }
  
      gameResult();
  
  }
  
  
  public TicTacToe(){
  
      board = new char[3][3];
      clearBoard();        
  } // end TicTacToe()
  
  
  public void clearBoard(){
      for(int a = 0; a &lt; 3; a++){
          for(int b = 0; b &lt; 3; b++){
              board[a][b] = ' ';
          }
      }
  }// end clearBoard()
  
  
  public void printBoard(){
      System.out.println("-------------");
  
      for (int a = 0; a &lt; 3; a++) {
          System.out.print("| ");
          for (int b = 0; b &lt; 3; b++) {
              System.out.print(board[a][b] + " | ");
          }
          System.out.println();
          System.out.println("-------------");
      }
  }
  
  
      public boolean fullBoard() {
  
          boolean full = true;
  
          for (int i = 0; i &lt; 3; i++) {
  
              for (int j = 0; j &lt; 3; j++) {
  
                  if (board[i][j] == ' ') {
  
                  full = false;
                  }
              }
          }
          return full;
      }
  
  
      public void explainGame(){
  
      System.out.println("-------------");
      System.out.println("| 1 | 2 | 3 |");
      System.out.println("-------------");
      System.out.println("| 4 | 5 | 6 |");
      System.out.println("-------------");
      System.out.println("| 7 | 8 | 9 |");
      System.out.println("-------------");
      System.out.println("The above board shows which numbers you must enter to place your mark in their respective locations.\n");
      System.out.println("Randomly assigning player marks. . .");
      System.out.println(playerName1+" will be Player "+playerMark1+", and "+playerName2+" will be Player "+playerMark2 + ".");
      System.out.println("Player X will go first.  Press any key to begin playing.");
      entry = input.nextLine();
  }
  
  
  public void assignNames(){
  
      System.out.println("Enter a name for Player 1.");
          entry = input.nextLine();
          playerName1 = entry;
  
      System.out.println("Enter a name for Player 2.");
          entry = input.nextLine();
          playerName2 = entry;
  }
  
  
  public void assignMarks(){
  
      Random rand = new Random();
      int i = rand.nextInt(2);
  
      if( i == 0 ){
          playerMark1 = 'X';
          playerMark2 = 'O';
      }
      else{ 
              playerMark1 = 'O';
              playerMark2 = 'X';
      }
  
      currentMark = 'X';
  
      if(playerMark1 == 'X')
          currentPlayer = playerName1;
      else
          currentPlayer = playerName2;
  }
  
  
  public void switchPlayer(){
  
      if(currentMark == 'X')
          currentMark = 'O';
          else{
              currentMark = 'X';
          }
  
  
      if(currentMark == 'O' &amp;&amp; playerMark1 == 'O'){
          currentPlayer = playerName1;
      }
          else{
              currentPlayer = playerName2;
          }
  } 
  
  
  public int getNum(){
  
      System.out.println(currentPlayer + ", place your mark (1-9).");
  
      num = input.nextInt();
  
      return num;
      }
  
  
  public boolean validSpot(int num){
  
      boolean goodSpot = false;
  
      if(num == 1 &amp;&amp; board[0][0] == ' ')
          goodSpot = true;
      if(num == 2 &amp;&amp; board[0][1] == ' ')
          goodSpot = true;
      if(num == 3 &amp;&amp; board[0][2] == ' ')
          goodSpot = true;
      if(num == 4 &amp;&amp; board[1][0] == ' ')
          goodSpot = true;
      if(num == 5 &amp;&amp; board[1][1] == ' ')
          goodSpot = true;
      if(num == 6 &amp;&amp; board[1][2] == ' ')
          goodSpot = true;
      if(num == 7 &amp;&amp; board[2][0] == ' ')
          goodSpot = true;
      if(num == 8 &amp;&amp; board[2][1] == ' ')
          goodSpot = true;
      if(num == 9 &amp;&amp; board[2][2] == ' ')
          goodSpot = true;
  
      if(goodSpot == false){
          System.out.println("Input error.  Make sure the spot is not taken.");
      }
  
      return goodSpot;
  }
  
  
  public boolean validRange(int num){
  
      boolean goodRange = false;
  
      if(num &gt;= 1 &amp;&amp; num &lt;= 9 ){
          goodRange = true;  
      }
  
      if(goodRange == false){
          System.out.println("Input error.  Make sure the number is between 1-9.");
      }
  
      return goodRange;
  }
  
  
  public boolean validNumber(){
  
      return ((validRange(num) &amp;&amp; validSpot(num)));
  }
  
  
  public void placeMark(){
  
      if(validNumber() == true){
          if(num == 1){
              board[0][0] = currentMark;
              switchPlayer();
          }
          if(num == 2){
              board[0][1] = currentMark;
              switchPlayer();
          }
          if(num == 3){
              board[0][2] = currentMark;
              switchPlayer();
          }
          if(num == 4){
              board[1][0] = currentMark;
              switchPlayer();
          }
          if(num == 5){
              board[1][1] = currentMark;
              switchPlayer();
          }
          if(num == 6){
              board[1][2] = currentMark;
              switchPlayer();
          }
          if(num == 7){
              board[2][0] = currentMark;    
              switchPlayer();
          }
          if(num == 8){
              board[2][1] = currentMark;
              switchPlayer();
          }
          if(num == 9){
              board[2][2] = currentMark;
              switchPlayer();
          }
  
  
      }
  }
  
  
  public boolean checkSpot(char c1, char c2, char c3) {
  
      return ((c1 != ' ') &amp;&amp; (c1 == c2) &amp;&amp; (c2 == c3));
  
  }
  
  
  public boolean checkWin() {
  
      return (checkWinRows() || checkWinColumns() || checkWinDiagonals());
  }
  
  
  public boolean checkWinRows() {
  
      for (int i = 0; i &lt; 3; i++) {
  
          if (checkSpot(board[i][0], board[i][1], board[i][2]) == true) {
  
              return true;
          }
      }
      return false;
  }
  
  
  public boolean checkWinColumns() {
  
      for (int i = 0; i &lt; 3; i++) {
  
          if (checkSpot(board[0][i], board[1][i], board[2][i]) == true) {
  
              return true;
          }
      }
      return false;
  }
  
  
  public boolean checkWinDiagonals() {
  
      return ((checkSpot(board[0][0], board[1][1], board[2][2]) == true) || (checkSpot(board[0][2], board[1][1], board[2][0]) == true));
  }
  
  
  public void gameResult(){
  
      switchPlayer();
  
      if(checkWin()){
  
          System.out.println("\nGAME OVER -- " +currentPlayer+ " WINS! WOO HOO!");
      }
  
      else if(fullBoard()){
  
          System.out.println("\nGAME OVER -- DRAW!");
      }
  }
  
  
  } // end TicTacToe
  </code></pre>