20X2 Games with a 20x4 LCD and keyboard: Noughts and crosses

Jamster

Senior Member
So this is a game of Noughts and crosses against the PICAXE, this was the first in a series of games which all involved the same setup.

Comments Always 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                 *
;*#Made a difficult AI: 28.5.11     *
;*#Added check for draw: 28.5.11    *
;*#Took Westausts Advice: 12.6.11   *
;*#Made the AI go for wins: 12.6.11 *
;************************************
'Possibly usefull info:
'~~~~~~~~~~~~~~~~~~~~~~
'Grid:     Variables:    Keys:      Computer codes:
'_|_|_      AA BA CA     7 8 9      1 4 7 (these are only 
'_|_|_      AB BB CB     4 5 6      2 5 8 used by the AI,will 
' | |       AC BC CC     1 2 3      3 6 9 tidy them sometime)
 
 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: 'Check if can win
 if AA=O and BA=O and CA=95 then let b11=7 : goto label endif '1_4_7
 if BA=O and CA=O and AA=95 then let b11=1 : goto label endif '!\!/!
 if AB=O and BB=O and CB=95 then let b11=8 : goto label endif '2_5_8
 if BB=O and CB=O and AB=95 then let b11=2 : goto label endif '!/!\!
 if AC=O and BC=O and CC=32 then let b11=9 : goto label endif '3_6_9
 if BC=O and CC=O and AC=32 then let b11=3 : goto label endif
 if AA=O and AB=O and AC=32 then let b11=3 : goto label endif
 if AB=O and AC=O and AA=95 then let b11=1 : goto label endif
 if BA=O and BB=O and BC=32 then let b11=6 : goto label endif
 if BB=O and BC=O and BA=95 then let b11=4 : goto label endif
 if CA=O and CB=O and CC=95 then let b11=9 : goto label endif
 if CB=O and CC=O and CA=95 then let b11=7 : goto label endif
 if AA=O and BB=O and CC=32 then let b11=9 : goto label endif
 if BB=O and CC=O and AA=95 then let b11=1 : goto label endif
 if AC=O and BB=O and CA=95 then let b11=7 : goto label endif
 if BB=O and CA=O and AC=32 then let b11=3 : goto label endif
 if AA=O and CA=O and BA=95 then let b11=4 : goto label endif
 if AB=O and CB=O and BB=95 then let b11=5 : goto label endif
 if AC=O and CC=O and BC=32 then let b11=6 : goto label endif
 if AA=O and AC=O and AB=95 then let b11=2 : goto label endif
 if BA=O and BC=O and BB=95 then let b11=5 : goto label endif
 if CA=O and CC=O and CB=95 then let b11=8 : goto label endif
 if AA=O and CC=O and BB=95 then let b11=5 : goto label endif
 if AC=O and CA=O and BB=95 then let b11=5 : goto label endif
 'Block
 if AA=X and BA=X and CA=95 then let b11=7 : goto label endif '1_4_7
 if BA=X and CA=X and AA=95 then let b11=1 : goto label endif '!\!/!
 if AB=X and BB=X and CB=95 then let b11=8 : goto label endif '2_5_8
 if BB=X and CB=X and AB=95 then let b11=2 : goto label endif '!/!\!
 if AC=X and BC=X and CC=32 then let b11=9 : goto label endif '3_6_9
 if BC=X and CC=X and AC=32 then let b11=3 : goto label endif
 if AA=X and AB=X and AC=32 then let b11=3 : goto label endif
 if AB=X and AC=X and AA=95 then let b11=1 : goto label endif
 if BA=X and BB=X and BC=32 then let b11=6 : goto label endif
 if BB=X and BC=X and BA=95 then let b11=4 : goto label endif
 if CA=X and CB=X and CC=95 then let b11=9 : goto label endif
 if CB=X and CC=X and CA=95 then let b11=7 : goto label endif
 if AA=X and BB=X and CC=32 then let b11=9 : goto label endif
 if BB=X and CC=X and AA=95 then let b11=1 : goto label endif
 if AC=X and BB=X and CA=95 then let b11=7 : goto label endif
 if BB=X and CA=X and AC=32 then let b11=3 : goto label endif
 if AA=X and CA=X and BA=95 then let b11=4 : goto label endif
 if AB=X and CB=X and BB=95 then let b11=5 : goto label endif
 if AC=X and CC=X and BC=32 then let b11=6 : goto label endif
 if AA=X and AC=X and AB=95 then let b11=2 : goto label endif
 if BA=X and BC=X and BB=95 then let b11=5 : goto label endif
 if CA=X and CC=X and CB=95 then let b11=8 : goto label endif
 if AA=X and CC=X and BB=95 then let b11=5 : goto label endif
 if AC=X and CA=X and BB=95 then let b11=5 : goto label endif
 random w6
 b11=w6/7281
 
label: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=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=O and BB=O and CA=O 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=O and BB=O and CC=O then                  'o|_|_
  inc Oscore                                     '_|o|_
  goto init                                      ' | |o
 endif
 if AA<>95 and AB<>95 and AC<>32 and BA<>95 and BB<>95 and BC<>32 and CA<>95 and CB<>95 and CC<>32 then
  goto init
 endif
 return
 

Attachments

Top