Forum Moderators: open

Message Too Old, No Replies

i need help for tic-tac-toe game

JavaScript cooding

         

sawkat

6:32 pm on Mar 9, 2009 (gmt 0)

10+ Year Member



This is a 2 player game and each player takes turns to play. Each player has the following markers:

Player 1 = X
Player 2 = 0

Use a mechanism such as two manually selected radio buttons to select what player is currently in play
A player clicks on one of the 9 cells in the grid to place their marker

If a cell already has an X or an O then that cell is not playable in any subsequent turns
ple any one know codding or ref site just inform me
thaks

Fotiman

2:06 am on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld! I actually have a JavaScript Tic Tac Toe game that I wrote myself. However, is this a homework assignment? If so, then it's probably not what you are looking for as it uses some more advanced JavaScript techniques. Also, it automatically switches between users. Is there a reason you would want to manually select what player is currently in play?

sawkat

2:52 am on Mar 10, 2009 (gmt 0)

10+ Year Member



yes its homework assignment. manually select betwin 2 players.

Fotiman

3:28 am on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, in that case, I'll give you some advice.

1. You will need to create your radio buttons with some event handlers (note, I actually recommend against using event handlers and instead using event listeners, but I am guessing your teacher hasn't gone over those so I'm going to use handlers instead):


<div><input type="radio" name="player" value="1" onclick="setPlayer(this.value)"> Player 1</div>
<div><input type="radio" name="player" value="2" onclick="setPlayer(this.value)"> Player 2</div>

Also note, I generally wouldn't use global functions but again, for a beginner homework assignment, I'm guessing that's what's being taught.

2. Next, you need to create your event handler function:


// Global variable that holds the current game piece
var piece = "X";
function setPlayer(n) {
// Update the current playing piece based on
// which player was passed in
piece = (n == 1? "X": "O");
}

3. On your game board, you will need to attach onclick event handlers to each cell. When a user clicks a cell, you will need to check to see if there is already a value in that cell, and if not, you will place the current game "piece" in that cell.

If you need more help, feel free to ask. :)

sawkat

9:02 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Fotiman - i need ur email address ple. i will send my project file. thanks

Fotiman

12:36 am on Mar 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Keeping the discussion on the boards allows for others to learn from the issue with the solution and also for others to contribute to the solution where one single person may not have seen that particular way to do it. Therefore, feel free to post code here in the discussion.