tic tac toe 2.0

Advanced game related topics
Realizimo
User
User
Posts: 64
Joined: Sun Nov 25, 2012 5:27 pm
Location: Sweden

tic tac toe 2.0

Post by Realizimo »

https://www.youtube.com/watch?v=9nq8SsolZp8
updated nr3

Code: Select all

EnableExplicit
#start=1 ;0=red starts / 1=green starts
#sq=90   ;size
#z=0.12  ;crosssize
#Backcol=#Blue ;not 0 or White
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0 , 0 , 0 , #sq*3 , #sq*5 , " tic tac toe 2.0       N = New Game" , #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0) , 0 , 0 , #sq*3 , #sq*5)

Global Dim x.f(14) , Dim y.f(14) , Dim intensi(14) , Dim size(14) , Dim sprite(14) , Dim txt$(4)
Global a , x , y , col  , player=#start , p , spr , mode , occupied , won , text
Global font = LoadFont(2 , "verdana" , 14)

col=#Red-7
For a=1 To 14 ;pieces
  If a=7 : a=9 : col=#Green : EndIf 
  CreateSprite(a , #sq , #sq)
  StartDrawing(SpriteOutput(a))
  Circle(#sq*0.5 , #sq*0.5 , ((a-1)&6+4) * #sq/27, col+a&7)
  StopDrawing()
Next a
CreateSprite(16 , #sq*3 , #sq*3) ;board
StartDrawing(SpriteOutput(16))
  Box(0 , 0 , #sq*3+1 , #sq*3+1 , #Backcol)
  For a=0 To 3
    x = (#sq*a)-(#sq*#z)/2
    Box( x , 0 , #sq*#z , #sq*3)
    Box( 0 , x , #sq*3 , #sq*#z)
  Next a
StopDrawing()
CreateSprite(17 , 20 , 20) ;pointer
StartDrawing(SpriteOutput(17))
  Circle(10 , 10 , 10) : Circle(10 , 10 , 4 , 0)
StopDrawing()
txt$(0)= "Red"    : txt$(1)= "Green"
txt$(2)= "'s Turn": txt$(4)= " Won.   N =New Game"
For a=20 To 23
  CreateSprite(a , 270 , 20)  ;text
  StartDrawing(SpriteOutput(a))
    DrawingFont(font) : DrawText(0 , 0 , txt$(a&1)+txt$(a&2+2) , #White)
  StopDrawing()
Next a

Procedure newgame()
  Global Dim board.i(3 , 3)
  x(1) = -0.30
  x(2) = 0.04
  x(3) = 0.46
  x(4) = 0.95
  x(5) = 1.51
  x(6) = 2.15
  For a=1 To 6
    x(a+8) = x(a)
    y(a)=0 : y(a+8)=4
    intensi(a)=255 : intensi(a+8)=255
    size(a)=(a-1)&6 : size(a+8)=(a-1)&6
    sprite(a)=1 : sprite(a+8)=1
  Next a
  won=0 :  mode=0 : text=20+player
EndProcedure

Procedure test(spr,x,y)
  Protected.i t1 , t2 , t3 , t4 
  x(spr)=x : y(spr)=y+1
  won = spr&8+16
  board(x , y)=spr+16
  intensi(spr)=255
  For a=0 To 2
    t1+Bool(board(a , y  )&24=won)
    t2+Bool(board(x , a  )&24=won)
    t3+Bool(board(a , a  )&24=won)
    t4+Bool(board(a , 2-a)&24=won)
  Next a
  Debug Str(t1) +" "+ Str(t2) +" "+ Str(t3) +" "+ Str(t4)
  If t1=3 Or t2=3 Or t3=3 Or t4=3
    text = 20+player+2 : player!1
    ProcedureReturn
  EndIf
  player!1 : won=0 :  mode=0 : text = 20+player
EndProcedure
  
newgame()
;- loop
Repeat 
  WindowEvent() : ExamineMouse() : ExamineKeyboard()
  ClearScreen(0) 
  DisplaySprite(16 , 0 , #sq) ;board
  For a=1 To 14
    If sprite(a) : DisplayTransparentSprite(a , x(a)*#sq , y(a)*#sq , intensi(a)) : EndIf 
  Next a
  If won=0
    If MouseButton(#PB_MouseButton_Right) : Mode=0 : intensi(spr)=255 : EndIf
    If MouseButton(#PB_MouseButton_Left)
      StartDrawing(ScreenOutput()) : p=Point(MouseX() , MouseY()) : StopDrawing()
      If p<>#White And p<>#Black        
        If (MouseY()<#sq Or MouseY()>#sq*4) And Mode=0
          If Bool(p>255)=player
            spr=p&7+Bool(p>255)*8
            mode=1
            intensi(spr)=100
          EndIf
        ElseIf mode=1 And (MouseY()>#sq And MouseY()<#sq*4)
          x=MouseX()/#sq : y=MouseY()/#sq-1
          occupied=board(x,y)&15
          If occupied=0 ;free square
            test(spr,x,y)
          ElseIf spr&8<>occupied&8 And size(spr&7)>size(occupied&7) ;other color and bigger
            test(spr,x,y) : sprite(occupied)=0 
          EndIf
        EndIf
      EndIf
    EndIf
  EndIf
  DisplayTransparentSprite(text, 0 , 0) ;text
  DisplayTransparentSprite(17 , MouseX()-10 , MouseY()-10) ;pointer
  FlipBuffers()
  If KeyboardPushed(#PB_Key_N) : newgame() : EndIf
Until KeyboardPushed(#PB_Key_Escape) 
Last edited by Realizimo on Sun Dec 12, 2021 10:00 am, edited 7 times in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: tic tac toe 2.0

Post by Kwai chang caine »

Works fine here :wink:
The most hard to find...it's a second player :lol:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
dige
Addict
Addict
Posts: 1247
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: tic tac toe 2.0

Post by dige »

@Realizimo: Very nice! Thx for sharing
"Daddy, I'll run faster, then it is not so far..."
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: tic tac toe 2.0

Post by Keya »

this very interesting variant doesn't appear to be mentioned in Wikipedia's Tic Tac Toe page ?
Realizimo
User
User
Posts: 64
Joined: Sun Nov 25, 2012 5:27 pm
Location: Sweden

Re: tic tac toe 2.0

Post by Realizimo »

Thanks for response all :)
No buggs?
I now one it eats memory because of "StartDrawing(ScreenOutput())"?
edit: no because of "DrawingFont()"
updated code. no leak "DrawingFont()" is now for sprite and only when it change.
Realizimo
User
User
Posts: 64
Joined: Sun Nov 25, 2012 5:27 pm
Location: Sweden

Re: tic tac toe 2.0

Post by Realizimo »

3th update just finer(shorter) code
Post Reply