'This is a first attempt at porting X n O's Tutorial
'from Max to Monkey
'
'
Import mojo
Class XnO Extends App
Const Cplayer1 : Int = 1
Const Cplayer2 : Int = 2
Const Cblank : Int = 0
Const Cx : Int = 1
Const Co : Int = 2
Const PlayerOne : Int = 1
Const PlayerTwo : Int = 2
Const Win : Int = 1
Const Draw : Int = 2
Field Iboard : Image
Field Ix : Image
Field Io : Image
Field Ipointer : Image
Field Iwin : Image
Field Idraw : Image
Field PlayerTurn : Int = PlayerOne
Field gamewon : Int = 0
Field TilesLeft : Int = 9
Field Aboard : Int[9]
Method OnCreate ()
'Load in our sprites.
Iboard = LoadImage ("board.png")
Ix = LoadImage ("x.png")
Io = LoadImage ("o.png")
Ipointer = LoadImage ("pointer.png")
Iwin = LoadImage ("win.png")
Idraw = LoadImage ("draw.png")
'goot no clue what this does.
SetUpdateRate(60)
End
Method OnUpdate ()
If KeyHit(KEY_SPACE) Then Self.ClearBoard()
Self.ScanforWin()
For Local tile:Int = 0 To 8
If MouseHit(MOUSE_LEFT)>=1 And Self.gamewon<>Self.Win And Self.gamewon<>Self.Draw
Select Self.PlayerTurn
Case Self.PlayerOne
Print("In Box P1 = "+Self.MouseInBox())
Self.Aboard[Self.MouseInBox()] = Self.Cx
Self.TilesLeft=Self.TilesLeft-1
Case Self.PlayerTwo
Print("In Box P2 = "+Self.MouseInBox())
Self.Aboard[Self.MouseInBox()] = Self.Co
Self.TilesLeft=Self.TilesLeft-1
End Select
End If
Next
End
Method ScanforWin()
If Self.Aboard[0] = Self.Cx And Self.Aboard[3] = Self.Cx And Self.Aboard[6] = Self.Cx Then Self.gamewon = Self.Win
If Self.Aboard[1] = Self.Cx And Self.Aboard[4] = Self.Cx And Self.Aboard[7] = Self.Cx Then Self.gamewon = Self.Win
If Self.Aboard[2] = Self.Cx And Self.Aboard[5] = Self.Cx And Self.Aboard[8] = Self.Cx Then Self.gamewon = Self.Win
If Self.Aboard[0] = Self.Cx And Self.Aboard[1] = Self.Cx And Self.Aboard[2] = Self.Cx Then Self.gamewon = Self.Win
If Self.Aboard[3] = Self.Cx And Self.Aboard[4] = Self.Cx And Self.Aboard[5] = Self.Cx Then Self.gamewon = Self.Win
If Self.Aboard[6] = Self.Cx And Self.Aboard[7] = Self.Cx And Self.Aboard[8] = Self.Cx Then Self.gamewon = Self.Win
If Self.Aboard[0] = Self.Cx And Self.Aboard[4] = Self.Cx And Self.Aboard[8] = Self.Cx Then Self.gamewon = Self.Win
If Self.Aboard[2] = Self.Cx And Self.Aboard[4] = Self.Cx And Self.Aboard[6] = Self.Cx Then Self.gamewon = Self.Win
If Self.Aboard[0] = Self.Co And Self.Aboard[3] = Self.Co And Self.Aboard[6] = Self.Co Then Self.gamewon = Self.Win
If Self.Aboard[1] = Self.Co And Self.Aboard[4] = Self.Co And Self.Aboard[7] = Self.Co Then Self.gamewon = Self.Win
If Self.Aboard[2] = Self.Co And Self.Aboard[5] = Self.Co And Self.Aboard[8] = Self.Co Then Self.gamewon = Self.Win
If Self.Aboard[0] = Self.Co And Self.Aboard[1] = Self.Co And Self.Aboard[2] = Self.Co Then Self.gamewon = Self.Win
If Self.Aboard[3] = Self.Co And Self.Aboard[4] = Self.Co And Self.Aboard[5] = Self.Co Then Self.gamewon = Self.Win
If Self.Aboard[6] = Self.Co And Self.Aboard[7] = Self.Co And Self.Aboard[8] = Self.Co Then Self.gamewon = Self.Win
If Self.Aboard[0] = Self.Co And Self.Aboard[4] = Self.Co And Self.Aboard[8] = Self.Co Then Self.gamewon = Self.Win
If Self.Aboard[2] = Self.Co And Self.Aboard[4] = Self.Co And Self.Aboard[6] = Self.Co Then Self.gamewon = Self.Win
' If we have run out of tiles to use, and no one has won the game, it must be a draw.
If Self.TilesLeft = 0 And Self.gamewon <> Self.Win Then Self.gamewon = Self.Draw
End Method
Method MouseInBox:Int()
Local tile:Int =0
For Local y:Int = 0 To 2
For Local x:Int = 0 To 2
If MouseX()>= 12+(x*130) And MouseX()<=12+(x*130)+100 And MouseY()>=12+(y*130) And MouseY()<=12+(y*130)+100
If Self.Aboard[tile] = Self.Cblank Then
Select Self.PlayerTurn
Case Self.PlayerOne
Self.PlayerTurn = Self.PlayerTwo
Case Self.PlayerTwo
Self.PlayerTurn = Self.PlayerOne
End Select
If tile>=9 Then tile=0
Return tile
End If
End If
tile=tile+1
Next
Next
If tile>=9 Then tile=0
Return tile
End Method
Method OnRender ()
Cls 255, 255, 255
DrawImage Iboard , 0 , 0
Local tile:Int = 0
For Local y:Int = 0 To 2
For Local x:Int = 0 To 2
Select Self.Aboard[tile]
Case Cx
DrawImage Self.Ix,12+(x*130),12+(y*130)
Case Co
DrawImage Self.Io,12+(x*130),12+(y*130)
Case Cblank
Default
End Select
tile=tile+1
Next
Next
'If gamewon = win, so a player has won the game, then draw the WIN image at 0 pixels in, and 0 pixels down.
If Self.gamewon = Self.Win Then DrawImage Self.Iwin , 0 , 0
'If gamewon = draw, so a player has won the game, then draw the draw image at 0 pixels in, and 0 pixels down.
If Self.gamewon = Self.Draw Then DrawImage Self.Idraw , 0 , 0
'this will draw our pointer at the mousex and mousey location.
DrawImage Self.Ipointer,MouseX(),MouseY()
DrawText "Mouse X [ "+MouseX()+"]",390,1
DrawText "Mouse Y [ "+MouseY()+"]",390,30
DrawText "Mouse C [ "+MouseHit(MOUSE_LEFT)+"]", 390,60
End
Method ClearBoard()
For Local tile:Int = 0 To 8
Self.Aboard[tile] = 0
Next
Self.gamewon = 0
Self.TilesLeft=9
End Method
End
Function Main ()
New XnO
End
Right in my max version, I click a box and I get my little X and then a little O drawn, but this seems to break on the array index, and just freaks out and I'm not sure why.
any help would be appreciated.
|