Hi folks....I have two separate copies of this program to show how my thought process worked in initial creation and how it has changed since I was helped by you folks in my airline reservation program. It does not work but I DID try to apply what I have learned from you all to get it to work.
Nothing special about this......all of the examples I have found out there were over complicated and involved things I can't use because I haven't learned it yet. I actually get it to let me put in input but that is it. For example when I choose player 1 it makes me enter row and column and x or o....and then repeats it for player 1 a few times before giving me my menu thing again. This obviously doesn't work out. Well tear apart my program and let me know where I suck and how to fix it and not suck. I actually started this before my airline reservation so the second iteration might have become somewhat mixed up in that I was trying to apply things I had learned from that program.

import java.util.*;
public class ticTacToe
{
  public static void main(String[] args)
  {
    char[][] gameBoard = new char[3][3];
    
    Scanner input = new Scanner(System.in);
    
    System.out.println("Player 1 Enter X or O");
    gameBoard[0][0] = input.next().charAt(0);
    System.out.println("Player 2 Enter X or O");
    gameBoard[0][1] = input.next().charAt(0);
    System.out.println("Player 1 Enter X or O");
    gameBoard[0][2] = input.next().charAt(0);
    System.out.println("Player 2 Enter X or O");
    gameBoard[1][0] = input.next().charAt(0);
    System.out.println("Player 1 Enter X or O");
    gameBoard[1][1] = input.next().charAt(0);
    System.out.println("Player 2 Enter X or O");
    gameBoard[1][2] = input.next().charAt(0);
    System.out.println("Player 1 Enter X or O");
    gameBoard[2][0] = input.next().charAt(0);
    System.out.println("Player 2 Enter X or O");
    gameBoard[2][1] = input.next().charAt(0);
    System.out.println("Player 1 Enter X or O");
    gameBoard[2][2] = input.next().charAt(0);
    
    printBoard(gameBoard);
  }
    
    
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[0].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        }
      System.out.println();
      }
    }
}

and the latest iteration.

import java.util.*;
public class ticTacToe2
{
  public static void main(String[] args)
  {
    char[][] gameBoard = new char[3][3];
    String player1 = "player1";
    String player2 = "player2";
    int turns = 9;
    int playerTurn;
    Scanner input = new Scanner(System.in);
    
    for(int i = 0; i < gameBoard.length;i++)
    {
    for(int j=0;j < gameBoard[0].length;j++)
    {
       gameBoard[i][j] = '_';
    }
    }
    while (turns > 0)
    {
      System.out.println("Who's turn is it? For Player 1 Hit 1, For Player 2 Hit 2");
      playerTurn = input.nextInt();
      
      switch(playerTurn)
      {
        case 1:
          System.out.println("Player 1 Hit X or O");
          playerChoice(gameBoard,input);
          System.out.println("Who's turn is it? For Player 1 Hit 1, For Player 2 Hit 2");
          playerTurn = input.nextInt();
          break;
        case 2:
          System.out.println("Player 2 Hit X or O");
          playerChoice(gameBoard,input);
          System.out.println("Who's turn is it? For Player 1 Hit 1, For Player 2 Hit 2");
          playerTurn = input.nextInt();
          break;
        default:
          System.out.println("Invalid Choice");
      }
      turns--;
    }
    printBoard(gameBoard);
  }  
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[0].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        }
      System.out.println();
      }
    }
    /*public static boolean isNextTurn(char[][]gameBoard, int turns)
    {
        boolean result;
        for (int index = 0; index < gameBoard.length; index++)
        {
        if (turns < gameBoard.length)
          result = true;
        else
          result = false;
    return result;
        }
    }*/
    public static char[][] playerChoice(char[][]gameBoard,Scanner input)
    {
      for (int index = 0; index < gameBoard.length; index++)
      {
        System.out.println("Which row do you want?");
        int row = input.nextInt();
        System.out.println("What column do you want?");
        int col = input.nextInt();
        System.out.println("Enter an X or O");
        gameBoard[row][col] = input.next().charAt(0);
      }
      return gameBoard;
    }
}

The commented out boolean was supposed to see whose turn it is but that never worked out for me lol.

If you want to know who's turn it is, simply ask the users at the beginning (before the loop and the game actually starts) who wants to go first. You can save a boolean variable firstPlayerTurn - when it's true it's player 1's turn, if false it's player 2's turn. Every loop iteration just do firstPlayerTurn = !firstPlayerTurn .

So then should I get rid of my switch menu system entirely and just use the boolean method to go back and forth? Another one of my hangups on this that is different from the airplane reservation program is the choosing of where I wish to place an X or an O which I am sure that the current methods I have DO NOT perform that task.

Give me a detailed pseudo code of the problem, showing the algorithm that needs to be solved.

pseudo code is another thing i suck at....lol

Well here is what I was attempting to do starting with my while loop I guess in the 2nd program I posted....
There are a total of 9 turns correct?
Well my while loop says to go through my menu as long as the turns are greater than 0....at the end if each menu select the turns decrement.

In the menu the user is supposed to be able to chose whose turn it is after each turn taken.

During each turn for a player that player enters the row they want and the column they want.....then they need to enter either X or O.

After this the menu pops up again asking which players turn it is.

Rinse and repeat until the decrementing turns counter hits 0 and the menu no longer runs and it moves to the print method to display the array and show who won. I suppose I also need to include a way to check to make sure that space isn't taken up as well lol.

Now I am thinking back to figuring out who wins.....perhaps I should tell the user which player is X and which player is O instead of giving them a choice?
That way when the game is over I can actually tell who won.

So then I guess I would need to change my method or create separate methods for each player? when option 1 is chosen it places an X in the space the player 1 chooses and when option 2 is chosen it places a 0 in the space the player 2 chooses.

Ok I tried something else.....it failed.....but I thought it would be a good idea.

import java.util.*;

  public class ticTacToeMadness2
  {
    public static void main(String[] args)
    {
      char[][] gameBoard = new char[3][3];
      String player1 = "player1";
      String player2 = "player2";
      int turns = 9;
      int playerTurn;
      int row;
      int col;
      Scanner input = new Scanner(System.in);
    
      fillBoard(gameBoard);
      
      while (turns > 0)
    {
      System.out.println("Who's turn is it? For Player 1 Hit 1, For Player 2 Hit 2");
      System.out.println("Player 1 is X  and Player 2 is O");
      playerTurn = input.nextInt();
      
      switch(playerTurn)
      {
        case 1:
          System.out.println("Player 1 Enter Row");
          row = input.nextInt();
          System.out.println("Player 1 Enter Column");
          col = input.nextInt();
          if (isFilled(gameBoard,row,col))
          {
            System.out.println("This Spot is Filled!");
          }
          else
          {
            System.out.println("X has been successfully placed" + playerOne(gameBoard,row,col));
          }
                               
            
          System.out.println("Who's turn is it? For Player 1 Hit 1, For Player 2 Hit 2");
          playerTurn = input.nextInt();
          break;
        case 2:
          System.out.println("Player 2 Enter Row");
          row = input.nextInt();
          System.out.println("Player 2 Enter Column");
          col = input.nextInt();
          if (isFilled(gameBoard,row,col))
          {
            System.out.println("This Spot is Filled!");
          }
          else
          {
            System.out.println("O has been successfully placed" + playerTwo(gameBoard,row,col));
          }
          System.out.println("Who's turn is it? For Player 1 Hit 1, For Player 2 Hit 2");
          playerTurn = input.nextInt();
          break;
        default:
          System.out.println("Invalid Choice");
      }
      turns--;
    }
    printBoard(gameBoard);
  }  
  
  public static void fillBoard(char[][] gameBoard)
  {
    for(int i = 0; i < gameBoard.length;i++)
    {
      for(int j=0;j < gameBoard[0].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
  }
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[0].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        return;
        }
      System.out.println();
      }
    }
    
    public static boolean isFilled(char[][] gameBoard, int row, int col)
    {
      boolean result;
      if (gameBoard[row][col] == 0)
        result = true;
      else
        result = false;
      return result;
    }
            
   public static char playerOne(char[][] gameBoard, int row, int col)
   {
     return gameBoard[row][col] = 'X';
   }
   public static char playerTwo(char[][] gameBoard, int row, int col)
   {
     return gameBoard[row][col] = 'O';
   }
  }

Looks like you are on the right track. Few comments:

  • You fill the board at the beginning with "_" and then with "X" or "O" depending on the user, yet your isFilled method is checking if (gameBoard[row][col] == 0) - will be a little problematic :)
  • As I mentioned before, you don't have to ask the user who's turn it is each time - either you determine that it is always player's one turn (you don't keep scores for multiple games so you don't care) or you can simply use a boolean variable - the first time as who wants to start the game, and then flip the boolean value each time:
    boolean playerOneTurn; //true means it's player 1 turn, false player 2
    System.out.println("Who is going to start? For Player 1 Hit 1, For Player 2 Hit 2");
    playerTurn = input.nextInt();
    if(playerTurn == 1)
    {
       playerOneTurn = true; 
    }
    else
    {
       playerOneTurn = false;
    }

    And at the end of each turn remember to switch:

    playerOneTurn = !playerOneTurn;
  • Not a real problem, but fyi - you are using a variable turns and a while loop, when you have the for loop:
    for(int turns = 0; turns < 9; ++turns)

    Will do the same trick in a more elegant fashion.

Edited 6 Years Ago by apines: n/a

How do you flip boolean variables? and Is that boolean code supposed to be a method? And does this mean the case switch menu system should be taken out? I suppose it would make less code for the main method.

Yes it will remove the current switch statement, and as I wrote to flip a boolean variable all you have to do is booleanVar = !booleanVar; - whether it will be in a method or not it is up to you, try to implement and see how it goes. Again, the boolean var is not necessary, it is an optional solution to not asking the users who's turn is it all the time - think about it, when you are playing against someone, don't you expect the program to be smart enough to know that if you just played, it's your opponent's turn now and vice versa?

How about this?
Getting closer? Hopefully...

import java.util.*;

  public class ticTacToeMadness2
  {
    public static void main(String[] args)
    {
      char[][] gameBoard = new char[3][3];
      int playerTurn;
      int row;
      int col;
      Scanner input = new Scanner(System.in);
    
      fillBoard(gameBoard);
      
      for (int turns = 0; turns < 9; turns++)
      {      
      System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2");
      System.out.println("Player 1 is X  and Player 2 is O");
      playerTurn = input.nextInt();
      
      if(isPlayerTurn(playerTurn))
      {
        System.out.println("Player 1 Enter Row");
          row = input.nextInt();
          System.out.println("Player 1 Enter Column");
          col = input.nextInt();
          if (isFilled(gameBoard,row,col))
          {
            System.out.println("This Spot is Filled!");
          }
          else
          {
            System.out.println("X has been successfully placed" + playerOne(gameBoard,row,col));
          }
      }
      else if(!isPlayerTurn(playerTurn))
      {
        System.out.println("Player 2 Enter Row");
          row = input.nextInt();
          System.out.println("Player 2 Enter Column");
          col = input.nextInt();
          if (isFilled(gameBoard,row,col))
          {
            System.out.println("This Spot is Filled!");
          }
          else
          {
            System.out.println("O has been successfully placed" + playerTwo(gameBoard,row,col));
          }
      }
      playerTurn = input.nextInt();
      }
      
    
    printBoard(gameBoard);
  }  
  
  public static void fillBoard(char[][] gameBoard)
  {
    for(int i = 0; i < gameBoard.length;i++)
    {
      for(int j=0;j < gameBoard[0].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
  }
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[0].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        return;
        }
      System.out.println();
      }
    }
    
    public static boolean isFilled(char[][] gameBoard, int row, int col)
    {
      boolean result;
      if (gameBoard[row][col] == 0)
        result = true;
      else
        result = false;
      return result;
    }
            
   public static char playerOne(char[][] gameBoard, int row, int col)
   {
     return gameBoard[row][col] = 'X';
   }
   public static char playerTwo(char[][] gameBoard, int row, int col)
   {
     return gameBoard[row][col] = 'O';
   }
   
   public static boolean isPlayerTurn(int playerTurn)
   {
     boolean playerOneTurn;
     if (playerTurn == 1)
      {
        playerOneTurn = true;
      }
      else
      {
        playerOneTurn = false;
      }
      return playerOneTurn;
  }
  }

Your isFilled method is still checking for 0, and every turn you ask who's going first. I didn't get what you have tried to do... Please read my previous comments again.

Edited 6 Years Ago by apines: n/a

Well I tried to get my isFilled to test for = '_' which is my blank spot in 2D array. But it has an issue with that since it gets a char value and it expects a boolean variable. So I put in the == 0 just like from the airplane reservation program b/c I figured that was the thing to do in a boolean method. Sorry if perhaps I must of misunderstood this. Booleans are slightly confusing to me since they always end up dealing with a double negative situation. And I have been reading the previous comments over and over and attempting to make some sort of sense. You guys are so pro at this it is kind of nuts and a little intimidating b/c it seems to me you guys don't even need to use a compiler to tell me what is wrong with my code and what would be best to do.
And as for understanding what I did to my code.....
I attempted to take out the switch menu and somewhat do as you say to switch between players. I didn't understand the placement of your !playerTurn boolean or your declaration of it and where that goes since I thought I had made a method that did that. Sorry for driving you folks crazy ^_^

Ok I changed my loop to a while loop.....added a new boolean.....and changed things in the if/else in my main.
Thoughts? opinions?

import java.util.*;

  public class ticTacToeMadness2
  {
    public static void main(String[] args)
    {
      char[][] gameBoard = new char[3][3];
      int playerTurn;
      int row;
      int col;
      Scanner input = new Scanner(System.in);
    
      fillBoard(gameBoard);
      
      System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2");
      System.out.println("Player 1 is X  and Player 2 is O");
      playerTurn = input.nextInt();
      
      while (!isBoardFull(gameBoard))
      {      
    
      if(isPlayerTurn(playerTurn))
      {
        System.out.println("Player 1 Enter Row");
          row = input.nextInt();
          System.out.println("Player 1 Enter Column");
          col = input.nextInt();
          if (isFilled(gameBoard,row,col))
          {
            System.out.println("This Spot is Filled!");
          }
          else
          {
            gameBoard[row][col] = 'X';
          }
      }
      else if(!isPlayerTurn(playerTurn))
      {
        System.out.println("Player 2 Enter Row");
          row = input.nextInt();
          System.out.println("Player 2 Enter Column");
          col = input.nextInt();
          if (isFilled(gameBoard,row,col))
          {
            System.out.println("This Spot is Filled!");
          }
          else
          {
            gameBoard[row][col] = 'O';
          }
      }
      System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
      playerTurn = input.nextInt();
      }
      
    
    printBoard(gameBoard);
  }  
  
  public static void fillBoard(char[][] gameBoard)
  {
    for(int i = 0; i < gameBoard.length;i++)
    {
      for(int j=0;j < gameBoard[0].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
  }
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[0].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        return;
        }
      System.out.println();
      }
    }
    
    public static boolean isFilled(char[][] gameBoard, int row, int col)
    {
      boolean result;
      if (gameBoard[row][col] == '_')
        result = false;
      else
        result = true;
      return result;
    }
   
   public static boolean isPlayerTurn(int playerTurn)
   {
     boolean playerOneTurn;
     if (playerTurn == 1)
      {
        playerOneTurn = true;
      }
      else
      {
        playerOneTurn = false;
      }
      return playerOneTurn;
  }
   public static boolean isBoardFull(char[][] gameBoard)
   {
     boolean result;
     for (int row = 0; row < 3; row++)
     {
       for (int col = 0; col < 3; col++)
            {
         if ((gameBoard[row][col] == 'O')||(gameBoard[row][col] == 'X'))
           return false;
       }
     }
     return true;
   }
  }

I don't think you understood me completely - you don't need the isPlayerTurn(int playerTurn) method.

System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2");
playerTurn = input.nextInt();
if(playerTurn == 1)  //you can use a switch statement as well.
{
   playerOneTurn = true;
}
else if(playerTurn == 2)
{
   playerOneTurn = false;  
}
else
{
  //the user entered invalid input
}

Now, in the loop, you know that if playerOneTurn = true; , it's player 1's turn, otherwise player 2's turn. At the end of the turn, inside the loop, simply do

playerOneTurn = !playerOneTurn;

And you have switched the players. No need for another method that will determine who's turn it is.

Does the program, as it is, works? I mean, do you want code review or help with errors you encounter? Both things are fine of course, and we'll be happy to help, I just want to know where you stand right now.

Omg YES please please review away.....I really would love that because it would let me know where I stand in my growth in the subject of programming java. Also, error help would be very helpful too.

I also asked whether the program right now works as you wish it to work or do you encounter any errors (as in exceptions, unexpected output etc')... help me to help you :)

I know hehe I just got home from work bleh but I wanted to make sure I put response out right away.
Anyways you did say I could use a switch again if I wanted and that is what I did b/c I didn't understand where to put all of my methods with what you posted unless I would do something like:

System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2, To Print the Board press 3");
      System.out.println("Player 1 is X  and Player 2 is O");
      playerTurn = input.nextInt();
      
      while (!isBoardFull(gameBoard))
      {      
        if(playerTurn == 1)
        {
          playerOneTurn = true;
          System.out.println("Player 1 Enter Row");
            row = input.nextInt();
            System.out.println("Player 1 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'X';
            }
        }
        else if(playerTurn == 2)
        {
          playerOneTurn = false;  
          System.out.println("Player 2 Enter Row");
            row = input.nextInt();
            System.out.println("Player 2 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'O';
            }
        }
        else
        {
          System.out.println("Invalid Option!");
          
        }
        System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
            playerTurn = input.nextInt();
      }

But here is the program I have so far after reading your post:

import java.util.*;

  public class ticTacToeMadness3
  {
    public static void main(String[] args)
    {
      char[][] gameBoard = new char[3][3];
      int playerTurn;
      int row;
      int col;
      Scanner input = new Scanner(System.in);
    
      fillBoard(gameBoard);
      
      System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2, To Print the Board press 3");
      System.out.println("Player 1 is X  and Player 2 is O");
      playerTurn = input.nextInt();
      
      while (!isBoardFull(gameBoard))
      {      
        switch(playerTurn)
        {
          case 1:
            System.out.println("Player 1 Enter Row");
            row = input.nextInt();
            System.out.println("Player 1 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'X';
            }
            System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
            playerTurn = input.nextInt();
            break;
          case 2:
            System.out.println("Player 2 Enter Row");
            row = input.nextInt();
            System.out.println("Player 2 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'O';
            }
            System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
            playerTurn = input.nextInt();
            break;
          case 3:
            printBoard(gameBoard);
            break;
          default:
            System.out.println("Invalid Choice");
            break;
        }
        System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
        playerTurn = input.nextInt();
      }
          
    
    printBoard(gameBoard);
  }  
  
  public static void fillBoard(char[][] gameBoard)
  {
    for(int i = 0; i < gameBoard.length;i++)
    {
      for(int j=0;j < gameBoard[0].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
  }
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[0].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        return;
        }
      System.out.println();
      }
    }
    
    public static boolean isFilled(char[][] gameBoard, int row, int col)
    {
      boolean result;
      if (gameBoard[row][col] == '_')
        result = false;
      else
        result = true;
      return result;
    }
   
   public static boolean isBoardFull(char[][] gameBoard)
   {
     boolean result;
     for (int row = 0; row < 3; row++)
     {
       for (int col = 0; col < 3; col++)
            {
         if ((gameBoard[row][col] == 'O')||(gameBoard[row][col] == 'X'))
           return false;
       }
     }
     return true;
   }
  }

And after picking player one it just outputs this:
_>

bleh

Please read my previous comments again. You still ask each turn who's turn to play and you changed your isBoardFull method so now they will not work.

public static boolean isBoardFull(char[][] gameBoard)
{
   boolean result;
   for (int row = 0; row < 3; row++)
   {
      for (int col = 0; col < 3; col++)
      {
          if ((gameBoard[row][col] == 'O')||(gameBoard[row][col] == 'X'))
          {
             return false;
          }
      }
   }
   return true;
}

The first time that the method will encounter a cell (a single cell) with 'O' or 'X' it will return false, as in the board is not full.

I keep reading the comments over and over and I am still confused as to the purpose of your if/else.....so is it JUST determining who is going first and based on that it no longer asks which player's turn it is...it just goes back and forth?
And I am not sure how to fix my isBoardFull.....I thought it was a good idea for my while loop for the entire thing. The loop I had based on how many turns was flawed b/c what if the user chose a spot that was already filled? Well that would still count as a turn and therefore it is possible with other error choices that the turns could run out before there was a winner.
Anyways is this possibly what you meant with the if/else?

import java.util.*;

  public class ticTacToeMadness4
  {
    public static void main(String[] args)
    {
      char[][] gameBoard = new char[3][3];
      int playerTurn;
      int row;
      int col;
      Scanner input = new Scanner(System.in);
      boolean playerOneTurn;
    
      fillBoard(gameBoard);
      
      System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2, To Print the Board press 3");
      System.out.println("Player 1 is X  and Player 2 is O");
      playerTurn = input.nextInt();
      
      while (!isBoardFull(gameBoard))
      {      
       System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2");
       playerTurn = input.nextInt();
       
       if(playerTurn == 1) 
       {
         playerOneTurn = true;
       }
       else if(playerTurn == 2)
       {
         playerOneTurn = false;  
       }
       else
       {
         System.out.println("Invalid Input!");
       }
       if(playerOneTurn = true)
       {
         System.out.println("Player 1 Enter Row");
            row = input.nextInt();
            System.out.println("Player 1 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'X';
            }
       }
       else if (!playerOneTurn)
       {
         System.out.println("Player 2 Enter Row");
            row = input.nextInt();
            System.out.println("Player 2 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'O';
            }
       }
       playerOneTurn = !playerOneTurn;
      }
          
    
    printBoard(gameBoard);
  }  
  
  public static void fillBoard(char[][] gameBoard)
  {
    for(int i = 0; i < gameBoard.length;i++)
    {
      for(int j=0;j < gameBoard[0].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
  }
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[0].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        return;
        }
      System.out.println();
      }
    }
    
    public static boolean isFilled(char[][] gameBoard, int row, int col)
    {
      boolean result;
      if (gameBoard[row][col] == '_')
        result = false;
      else
        result = true;
      return result;
    }
   
   public static boolean isBoardFull(char[][] gameBoard)
   {
     boolean result;
     for (int row = 0; row < 3; row++)
     {
       for (int col = 0; col < 3; col++)
            {
         if ((gameBoard[row][col] == 'O')||(gameBoard[row][col] == 'X'))
           return false;
       }
     }
     return true;
   }
  }

The way I am interpreting what I am reading from the comments is that your if/else just decides only who goes first....and then at the end it sets it to false so that it goes all over again and asks.
Also.....my switch based off of your if/else was no good?

I was not talking about the while loop, I said that the method isBoardFull(char[][] gameBoard) returns the wrong value - look at the method, take different matrices and try to run the method on them, you will see that it returns a wrong value. Regarding the boolean variable - yes it is for eliminating the need for asking the user who's turn it is - read the comments again please regarding how to use it.

I am going to be completely honest with you that I have read your comments over and over again and simply cannot get how to properly use your if/else.
I am trying my hardest to understand it but can't figure out where to go about doing all of the input needed. And how to move to the next player.
The switch however DOES work I believe now.....It lets me go from player to play and it lets me know when the board is full and when a spot is full.
MY issue as it stands is the output. It gives me _X> at the end which is weird.
I don't want to anger you or anything by continuously disregard your if/else in my program it is just even after thorough reading I can't grasp the idea.

import java.util.*;

  public class ticTacToeMadness3
  {
    public static void main(String[] args)
    {
      char[][] gameBoard = new char[3][3];
      int playerTurn;
      int row;
      int col;
      Scanner input = new Scanner(System.in);
    
      //fillBoard(gameBoard);
      for(int i = 0; i < gameBoard.length;i++)
    {
      for(int j=0;j < gameBoard[0].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
      
      
      System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2, To Print the Board press 3");
      System.out.println("Player 1 is X  and Player 2 is O");
      playerTurn = input.nextInt();
      
      while (!isBoardFull(gameBoard))
      {      
        switch(playerTurn)
        {
          case 1:
            System.out.println("Player 1 Enter Row");
            row = input.nextInt();
            System.out.println("Player 1 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'X';
            }
            System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
            playerTurn = input.nextInt();
            break;
          case 2:
            System.out.println("Player 2 Enter Row");
            row = input.nextInt();
            System.out.println("Player 2 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'O';
            }
            System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
            playerTurn = input.nextInt();
            break;
          case 3:
            printBoard(gameBoard);
            break;
          default:
            System.out.println("Invalid Choice");
            break;
        }
      }
          
    
    printBoard(gameBoard);
  }  
  
  public static void fillBoard(char[][] gameBoard)
  {
    for(int i = 0; i < gameBoard.length;i++)
    {
      for(int j=0;j < gameBoard[0].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
  }
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[0].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        return;
        }
      System.out.println();
      }
    }
    
    public static boolean isFilled(char[][] gameBoard, int row, int col)
    {
      boolean result;
      if (gameBoard[row][col] == '_')
        result = false;
      else
        result = true;
      return result;
    }
   
   public static boolean isBoardFull(char[][] gameBoard)
   {
     boolean result;
     for (int row = 0; row < 3; row++)
     {
       for (int col = 0; col < 3; col++)
            {
         if (gameBoard[row][col] == '_')
           return false;
             }
     }
     return true;
   }
  }

I will do my best to read over it some more though. But perhaps at the moment there is an issue with my print method?

omg omg omg I think I fixed it!!!!!
I get this output:
XOX
OXO
XOO

This is my code now:

import java.util.*;

  public class ticTacToeMadness3
  {
    public static void main(String[] args)
    {
      char[][] gameBoard = new char[3][3];
      int playerTurn;
      int row;
      int col;
      Scanner input = new Scanner(System.in);
    
      //fillBoard(gameBoard);
      for(int i = 0; i < gameBoard.length;i++)
      {
      for(int j = 0;j < gameBoard[i].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
       printBoard(gameBoard);
      
      System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2, To Print the Board press 3");
      System.out.println("Player 1 is X  and Player 2 is O");
      playerTurn = input.nextInt();
      
      while (!isBoardFull(gameBoard))
      {      
        switch(playerTurn)
        {
          case 1:
            System.out.println("Player 1 Enter Row");
            row = input.nextInt();
            System.out.println("Player 1 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'X';
            }
            System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
            playerTurn = input.nextInt();
            break;
          case 2:
            System.out.println("Player 2 Enter Row");
            row = input.nextInt();
            System.out.println("Player 2 Enter Column");
            col = input.nextInt();
            if (isFilled(gameBoard,row,col))
            {
              System.out.println("This Spot is Filled!");
            }
            else
            {
              gameBoard[row][col] = 'O';
            }
            System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
            playerTurn = input.nextInt();
            break;
          case 3:
            printBoard(gameBoard);
            break;
          default:
            System.out.println("Invalid Choice");
            break;
        }
      }
          
    
    printBoard(gameBoard);
  }  
  
  public static void fillBoard(char[][] gameBoard)
  {
    for(int i = 0; i < gameBoard.length;i++)
    {
      for(int j=0;j < gameBoard[0].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
  }
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[row].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        }
      System.out.println();
      }
    }
    
    public static boolean isFilled(char[][] gameBoard, int row, int col)
    {
      boolean result;
      if (gameBoard[row][col] == '_')
        result = false;
      else
        result = true;
      return result;
    }
   
   public static boolean isBoardFull(char[][] gameBoard)
   {
     boolean result;
     for (int row = 0; row < 3; row++)
     {
       for (int col = 0; col < 3; col++)
            {
         if (gameBoard[row][col] == '_')
           return false;
             }
     }
     return true;
   }
  }

Tell me what you think! =)

What have you fixed? you are still asking each turn who's turn it is. Think about it like this - you are inside the switch statement, case 1, meaning you know it's player 1's turn - and at the end you still ask

System.out.println("Next Turn! For Player 1 Hit 1, For Player 2 Hit 2");
playerTurn = input.nextInt();

Is this what the exercise specification state? Why not use the fact that you know who is playing now in order to set who is playing next, and each turn automatically print the screen because the players will want to look at the updated board?

Ugh you are right......I just couldn't figure out the if/else you posted even after reading the comments. I just wanted to see the program finally work and do what I wanted regardless of the player aspect and thankfully it finally does.
With your if/else I didn't know where I would place all of my methods. Booleans are a weak point for me and I realize this.

System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2");
playerTurn = input.nextInt();
if(playerTurn == 1) 
{
   playerOneTurn = true;
}
else if(playerTurn == 2)
{
   playerOneTurn = false;  
}
else
{
  System.out.println("Invalid Input!');
}

//And then this is where my if/else to check if a spot is full and place an X or an O goes?
//and then I put that playerOneTurn = !playerOneTurn?
//And then it knows to go back to the beginning of the loop but how does it know to do the whole player 2 thing?

Here is a sample code to demonstrate our problem:

public static void main(String args)
{
  String player1 = "Josh"; 
  String player2 = "David";
  boolean isTurnJosh = true; // if isTurnJosh = true, it's Josh's turn, otherwise it is David's.
  while(true)
  { 
     if(isTurnJosh)
     {
        System.out.println("Hi Josh, I know that this is your turn because I am using a boolean variable");
     }
     else
     {
        System.out.println("Hi David, Where is Josh?"); 
     }
     isTurnJosh = !isTurnJosh;
  }
}

Now, can you see how a boolean variable can be your friend? :)

Edited 6 Years Ago by apines: n/a

So my interpretation of your if/else was totally off?
I understand somewhat of what it is trying to do.
It just wants to let me know who is going first.....so then where do my methods of placing the X and the O fit in? within the if/else?
and if you are setting at the end of the if/else that playerOneTurn = !playerOneTurn then how would you go through the thing again? Or if that just determines who goes first and then everything else comes after your if/else how would it know to ask the 2nd player for input? wouldn't it only ask for input from just the first player?
Let me attempt some sort of pseudo code even though I am horrible at that as well.

//Your If/Else
//Player One Turn if/else that places an X and checks if spot is filled
//Player Two Turn if/else that places an O and checks if spot is filled
//playerOneTurn = !playerOneTurn;
//returns to the beginning of the player one turn and constantly allows input from both users until the board is full?

Edited 6 Years Ago by Tankadin: n/a

Now I think that it is time that you take the code and try to run some examples for yourself, see how it works.

Please please please tell me I've finally captured what you have meant with the boolean stuff LOL
Ok I must tell you though that I had an error until I initialized playerOneTurn in main to true. Leaving it just as boolean playerOneTurn; didn't work out.

import java.util.*;

  public class ticTacToeMadness4
  {
    public static void main(String[] args)
    {
      char[][] gameBoard = new char[3][3];
      int playerTurn;
      int row;
      int col;
      Scanner input = new Scanner(System.in);
      boolean playerOneTurn = true;
    
      fillBoard(gameBoard);
      
      System.out.println("Who Wants to Begin? For Player 1 Hit 1, For Player 2 Hit 2");
      System.out.println("Player 1 is X  and Player 2 is O");
      playerTurn = input.nextInt();
      
      while (!isBoardFull(gameBoard))
      {      
        if(playerTurn == 1) 
       {
         playerOneTurn = true;
       }
       else if(playerTurn == 2)
       {
         playerOneTurn = false;  
       }
       else
       {
         System.out.println("Invalid Input!");
       }
       System.out.println("Player 1 Enter Row");
       row = input.nextInt();
       System.out.println("Player 1 Enter Column");
       col = input.nextInt();
       if (isFilled(gameBoard,row,col))
       {
         System.out.println("This Spot is Filled!");
       }
       else
       {
         gameBoard[row][col] = 'X';
       }
      System.out.println("Player 2 Enter Row");
      row = input.nextInt();
      System.out.println("Player 2 Enter Column");
      col = input.nextInt();
      if (isFilled(gameBoard,row,col))
      {
        System.out.println("This Spot is Filled!");
      }
      else
      {
        gameBoard[row][col] = 'O';
      }
      playerOneTurn = !playerOneTurn;
      }   
    printBoard(gameBoard);
    }  
  
  public static void fillBoard(char[][] gameBoard)
  {
    for(int i = 0; i < gameBoard.length;i++)
    {
      for(int j=0;j < gameBoard[0].length;j++)
      {
        gameBoard[i][j] = '_';
      }
    }
  }
    public static void printBoard(char[][] gameBoard)
    {
      for (int row = 0; row < gameBoard.length; row++)
      {
        for (int col = 0; col < gameBoard[row].length; col++)
        {
        System.out.print(gameBoard[row][col]);
        }
      System.out.println();
      }
    }
    
    public static boolean isFilled(char[][] gameBoard, int row, int col)
    {
      boolean result;
      if (gameBoard[row][col] == '_')
        result = false;
      else
        result = true;
      return result;
    }
   
   public static boolean isBoardFull(char[][] gameBoard)
   {
     boolean result;
     for (int row = 0; row < 2; row++)
     {
       for (int col = 0; col < 2; col++)
            {
         if (gameBoard[row][col] == '_')
           return false;
       }
     }
     return true;
   }
  }

Well I take it from that comment that I did this wrong again.
And I did not ask you to write the code for me, thank you.
I am only trying to understand what your if/else does and why it is better than my switch. This did work this way by the way. But I didn't run the program till it printed I just tested out a few turns.

This question has already been answered. Start a new discussion instead.