Tic Tac Toe game

Help on Java code

Page 1 of 1

3 Replies - 7357 Views - Last Post: 28 November 2011 - 10:50 AM Rate Topic: -----

#1 Guest_DHow*


Reputation:

Tic Tac Toe game

Posted 14 January 2011 - 08:55 AM

Hey I need help on editing this java code for a tic tac toe game. Firstly I have to use the JOptionPaneInputDialog for a loop for user input until you get a valid value from the user. then secondly I
have to modify the game code so it determines a winner or a tie and halts the game when it is finished with some msg indicating the results. i.e. X Wins, O Wins, TIE.

Here is the java code for the game so far

/**
   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;
   }
}



Edited by Dogstopper: :code:

Is This A Good Question/Topic? 0

Replies To: Tic Tac Toe game

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Tic Tac Toe game

Posted 14 January 2011 - 09:09 AM

Quote

Firstly I have to use the JOptionPaneInputDialog for a loop for..

That's right. You have to. We will not do it for you. We can however offer guidance and help.
Do you know how to use loops?
http://download.orac...olts/while.html
JOptionPanes?
http://download.orac...nts/dialog.html
start with these. If you still have any issue, feel free to post again :)
Was This Post Helpful? 0
  • +
  • -

#3 tinase  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 33
  • Joined: 19-June 08

Re: Tic Tac Toe game

Posted 14 January 2011 - 09:20 AM

I am happy to see the code is so small. So Is is the only amount of code required to attempt a Tic Tac Toe game in java.I will wait for the answer with you(Guest_DHow*).
It was also my question...
But Do you know the full code of the TicTacToe game is found on this book

Specifically

(Chapter 3,Introduction to Java Applets) on Page 107
It has the code With full Explanation and commenting

Please if you find this post helpfull
Click on + button for me on "Was this post helpfull" section below this post
Was This Post Helpful? 0
  • +
  • -

#4 jediv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-October 11

Re: Tic Tac Toe game

Posted 28 November 2011 - 10:50 AM

View Posttinase, on 14 January 2011 - 09:20 AM, said:

I am happy to see the code is so small. So Is is the only amount of code required to attempt a Tic Tac Toe game in java.I will wait for the answer with you(Guest_DHow*).
It was also my question...
But Do you know the full code of the TicTacToe game is found on this book

Specifically

(Chapter 3,Introduction to Java Applets) on Page 107
It has the code With full Explanation and commenting

Please if you find this post helpfull
Click on + button for me on "Was this post helpfull" section below this post


Could you please let me know who is the author of this book Introduction to java applets.

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1