Got bored today so I decided to write a game of noughts and crosses, I don't think its been done before (after searching Tic tac toe, noughts and crosses, etc). It has only been tested in the sim as i don't have all the parts needed to build it so if anyone wants to try it out for real go for it .
Unfortunutaly the AI is rather simple and just picks a random place to go so isn't to hard to beat.
Comments Welcome
Jamster
Code:
;*************************************
;* Noughts and crosses *
;*************************************
;* Sometimes called tic-tac-toe *
;*************************************
;
;Designed to be used with a 20x4 serial display on pin B.6
;of a PICAXE 20X2 (To simulate goto Options>>Simulation>>
;Simulate Serial LCD with 'serout' commands on output: and change
;the pin name to B.6) and a keyboard or numeric keypad connected
;to C.1 and C.2
#picaxe 20X2
;When it is your go use the numpad to select the cell you want
;Note: it is not neccesery to have numlock on provided you dont
;want to go in the center square...
;***************************
;*REVISIONS: *
;*~~~~~~~~~~ *
;*#Created: 28.5.11 *
;***************************
'Possibly usefull info:
'~~~~~~~~~~~~~~~~~~~~~~
'Grid: Variables: Keys:
'_|_|_ AA BA CA 7 8 9
'_|_|_ AB BB CB 4 5 6
' | | AC BC CC 1 2 3
SYMBOL Xscore=b9
SYMBOL Oscore=b10
init: 'Cell locations
SYMBOL AA=B0
SYMBOL AB=B1
SYMBOL AC=B2
SYMBOL BA=B3
SYMBOL BB=B4
SYMBOL BC=B5
SYMBOL CA=B6
SYMBOL CB=B7
SYMBOL CC=B8
'Constants
SYMBOL X=88
SYMBOL O=79
AA=95
AB=95
AC=32
BA=95
BB=95
BC=32
CA=95
CB=95
CC=32
gosub disp
'Take user choice:
'~~~~~~~~~~~~~~~~~
you: kbin b11
select b11
case $6C
if AA=95 then
AA=X
else
goto you
endif
case $6B
if AB=95 then
AB=X
else
goto you
endif
case $69
if AC=32 then
AC=X
else
goto you
endif
case $75
if BA=95 then
BA=X
else
goto you
endif
case $73
if BB=95 then
BB=X
else
goto you
endif
case $72
if BC=32 then
BC=X
else
goto you
endif
case $7D
if CA=95 then
CA=X
else
goto you
endif
case $74
if CB=95 then
CB=X
else
goto you
endif
case $7A
if CC=32 then
CC=X
else
goto you
endif
else
goto you
endselect
gosub disp
gosub chek
goto comp
'Create a coputer choice
'~~~~~~~~~~~~~~~~~~~~~~~
comp: random w6
b11=w6/7281
select b11
case 1
if AA=95 then
AA=O
else
goto comp
endif
case 2
if AB=95 then
AB=O
else
goto comp
endif
case 3
if AC=32 then
AC=O
else
goto comp
endif
case 4
if BA=95 then
BA=O
else
goto comp
endif
case 5
if BB=95 then
BB=O
else
goto comp
endif
case 6
if BC=32 then
BC=O
else
goto comp
endif
case 7
if CA=95 then
CA=O
else
goto comp
endif
case 8
if CB=95 then
CB=O
else
goto comp
endif
case 9
if CC=32 then
CC=O
else
goto comp
endif
else
goto comp
endselect
gosub disp
gosub chek
goto you
'Display routine
'~~~~~~~~~~~~~~~
disp: serout B.6,N2400,(254,1)
serout B.6,N2400,(" TIC TAC TOE ")
serout B.6,N2400,(AB,"|",BB,"|",CB," ")
serout B.6,N2400,(AA,"|",BA,"|",CA," ")
serout B.6,N2400,(AC,"|",BC,"|",CC," X=",#b9," O=",#b10," ")
return
'Win routine
'~~~~~~~~~~~
chek: if AA=X and AB=X and AC=X then 'x|_|_
inc Xscore 'x|_|_
goto init 'x| |
endif
if AA=O and AB=O and AC=O then 'o|_|_
inc Oscore 'o|_|_
goto init 'o| |
endif
if BA=X and BB=X and BC=X then '_|x|_
inc Xscore '_|x|_
goto init ' |x|
endif
if BA=O and BB=O and BC=O then '_|o|_
inc Oscore '_|o|_
goto init ' |o|
endif
if CA=X and CB=X and CC=X then '_|_|x
inc Xscore '_|_|x
goto init ' | |x
endif
if CA=O and CB=O and CC=O then '_|_|o
inc Oscore '_|_|o
goto init ' | |o
endif
if CA=X and CB=X and CC=X then '_|_|x
inc Xscore '_|_|x
goto init ' | |x
endif
if AA=X and BA=X and CA=X then 'x|x|x
inc Xscore '_|_|_
goto init ' | |
endif
if AA=O and BA=O and CA=O then 'o|o|o
inc Oscore '_|_|_
goto init ' | |
endif
if AB=X and BB=X and CB=X then '_|_|_
inc Xscore 'x|x|x
goto init ' | |
endif
if AB=O and BB=O and CB=O then '_|_|_
inc Oscore 'o|o|o
goto init ' | |
endif
if AC=X and BC=X and CC=X then '_|_|_
inc Xscore '_|_|_
goto init 'x|x|x
endif
if AC=O and BC=O and CC=O then '_|_|_
inc Oscore '_|_|_
goto init 'o|o|o
endif
if AC=X and BB=X and CA=X then '_|_|x
inc Xscore '_|x|_
goto init 'x| |
endif
if AC=X and BB=X and CA=X then '_|_|o
inc Oscore '_|o|_
goto init 'o| |
endif
if AA=X and BB=X and CC=X then 'x|_|_
inc Xscore '_|x|_
goto init ' | |x
endif
if AA=X and BB=X and CC=X then 'o|_|_
inc Oscore '_|o|_
goto init ' | |o
endif
return
Comments Welcome
Jamster
Last edited: