Vajira Posted November 21, 2006 Share Posted November 21, 2006 Hi everybody, I'm having a little problem about integrating a human vs human feature in the following javascript game: http://javascript.internet.com/games/tic-tac-toe.html. I want to let the user choose to play against either the computer or a human opponent. I'm really new to javascript so I have no idea how to start, can anyone help me out?I know that I can create a button or a checkbox so that the script can detect if a human player is present or not. Then I should probably use a if statement to let the program identify the human player and run a function for a twoplayer game. If it is human versus computer, then I should use else statement and let the original code run. I hope I have the programming logic correct.Thanks. Link to comment Share on other sites More sharing options...
aspnetguy Posted November 21, 2006 Share Posted November 21, 2006 you'll need to make a Java Applet or use Ajax, JavaScript alone will not work for a 2 player game. Link to comment Share on other sites More sharing options...
Vajira Posted November 21, 2006 Author Share Posted November 21, 2006 Really? I have been able to find a tic-tac-toe that allows human vs human and human vs computer in javascript, but it's too hard for me to understand. That is why I am trying to modify a human vs computer tic-tac-toe to thoroughly understand the concept. Here is the website of the tic-tac-toe that I'm talking about: http://ostermiller.org/calc/tictactoe.html Link to comment Share on other sites More sharing options...
aspnetguy Posted November 21, 2006 Share Posted November 21, 2006 ok, human vs human using the same PC, yes, not on different PCs tho.all you need to do it keep track of which to place on click, X or O Link to comment Share on other sites More sharing options...
Vajira Posted November 21, 2006 Author Share Posted November 21, 2006 Yeah, I meant human vs human on the same PC, sorry for not being specific enough.Anyways, how do I keep track of the onClick? Does that mean I should declare some variable under yourChoice() of the first program, so the program knows whose (X or O) turn it is? Do I use a loop for this? Link to comment Share on other sites More sharing options...
aspnetguy Posted November 21, 2006 Share Posted November 21, 2006 I'll whip something up at lunch time for you. Link to comment Share on other sites More sharing options...
Vajira Posted November 21, 2006 Author Share Posted November 21, 2006 Thanks so much...I need this to study for my final. Link to comment Share on other sites More sharing options...
aspnetguy Posted November 21, 2006 Share Posted November 21, 2006 here is a simple form of tic tac toe, it does not check to see if anyone has won or not. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head> <title>Tic Tac Toe</title> <script type="text/javascript"> var xturn = true; //is is x's turn? var xtoken = "X"; //letter to place var otoken = "O"; //letter to place var board; var cell; var cells; var row; var rows; var MSIE = false; //is internet explorer onload = newGame; function newGame() { //get game board board = document.getElementById('gameBoard'); //setup onclick event handlers in all cells of game board rows = board.getElementsByTagName('tr'); for(row=0;row<rows.length;row++) { cells = rows[row].getElementsByTagName('td'); for(cell=0;cell<cells.length;cell++) { cells[cell].onclick = move; cells[cell].style.cursor = "pointer"; } } } function move(e) { var srcCell; if(!e) { e = window.event; MSIE = true; srcCell = e.srcElement; } else srcCell = e.target; if(srcCell.innerHTML == "Â ") { if(xturn) srcCell.innerHTML = xtoken; else srcCell.innerHTML = otoken; xturn = !xturn; } //uncomment else below to display //error message if user clicks //a cell that is already used //else alert("Already Used!"); } </script> <style type="text/css"> body { } #gameBoard { width: 100%; height: 200px; border-top: 1px solid #dedede; border-left: 1px solid #dedede; } #gameBoard td { border-bottom: 1px solid #dedede; border-right: 1px solid #dedede; text-align: center; vertical-align: middle; font-family: monospace; font-size: 25px; font-weight: bold; } #wrapper { width: 200px; margin: auto; } </style></head><body> <div id="wrapper"> <table id="gameBoard" cellspacing="0"> <tr> <td>Â </td> <td>Â </td> <td>Â </td> </tr> <tr> <td>Â </td> <td>Â </td> <td>Â </td> </tr> <tr> <td>Â </td> <td>Â </td> <td>Â </td> </tr> </table> </div></body></html> Link to comment Share on other sites More sharing options...
Vajira Posted November 21, 2006 Author Share Posted November 21, 2006 Thanks for the code...just a few questions:- Is the variable MSIE really necessary?- What does "Â " do?- What is "e"? Why is it declared only within the if statement? Link to comment Share on other sites More sharing options...
aspnetguy Posted November 21, 2006 Share Posted November 21, 2006 MSIE is not necessary. I changed how I was going to write the code but forgot to take it out.& nbsp; (without the space) is an HTML special character that represents a space. IE will not apply style to empty table cells.e is the event arguments for onclick. IE does not pass this variable to the function (notice the move(e)) like other browsers do so you have to manually set it with window.event.you can re-write this var srcCell; if(!e) { e = window.event; MSIE = true; srcCell = e.srcElement; } else srcCell = e.target; to this var srcCell; if(!e) srcCell = window.event.srcElement; else srcCell = e.target; Link to comment Share on other sites More sharing options...
Vajira Posted November 22, 2006 Author Share Posted November 22, 2006 I've tinkered around your code and the original code a bit and tried to integrate them both into one single code. The result program however, ignore the two player mode even after I clicked the button and called the newGame...the game just run the single player mode. I've even tried to force the game to load in two player mode upon loading, but it doesn't work. Another problem is that the scores can't be accessed, I'm suspecting it's because the table values can't be accessed through DOM like this "document.gameBoard.xuser.value = wn;"Here's all my code: <?xml version = "1.0"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd"><html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Tic-Tac-Toe</title> <style type= "text/css"> body {} #gameBoard { width: 100%; height: 200px; border-top: 1px solid #dedede; border-left: 1px solid #dedede; } #gameBoard td { border-bottom: 1px solid #dedede; border-right: 1px solid #dedede; text-align: center; vertical-align: middle; } #wrapper { width: 200px; margin: auto; } </style><script type="text/javascript"><!-- Beginvar x = "x.jpg";var o = "o.jpg";var blank = "blank.jpg";var xturn = true;var board;var cell;var cells;var row;var rows;var pause = 0;var all = 0;var a = 0;var b = 0;var c = 0;var d = 0;var e = 0;var f = 0;var g = 0;var h = 0;var i = 0;var temp="";var ok = 0;var cf = 0;var choice=9;var aRandomNumber = 0;var comp = 0; var t = 0;var wn = 0;var ls = 0;var ts = 0;onload = newGame;<!--newGame-->function newGame() { //get game board board = document.getElementById('gameBoard'); //setup onclick event handlers in all cells of game board rows = board.getElementsByTagName('tr'); for(row=0;row<rows.length;row++) { cells = rows[row].getElementsByTagName('td'); for(cell=0;cell<cells.length;cell++) { cells[cell].onclick = move; cells[cell].style.cursor = "pointer"; } } }<!--move--> function move(e) { var srcCell; if(!e) srcCell = window.event.srcElement; else srcCell = e.target; if(srcCell.innerHTML == " ") { if(xturn) srcCell.innerHTML = x; else srcCell.innerHTML = o; xturn = !xturn; } //uncomment else below to display //error message if user clicks //a cell that is already used //else alert("Already Used!"); }<!--help-->function help() {alert("Welcome to Tic-Tac-Toe! You play as the X's and the computer is the O's. Select the square you want to put your X into by clicking them. You cannot occupy a square that is already occupied. The first player to get three squares in a row wins. Good Luck!!")}<!--logicOne-->function logicOne() {if ((a==1)&&(b==1)&&(c==1)) all=1;if ((a==1)&&(d==1)&&(g==1)) all=1;if ((a==1)&&(e==1)&&(i==1)) all=1;if ((b==1)&&(e==1)&&(h==1)) all=1;if ((d==1)&&(e==1)&&(f==1)) all=1;if ((g==1)&&(h==1)&&(i==1)) all=1;if ((c==1)&&(f==1)&&(i==1)) all=1;if ((g==1)&&(e==1)&&(c==1)) all=1;if ((a==2)&&(b==2)&&(c==2)) all=2;if ((a==2)&&(d==2)&&(g==2)) all=2;if ((a==2)&&(e==2)&&(i==2)) all=2;if ((b==2)&&(e==2)&&(h==2)) all=2;if ((d==2)&&(e==2)&&(f==2)) all=2;if ((g==2)&&(h==2)&&(i==2)) all=2;if ((c==2)&&(f==2)&&(i==2)) all=2;if ((g==2)&&(e==2)&&(c==2)) all=2;if ((a != 0)&&(b != 0)&&(c != 0)&&(d != 0)&&(e != 0)&&(f != 0)&&(g != 0)&&(h != 0)&&(i != 0)&&(all == 0)) all = 3;} <!--logicTwo-->function logicTwo() {if ((a==2)&&(b==2)&&(c== 0)&&(temp=="")) temp="C";if ((a==2)&&(b== 0)&&(c==2)&&(temp=="")) temp="B";if ((a== 0)&&(b==2)&&(c==2)&&(temp=="")) temp="A";if ((a==2)&&(d==2)&&(g== 0)&&(temp=="")) temp="G";if ((a==2)&&(d== 0)&&(g==2)&&(temp=="")) temp="D";if ((a== 0)&&(d==2)&&(g==2)&&(temp=="")) temp="A";if ((a==2)&&(e==2)&&(i== 0)&&(temp=="")) temp="I";if ((a==2)&&(e== 0)&&(i==2)&&(temp=="")) temp="E";if ((a== 0)&&(e==2)&&(i==2)&&(temp=="")) temp="A";if ((b==2)&&(e==2)&&(h== 0)&&(temp=="")) temp="H";if ((b==2)&&(e== 0)&&(h==2)&&(temp=="")) temp="E";if ((b== 0)&&(e==2)&&(h==2)&&(temp=="")) temp="B";if ((d==2)&&(e==2)&&(f== 0)&&(temp=="")) temp="F";if ((d==2)&&(e== 0)&&(f==2)&&(temp=="")) temp="E";if ((d== 0)&&(e==2)&&(f==2)&&(temp=="")) temp="D";if ((g==2)&&(h==2)&&(i== 0)&&(temp=="")) temp="I";if ((g==2)&&(h== 0)&&(i==2)&&(temp=="")) temp="H";if ((g== 0)&&(h==2)&&(i==2)&&(temp=="")) temp="G";if ((c==2)&&(f==2)&&(i== 0)&&(temp=="")) temp="I";if ((c==2)&&(f== 0)&&(i==2)&&(temp=="")) temp="F";if ((c== 0)&&(f==2)&&(i==2)&&(temp=="")) temp="C";if ((g==2)&&(e==2)&&(c== 0)&&(temp=="")) temp="C";if ((g==2)&&(e== 0)&&(c==2)&&(temp=="")) temp="E";if ((g== 0)&&(e==2)&&(c==2)&&(temp=="")) temp="G";}<!--logicThree-->function logicThree() {if ((a==1)&&(b==1)&&(c==0)&&(temp=="")) temp="C";if ((a==1)&&(b==0)&&(c==1)&&(temp=="")) temp="B";if ((a==0)&&(b==1)&&(c==1)&&(temp=="")) temp="A";if ((a==1)&&(d==1)&&(g==0)&&(temp=="")) temp="G";if ((a==1)&&(d==0)&&(g==1)&&(temp=="")) temp="D";if ((a==0)&&(d==1)&&(g==1)&&(temp=="")) temp="A";if ((a==1)&&(e==1)&&(i==0)&&(temp=="")) temp="I";if ((a==1)&&(e==0)&&(i==1)&&(temp=="")) temp="E";if ((a==0)&&(e==1)&&(i==1)&&(temp=="")) temp="A";if ((b==1)&&(e==1)&&(h==0)&&(temp=="")) temp="H";if ((b==1)&&(e==0)&&(h==1)&&(temp=="")) temp="E";if ((b==0)&&(e==1)&&(h==1)&&(temp=="")) temp="B";if ((d==1)&&(e==1)&&(f==0)&&(temp=="")) temp="F";if ((d==1)&&(e==0)&&(f==1)&&(temp=="")) temp="E";if ((d==0)&&(e==1)&&(f==1)&&(temp=="")) temp="D";if ((g==1)&&(h==1)&&(i==0)&&(temp=="")) temp="I";if ((g==1)&&(h==0)&&(i==1)&&(temp=="")) temp="H";if ((g==0)&&(h==1)&&(i==1)&&(temp=="")) temp="G";if ((c==1)&&(f==1)&&(i==0)&&(temp=="")) temp="I";if ((c==1)&&(f==0)&&(i==1)&&(temp=="")) temp="F";if ((c==0)&&(f==1)&&(i==1)&&(temp=="")) temp="C";if ((g==1)&&(e==1)&&(c==0)&&(temp=="")) temp="C";if ((g==1)&&(e==0)&&(c==1)&&(temp=="")) temp="E";if ((g==0)&&(e==1)&&(c==1)&&(temp=="")) temp="G";}<!--clearOut-->function clearOut() {document.game.you.value="0";document.game.computer.value="0";document.game.ties.value="0";}<!--checkSpace-->function checkSpace() {if ((temp=="A")&&(a==0)) {ok=1;if (cf==0) a=1;if (cf==1) a=2;}if ((temp=="B")&&(b==0)) {ok=1;if (cf==0) b=1;if (cf==1) b=2;}if ((temp=="C")&&(c==0)) {ok=1;if (cf==0) c=1;if (cf==1) c=2;}if ((temp=="D")&&(d==0)) {ok=1;if (cf==0) d=1;if (cf==1) d=2;}if ((temp=="E")&&(e==0)) {ok=1;if (cf==0) e=1;if (cf==1) e=2;}if ((temp=="F")&&(f==0)) {ok=1if (cf==0) f=1;if (cf==1) f=2;}if ((temp=="G")&&(g==0)) {ok=1if (cf==0) g=1;if (cf==1) g=2;}if ((temp=="H")&&(h==0)) {ok=1;if (cf==0) h=1;if (cf==1) h=2;}if ((temp=="I")&&(i==0)) {ok=1;if (cf==0) i=1; if (cf==1) i=2; }}<!--yourChoice-->function yourChoice(chName) {pause = 0;if (all!=0) ended();if (all==0) {cf = 0;ok = 0;temp=chName;checkSpace();if (ok==1) {document.images[chName].src = x;}if (ok==0)taken();process();if ((all==0)&&(pause==0)) myChoice(); }}<!--taken-->function taken() {alert("That square is already occupied. Please select another square.")pause=1;}<!--myChoice-->function myChoice() {temp="";ok = 0;cf=1;logicTwo();logicThree();checkSpace();while(ok==0) {aRandomNumber=Math.random()comp=Math.round((choice-1)*aRandomNumber)+1;if (comp==1) temp="A";if (comp==2) temp="B";if (comp==3) temp="C";if (comp==4) temp="D";if (comp==5) temp="E";if (comp==6) temp="F";if (comp==7) temp="G";if (comp==8) temp="H";if (comp==9) temp="I";checkSpace();}document.images[temp].src= o;process();}<!--ended-->function ended() {alert("The game has already ended. To play a new game click the Play Again button.")}<!--process-->function process() {logicOne();if (all==1){ alert("X user wins!"); wn++; }if (all==2){ alert("O user wins!"); ls++; }if (all==3){ alert("Tie game."); ts++; }if (all!=0) {document.gameBoard.xuser.value = wn;document.gameBoard.ouser.value = ls;document.gameBoard.ties.value = ts; }}<!--playAgain-->function playAgain() {if (all==0) {if(confirm("This will restart the game and clear all the current scores. OK?")) reset();}if (all>0) reset();}<!--reset-->function reset() {all = 0;a = 0;b = 0;c = 0;d = 0;e = 0;f = 0;g = 0;h = 0;i = 0;temp="";ok = 0;cf = 0;choice=9;aRandomNumber = 0;comp = 0; document.images.A.src= blank;document.images.B.src= blank;document.images.C.src= blank;document.images.D.src= blank;document.images.E.src= blank;document.images.F.src= blank;document.images.G.src= blank;document.images.H.src= blank;document.images.I.src= blank;if (t==0) { t=2; myChoice(); }t--;}</script></head> <body><center><div id="wrapper"><table id="gameBoard" cellspacing="0"><tr><td><a href="java script:yourChoice('A')"><img src="blank.jpg" border=0 height=100 width=100 name=A alt="Top-Left"></a></td><td><a href="java script:yourChoice('B')"><img src="blank.jpg" border=0 height=100 width=100 name=B alt="Top-Center"></a></td><td><a href="java script:yourChoice('C')"><img src="blank.jpg" border=0 height=100 width=100 name=C alt="Top-Right"></a></td></tr><tr><td><a href="java script:yourChoice('D')"><img src="blank.jpg" border=0 height=100 width=100 name=D alt="Middle-Left"></a></td><td><a href="java script:yourChoice('E')"><img src="blank.jpg" border=0 height=100 width=100 name=E alt="Middle-Center"></a></td><td><a href="java script:yourChoice('F')"><img src="blank.jpg" border=0 height=100 width=100 name=F alt="Middle-Right"></a></td></tr><tr><td><a href="java script:yourChoice('G')"><img src="blank.jpg" border=0 height=100 width=100 name=G alt="Bottom-Left"></a></td><td><a href="java script:yourChoice('H')"><img src="blank.jpg" border=0 height=100 width=100 name=H alt="Bottom-Center"></a></td><td><a href="java script:yourChoice('I')"><img src="blank.jpg" border=0 height=100 width=100 name=I alt="Bottom-Right"></a></td></tr></table><td><table><tr><td><input type=text size=5 name=xuser></td><td>You</td></tr><tr><td><input type=text size=5 name=ouser></td><td>Computer</td></tr><tr><td><input type=text size=5 name=ties></td><td>Ties</td></tr></table></td></table><input type=button value="Two Player/Play against human again" onClick="newGame();"><input type=button value="One Player/Play against computer again" onClick="playAgain();"><input type=button value="Game Help" onClick="help();"></form></center> </div> </body></html> What's wrong with my code? It's driving mg nuts. Link to comment Share on other sites More sharing options...
Chocolate570 Posted November 22, 2006 Share Posted November 22, 2006 From what I can see, everything is working fine. I just commented out the HTML comments in the javascript. That made it work like a charm.And nice AI. I can't win :)seekond.com/scripts/tictac.txtYour code is located there, fixed. I didn't want to paste it here...it gets screwed up by IPB.Hope that helps. :)Choco Link to comment Share on other sites More sharing options...
Vajira Posted November 22, 2006 Author Share Posted November 22, 2006 The code that you edited does work, but only for user vs. computer only. You see, the problem with my code is that the player vs. player mode (On same PC) doesn't work for some odd reason. I'm thinking if I need to add a variable to indicate that the two player mode is on, but I don't know how and where to start.But thanks for spending the time to help me out. Link to comment Share on other sites More sharing options...
Vajira Posted November 26, 2006 Author Share Posted November 26, 2006 Alright, after much effort I've managed to come up with a new code, much better than the disastrous code I had earlier. I'm aware my previous code was a total mess, so I wasn't surprised nobody replied to me.I've double checked everything for this new code but the program doesn't seem to run, neither one player nor two player would work. I'm wondering if anyone would be kind enough to help me debug it? <?xml version = "1.0"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd"><html xmlns = "http://www.w3.org/1999/xhtml"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head> <title>Tic Tac Toe</title> <script type="text/javascript">var x = "http://www.geocities.com/xxxxx/x.jpg";var o = "http://www.geocities.com/xxxxx/o.jpg";var blank = "http://www.geocities.com/xxxxx/blank.jpg";var pause = 0;var all = 0;var a = 0;var b = 0;var c = 0;var d = 0;var e = 0;var f = 0;var g = 0;var h = 0;var i = 0;var temp="";var ok = 0;var cf = 0;var choice=9;var aRandomNumber = 0;var comp = 0; var t = 0;var xwn = 0;var own = 0;var ts = 0;var twoPlayer = false;<!--Two play Mode-->function twoPlay(){newTwoPlay();twoXmove;twoOmove;win();}<!--Two play: moves-->function twoXmove(chName){pause = 0;if (all!=0) ended();if (all==0) {cf = 0;ok = 0;temp=chName;checkSpace();if (ok==1) {document.images[chName].src = x;}if (ok==0)taken();process();if ((all==0)&&(pause==0)) twoOmove(); }}function twoOmove(chName){pause = 0;if (all!=0) ended();if (all==0) {cf = 0;ok = 0;temp=chName;checkSpace();if (ok==1) {document.images[chName].src = o;}if (ok==0)taken();process();if ((all==0)&&(pause==0)) twoXmove(); }}<!--Two play: new game-->function newTwoPlay() {if (all==0) {if(confirm("This will restart the game and clear all the current scores. OK?")) reset();}if (all>0) reset();}function reset() {all = 0;a = 0;b = 0;c = 0;d = 0;e = 0;f = 0;g = 0;h = 0;i = 0;temp="";ok = 0;cf = 0;choice=9;aRandomNumber = 0;comp = 0; document.images.A.src= blank;document.images.B.src= blank;document.images.C.src= blank;document.images.D.src= blank;document.images.E.src= blank;document.images.F.src= blank;document.images.G.src= blank;document.images.H.src= blank;document.images.I.src= blank;if (t==0) { t=2; myChoice(); }t--;}<!--Two play: check win-->function check(){if ((a=="image for x")&&(b==1)&&(c==1)) all=1;if ((a==1)&&(d==1)&&(g==1)) all=1;if ((a==1)&&(e==1)&&(i==1)) all=1;if ((b==1)&&(e==1)&&(h==1)) all=1;if ((d==1)&&(e==1)&&(f==1)) all=1;if ((g==1)&&(h==1)&&(i==1)) all=1;if ((c==1)&&(f==1)&&(i==1)) all=1;if ((g==1)&&(e==1)&&(c==1)) all=1;if ((a=="image for o")&&(b==2)&&(c==2)) all=2;if ((a==2)&&(d==2)&&(g==2)) all=2;if ((a==2)&&(e==2)&&(i==2)) all=2;if ((b==2)&&(e==2)&&(h==2)) all=2;if ((d==2)&&(e==2)&&(f==2)) all=2;if ((g==2)&&(h==2)&&(i==2)) all=2;if ((c==2)&&(f==2)&&(i==2)) all=2;if ((g==2)&&(e==2)&&(c==2)) all=2;if ((a != 0)&&(b != 0)&&(c != 0)&&(d != 0)&&(e != 0)&&(f != 0)&&(g != 0)&&(h != 0)&&(i != 0)&&(all == 0)) all = 3;} function win() {check();if (all==1){ alert("X user wins!"); xwn++; }if (all==2){ alert("O user wins!"); own++; }if (all==3){ alert("Tie game."); ts++; }if (all!=0) {document.game.xplayer.value = xwn;document.game.oplayer.value = own;document.game.ties.value = ts;}<!--======================================================================================--><!--One play mode--><!--One play: logics-->function logicTwo() {if ((a==2)&&(b==2)&&(c== 0)&&(temp=="")) temp="C";if ((a==2)&&(b== 0)&&(c==2)&&(temp=="")) temp="B";if ((a== 0)&&(b==2)&&(c==2)&&(temp=="")) temp="A";if ((a==2)&&(d==2)&&(g== 0)&&(temp=="")) temp="G";if ((a==2)&&(d== 0)&&(g==2)&&(temp=="")) temp="D";if ((a== 0)&&(d==2)&&(g==2)&&(temp=="")) temp="A";if ((a==2)&&(e==2)&&(i== 0)&&(temp=="")) temp="I";if ((a==2)&&(e== 0)&&(i==2)&&(temp=="")) temp="E";if ((a== 0)&&(e==2)&&(i==2)&&(temp=="")) temp="A";if ((b==2)&&(e==2)&&(h== 0)&&(temp=="")) temp="H";if ((b==2)&&(e== 0)&&(h==2)&&(temp=="")) temp="E";if ((b== 0)&&(e==2)&&(h==2)&&(temp=="")) temp="B";if ((d==2)&&(e==2)&&(f== 0)&&(temp=="")) temp="F";if ((d==2)&&(e== 0)&&(f==2)&&(temp=="")) temp="E";if ((d== 0)&&(e==2)&&(f==2)&&(temp=="")) temp="D";if ((g==2)&&(h==2)&&(i== 0)&&(temp=="")) temp="I";if ((g==2)&&(h== 0)&&(i==2)&&(temp=="")) temp="H";if ((g== 0)&&(h==2)&&(i==2)&&(temp=="")) temp="G";if ((c==2)&&(f==2)&&(i== 0)&&(temp=="")) temp="I";if ((c==2)&&(f== 0)&&(i==2)&&(temp=="")) temp="F";if ((c== 0)&&(f==2)&&(i==2)&&(temp=="")) temp="C";if ((g==2)&&(e==2)&&(c== 0)&&(temp=="")) temp="C";if ((g==2)&&(e== 0)&&(c==2)&&(temp=="")) temp="E";if ((g== 0)&&(e==2)&&(c==2)&&(temp=="")) temp="G";}function logicThree() {if ((a==1)&&(b==1)&&(c==0)&&(temp=="")) temp="C";if ((a==1)&&(b==0)&&(c==1)&&(temp=="")) temp="B";if ((a==0)&&(b==1)&&(c==1)&&(temp=="")) temp="A";if ((a==1)&&(d==1)&&(g==0)&&(temp=="")) temp="G";if ((a==1)&&(d==0)&&(g==1)&&(temp=="")) temp="D";if ((a==0)&&(d==1)&&(g==1)&&(temp=="")) temp="A";if ((a==1)&&(e==1)&&(i==0)&&(temp=="")) temp="I";if ((a==1)&&(e==0)&&(i==1)&&(temp=="")) temp="E";if ((a==0)&&(e==1)&&(i==1)&&(temp=="")) temp="A";if ((b==1)&&(e==1)&&(h==0)&&(temp=="")) temp="H";if ((b==1)&&(e==0)&&(h==1)&&(temp=="")) temp="E";if ((b==0)&&(e==1)&&(h==1)&&(temp=="")) temp="B";if ((d==1)&&(e==1)&&(f==0)&&(temp=="")) temp="F";if ((d==1)&&(e==0)&&(f==1)&&(temp=="")) temp="E";if ((d==0)&&(e==1)&&(f==1)&&(temp=="")) temp="D";if ((g==1)&&(h==1)&&(i==0)&&(temp=="")) temp="I";if ((g==1)&&(h==0)&&(i==1)&&(temp=="")) temp="H";if ((g==0)&&(h==1)&&(i==1)&&(temp=="")) temp="G";if ((c==1)&&(f==1)&&(i==0)&&(temp=="")) temp="I";if ((c==1)&&(f==0)&&(i==1)&&(temp=="")) temp="F";if ((c==0)&&(f==1)&&(i==1)&&(temp=="")) temp="C";if ((g==1)&&(e==1)&&(c==0)&&(temp=="")) temp="C";if ((g==1)&&(e==0)&&(c==1)&&(temp=="")) temp="E";if ((g==0)&&(e==1)&&(c==1)&&(temp=="")) temp="G";}<!--One play: process functions-->function clearOut() {document.game.xplayer.value="0";document.game.oplayer.value="0";document.game.ties.value="0";}function checkSpace() {if ((temp=="A")&&(a==0)) {ok=1;if (cf==0) a=1;if (cf==1) a=2;}if ((temp=="B")&&(b==0)) {ok=1;if (cf==0) b=1;if (cf==1) b=2;}if ((temp=="C")&&(c==0)) {ok=1;if (cf==0) c=1;if (cf==1) c=2;}if ((temp=="D")&&(d==0)) {ok=1;if (cf==0) d=1;if (cf==1) d=2;}if ((temp=="E")&&(e==0)) {ok=1;if (cf==0) e=1;if (cf==1) e=2;}if ((temp=="F")&&(f==0)) {ok=1if (cf==0) f=1;if (cf==1) f=2;}if ((temp=="G")&&(g==0)) {ok=1if (cf==0) g=1;if (cf==1) g=2;}if ((temp=="H")&&(h==0)) {ok=1;if (cf==0) h=1;if (cf==1) h=2;}if ((temp=="I")&&(i==0)) {ok=1;if (cf==0) i=1; if (cf==1) i=2; }}function ended() {alert("The game has already ended. To play a new game click the Play Again button.")}function process() {logicOne();if (all==1){ alert("You won, congratulations!"); xwn++; }if (all==2){ alert("Gotcha! I win!"); own++; }if (all==3){ alert("We tied."); ts++; }if (all!=0) {document.game.xplayer.value = xwn;document.game.oplayer.value = own;document.game.ties.value = ts; }}function playAgain() {if (all==0) {if(confirm("This will restart the game and clear all the current scores. OK?")) reset();}if (all>0) reset();}function reset() {all = 0;a = 0;b = 0;c = 0;d = 0;e = 0;f = 0;g = 0;h = 0;i = 0;temp="";ok = 0;cf = 0;choice=9;aRandomNumber = 0;comp = 0; document.images.A.src= blank;document.images.B.src= blank;document.images.C.src= blank;document.images.D.src= blank;document.images.E.src= blank;document.images.F.src= blank;document.images.G.src= blank;document.images.H.src= blank;document.images.I.src= blank;if (t==0) { t=2; myChoice(); }t--;}<!--One play: moves-->function yourChoice(chName) {pause = 0;if (all!=0) ended();if (all==0) {cf = 0;ok = 0;temp=chName;checkSpace();if (ok==1) {document.images[chName].src = x;}if (ok==0)taken();process();if ((all==0)&&(pause==0)) myChoice(); }}function taken() {alert("That square is already occupied. Please select another square.")pause=1;}function myChoice() {temp="";ok = 0;cf=1;logicTwo();logicThree();checkSpace();while(ok==0) {aRandomNumber=Math.random()comp=Math.round((choice-1)*aRandomNumber)+1;if (comp==1) temp="A";if (comp==2) temp="B";if (comp==3) temp="C";if (comp==4) temp="D";if (comp==5) temp="E";if (comp==6) temp="F";if (comp==7) temp="G";if (comp==8) temp="H";if (comp==9) temp="I";checkSpace();}document.images[temp].src= o;process();}<!--Input check--> //checks which mode should the values in the cells be passed tofunction pass(){if (twoPlayer){twoPlay();} else{yourChoice();}if (twoPlayer){twoPlay();}if (!twoPlayer){yourChoice(chName);}</script></head><!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!--><body><center><form name=game><table border=0><td><table border=1><tr><td><a href="java script:pass('A')"><img src="http://www.geocities.com/xxxxx/blank.jpg" border=0 height=100 width=100 name=A alt="Top-Left"></a></td><td><a href="java script:pass('B')"><img src="http://www.geocities.com/xxxxx/blank.jpg" border=0 height=100 width=100 name=B alt="Top-Center"></a></td><td><a href="java script:pass('C')"><img src="http://www.geocities.com/xxxxx/blank.jpg" border=0 height=100 width=100 name=C alt="Top-Right"></a></td></tr><tr><td><a href="java script:pass('D')"><img src="http://www.geocities.com/xxxxx/blank.jpg" border=0 height=100 width=100 name=D alt="Middle-Left"></a></td><td><a href="java script:pass('E')"><img src="http://www.geocities.com/xxxxx/blank.jpg" border=0 height=100 width=100 name=E alt="Middle-Center"></a></td><td><a href="java script:pass('F')"><img src="http://www.geocities.com/xxxxx/blank.jpg" border=0 height=100 width=100 name=F alt="Middle-Right"></a></td></tr><tr><td><a href="java script:pass('G')"><img src="http://www.geocities.com/xxxxx/blank.jpg" border=0 height=100 width=100 name=G alt="Bottom-Left"></a></td><td><a href="java script:pass('H')"><img src="http://www.geocities.com/xxxxxx/blank.jpg" border=0 height=100 width=100 name=H alt="Bottom-Center"></a></td><td><a href="java script:pass('I')"><img src="http://www.geocities.com/xxxxxx/blank.jpg" border=0 height=100 width=100 name=I alt="Bottom-Right"></a></td></tr></table></td><td><table><tr><td><input type=text size=5 name=xplayer></td><td>X</td></tr><tr><td><input type=text size=5 name=oplayer></td><td>O</td></tr><tr><td><input type=text size=5 name=ties></td><td>Ties</td></tr></table></td></table><input type=button value="One Player/Play against computer again" onClick="twoPlayer = false; playAgain();"><input type=button value="Two Player/Play against human again" onClick="twoPlayer = true; twoplay();"> </form></center></body></html> I know this code is very long, but the program logic is quite easy to understand, so just alot of words, nothing too complicated I guess.This is pretty urgent so any help is appreciated. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now