This is a tic tac toe "script" human always goes
first, can select char(X or O). My next is to implement
a routine for the computer to go first.
And if someone wants to do that it's OK but do it in
another thread please. I want to figure it out myself.
But I enjoyed writing it. What I would like, is some
pointers on making my grid look better.
And if anyone finds a bug I would also like to know
that. It was funny I had written the thing and played
it "logically" and I couldn't beat it. I ask my son
to play it beats is first time....duhh. Had to write
the defend corners section for unlogical play. Just like
the real world huh.
Enjoy it make any changes you want for yourself or your
site.
Thanks again to everyone who participates in the forum
alot of what I used in this script came from knowledge
absorbed here.
2b||!2b
first, can select char(X or O). My next is to implement
a routine for the computer to go first.
And if someone wants to do that it's OK but do it in
another thread please. I want to figure it out myself.
But I enjoyed writing it. What I would like, is some
pointers on making my grid look better.
And if anyone finds a bug I would also like to know
that. It was funny I had written the thing and played
it "logically" and I couldn't beat it. I ask my son
to play it beats is first time....duhh. Had to write
the defend corners section for unlogical play. Just like
the real world huh.
Enjoy it make any changes you want for yourself or your
site.
Thanks again to everyone who participates in the forum
alot of what I used in this script came from knowledge
absorbed here.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Tic-Tac-To Ver 1.0000</title>
<style type="text/css">
#tac{background-color: #FEBF01; color: #000000;
font-size: 30px; font-family: serif; font-weight:bold;
border: medium none;}
</style>
<script language="JavaScript">
var pos = new Array("[ABC]","[DEF]","[GHI]","[ADG]",
"[BEH]","[CFI]","[CEG]","[AEI]");
var crnrs = new Array("[DAB]","[BCF]","[FIH]","[HGD]");
var plr = 0;
var turn = ""; // x or o
var mvs = 0;
var X_str = ""; // x move history
var O_str = ""; // y move history
var defcrnr = false;
var csafe = true;
function init() {
document.tictac.reset();
turn = "";
X_str = "";
O_str = "";
mvs = 0;
plr = 0;
defcrnr = false;
csafe = true;
}
function ck_win(str) {
for(var i=0;i<pos.length;i++) {
var restr = new RegExp(pos[i],"g");
strb = str.match(restr);
if(strb && strb.length == 3) { alert(turn+ " wins!"); init(); break;}}
if(mvs == 9) { alert("Meow Cat Wins-Try Again"); init();}
}
function tic(el) {
if(plr == 0) { alert("Select Player to Start.."); }
else { mvs++; el.value = turn;
if(turn == 'X') { X_str += el.name; ck_win(X_str); turn = 'O';
if(mvs == 1 && turn == 'O'){ comp = 'O';}}
else { O_str += el.name; ck_win(O_str); turn = 'X';
if(mvs == 1 && turn == 'X'){ comp = 'X'; }}}
if(plr != 0 && comp == turn) { if(comp == 'X') { calcmv(turn,O_str,X_str); }
else{calcmv(turn,X_str,O_str);}}}
function calcmv(turn,strhm,strpc) {
var defend = false;
var pcwin = false;
var cat = true;
d = document.tictac;
if(mvs == 1 && d.E.value == ""){
var cmd = 'tic(d.E)';
cat = false; defcrnr = true;}
else { if(mvs == 1) {var cmd = 'tic(d.C)'; cat = false;}}
if(mvs > 1){ for(j=0;j<pos.length;j++) {
restr = new RegExp(pos[j],"g");
strb = strpc.match(restr);
if(strb && strb.length == 2){
comp_pos = pos[j].substring(1,4);
comp_str = strb[0]+strb[1];
for(k=0;k<comp_pos.length;k++){
cval = comp_str.indexOf(comp_pos.charAt(k))
if(cval == -1 && d[comp_pos.charAt(k)].value == ""){
cmd = 'tic(d.' +comp_pos.charAt(k)+')';
cat = false; pcwin = true; break;}}}}}
if(mvs > 1 && !pcwin){ for(var j=0;j<pos.length;j++) {
var restr = new RegExp(pos[j],"g");
strb = strhm.match(restr);
if(strb && strb.length == 2){
comp_pos = pos[j].substring(1,4);
comp_str = strb[0]+strb[1];
for(var k=0;k<comp_pos.length;k++){
cval = comp_str.indexOf(comp_pos.charAt(k));
if(cval == -1 && d[comp_pos.charAt(k)].value == ""){
defend = true;
cmd = 'tic(d.'+comp_pos.charAt(k)+')';
cat = false; break;}}}}}
if(!defend && !pcwin){ if(defcrnr){
for(k=0;k<crnrs.length;k++) {
restr = new RegExp(crnrs[k],"g");
strb = strhm.match(restr);
if(strb && strb.length == 2) {
comp_pos = crnrs[k].substring(1,4)
comp_str = strb[0] + strb[1];
for(j=0;j<comp_pos.length;j++) {
if(comp_str.indexOf(comp_pos.charAt(j)) == -1 &&
d[comp_pos.charAt(j)].value == ""){
csafe = false; cat = false; defcrnr = false;
cmd = 'tic(d.'+comp_pos.charAt(j)+')'; break;}}}}}
if(csafe) { for(j=0;j<pos.length;j++) {
restr = new RegExp(pos[j],"g");
strb = strpc.match(restr);
if(strb && strb.length == 1){
nwpos = '['+strb[0]+']';
nwpos = new RegExp(nwpos,"g");
nwposb = pos[j].replace(nwpos,"");
if(d[nwposb.charAt(1)].value == "" && d[nwposb.charAt(2)].value == ""){
cmd = 'tic(d.'+nwposb.charAt(1)+')';
cat = false; break;}}}}}
if(cat){ for(var e=0;e<d.elements.length;e++) {
if(d.elements[e].value == "") {
cmd = 'tic(d.'+d.elements[e].name+')'; break;}}}
// write random fill for corner take
csafe = true;
setTimeout(cmd,1200);
}
</script>
</head>
<body><form name="tictac">
<table width="74" height="72" bgcolor='white' cellpadding="0" cellspacing="2"><tbody>
<tr><td width="23">
<input id="tac" type="text" name="A" value="" size="1" onClick="tic(this);">
</td><td width="20" bgcolor='blue' rowspan="5"> </td><td width="23">
<input id="tac" type="text" name="B" value="" size="1" onClick="tic(this);">
</td><td width="20" bgcolor='blue' rowspan="5"> </td><td width="23">
<input id="tac" type="text" name="C" value="" size="1" onClick="tic(this);">
</td></tr><tr><td colspan="5" height="6" bgcolor='blue'> </td></tr>
<tr><td width="23">
<input id="tac" type="text" name="D" value="" size="1" onClick="tic(this);">
</td><td width="23">
<input id="tac" type="text" name="E" value="" size="1" onClick="tic(this);">
</td><td width="23">
<input id="tac" type="text" name="F" value="" size="1" onClick="tic(this);">
</td></tr><tr><td colspan="5" height="6" bgcolor='blue'> </td></tr>
<tr><td width="23">
<input id="tac" type="text" name="G" value="" size="1" onClick="tic(this);">
</td><td width="23">
<input id="tac" type="text" name="H" value="" size="1" onClick="tic(this);">
</td><td width="23">
<input id="tac" type="text" name="I" value="" size="1" onClick="tic(this);">
</td><td width="23">
</td></tr></tbody></table>
<br><input type="button" value=" X Goes First " onClick="turn='X'; plr = 1;">
<br><br><input type="button" value=" O Goes First " onClick="turn='O'; plr = 1;">
<br><br><input type="button" value=" New Game " onClick="reset();init();"
</form></body></html>
2b||!2b