TGC Codebase Backup



simple logic, mouse control by Anonymous Coder

28th Mar 2004 18:13
Summary

tic-tac-toe game for newbies (me)



Description

non-typist sparky doing code the hard way. earlier versions had pictures and sounds of my grandkids for x and o. where the heck are those line numbers I used to GOTO?



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    set text font "times new roman"
set text size 20
cls
rem --- dedication to kids and grandkids ---
center text 320,0,"This is my first complete and working game made with"
center text 320,20,"the DARK BASIC programming language. It still has"
center text 320,40,"a few minor bugs in it, but seems to work fine."
center text 320,80,"It took me about 24 hours total to design and"
center text 320,100,"work out the bugs. It's crude and has no"
center text 320,120,"artificial intelligence or learning program,"
center text 320,140,"it's just an exercise to create a simple game."
center text 320,180,"Thanks to the unknown author of the Zonecheck"
center text 320,200,"Function, which allows the program to read mouse clicks."
center text 320,260,"This game is dedicated to Amy, James, Alexis, and Erica."
center text 320,300,"DWE 3/28/2004"
center text 320,400,"Hit any key to continue"
suspend for key
rem --- sets number of wins to zero -----
pwin=0
cwin=0

start:

p1=0:p2=0:p3=0:p4=0:p5=0:p6=0:p7=0:p8=0:p9=0
cp1=0:cp2=0:cp3=0:cp4=0:cp5=0:cp6=0:cp7=0:cp8=0:cp9=0
pt=p1+p2+p3+p4+p5+p6+p7+p8+p9
cl=0
cm=0
t=0
cp=0
pm=0
set text font "times new roman"
set text size 30
cls
get image 1,0,0,30,50
rem *****set ink and draw board***
ink rgb(255,255,255),10
line 150,0,450,0
line 150,0,150,330
line 150,330,450,330
line 450,0,450,330
line 250,0,250,330
line 350,0,350,330
line 150,110,450,110
line 150,220,450,220
get image 1,0,0,460,340
rem *****draw X***
ink rgb(0,255,0),0
line 170,18,230,88
line 170,19,230,89
line 170,20,230,90
line 170,21,230,91
line 170,22,230,92
line 170,88,230,18
line 170,92,230,22
line 170,90,230,20
line 170,89,230,19
line 170,91,230,21
get image 2,151,1,249,109
rem *****draw O***
ink rgb(255,0,0),0
circle 300,55,30
circle 300,55,31
circle 300,55,32
circle 300,55,33
get image 3,251,1,349,109
rem *****get image to clear text***
get image 4,1,378,640,480
cls

paste image 1,0,0
ink rgb(255,255,255),0


paste image 4,1,400
rem --- who goes first ---
gosub whostart
rem *** startofgame *****
startofgame:
rem --- clear screen ---
gosub setc
if cp=0 then gosub controlpanel

if cp=10 then gosub compmove
rem suspend for key

goto startofgame
end

rem -----Elegant Zone Check Function
function zoneclicked(x1,y1,x2,y2)
   clicked=0
   if mousex()>x1 and mousex()<x2
      if mousey()>y1 and mousey()<y2
         if mouseclick()=1
            clicked=1
         endif
      endif
   endif
endfunction clicked

rem *** controlpanel is player's move *****
controlpanel:
gosub setc
cp=0
repeat
wait 1
until mouseclick()=1

   if zoneclicked(149,0,249,109)=1 and p1<>0 then goto youcant
   if zoneclicked(251,0,349,109)=1 and p2<>0 then goto youcant
   if zoneclicked(351,0,449,109)=1 and p3<>0 then goto youcant
   if zoneclicked(151,111,249,219)=1 and p4<>0 then goto youcant
   if zoneclicked(251,111,349,219)=1 and p5<>0 then goto youcant
   if zoneclicked(351,111,449,219)=1 and p6<>0 then goto youcant
   if zoneclicked(151,221,249,329)=1 and p7<>0 then goto youcant
   if zoneclicked(251,221,349,329)=1 and p8<>0 then goto youcant
   if zoneclicked(351,221,449,329)=1 and p9<>0 then goto youcant


   if zoneclicked(149,0,249,109)=1 then cp=1:p1=4:pm=pm+1:paste image 2,151,1
   if zoneclicked(251,0,349,109)=1 then cp=2:p2=4:pm=pm+1:paste image 2,251,1
   if zoneclicked(351,0,449,109)=1 then cp=3:p3=4:pm=pm+1:paste image 2,351,1
   if zoneclicked(151,111,249,219)=1 then cp=4:p4=4:pm=pm+1:paste image 2,151,111
   if zoneclicked(251,111,349,219)=1 then cp=5:p5=4:pm=pm+1:paste image 2,251,111
   if zoneclicked(351,111,449,219)=1 then cp=6:p6=4:pm=pm+1:paste image 2,351,111
   if zoneclicked(151,221,249,329)=1 then cp=7:p7=4:pm=pm+1:paste image 2,151,221
   if zoneclicked(251,221,349,329)=1 then cp=8:p8=4:pm=pm+1:paste image 2,251,221
   if zoneclicked(351,221,449,329)=1 then cp=9:p9=4:pm=pm+1:paste image 2,351,221

if p1+p2+p3=12 then cl=123:gosub playwin
if p4+p5+p6=12 then cl=456:gosub playwin
if p7+p8+p9=12 then cl=789:gosub playwin
if p1+p4+p7=12 then cl=147:gosub playwin
if p2+p5+p8=12 then cl=258:gosub playwin
if p3+p6+p9=12 then cl=369:gosub playwin
if p1+p5+p9=12 then cl=159:gosub playwin
if p3+p5+p7=12 then cl=357:gosub playwin

   if cp=0 then wait 5:goto controlpanel
if cm=4 and pm=5 then goto draw
if cm=5 and pm=4 then goto draw
cp=10
return
end

rem *** Computer's Move *****
compmove:
gosub setc

c=0
repeat
text 40,400, "Computer thinking..."
wait 5
t=rnd(9)






if p2+p3=2 and p1=0 then cm=cm+1:p1=1:c=1:paste image 3,151,1:goto next
if p1+p3=2 and p2=0 then cm=cm+1:p2=1:c=1:paste image 3,251,1:goto next
if p1+p2=2 and p3=0 then cm=cm+1:p3=1:c=1:paste image 3,351,1:goto next

if p5+p6=2 and p4=0 then cm=cm+1:p4=1:c=1:paste image 3,151,111:goto next
if p4+p6=2 and p5=0 then cm=cm+1:p5=1:c=1:paste image 3,251,111:goto next
if p4+p5=2 and p6=0 then cm=cm+1:p6=1:c=1:paste image 3,351,111:goto next

if p8+p9=2 and p7=0 then cm=cm+1:p7=1:c=1:paste image 3,151,221:goto next
if p7+p9=2 and p8=0 then cm=cm+1:p8=1:c=1:paste image 3,251,221:goto next
if p7+p8=2 and p9=0 then cm=cm+1:p9=1:c=1:paste image 3,351,221:goto next

if p4+p7=2 and p1=0 then cm=cm+1:p1=1:c=1:paste image 3,151,1:goto next
if p1+p7=2 and p4=0 then cm=cm+1:p4=1:c=1:paste image 3,151,111:goto next
if p1+p4=2 and p7=0 then cm=cm+1:p7=1:c=1:paste image 3,151,221:goto next

if p5+p8=2 and p2=0 then cm=cm+1:p2=1:c=1:paste image 3,251,1:goto next
if p2+p8=2 and p5=0 then cm=cm+1:p5=1:c=1:paste image 3,251,111:goto next
if p2+p5=2 and p8=0 then cm=cm+1:p8=1:c=1:paste image 3,251,221:goto next

if p6+p9=2 and p3=0 then cm=cm+1:p3=1:c=1:paste image 3,351,1:goto next
if p3+p9=2 and p6=0 then cm=cm+1:p6=1:c=1:paste image 3,351,111:goto next
if p3+p6=2 and p9=0 then cm=cm+1:p9=1:c=1:paste image 3,351,221:goto next

if p5+p9=2 and p1=0 then cm=cm+1:p1=1:c=1:paste image 3,151,1:goto next
if p1+p9=2 and p5=0 then cm=cm+1:p5=1:c=1:paste image 3,251,111:goto next
if p1+p5=2 and p9=0 then cm=cm+1:p9=1:c=1:paste image 3,351,221:goto next

if p5+p7=2 and p3=0 then cm=cm+1:p3=1:c=1:paste image 3,351,1:goto next
if p3+p7=2 and p5=0 then cm=cm+1:p5=1:c=1:paste image 3,251,111:goto next
if p3+p5=2 and p7=0 then cm=cm+1:p7=1:c=1:paste image 3,151,221:goto next


if p2+p3=8 and p1=0 then cm=cm+1:p1=1:c=1:paste image 3,151,1:goto next
if p1+p3=8 and p2=0 then cm=cm+1:p2=1:c=1:paste image 3,251,1:goto next
if p1+p2=8 and p3=0 then cm=cm+1:p3=1:c=1:paste image 3,351,1:goto next

if p5+p6=8 and p4=0 then cm=cm+1:p4=1:c=1:paste image 3,151,111:goto next
if p4+p6=8 and p5=0 then cm=cm+1:p5=1:c=1:paste image 3,251,111:goto next
if p4+p5=8 and p6=0 then cm=cm+1:p6=1:c=1:paste image 3,351,111:goto next

if p8+p9=8 and p7=0 then cm=cm+1:p7=1:c=1:paste image 3,151,221:goto next
if p7+p9=8 and p8=0 then cm=cm+1:p8=1:c=1:paste image 3,251,221:goto next
if p7+p8=8 and p9=0 then cm=cm+1:p9=1:c=1:paste image 3,351,221:goto next

if p4+p7=8 and p1=0 then cm=cm+1:p1=1:c=1:paste image 3,151,1:goto next
if p1+p7=8 and p4=0 then cm=cm+1:p4=1:c=1:paste image 3,151,111:goto next
if p1+p4=8 and p7=0 then cm=cm+1:p7=1:c=1:paste image 3,151,221:goto next

if p5+p8=8 and p2=0 then cm=cm+1:p2=1:c=1:paste image 3,251,1:goto next
if p2+p8=8 and p5=0 then cm=cm+1:p5=1:c=1:paste image 3,251,111:goto next
if p2+p5=8 and p8=0 then cm=cm+1:p8=1:c=1:paste image 3,251,221:goto next

if p6+p9=8 and p3=0 then cm=cm+1:p3=1:c=1:paste image 3,351,1:goto next
if p3+p9=8 and p6=0 then cm=cm+1:p6=1:c=1:paste image 3,351,111:goto next
if p3+p6=8 and p9=0 then cm=cm+1:p9=1:c=1:paste image 3,351,221:goto next

if p5+p9=8 and p1=0 then cm=cm+1:p1=1:c=1:paste image 3,151,1:goto next
if p1+p9=8 and p5=0 then cm=cm+1:p5=1:c=1:paste image 3,251,111:goto next
if p1+p5=8 and p9=0 then cm=cm+1:p9=1:c=1:paste image 3,351,221:goto next

if p5+p7=8 and p3=0 then cm=cm+1:p3=1:c=1:paste image 3,351,1:goto next
if p3+p7=8 and p5=0 then cm=cm+1:p5=1:c=1:paste image 3,251,111:goto next
if p3+p5=8 and p7=0 then cm=cm+1:p7=1:c=1:paste image 3,151,221:goto next




if t=1 and p1=0 then cm=cm+1:p1=1:c=1:paste image 3,151,1
if t=2 and p2=0 then cm=cm+1:p2=1:c=1:paste image 3,251,1
if t=3 and p3=0 then cm=cm+1:p3=1:c=1:paste image 3,351,1
if t=4 and p4=0 then cm=cm+1:p4=1:c=1:paste image 3,151,111
if t=5 and p5=0 then cm=cm+1:p5=1:c=1:paste image 3,251,111
if t=6 and p6=0 then cm=cm+1:p6=1:c=1:paste image 3,351,111
if t=7 and p7=0 then cm=cm+1:p7=1:c=1:paste image 3,151,221
if t=8 and p8=0 then cm=cm+1:p8=1:c=1:paste image 3,251,221
if t=9 and p9=0 then cm=cm+1:p9=1:c=1:paste image 3,351,221

until c=1

next:
c=0
cp=0

if p1+p2+p3=3 then cl=123:gosub compwin
if p4+p5+p6=3 then cl=456:gosub compwin
if p7+p8+p9=3 then cl=789:gosub compwin
if p1+p4+p7=3 then cl=147:gosub compwin
if p2+p5+p8=3 then cl=258:gosub compwin
if p3+p6+p9=3 then cl=369:gosub compwin
if p1+p5+p9=3 then cl=159:gosub compwin
if p3+p5+p7=3 then cl=357:gosub compwin
if cm=4 and pm=5 then goto draw
if cm=5 and pm=4 then goto draw
return

rem *** Computer Wins *****
compwin:
   cwin=cwin+1

ink rgb(255,0,0),0
   if cl=123 then line 150,55,450,55
   if cl=456 then line 150,165,450,165
   if cl=789 then line 150,275,450,275
   if cl=147 then line 200,0,200,330
   if cl=258 then line 300,0,300,330
   if cl=369 then line 400,0,400,330
   if cl=159 then line 150,0,450,330
   if cl=357 then line 450,0,150,330
wait 500
gosub setc
ink rgb(255,255,255),10
text 0,400, "Computer WINS!"
gosub score

goto start

rem *** clear text field *****
setc:
paste image 4,0,378
set cursor 0,460
print "Hit ESC to quit game                                                                                 LEFT-CLICK to continue"
return

youcant:
gosub setc

text 0, 400,"You can't move there!"
wait 500
goto controlpanel

playwin:
   pwin=pwin+1

ink rgb(0,255,0),0
   if cl=123 then line 150,55,450,55
   if cl=456 then line 150,165,450,165
   if cl=789 then line 150,275,450,275
   if cl=147 then line 200,0,200,330
   if cl=258 then line 300,0,300,330
   if cl=369 then line 400,0,400,330
   if cl=159 then line 150,0,450,330
   if cl=357 then line 450,0,150,330
wait 500
gosub setc
ink rgb(255,255,255),10
text 0,400, "Player WINS!"

gosub score
goto start

score:

text 0,0,"Computer"
text 50,30,str$(cwin)
text 530,0,"You"
text 550,30,str$(pwin)
text 270,400,"Draw"
text 290,430,str$(dr)
   repeat
   wait 1
   until mouseclick()=1

return

draw:
gosub setc
text 0,400, "No Winner!"
dr=dr+1
gosub score
goto start

whostart:

set text font "times new roman"
set text size 30

center text 320,400, "Who goes first?"
text 25,400, "Computer"
text 530,400, "You"
line 10,380,150,380
line 10,450,150,450
line 10,380,10,450
line 150,380,150,450

line 490,380,630,380
line 490,450,630,450
line 490,380,490,450
line 630,380,630,450
   repeat
   wait 1
   until mouseclick()=1
   if zoneclicked(10,380,150,450)=1 then cp=10
   if zoneclicked(490,380,6300,450)=1 then cp=0
gosub setc
return