I figured I'd go back to trying out some AppGameKit after the my last attempt last year so picked I picked up my copy of Hands On AppGameKit and started working through the chapters again. I've got up to Chapter 21 which is network a 9x9 Tic Tac Toe game. Now after testing the game I've noticed two issues;
#1 - When a player take's their turn the X/O is placed off the left of the board on the receiving games screen i.e. it is incorrectly placed. Ive checked my code repeatedly against the book and it seems to match but chances are I'm missing something here.
#2 - After the 4 placement i.e. end of turn 2, the game will crash on PC or lockup on AppGameKit Player on Android and iOS with image error related to UpdateScreen specifically
I've included my hand typed code below but I can't provide images as I'm unsure of the distribution license on those.
What I'm hoping for it someone to come in and ay Tal, your an idiot you mistyped 'blah' or something along those lines =p
rem *********************************************************************************
rem * Tic Tac Toe *
rem *********************************************************************************
rem *** Global Variables ***
global dim board[9] =[0,0,0,0,0,0,0,0,0] //The playing area - 0=Empty, 1:X, 2=O
global player //Current player - 1=X, 2=O
global joined=0 //Joined network - 0=No, 1=Yes
global netid //Network ID
global clientid //This client's ID
global movesmade //Number of moves made
rem *********************************************************************************
rem * Main Program Logic *
rem *********************************************************************************
SetUpNetwork()
SetUpBoard()
rem *** X to play ***
player = 1
rem ***start the game logic ***
repeat
rem *** if this clients turn ***
if player = clientid
rem *** enter move ***
move = MakeMove()
else
rem *** Gets opponents move ***
move = GetMove()
endif
rem *** Update screen with new mvoe ***
UpdateScreen(move)
rem *** change player to move
player = 3 - player
rem *** check if the game is finished
r = IsComplete()
rem *** sync it ***
sync()
until r<>0
rem *** show winning line or dame drawn message ***
FinishGame(r mod 10)
sync()
end
rem *********************************************************************************
rem * Level 1 Functions *
rem *********************************************************************************
function SetUpNetwork()
rem *** create host and client buttons ***
AddVirtualButton(1,10,20,10)
SetVirtualButtonText(1,"Host")
AddVirtualButton(2,30,20,10)
SetVirtualButtonText(2,"Join")
rem *** wait till button pressed ***
repeat
HandleButtons()
Sync()
until joined = 1
rem *** wait until both machines connected or timed out (30 seconds time out) ***
time = GetSeconds()
repeat
until GetNetworkNumClients(netid)=2 or GetSeconds() - time > 30
rem *** If we dont have two clients close the game ***
if GetNetworkNumClients(netid) <>2
CloseGame()
endif
rem *** get clients id ***
clientid = GetNetworkMyClientID(netid)
rem *** delete the network buttons ***
DeleteVirtualButton(1)
DeleteVirtualButton(2)
endfunction
function SetUpBoard()
rem *** setup aspect ratio ***
SetDisplayAspect(768/1024.0)
SetClearColor(180,180,180)
Sync()
rem *** load images ***
loadimage(1,"TTTX.png")
loadimage(2,"TTTO.png")
loadimage(3,"TTTTitle.png")
loadimage(4,"TTTBoard.png")
loadimage(5,"TTTMySymbol.png")
loadimage(6,"TTTNxtPlay.png")
loadimage(7,"TTTTile01.png")
rem *** title ***
CreateSprite(3,3)
SetSpriteSize(3,60,-1)
SetSpritePosition(3,20,5)
rem *** board ***
CreateSprite(4,4)
SetSpriteSize(4,80,-1)
SetSpritePosition(4,10,15)
rem *** my symbol legend ***
CreateSprite(5,5)
SetSpriteSize(5,25,-1)
SetSpritePosition(5,10,80)
rem *** symbol box ***
CreateSprite(7,7)
SetSpriteSize(7,12,-1)
SetSpritePosition(7,36,78)
rem *** player shape ***
CreateSprite(1,clientid)
SetSpriteSize(1,8,-1)
SetSpritePosition(1,38,79.5)
rem *** next player legend ***
CreateSprite(6,6)
SetSpriteSize(6,25,-1)
SetSpritePosition(6,50,80)
rem *** symbol box ***
CreateSprite(8,7)
SetSpriteSize(8,12,-1)
SetSpritePosition(8,76,78)
rem *** next players shape ***
CreateSprite(9,1)
SetSpriteSize(9,8,-1)
SetSpritePosition(9,78,79.5)
rem *** both symbols (x and o) in sprite ***
SetSpriteAnimation(9,153,164,1)
AddSpriteAnimationFrame(9,2)
rem *** create a set of hidden sprites ***
rem *** one over each of the 9 areas on the board ***
id = 100
for row = 0 to 2
for col = 0 to 2
inc id
CreateSprite(id,0)
SetSpriteDepth(id,9)
SetSpriteSize(id,25,-1)
SetSpritePosition(id,10+col*27,16.5+row*20)
SetSpriteVisible(id,0)
next col
next row
sync()
endfunction
function MakeMove()
rem *** get empty square seleceted by player ***
state = 0
repeat
repeat
if GetPointerState() = 1 and state = 0
id = GetSpriteHit(GetPointerX(),GetPointerY())
state = 1
else
state = 0
endif
sync()
until id >= 101 and id <= 109
square = id - 100
until board[square]=0
rem *** record players move in the array ***
board[square] = player
rem *** send message of the move to other device ***
messageID = CreateNetworkMessage()
AddNetworkMessageInteger(messageID,player)
AddNetworkMessageInteger(messageID,square)
SendNetworkMessage(netid,3-clientid,messageID)
endfunction square
function GetMove()
rem *** get messages from the network ***
repeat
messageID = GetNetworkMessage(netid)
until messageID <> 0
player = GetNetworkMessageInteger(messageID)
square = GetNetworkMessageInteger(messageID)
rem *** update the board array ***
board[square] = player
rem *** delete the message ***
DeleteNetworkMessage(messageID)
endfunction sqaure
function UpdateScreen(square)
rem ***create the sprite to be placed on the board ***
spriteid = 20+square
CreateSprite(spriteid,player)
SetSpriteSize(spriteid,12,-1)
rem *** position sprite ***
SetSpritePosition(spriteid,17+(spriteid-21) mod 3*27,21+(spriteid-21)/3*20)
rem *** change next player symbol ***
SetSpriteFrame (9, 3-player)
sync()
rem *** add 1 to the number of moves made ***
inc movesmade
endfunction
function IsComplete()
winner = 0
line =0
for c = 1 to 2
rem *** check rows ***
for row = 1 to 7 step 3
if board[row]=c and board[row+1]=c and board[row+2]=c
winner = c
line = (row-1) / 3
endif
next row
rem *** check columns*
for col = 1 to 3
if board[col]=c and board[col+3]=c and board [col+6]=c
winner = c
line = 2+col
endif
next col
rem *** check diagnoals ***
if board[1]=c and board[5]=c and board[9]=c
winner = c
line = 6
elseif board[3]=c and board[5]=c and board[7]=c
winner = c
line = 7
endif
next c
result = winner*10+line
if result = 0 and movesmade = 9
result = 9
endif
endfunction result
function FinishGame(status)
rem *** draw or win ***
if status = 9
GameDrawn()
else
ShowWinningLine(status)
endif
endfunction
rem *********************************************************************************
rem * Level 2 Functions *
rem *********************************************************************************
function HandleButtons()
rem *** if host button pressed and not already joined a session ***
if GetVirtualButtonPressed(1) = 1 and joined = 0
rem *** host a match ***
netid = HostNetwork("TicTacToe","HostMachine",1026)
rem *** record as joined ***
joined = 1
endif
rem *** if client button is pressed and not already joined ***
if GetVirtualButtonPressed(2) = 1 and joined = 0
rem *** join as client ***
clientname$ = str(GetSeconds())
netid = JoinNetwork("TicTacToe",clientname$)
joined = 1
endif
endfunction
function CloseGame()
rem *** Display message ***
CreateText(1, "Game needs two machines to play")
SetTextSize (1,3)
SetTextPosition(1,10,47)
Sync()
rem *** wait 5 seconds then close game ***
sleep (5000)
end
endfunction
function GameDrawn()
rem *** load image used ***
loadimage(99,"TTTDraw.png")
rem *** display message ***
CreateSprite(99,99)
SetSpriteSize(99,60,-1)
SetSpritePositionByOffset(99,50,47)
Sync()
rem *** wait 5 seconds then close game ***
sleep (5000)
endfunction
function ShowWinningLine(line)
rem *** load image used ***
loadimage(99,"TTTLine.png")
rem *** create sprite ***
CreateSprite(99,99)
SetSpriteSize(99,-1,60)
rem *** Place line horizontally ***
if line <=2
SetSpriteAngle(99,90)
SetSpritePositionByOffset(99,49,26+line*20)
endif
rem *** Place line vertically ***
if line >= 3 and line <= 5
SetSpritePosition(99,20+(line-3)*27,16)
endif
rem *** diagonal TL to BR ***
if line = 6
SetSpriteAngle(99,135)
SetSpritePositionByOffset(99,49,47)
endif
rem *** diagonal BL to TR ***
if line = 7
SetSpriteAngle(99,45)
SetSpritePositionByOffset(99,49,47)
endif
Sync()
rem *** wait 5 seconds then close game ***
sleep (5000)
endfunction