Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By sehudson

Thread: Tic Tac Toe game!!

  1. #1
    GoBreak is offline Member
    Join Date
    Jul 2014
    Posts
    2
    Rep Power
    0

    Angry Tic Tac Toe game!!

    How in the world would you fix the if statement in the "won" method in this Tic Tac Toe game, its hurting my brain!!!
    and also a few other Java forum people told me my stalemate method is also another problem!!!

    Java Code:
    import java.util.Scanner;
     
    public class TicTacToe{
     
     
     
       private char[][] board;
       private char player; // 'X' or 'O'
     
     
       /*
        * Instantiate board to be a 3 by 3 char array of spaces.
        * Set player to be 'X'.
        */
       public TicTacToe() {
       player = 'X';
           board = new char[3][3];
           for (int i = 0; i <= 2; i++) {
               for (int j = 0; j <= 2; j++) {
               board[i][j] = ' ';
           }
           }
       }
     
     
     
       /*
        * If s represents a valid move, add the current player's symbol to the board and return true.
        * Otherwise return false.
        */
       public boolean play(String s) {
       int row = 0, column = 0;
       if(s.toLowerCase().contains("a")) {
           row = 0;
       } if(s.toLowerCase().contains("b")) {
           row = 1;
       }if(s.toLowerCase().contains("c")) {
           row = 2;
       }if(s.toLowerCase().contains("1")) {
           column = 0;
       }if(s.toLowerCase().contains("2")) {
           column = 1;
       }if(s.toLowerCase().contains("3")) {
           column = 2;
       } if(board[row][column] != ' ') {
           board[row][column] = getPlayer();
           return true;
       } else {
           return false;
       }
       }
     
       /*
        * Switches the current player from X to O, or O to X.
        */
       public void switchTurn() {
    if (player == 'X') {
         player = 'O';
       } else {
           player = 'X';
       }
       }
     
       /*
        * Returns true if the current player has won the game.
        * Three in a row, column or either diagonal.
        * Otherwise, return false.
        */
       public boolean won() {
     //Rows
     if((board[0][0] == board[0][1]) && (board[0][1] == board[0][2])) {
          return true;
     }if((board[1][0] == board[1][1]) && (board[1][1] == board[1][2])) {
          return true;
     }if((board[2][0] == board[2][1]) && (board[2][1] == board[2][2])) {
          return true;
    //Columns
     }if((board[0][0] == board[1][0]) && (board[1][0] == board[2][0])) {
          return true;
     }if((board[0][1] == board[1][1]) && (board[1][1] == board[2][1])) {
          return true;
     }if((board[0][2] == board[1][2]) && (board[1][2] == board[2][2])) {
          return true;
     }
     //Diagonals
     if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) {
          return true;
     }if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) {
          return true;
     } else {
          return false;
     }
     
     
       }
     
       /*
        * Returns true if there are no places left to move
        */
       public boolean stalemate() {
           for (int i = 0; i < 3; i++) {
               for (int j = 0; j < 3; j++) {
                   if (board[i][j] != ' ') {
                       return false;
                   } else {
                       return true;
                   }
               }
           }
        return true;
       }
       public char getPlayer() {
           return player;
       }
       public void print() {
           System.out.println();
           System.out.println("\t  1 2 3");
           System.out.println();
           System.out.println("\tA "+board[0][0]+"|"+board[0][1]+"|"+board[0][2]);
           System.out.println("\t  -----");
           System.out.println("\tB "+board[1][0]+"|"+board[1][1]+"|"+board[1][2]);
           System.out.println("\t  "+"-----");
           System.out.println("\tC "+board[2][0]+"|"+board[2][1]+"|"+board[2][2]);
           System.out.println();
       }
     
     
       public static void main(String[] args) {
     
           Scanner in = new Scanner(System.in);
           TicTacToe game = new TicTacToe();
           System.out.println("Welcome to tic-tac-toe");
           System.out.println("Enter coordinates for your move following the X and O prompts");
     
           while(!game.stalemate()) {
              game.print();
              String move = in.next();
              if(game.play(move)) {
                  game.switchTurn();
              } else {
                  System.out.println("Enter a valid move");
                  break;
              }
     
     
     
               //If the game is won, call break;
              if(game.won()) {
                  break;
              }
     
     
           }
           if(game.won()){
               System.out.println("Player "+game.getPlayer()+" Wins!!!!");
           } else {
               System.out.println("Stalemate");
           }
       }
    }
    Last edited by GoBreak; 07-16-2014 at 05:49 PM.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Tic Tac Toe game!!

    Not looking too far into your code, your stalemate method does seem flawed:

    Java Code:
       public boolean stalemate() {
           for (int i = 0; i < 3; i++) {
               for (int j = 0; j < 3; j++) {
                   if (board[i][j] != ' ') {
                       return false;
                   } else {
                       return true;
                   }
               }
           }
        return true;
       }
    The comment above the method says that it 'Returns true if there are no places left to move'
    The way your method is written, the char at board[0][0] is going to determine what the method returns, and no other board spaces will be evaluated.
    Do you see why?

    If you think about it logically, you want your method to essentially return whether or not all of the spaces are occupied.
    So, you could start with something like:

    Java Code:
       public boolean stalemate() {
          boolean allSpacesOccupied = true;
          //core logic
          return allSpacesOccupied;
       }
    Again, I haven't looked at all of your code, but based on what the comment says for that particular method, it isn't logically correct. At least for me, I like to think of things in terms of small units of functionality. So, if you're going to have a method that is supposed to return whether or not there is an empty space left on the board, and it's crucial to your game, tackle that first, and make sure it's giving you the correct output. Then, move on to the next part. Take it in steps.
    Last edited by sehudson; 07-16-2014 at 07:27 PM.

  3. #3
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default Re: Tic Tac Toe game!!

    You say how would you fix it? whats wrong with it? explain more

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Tic Tac Toe game!!

    At the point in your method when the return statement is reached, no more of that method is evaluated.
    So the point that I was trying to make is that your method is written such that the very first time that your inner loop is
    run, the method will return either true or false. Your method says if it's not an empty space, return false, else return true.
    So one of those 2 will hold true, it either will be a space, or it won't, so the method will return. That isn't what you want.

    You need to rethink your logic. You want to continue to look at each individual char in the board[][].
    If you find a ' ', you can safely return false, because you know that there is at least one empty space.
    If you come across a non ' ', that doesn't mean that you want to return true, which is what your code is doing.
    So the hint that I was trying to give you with the code outline was how you can think about it.
    You only want to return when you are sure that either:

    -There is at least one empty space. At the point in your method that you can say with certainty that there is at least one empty space, return false.
    or
    -There are no empty spaces. At the point in your method that you can say with certainty that there are NO empty spaces, return true.

    It really is fairly simple, I'm just trying to get you to think about your logic without writing the code for you.
    It boils down to a logic issue that you have in that particular method.
    Start with the outline I gave you, if you want to, and rethink the logic inside your nested for loop.
    Last edited by sehudson; 07-17-2014 at 04:48 AM.
    gimbal2 likes this.

Similar Threads

  1. Replies: 2
    Last Post: 03-03-2014, 06:48 PM
  2. Replies: 4
    Last Post: 07-08-2012, 09:07 AM
  3. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  4. Replies: 2
    Last Post: 08-05-2010, 04:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •