Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Why is my program not working....Help pls

    Hi

    I have this program that is a tic-tac-toe game
    I was adding a method called "getWinner" for it so it would print out the person who won it...
    either "X" or "O" .

    I have two programs, one "TicTacToe" and the other "TicTacToeTester"

    I have tried to get this additional method to work properly by using both arrays and also the old fashioned if statements.

    Im a beginner hence im not very well versed in advanced java concepts

    this is the code for the TicTacToe program(i have commented out the other method i tried(using 2 dimensional arrays) both work..but sometimes they do not work...i do not know why...I have tried for 2 days now..non-stop;lest i be termed as if im asking for help without trying.lol
    I will be very grateful if any of you can tell me where the problem is.



    TicTacToe.java
    Code:
    /**
       A 3 x 3 tic-tac-toe board.
    */
    public class TicTacToe
    {
       String result;
       /**
          Constructs an empty board.
       */
       public TicTacToe()
       {
          result = " ";
          
          board = new String[ROWS][COLUMNS];
          // Fill with spaces
          for (int i = 0; i < ROWS; i++)
             for (int j = 0; j < COLUMNS; j++)
                board[i][j] = " ";
       }
    
       /**
          Sets a field in the board. The field must be unoccupied.
          @param i the row index 
          @param j the column index 
          @param player the player ("x" or "o")
       */
       public void set(int i, int j, String player)
       {
          if (board[i][j].equals(" "))
             board[i][j] = player;
       }
    
       
       
       
       public String getWinner()
       {
         //----------------------------------------------------------------------------------
          /**
          //for for comparing rows
          for(int i=0; i<ROWS; i++)
          {
             
             //if for x
                if((board[i][0].equalsIgnoreCase("x")&& board[i][1].equalsIgnoreCase("x"))||(board[i][2].equalsIgnoreCase("x")&& board[i][1].equalsIgnoreCase("x")))
                {
                   if((board[i][1].equalsIgnoreCase("x")&& board[i][2].equalsIgnoreCase("x"))||(board[i][0].equalsIgnoreCase("x")&& board[i][1].equalsIgnoreCase("x")))
                   {    
                      System.out.println("In first for -row-x");
                      result="X";
                      return result;
                   }
                   else
                   {
                      return result;
                   }
                }
                
             //if for o
                
                else if((board[i][0].equalsIgnoreCase("o")&& board[i][1].equalsIgnoreCase("o"))||(board[i][2].equalsIgnoreCase("o")&& board[i][1].equalsIgnoreCase("o")))
                {
                   if((board[i][1].equalsIgnoreCase("o")&& board[i][2].equalsIgnoreCase("o"))||(board[i][0].equalsIgnoreCase("o")&& board[i][1].equalsIgnoreCase("o")))
                   {
                      System.out.println("In first for -row-o");
                      result="O";
                      return result;
                   }
                   else
                   {
                      return result;
                   }
                }
                
          }   //end for for 1(rows)
           
          
          //for comparing columns
          for (int j=0; j<COLUMNS; j++)
          {
             
             //if for x
             if((board[0][j].equalsIgnoreCase("x")&& board[1][j].equalsIgnoreCase("x"))||(board[2][j].equalsIgnoreCase("x")&& board[1][j].equalsIgnoreCase("x")))
             {
                if((board[1][j].equalsIgnoreCase("x")&& board[2][j].equalsIgnoreCase("x"))||(board[0][j].equalsIgnoreCase("x")&& board[1][j].equalsIgnoreCase("x")))
                {
                   System.out.println("In second for -column-x");
                   result="x";
                   return result;
                }
                else
                {
                   return result;
                }
             }
             
             
             //if for o
             else if((board[0][j].equalsIgnoreCase("o")&& board[1][j].equalsIgnoreCase("o"))||(board[2][j].equalsIgnoreCase("o")&& board[1][j].equalsIgnoreCase("o")))
             {
                if((board[1][j].equalsIgnoreCase("o")&& board[2][j].equalsIgnoreCase("o"))||(board[0][j].equalsIgnoreCase("o")&& board[1][j].equalsIgnoreCase("o")))
                {   
                   System.out.println("In second for -row-o");
                   result="O";
                   return result;
                }
                else
                {
                   return result;
                }
             }
             
             
          }//end for for j(columns)
             
          
          
          //if for crosses
          if(board[0][0].equalsIgnoreCase("x")&& board[1][1].equalsIgnoreCase("x"))
          {
             if(board[1][1].equalsIgnoreCase("x")&& board[2][2].equalsIgnoreCase("x"))
             {
                System.out.println("In first cross -x");
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          else if(board[0][0].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o"))
          {
             if(board[1][1].equalsIgnoreCase("o")&& board[2][2].equalsIgnoreCase("o"))
             { 
                System.out.println("In first for -O");
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          
          
          else if(board[0][2].equalsIgnoreCase("x")&& board[1][1].equalsIgnoreCase("x"))
          {
             if(board[1][1].equalsIgnoreCase("x")&& board[2][0].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          else if(board[0][2].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o"))
          {
             if(board[1][1].equalsIgnoreCase("o")&& board[2][0].equalsIgnoreCase("o"))
             {
                result="O";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          return result;
       }   
          */
    //-----------------------------------------------------------------------------------------
         
    //    if for x row 0
          if(board[0][0].equalsIgnoreCase("x")&& board[0][1].equalsIgnoreCase("x"))
          {
             if(board[0][1].equalsIgnoreCase("x")&& board[0][2].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          //changed
          else if(board[0][1].equalsIgnoreCase("x")&& board[0][2].equalsIgnoreCase("x"))
          {
             if(board[0][0].equalsIgnoreCase("x")&& board[0][1].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          
          
          //if for x row 1
          else if(board[1][0].equalsIgnoreCase("x")&& board[1][1].equalsIgnoreCase("x"))
          {
             if(board[1][1].equalsIgnoreCase("x")&& board[1][2].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          //changed
          else if(board[1][1].equalsIgnoreCase("x")&& board[1][2].equalsIgnoreCase("x"))
          {
             if(board[1][0].equalsIgnoreCase("x")&& board[1][1].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          
          //if for x row 2
          else if(board[2][0].equalsIgnoreCase("x")&& board[2][1].equalsIgnoreCase("x"))
          {
             if(board[2][1].equalsIgnoreCase("x")&& board[2][2].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          //changed
          else if(board[2][1].equalsIgnoreCase("x")&& board[2][2].equalsIgnoreCase("x"))
          {
             if(board[2][2].equalsIgnoreCase("x")&& board[2][1].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          //the same for O
          else if(board[0][0].equalsIgnoreCase("o")&& board[0][1].equalsIgnoreCase("o"))
          {
             if(board[0][1].equalsIgnoreCase("o")&& board[0][2].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          //changed
          else if(board[0][1].equalsIgnoreCase("o")&& board[0][2].equalsIgnoreCase("o"))
          {
             if(board[0][0].equalsIgnoreCase("o")&& board[0][1].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          //if for O row 1
          else if(board[1][0].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o"))
          {
             if(board[1][1].equalsIgnoreCase("o")&& board[1][2].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          //changed
          else if(board[1][1].equalsIgnoreCase("o")&& board[1][2].equalsIgnoreCase("o"))
          {
             if(board[1][0].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          //if for O row 2
          else if(board[2][0].equalsIgnoreCase("o")&& board[2][1].equalsIgnoreCase("o"))
          {
             if(board[2][1].equalsIgnoreCase("o")&& board[2][2].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          //changed
          else if(board[2][1].equalsIgnoreCase("o")&& board[2][2].equalsIgnoreCase("o"))
          {
             if(board[2][2].equalsIgnoreCase("o")&& board[2][1].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          
          //now for columns
          
          //col 0 for x
          else if(board[0][0].equalsIgnoreCase("x")&& board[1][0].equalsIgnoreCase("x"))
          {
             if(board[1][0].equalsIgnoreCase("x")&& board[2][0].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          //for col 1 for x
          else if(board[0][1].equalsIgnoreCase("x")&& board[1][1].equalsIgnoreCase("x"))
          {
             if(board[1][1].equalsIgnoreCase("x")&& board[2][1].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
         //for col 2 for x
          else if(board[0][2].equalsIgnoreCase("x")&& board[1][2].equalsIgnoreCase("x"))
          {
             if(board[1][2].equalsIgnoreCase("x")&& board[2][2].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          //the same for o
          else if(board[0][0].equalsIgnoreCase("o")&& board[1][0].equalsIgnoreCase("o"))
          {
             if(board[1][0].equalsIgnoreCase("o")&& board[2][0].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          //for col 1 for o
          else if(board[0][1].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o"))
          {
             if(board[1][1].equalsIgnoreCase("o")&& board[2][1].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          
         //for col 2 for o
          else if(board[0][2].equalsIgnoreCase("o")&& board[1][2].equalsIgnoreCase("o"))
          {
             if(board[1][2].equalsIgnoreCase("o")&& board[2][2].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          //now for cross testing
          
          //cross testing for x
          else if(board[0][0].equalsIgnoreCase("x")&& board[1][1].equalsIgnoreCase("x"))
          {
             if(board[1][1].equalsIgnoreCase("x")&& board[2][2].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          else if(board[0][2].equalsIgnoreCase("x")&& board[1][1].equalsIgnoreCase("x"))
          {
             if(board[1][1].equalsIgnoreCase("x")&& board[2][0].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
    //    cross testing for o
          else if(board[0][0].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o"))
          {
             if(board[1][1].equalsIgnoreCase("o")&& board[2][2].equalsIgnoreCase("o"))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          else if((board[0][2].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o"))||(board[2][0].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o")))
          {
             if((board[1][1].equalsIgnoreCase("o")&& board[2][0].equalsIgnoreCase("o"))||(board[0][2].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o")))
             {
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          return result;
       }//end method getWinner
       
       
       
       
       
      
       
       /**
          Creates a string representation of the board, such as
          |x  o|
          |  x |
          |   o|
          @return the string representation
       */
       public String toString()
       {
          String r = "";
          for (int i = 0; i < ROWS; i++)
          {
             r = r + "|";
             for (int j = 0; j < COLUMNS; j++)         
                r = r + board[i][j];
             r = r + "|\n";
          }
          return r;
       }
    
       private String[][] board;
       private static final int ROWS = 3;
       private static final int COLUMNS = 3;
    }
    TicTacToeTester.java
    Code:
    import java.util.Scanner;
    
    /**
       This program tests the TicTacToe class by prompting the
       user to set positions on the board and printing out the
       result.
    */
    public class TicTacToeTester
    {
       
       public static void main(String[] args)
       {
          String result=" ";
          Scanner in = new Scanner(System.in);
          String player = "x";
          TicTacToe game = new TicTacToe();
          boolean done = false;
          while (!done)
          {
             System.out.print(game.toString()); 
             result=game.getWinner();
             
             if(result.equals(" "))
             {
             System.out.println("\n There is no winner yet");
             }
             else
             {
                System.out.println("\n The winner is   " + result);
             }
             
             System.out.print(
                   "Row for " + player + " (-1 to exit): ");
             
             int row = in.nextInt();
             if (row < 0) done = true;
             else
             {
                System.out.print("Column for " + player + ": ");
                int column = in.nextInt();
                game.set(row, column, player);
                if (player.equals("x")) 
                   player = "o"; 
                else 
                   player = "x";    
             }
          }
       }
    }
    Last edited by starsiege2005; 04-25-2007 at 02:38 AM.

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Code:
    public String getWinner()
       {
       	  result = " ";	
       	
       	  //Check Rows
       	  for(int i = 0; i < ROWS; i++)
       	  {
       	  	if(board[i][0].equalsIgnoreCase(board[i][1])&& board[i][1].equalsIgnoreCase(board[i][2]))
       	  		return board[i][0];
       	  }
       	
       	  //Check Columns
       	  for(int j = 0; j < COLUMNS; j++)
       	  {
       	  	if(board[0][j].equalsIgnoreCase(board[1][j])&& board[1][j].equalsIgnoreCase(board[2][j]))
       	  		return board[0][j];
       	  }
       	  
       	  //Check one diagonal
       	  if(board[0][0].equalsIgnoreCase(board[1][1])&& board[1][1].equalsIgnoreCase(board[2][2]))
       	  		return board[0][0];
       	  		
       	  //Check the other diagonal
       	  if(board[0][2].equalsIgnoreCase(board[1][1])&& board[1][1].equalsIgnoreCase(board[2][0]))
       	  		return board[0][2];
       	  
       	  
       	  //No winner yet
       	  return result;  
      
    
       }//end method getWinner
    So I think the real problem was that you were over-thinking things. You wanted to hard-code every possible combination of Wins for both X and O. You still had some basic logic issues even in that thinking however.

    All we really need is to check row 0,1,2 and see if every letter in the row is the same. If it is, then we can just report what that letter is. That way we don't have to have If code for both X's and O's. Do, the same for columns etc.

    Things boils down to if spot 1 == 2 and spot 2 == 3, then return the character in that row (any spot). Doing it this way eliminates for extra code (which can be a source of confusion) of if spot 1 == x and spot 2 == x etc. There more comparsions if you go this route, and let's face it no one wants to right more code then they have too.

    I haven't checked my code exhaustively so there might be an error somewhere, but I think it should at least give you a different perspective on how to approach the problem. Sometimes when you've been trying to change code or logic for hours and things never seem to work, it can be better just to start over with a clean slate.

    Good Luck,
    Gox
    Last edited by Gox; 04-25-2007 at 03:15 AM.

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thats what i have done.....i tried both ways..i did what u suggested(the code for that is commented out) then when it wasn't working perfectly i tried the other tedious method...the earlier segment of the code i tried is before the current code(its commented out)

    but i will try to see if i have made some logic errors.

    thanks

  • #4
    New Coder
    Join Date
    Apr 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh god!! ur right!!!

    i was so stupid!! i had logic errors in my previous code

    Code:
    //for for comparing rows
          for(int i=0; i<ROWS; i++)
          {
             
             //if for x
                if((board[i][0].equalsIgnoreCase("x")&& board[i][1].equalsIgnoreCase("x"))||(board[i][2].equalsIgnoreCase("x")&& board[i][1].equalsIgnoreCase("x")))
                {
                   if((board[i][1].equalsIgnoreCase("x")&& board[i][2].equalsIgnoreCase("x"))||(board[i][0].equalsIgnoreCase("x")&& board[i][1].equalsIgnoreCase("x")))
                   {    
                      System.out.println("In first for -row-x");
                      result="X";
                      return result;
                   }
                   else
                   {
                      return result;
                   }
                }
                
             //if for o
                
                else if((board[i][0].equalsIgnoreCase("o")&& board[i][1].equalsIgnoreCase("o"))||(board[i][2].equalsIgnoreCase("o")&& board[i][1].equalsIgnoreCase("o")))
                {
                   if((board[i][1].equalsIgnoreCase("o")&& board[i][2].equalsIgnoreCase("o"))||(board[i][0].equalsIgnoreCase("o")&& board[i][1].equalsIgnoreCase("o")))
                   {
                      System.out.println("In first for -row-o");
                      result="O";
                      return result;
                   }
                   else
                   {
                      return result;
                   }
                }
                
          }   //end for for 1(rows)
           
          
          //for comparing columns
          for (int j=0; j<COLUMNS; j++)
          {
             
             //if for x
             if((board[0][j].equalsIgnoreCase("x")&& board[1][j].equalsIgnoreCase("x"))||(board[2][j].equalsIgnoreCase("x")&& board[1][j].equalsIgnoreCase("x")))
             {
                if((board[1][j].equalsIgnoreCase("x")&& board[2][j].equalsIgnoreCase("x"))||(board[0][j].equalsIgnoreCase("x")&& board[1][j].equalsIgnoreCase("x")))
                {
                   System.out.println("In second for -column-x");
                   result="x";
                   return result;
                }
                else
                {
                   return result;
                }
             }
             
             
             //if for o
             else if((board[0][j].equalsIgnoreCase("o")&& board[1][j].equalsIgnoreCase("o"))||(board[2][j].equalsIgnoreCase("o")&& board[1][j].equalsIgnoreCase("o")))
             {
                if((board[1][j].equalsIgnoreCase("o")&& board[2][j].equalsIgnoreCase("o"))||(board[0][j].equalsIgnoreCase("o")&& board[1][j].equalsIgnoreCase("o")))
                {   
                   System.out.println("In second for -row-o");
                   result="O";
                   return result;
                }
                else
                {
                   return result;
                }
             }
             
             
          }//end for for j(columns)
             
          
          
          //if for crosses
          if(board[0][0].equalsIgnoreCase("x")&& board[1][1].equalsIgnoreCase("x"))
          {
             if(board[1][1].equalsIgnoreCase("x")&& board[2][2].equalsIgnoreCase("x"))
             {
                System.out.println("In first cross -x");
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          else if(board[0][0].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o"))
          {
             if(board[1][1].equalsIgnoreCase("o")&& board[2][2].equalsIgnoreCase("o"))
             { 
                System.out.println("In first for -O");
                result="o";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          
          
          else if(board[0][2].equalsIgnoreCase("x")&& board[1][1].equalsIgnoreCase("x"))
          {
             if(board[1][1].equalsIgnoreCase("x")&& board[2][0].equalsIgnoreCase("x"))
             {
                result="x";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          else if(board[0][2].equalsIgnoreCase("o")&& board[1][1].equalsIgnoreCase("o"))
          {
             if(board[1][1].equalsIgnoreCase("o")&& board[2][0].equalsIgnoreCase("o"))
             {
                result="O";
                return result;
             }
             else
             {
                return result;
             }
          }
          
          return result;
       }   
          */
    but thank u soo much for helping me figure this out!!

  • #5
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Glad you got it working. Feels good when a plan comes together doesn't it?


  •  

    Posting Permissions

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