5 Replies - 913 Views - Last Post: 21 November 2011 - 06:22 PM Rate Topic: -----

#1 TuckNorris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-November 11

TicTacToe Problem! Please help!

Posted 20 November 2011 - 11:21 PM

/**

   A 3 x 3 Tic-Tac-Toe board.

*/


public class TicTacToe

{

   private String[][] board;

   private static final int ROWS = 3;

   private static final int COLUMNS = 3;



   /**

      Constructs an empty board.

   */


   public TicTacToe()

   {

      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;

   }



   /**

      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;

   }


   
   /**

      Gets the winner

      @return the winner

    */


   public String getWinner()

   {
	...
   }
}



and

import java.util.Scanner;



/**

   This program tests the getWinner method of the TicTacToe class.

*/


public class TicTacToeTester

{

   public static void main(String[] args)

   {

      TicTacToe game = new TicTacToe();

      game.set(0, 0, "x");

      game.set(2, 0, "o");

      game.set(1, 1, "x");

      game.set(2, 1, "o");

      
System.out.println("Winner: " + game.getWinner());

      System.out.println("Expected: ");

      game.set(2, 2, "x");

      System.out.println("Winner: " + game.getWinner());

      System.out.println("Expected: x");


      game = new TicTacToe();

      game.set(0, 0, "x");

      game.set(2, 0, "o");

      game.set(1, 1, "x");

      game.set(2, 1, "o");
      game.set(2, 2, "o");

      System.out.println("Winner: " + game.getWinner());

      System.out.println("Expected: o"); 

   }

}



idk what to put inplace of the . . .
Please help!

Is This A Good Question/Topic? 0
  • +

Replies To: TicTacToe Problem! Please help!

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: TicTacToe Problem! Please help!

Posted 21 November 2011 - 12:32 AM

Quote

idk what to put inplace of the . . .

What you want it to return?
Because I dont see what exactly you are doing with your set() method. You have first to relook it:
 public void set(int i, int j, String player)
	   {
	 
      if (board[i][j].equals(" ")) //if location contains empty space! shouldnt it be ""
 
         board[i][j] = player;
	   }

Was This Post Helpful? 0
  • +
  • -

#3 TuckNorris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-November 11

Re: TicTacToe Problem! Please help!

Posted 21 November 2011 - 02:29 PM

All it says is

"Add a method getWinner to the TicTacToe class. It should return "x" or "o" to indicate a winner, or " " if there is no winner yet. Recall that a winning position has three matching marks in a row, column, or diagonal."
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: TicTacToe Problem! Please help!

Posted 21 November 2011 - 02:32 PM

Well so you will have to check all cases of a win by either player in the 2d board (horizontal, vertical and diagonal)that you have already in the class.

When you have found the winner, just return their symbol as a string, otherwise if there is no winner yet return an empty string.
Was This Post Helpful? 0
  • +
  • -

#5 TuckNorris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-November 11

Re: TicTacToe Problem! Please help!

Posted 21 November 2011 - 04:13 PM

How would I do that?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: TicTacToe Problem! Please help!

Posted 21 November 2011 - 06:22 PM

Have another array of int called gameValue
int[][] gameValue = new int[ROWS][COLUMNS];

When the O is selected put 10 in gameValue[x][y]
When the X is selected put 100 in gameValue[x][y]

If the sum of a row or a column or a diagnonal is 30, the O have won
If the sum of a row or a column or a diagnonal is 300, the X have won
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1