The Artima Developer Community
Sponsored Link

Java Answers Forum
tic -tac-toe

1 reply on 1 page. Most recent reply: Jul 2, 2008 12:34 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
intezer007 intezer007

Posts: 1
Nickname: intezer007
Registered: Jun, 2008

tic -tac-toe Posted: Jun 20, 2008 3:29 AM
Reply to this message Reply
Advertisement
Since I am just learning the basics of java so doing tic-tac-toe alone is impossible for me that's why i am waiting for someone's complete java code of it. Here is all i know about it:

Tic-tac-toe is played on a 3 x 3 grid. Two players take turns at selecting squares on the grid, the first marking his or her moves with an "O" (British, "nought"), the second, with "X" ("cross"). The first player to select three squares in a row -- horizontally, vertically, or diagonally -- wins the game.

The program should draw the game board using a simple representation, with the squares numbered from 1 to 9, such as:
1 | 2 | 3
-----------
4 | 5 | 6
-----------
7 | 8 | 9

It should then prompt the user to select one of the squares, by number. It should check that a valid selection has been made (i.e., the number is in range and was not previously selected by either player) and, if not, print an appropriate message and prompt again. The following example illustrates such an interaction:
X | X | 3
-----------
4 | O | 6
-----------
7 | O | 9

Your move? 2
Please enter one of the digits shown on the board.
Your move? 17
Please enter one of the digits shown on the board.
Your move? 3

Once the human player has made a valid selection, the program should choose a square for its move. It should then indicate which square was selected and print an updated representation of the board, indicating the human player's moves with O's and its own moves with X's. The next round of play then commences with another prompt to the human player, as in:
I chose square 4.

X | X | O
-----------
X | O | 6
-----------
7 | O | 9

Your move?
After each move -- both the human's and its own -- the program should check for a winner. If a player has won, an appropriate message should be printed and the game ends.
For example, if the human player wins, it might print:
X | X | O
-----------
4 | O | 6
-----------
O | 8 | 9

Congratulations. You won.
If the computer wins, it might print the following:
X | X | O
-----------
X | O | O
-----------
X | O | 9

I won. Let's play again soon.
The program should also check for a no-win condition, in which all squares have been selected, but neither player has won:
X | O | X
-----------
X | O | O
-----------
O | X | 9

Your move? 9
The game ended with no winner.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: tic -tac-toe Posted: Jul 2, 2008 12:34 AM
Reply to this message Reply
Then wait for a long time.

We don't do homework here.


But I give you a few hints.

1st of all create a enum called ClickedBy with the values NONE, PLAYER1 and PLAYER2.

Would also work with int, but a enum is more elegant and easier to read

now create a array ClickedBy[3][3] and set every value to ClickedBy.NONE

how to draw the board and the numbers I let up to you.
A approach might be to use a gridlayout, put in 9 labels and that's it. set Text alignment of each to center.

add a mouseclick listener to each. When clicked, check if
the specified field is allready taken (velue must be different from NONE) and set it according to the player who's turn is it. After each click you must set the new player of course and start the evaluation if one row contains 3 equal values != NONE

that'S quite about it.

Flat View: This topic has 1 reply on 1 page
Topic: java Previous Topic   Next Topic Topic: Problem with code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use