A TicTacToe game my friend & I made [JAVASCRIPT]

Forum rules
- Ask questions about or share code from languages other than Source Engine scripting.
- Start new topic titles with the code language in brackets (i.e. [C#], [C++], [PHP], etc.)
User avatar
fire1000678
750+ Posts
750+ Posts
Posts: 930
Joined: Fri Feb 03, 2012 8:10 pm

A TicTacToe game my friend & I made [JAVASCRIPT]

Post by fire1000678 »

Code: Select all

<html>

<body>
<input type = "button" id = "tl" onclick = "buttonclick(this);" value = " ">
<input type = "button" id = "tc" onclick = "buttonclick(this);" value = " ">
<input type = "button" id = "tr" onclick = "buttonclick(this);" value = " "> <br>
<input type = "button" id = "cl" onclick = "buttonclick(this);" value = " ">
<input type = "button" id = "cc" onclick = "buttonclick(this);" value = " ">
<input type = "button" id = "cr" onclick = "buttonclick(this);" value = " "> <br>
<input type = "button" id = "bl" onclick = "buttonclick(this);" value = " ">
<input type = "button" id = "bc" onclick = "buttonclick(this);" value = " ">
<input type = "button" id = "br" onclick = "buttonclick(this);" value = " "> <br>

<script type="text/javascript">
clearboard();
var tl = document.getElementById("tl");
var tc = document.getElementById("tc");
var tr = document.getElementById("tr");
var cl = document.getElementById("cl");
var cc = document.getElementById("cc");
var cr = document.getElementById("cr");
var bl = document.getElementById("bl");
var bc = document.getElementById("bc");
var br = document.getElementById("br");
function buttonclick(but){
but.value = "X";
checkforwin();
}
function checkforwin(){
if(tl.value == "X" && tc.value == "X" && tr.value == "X"){alert("You win!");clearboard();return;}
if(tl.value == "X" && cl.value == "X" && bl.value == "X"){alert("You win!");clearboard();return;}
if(tc.value == "X" && cc.value == "X" && bc.value == "X"){alert("You win!");clearboard();return;}
if(tr.value == "X" && cr.value == "X" && br.value == "X"){alert("You win!");clearboard();return;}
if(cl.value == "X" && cc.value == "X" && cr.value == "X"){alert("You win!");clearboard();return;}
if(bl.value == "X" && bc.value == "X" && br.value == "X"){alert("You win!");clearboard();return;}
if(tl.value == "X" && cc.value == "X" && br.value == "X"){alert("You win!");clearboard();return;}
if(tr.value == "X" && cc.value == "X" && bl.value == "X"){alert("You win!");clearboard();return;}
AIGo();
if(tl.value == "O" && tc.value == "O" && tr.value == "O"){alert("You lose!");clearboard();return;}
if(tl.value == "O" && cl.value == "O" && bl.value == "O"){alert("You lose!");clearboard();return;}
if(tc.value == "O" && cc.value == "O" && bc.value == "O"){alert("You lose!");clearboard();return;}
if(tr.value == "O" && cr.value == "O" && br.value == "O"){alert("You lose!");clearboard();return;}
if(cl.value == "O" && cc.value == "O" && cr.value == "O"){alert("You lose!");clearboard();return;}
if(bl.value == "O" && bc.value == "O" && br.value == "O"){alert("You lose!");clearboard();return;}
if(tl.value == "O" && cc.value == "O" && br.value == "O"){alert("You lose!");clearboard();return;}
if(tr.value == "O" && cc.value == "O" && bl.value == "O"){alert("You lose!");clearboard();return;}
if(xoro(tl) && xoro(tr) && xoro(tc) && xoro(cl) && xoro(cc) && xoro(cr) && xoro(bl) && xoro(bc) && xoro(br)){
alert("Stalemate!");
clearboard(); 
return;}
}
function clearboard(){
document.getElementById("tl").value = " ";
document.getElementById("tc").value = " ";
document.getElementById("tr").value = " ";
document.getElementById("cl").value = " ";
document.getElementById("cc").value = " ";
document.getElementById("cr").value = " ";
document.getElementById("bl").value = " ";
document.getElementById("bc").value = " ";
document.getElementById("br").value = " ";
WhoGoesFirst();
}
function AIGo(){
//checkforwin
if (checkbuttons(tl,tc,tr,"O")){return;}
if (checkbuttons(bl,cc,tr,"O")){return;}
if (checkbuttons(tl,cc,br,"O")){return;}
if (checkbuttons(tr,cr,br,"O")){return;}
if (checkbuttons(tc,bc,cc,"O")){return;}
if (checkbuttons(tl,cl,bl,"O")){return;}
if (checkbuttons(cl,cc,cr,"O")){return;}
if (checkbuttons(bl,bc,br,"O")){return;}
//checkforblock
if (checkbuttons(tl,tc,tr,"X")){return;}
if (checkbuttons(bl,cc,tr,"X")){return;}
if (checkbuttons(tl,cc,br,"X")){return;}
if (checkbuttons(tr,cr,br,"X")){return;}
if (checkbuttons(tc,bc,cc,"X")){return;}
if (checkbuttons(tl,cl,bl,"X")){return;}
if (checkbuttons(cl,cc,cr,"X")){return;}
if (checkbuttons(bl,bc,br,"X")){return;}
//placeanO
if (tl.value == " "){tl.value = "O"; return;}
if (tr.value == " "){tr.value = "O"; return;}
if (tc.value == " "){tc.value = "O"; return;}
if (cl.value == " "){cl.value = "O"; return;}
if (cc.value == " "){cc.value = "O"; return;}
if (cr.value == " "){cr.value = "O"; return;}
if (bl.value == " "){bl.value = "O"; return;}
if (br.value == " "){br.value = "O"; return;}
if (bc.value == " "){bc.value = "O"; return;}
}
function ooblank(elm1,elm2,elm3,letter){
if(elm1.value == letter && elm2.value == letter && elm3.value == " "){return 3;}
if(elm1.value == letter && elm2.value == " " && elm3.value == letter){return 2;}
if(elm1.value == " " && elm2.value == letter && elm3.value == letter){return 1;}
return 0; 
}
function checkbuttons(elm1,elm2,elm3,letter){
var index = ooblank(elm1,elm2,elm3,letter);
switch(index){
case 1: 
elm1.value = "O";
return true;
case 2:
elm2.value = "O";
return true;
case 3:
elm3.value = "O";
return true;
}
return false;
}

function xoro(elm1){
if (elm1.value == "X" || elm1.value == "O") return true;
return false;
}
function randomo(){
var number = Math.floor(Math.random()*9)+1;
switch(number){
case 1:
tl.value = "O";
return;
case 2:
tc.value = "O";
return;
case 3:
tr.value = "O";
return;
case 4:
cl.value = "O";
return;
case 5:
cc.value = "O";
return;
case 6:
cr.value = "O";
return;
case 7:
bl.value = "O";
return;
case 8:
bc.value = "O";
return;
case 9:
br.value = "O";
return;
}
}
function WhoGoesFirst(){
if (Math.random()<=0.5){
randomo();
}
}
</script>
</body>

</html>
上手ですか?

Image

"Just as the tree and the earth need each other for life, so is the body and the soul, for without the other, one is nothing."

Ingame Name: [GR]人生Positively Shinobi{A}
[GR]doodleninja
1000+ Posts
1000+ Posts
Posts: 1086
Joined: Mon Feb 27, 2012 11:08 am

Re: A TicTacToe game my friend & I made [JAVASCRIPT]

Post by [GR]doodleninja »

This is well made
Nice
User avatar
fire1000678
750+ Posts
750+ Posts
Posts: 930
Joined: Fri Feb 03, 2012 8:10 pm

Re: A TicTacToe game my friend & I made [JAVASCRIPT]

Post by fire1000678 »

[GR]doodleninja wrote:This is well made
Nice
Thank you :D
I thought it was nice.
上手ですか?

Image

"Just as the tree and the earth need each other for life, so is the body and the soul, for without the other, one is nothing."

Ingame Name: [GR]人生Positively Shinobi{A}
Nicdel
Server Admin
Posts: 2417
Joined: Sun Apr 11, 2010 4:50 am

Re: A TicTacToe game my friend & I made [JAVASCRIPT]

Post by Nicdel »

Amazing =]
Ingame: -[SaD]- Nico

Image
User avatar
fire1000678
750+ Posts
750+ Posts
Posts: 930
Joined: Fri Feb 03, 2012 8:10 pm

Re: A TicTacToe game my friend & I made [JAVASCRIPT]

Post by fire1000678 »

I love you to, Nico. How have you been?
上手ですか?

Image

"Just as the tree and the earth need each other for life, so is the body and the soul, for without the other, one is nothing."

Ingame Name: [GR]人生Positively Shinobi{A}
Nicdel
Server Admin
Posts: 2417
Joined: Sun Apr 11, 2010 4:50 am

Re: A TicTacToe game my friend & I made [JAVASCRIPT]

Post by Nicdel »

fire1000678 wrote:I love you to, Nico. How have you been?
Oh, pretty good. Finally back home.
Ingame: -[SaD]- Nico

Image
User avatar
fire1000678
750+ Posts
750+ Posts
Posts: 930
Joined: Fri Feb 03, 2012 8:10 pm

Re: A TicTacToe game my friend & I made [JAVASCRIPT]

Post by fire1000678 »

Good, good.
btw, I'm adding a win/lose counter and a title.
Submit code later :3
上手ですか?

Image

"Just as the tree and the earth need each other for life, so is the body and the soul, for without the other, one is nothing."

Ingame Name: [GR]人生Positively Shinobi{A}