# Thread: TIC TAC TOE VAR PutCross not defined HELP!

1. Registered User
Join Date
Oct 2012
Posts
8

## TIC TAC TOE VAR PutCross not defined HELP!

When I try to run it everything is displayed but I can't play because the Error Console says that my PutCross function is not defined. Can anyone help me with this? Thanks

<script language="javascript" type="text/javascript">
//Tic Tac Toe

//variable global
var playing = true

//function check movement------------------------------------------------
function CheckMovement(board, chip) {
//checks that there is a probability to win the game in a row, column or diagonal.
//returns the position where you have to put your chip. To do it and - 1 if there is not game.
var x, y

for(x = 0; x < 9; x += 3) { //check rows
if ((board.elements[x].value == chip) && (board.elements[x + 1].value == chip))
if (board.elements[x + 2].value == "")
return (x + 2)
if ((board.elements[x].value == chip) && (board.elements[x + 2].value == chip))
if (board.elements[x + 1].value == "")
return (x + 1)
if ((board.elements[x + 1].value == chip) && (board.elements[x + 2].value == chip))
if (board.elements[x].value == "")
return (x)
}

for(x = 0; x < 3; x++) { //check columns
if ((board.elements[x].value == chip) && (board.elements[x + 3].value == chip))
if (board.elements[x + 6].value == "")
return (x + 6)
if ((board.elements[x].value == chip) && (board.elements[x + 6].value == chip))
if (board.elements[x + 3].value == "")
return (x + 3)
if ((board.elements[x + 3].value == chip) && (board.elements[x + 6].value == chip))
if (board.elements[x].value == "")
return (x)
}

//check diagonals
if ((board.elements[2].value == chip) && (board.elements[4].value == chip) && (board.elements[6].value == ""))
return (6)
if ((board.elements[2].value == chip) && (board.elements[4].value == "") && (board.elements[6].value == chip))
return (4)
if ((board.elements[2].value == "") && (board.elements[4].value == chip) && (board.elements[6].value == chip))
return (2)
if ((board.elements[0].value == chip) && (board.elements[4].value == chip) && (board.elements[8].value == ""))
return (8)
if ((board.elements[0].value == chip) && (board.elements[4].value == "") && (board.elements[8].value == chip))
return (4)
if ((board.elements[0].value == "") && (board.elements[4].value == chip) && (board.elements[8].value == chip))
return (0)

return -1
}

//Function Horizontal Pair---------------------------------------------------
function HorizontalPair(board, chip) {
//Checks if it's possible to put 2 in a row, if the other case of the same row is empty.
//Returns the row to allows to do it, or - 1 in the opposite.

var x, y

for(x = 0; x < 9; x += 3) { //check rows
if ((board.elements[x].value == chip) && (board.elements[x + 1].value == "") && (board.elements[x + 2].value == ""))
return (x)
if ((board.elements[x].value == "") && (board.elements[x + 1].value == chip) && (board.elements[x + 2].value == ""))
return (x + 1)
if ((board.elements[x].value == "") && (board.elements[x + 1].value == "") && (board.elements[x + 2].value == chip))
return (x + 2)
}

return -1
}

//FUNCION Vertical Pair---------------------------------------------------
function VerticalPair(board, chip, gameHtal) {
//Checks if it's possible to put 2 in a column, if the other case of the same column is empty.
//Returns the row to allows to do it, or - 1 in the opposite.

var x, y

for(x = 0; x < 3; x++) { //check columns
if ((board.elements[x].value == chip) && (board.elements[x + 3].value == "") && (board.elements[x + 6].value == ""))
if (x != gameHtal) //If it's horizontal pair it doesn't interest as much as vertical
return (x)
if ((board.elements[x].value == "") && (board.elements[x + 3].value == chip) && (board.elements[x + 6].value == ""))
if ((x + 3) != gameHtal)
return (x + 3)
if ((board.elements[x].value == "") && (board.elements[x + 3].value == "") && (board.elements[x + 6].value == chip))
if ((x + 6) != gameHtal)
return (x + 6)
}

return -1
}

//FUNCTION OBTAIN POSITION-------------------------------------------------
function ObtainPosition(gameHtal, gameVcal) {
//looks for the position to make a vertical and horizontal game at the same time
//known the rows and columns where you can make pairs

var x, y, row, column
var posit= 0
position = new Array(3)

for (x = 0; x < 3; x++) { //creates a position for rows and columns
position[x] = new Array(3)
for (y = 0; y < 3; y++) {
position[x][y] = posit
posit++
}
}

for (x = 0; x < 3; x++) { //looks for row and column
for (y = 0; y < 3; y++) {
if (position[x][y] == gameHtal) //found the row
row = x
if (position[x][y] == gameVcal) //found the column
}
}

return (position[row][column]) //returns posit game
}

//Function put the circle-------------------------------------------------
function PutCircle(board) {
//plays the computer

var gamet, gameHtal, gameVcal

//attack and win
gamet = CheckMovement(board, "O")
if (gamet != -1) {
board.elements[gamet].value = "O"
alert('ˇYo gano!')
document.score.lose.value++
playing = false
return 1
}

//defends trying to don't lose
gamet = CheckMovement(board, "X")
if (gamet != -1) {
board.elements[gamet].value = "O"
return 1
}

//attack and win next
gameHtal = HorizontalPair(board, "O")
gameVcal = VerticalPair(board, "O", gameHtal)
if ((gameHtal != -1) && (gameVcal != -1)) { //Can put 2 in the row and 2 in column.
if ((gameHtal != 4) || (gameVcal != 4)) { //it's not the center.
gamet = ObtainPosition(gameHtal, gameVcal) //finds where to move.
board.elements[gamet].value = "O"
return 1
}
}

//attacks and puts 2 in row
if ((gameHtal != -1) && (gameVcal == -1)) {
if ((gameHtal != 2) && (gameHtal != 5) && (gameHtal != 8)) //this is arbitrary
board.elements[gameHtal + 1].value = "O" //move right
else
board.elements[gameHtal - 1].value = "O" //move left
return 1
}

//attacks and puts 2 in column
if ((gameHtal == -1) && (gameVcal != -1)) {
if ((gameVcal != 6) && (gameVcal != 7) && (gameVcal != 8))
board.elements[gameVcal + 3].value = "O" //moves down
else
board.elements[gameVcal - 3].value = "O" //moves up
return 1
}

//moves center
if (board.elements[4].value == "") {
board.elements[4].value = "O"
return 1
}

//moves the first free one
for (x = 0; x < 9; x++)
if (board.elements[x].value == "") {
board.elements[x].value = "O"
return 1
}

alert('draw.')
document.score.draw.value++
playing = false
return -1
}

//Function put cross------------------------------------------------------
function PutCross(board, position) {

//checsk that we're playing
if (playing) {
if (board.elements[position].value != "") //box not empty
alert('This box is has been already taken.')
else { //box empty, you can move there
board.elements[position].value = "X"

//checks if win
if ( CheckVictory(board, "X") ) {
alert('ˇCongratulations! You won.')
document.score.won.value++
playing = false
} else
PutCircle(board)
}
} else {
alert('To start a new game press/ New game.')
}
}

//Function Check Victory ----------------------------------------------
function CheckVictory(board, chip) {

//Checks the winner

var x

for(x = 0; x < 9; x += 3) { //check rows
if ((board.elements[x].value == chip) && (board.elements[x + 1].value == chip) && (board.elements[x + 2].value == chip))
return true
}

for(x = 0; x < 3; x++) { //check columns
if ((board.elements[x].value == chip) && (board.elements[x + 3].value == chip) && (board.elements[x + 6].value == chip))
return true
}

//check diagonales
if ((board.elements[2].value == chip) && (board.elements[4].value == chip) && (board.elements[6].value == chip))
return true
if ((board.elements[0].value == chip) && (board.elements[4].value == chip) && (board.elements[8].value == chip))
return true

return false
}

</script>

2. Registered User
Join Date
Oct 2012
Posts
8
<table><center><tr><td>
<p align="center"><font size="6" color="#000080"><b>*** TIC TAC TOE GAME</b></font></p>
<p align="center" style="margin-top: 0; margin-bottom: 0"><b><font color="#000080" size="3" face="Arial">Click buttons of X to make your movements.</font></b></p>
<center>
<form name="board">
<div align="center"><center><table border="0" width="69%" cellpadding="5">
<tr>
<td width="20%" align="center">
<p align="center"><input type="text" name="T0" size="1"></p>
</td>
<td width="20%" align="center"><font color="#00FF00"><input type="text" name="T1" size="1"></font></td>
<td width="20%" align="center"><input type="text" name="T2" size="1"></td>
</div>
</td>
</tr>
<tr>
<td width="20%" align="center"><input type="text" name="T3" size="1"></td>
<td width="20%" align="center"><input type="text" name="T4" size="1"></td>
<td width="20%" align="center"><input type="text" name="T5" size="1"></td>
<td width="85%" align="center"></td>
</tr>
<tr>
<td width="20%" align="center"><input type="text" name="T6" size="1"></td>
<td width="20%" align="center"><input type="text" name="T7" size="1"></td>
<td width="20%" align="center"><input type="text" name="T8" size="1"></td>
<td width="85%" align="center"></td>
</tr>
<tr>
<td width="20%" align="center"><input type="button" value=" X " name="B0"
onClick="PutCross(board, 0)" style="font-family: arial"></td>
<td width="20%" align="center"><input type="button" value=" X " name="B1"
onClick="PutCross(board, 1)" style="font-family: arial"></td>
<td width="20%" align="center"><input type="button" value=" X " name="B2"
onClick="PutCross(board, 2)" style="font-family: arial"></td>
<td width="85%" align="center"><input type="reset" value="New game" name="new"
onClick="playing=true" style="font-family: arial"></td>
</tr>
<tr>
<td width="18%" align="center"><input type="button" value=" X " name="B3"
onClick="PutCross(board, 3)" style="font-family: arial"></td>
<td width="17%" align="center"><input type="button" value=" X " name="B4"
onClick="PutCross(board, 4)" style="font-family: arial"></td>
<td width="18%" align="center"><input type="button" value=" X " name="B5"
onClick="PutCross(board, 5)" style="font-family: arial"></td>
</tr>
<tr>
<td width="18%" align="center"><input type="button" value=" X " name="B6"
onClick="PutCross(board, 6)" style="font-family: arial"></td>
<td width="17%" align="center"><input type="button" value=" X " name="B7"
onClick="PutCross(board, 7)" style="font-family: arial"></td>
<td width="18%" align="center"><input type="button" value=" X " name="B8"
onClick="PutCross(board, 8)" style="font-family: arial"></td>
<td width="85%" align="right"></td>
</tr>
</td></tr>
</table>
</center></div>
</form>

<form name="score">
<div align="center"><center><table border="0" width="67%" cellpadding="5" cellspacing="0">
<tr>
<td width="20%" align="center"><div align="left"><p><b><font color="#000080" face="Arial" size="2">Draw:<br>
<input type="text" name="draw" value="0" size="6"></font></b>
</div>
</td>
<td width="20%" align="center"><div align="left"><p><b><font color="#000080" face="Arial" size="2">Lose:<br>
<input type="text" name="lose" value="0" size="6"></font></b>
</div>
</td>
<td width="20%"><div align="left"><p><b><font color="#000080" face="Arial" size="2">Won:<br>
<input type="text" name="won" value="0" size="6"></font></b></td>
<td width="85%" align="center"><div align="right"><p align="center"> <b> <font color="#000080" face="Arial" size="2"> <input type="reset"
value="Clean" name="delete" style="font-family: arial"></font></b></td>
</tr>
</table>
</center></div>
</form>
</body>
</html>

3. Registered User
Join Date
Dec 2005
Posts
279
Don't know if it is your problem, but ObtainPosition() has errors.

4. Registered User
Join Date
Oct 2012
Posts
8
How can I solve it?

5. Registered User
Join Date
Dec 2005
Posts
279
I don't think your curly brackets matched up, but not positive. I strongly recommend including curly brackets for all if statements, and terminating each line with a semi-column. For instance, change your code from:
Code:
```if (position[x][y] == gameHtal) //found the row
row = x```
to
Code:
```if (position[x][y] == gameHtal) {//found the row
row = x;
}```
Also, you didn't show how the JavaScript is initialized. You just posted some functions and a html table. Make a working example on jsfiddle, and I'll take a look.

6. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
See this page and his source for explanations...
Two dimensional array are never useful for this kind of games. See this other connect four game !
Last edited by 007Julien; 10-29-2012 at 08:46 PM.

#### Thread Information

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

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

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.26327 seconds
• Memory Usage 2,933KB
• Queries Executed 15 (?)
More Information
Template Usage (33):
• (1)SHOWTHREAD
• (1)ad_footer_end
• (1)ad_footer_start
• (1)ad_global_above_footer
• (1)ad_global_below_navbar
• (1)ad_global_header1
• (1)ad_global_header2
• (1)ad_navbar_below
• (1)ad_showthread_firstpost_sig
• (1)ad_showthread_firstpost_start
• (1)ad_thread_first_post_content
• (1)ad_thread_last_post_content
• (2)bbcode_code
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (1)header
• (1)headinclude
• (1)headinclude_bottom
• (6)memberaction_dropdown
• (1)navbar
• (4)navbar_link
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (6)postbit
• (6)postbit_onlinestatus
• (6)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
• showthread
Included Files (26):
• ./showthread.php
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/functions_navigation.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_facebook.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (72):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_postinfo_query
• fetch_postinfo
• fetch_threadinfo_query
• fetch_threadinfo
• fetch_foruminfo
• load_show_variables
• load_forum_show_variables
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• showthread_start
• showthread_getinfo
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• fetch_musername
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• showthread_post_start
• showthread_query_postids
• showthread_query
• bbcode_fetch_tags
• bbcode_create
• showthread_postbit_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete
• build_navigation_data
• build_navigation_array
• check_navigation_permission
• process_navigation_links_start
• process_navigation_links_complete
• set_navigation_menu_element
• build_navigation_menudata
• build_navigation_listdata
• build_navigation_list
• set_navigation_tab_main
• set_navigation_tab_fallback
• navigation_tab_complete
• fb_like_button
• showthread_complete
• page_templates