forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

My first game. Tic Tac Toe in lua

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
Xs



Joined: 02 Sep 2005
Posts: 4

PostPosted: Fri Sep 02, 2005 6:10 am    Post subject: My first game. Tic Tac Toe in lua Reply with quote

First i woudl like to thank everyone developing the lua player. your doing a GREAT job!

It would be great if people can test it out and give me some feedback. its a 2 player game at the moment, AI will be included later.

Controls:
Cross = select square
Triangle = reset game
Start = exit game

All the best,
Xs.

-----------------------------------------------------------------------------

- BEGIN CODE -

-- global variables

-- position on board
local xPos = 1
local yPos = 1
-- position on board

-- which players turn
local playerTurn = 1
-- which players turn

-- controls read
local pad
local oldPad
-- controls read

-- current board
local board
-- current board

-- pixel x,y positions
local xPosPrint
local yPosPrint
-- pixel x,y positions

-- player won variable
local tempwin
local won
local XorO
-- player won variable

-- game draw variable
local isDraw
-- game draw variable

-- colors
local RED = Color.new(255, 0, 0)
local GREEN = Color.new(0, 255, 0)
local BLUE = Color.new(0, 0, 255)
local BLACK = Color.new(0,0,0)
local WHITE = Color.new(255,255,255)
-- colors

-- game dimensions
local SQUARE_SIZE = 80
local BOARD_SIZE = 240
local BOARD_TOP = 15
local BOARD_LEFT = 15
-- game dimensions

-- end global

function resetBoard()
isDraw = nil
xPos = 1
yPos = 1
playerTurn = 1
won = nil
XorO = nil
tempWon = nil
board = { {"-","-","-"},
{"-","-","-"},
{"-","-","-"} }
end

function resetGame()
resetBoard()
end

function checkDraw()
for xIndex, line in board do
for yIndex, key in line do
if key == "-" then
isDraw = nil
return isDraw
end
end
end
isDraw = true
return isDraw
end

function drawRect(left, top, width, height, color)
screen:drawLine(left, top, left+width, top, color)
screen:drawLine(left, top, left, top+height, color)
screen:drawLine(left+width, top, left+width, top+height, color)
screen:drawLine(left+width, top+height, left, top+height, color)
end

function drawBoard()
drawRect(BOARD_LEFT, BOARD_TOP, BOARD_SIZE, BOARD_SIZE, RED)

local incY = BOARD_TOP
local incX = BOARD_LEFT
for i = 1, 3 do
for j = 1, 3 do
drawRect(incX, incY, SQUARE_SIZE, SQUARE_SIZE, RED)
incX = incX + SQUARE_SIZE
end
incY = incY + SQUARE_SIZE
incX = BOARD_LEFT
end
end

function clearScreen()
screen:clear()
end

function flip()
screen.waitVblankStart()
screen.flip()
end

function activateUSB(state)
if not state then
System.usbDiskModeDeactivate()
return
end
System.usbDiskModeActivate()
end

function fillRect(left, top, width, height, color)
screen:fillRect(left, top, width, height, color)
end

function drawPicked()
for xIndex, line in board do
for yIndex, key in line do
if key ~= "-" then
x = xIndex * SQUARE_SIZE + BOARD_LEFT - SQUARE_SIZE
y = yIndex * SQUARE_SIZE + BOARD_LEFT - SQUARE_SIZE
--debug("table X=" .. xIndex .. "-Y=" .. yIndex)
printToScreen(x + 37, y + 37, key, GREEN)
end
end
end
end

function selectSquare()
if not isDraw then
if won then
printToScreen(300, 120, "Player " .. playerTurn .. " Wins!", RED)
printToScreen(300, 140, "Press triangle to", WHITE)
printToScreen(300, 150, "restart!", WHITE)
end
else
printToScreen(300, 140, "Draw game, press ", WHITE)
printToScreen(300, 150, "triangle to restart!", WHITE)
end
for i = 1, 3 do
for j = 1, 3 do
if xPos == i and yPos == j then
xPosPrint = i * SQUARE_SIZE + BOARD_LEFT - SQUARE_SIZE
yPosPrint = j * SQUARE_SIZE + BOARD_TOP - SQUARE_SIZE
fillRect(xPosPrint + 1, yPosPrint + 1, SQUARE_SIZE - 2, SQUARE_SIZE - 2, BLUE)

if playerTurn == 2 then
printToScreen(300, BOARD_TOP, "Player 2's turn", GREEN)
elseif playerTurn == 1 then
printToScreen(300, BOARD_TOP, "Player 1's turn", GREEN)
end

drawPicked()
end
printTable()
end
end
end

function quitGame()
if not pad:start() then
return nil
end
return true
end

function debug(message)
--clearScreen()
screen:print(300, 30, message, GREEN)
end

function doPadUP()
--debug("Pad UP")
yPos = yPos - 1
if yPos == 0 then
yPos = 3
end
end

function doPadDOWN()
--debug("Pad DOWN")
yPos = yPos + 1
if yPos == 4 then
yPos = 1
end
end

function doPadRIGHT()
--debug("Pad RIGHT")
xPos = xPos + 1
if xPos == 4 then
xPos = 1
end
end

function doPadLEFT()
--debug("Pad LEFT")
xPos = xPos - 1
if xPos == 0 then
xPos = 3
end
end

function printToScreen(x, y, message, color)
screen:print(x, y, message, color)
end

function printTable()
--local tableX = 300
--local tableY = 60

--for xIndex,line in board do
-- for yIndex, key in line do
-- printToScreen(tableX, tableY, key, WHITE)
--
-- tableX = tableX + 30
-- end
-- tableY = tableY + 30
-- tableX = 300
--end
end


function checkOccupied(x, y)
if board[x][y] ~= "-" then
return nil
end
return true
end

function changePlayerTurn()
if checkOccupied(xPos, yPos) then
if playerTurn == 2 then
-- WORK OUT HOW TO CONSTRUCT A 2D ARRAY
-- THIS METHOD AND DRAWPICKED METHOD
board[xPos][yPos] = "O"
printToScreen(xPosPrint + 37, yPosPrint + 37, "O", GREEN)

if hasWon("O") then
-- display won

printToScreen(300, 120, "Player " .. playerTurn .. " Wins!", RED)
printToScreen(300, 140, "Press triangle to", WHITE)
printToScreen(300, 150, "restart!", WHITE)
else
if checkDraw() then
printToScreen(300, 140, "Draw game, press ", WHITE)
printToScreen(300, 150, "triangle to restart!", WHITE)
else
playerTurn = playerTurn - 1
end
end
elseif playerTurn == 1 then
board[xPos][yPos] = "X"
printToScreen(xPosPrint + 37, yPosPrint + 37, "X", GREEN)

if hasWon("X") then
-- display won
printToScreen(300, 120, "Player " .. playerTurn .. " Wins!", RED)
printToScreen(300, 140, "Press triangle to", WHITE)
printToScreen(300, 150, "restart!", WHITE)
else
if checkDraw() then
printToScreen(300, 140, "Draw game, press ", WHITE)
printToScreen(300, 150, "triangle to restart!", WHITE)
else
playerTurn = playerTurn + 1
end
end
end
else
printToScreen(300, 250, "Position occupied", RED)
end
end

function hasWon(symbol)
tempwin = true
XorO = symbol

-- check current row
for col = 1, 3 do
if not (board[xPos][col] == symbol) then
tempwin = nil
end
end

-- check current column
if not tempwin then
tempwin = true
for row = 1, 3 do
if not (board[row][yPos] == symbol) then
tempwin = nil
end
end
end

-- check left diagonal
if not tempwin then
tempwin = true
for i = 1, 3 do
if not (board[i][i] == symbol) then
tempwin = nil
end
end
end

-- check right diagonal
if not tempwin then
tempwin = true --true
for i = 1, 3 do
-- DOES NOT WORK!?!
if not (board[4 - i][i] == symbol) then
tempwin = nil
end
end
end

won = tempwin
return won
end

function doPadCROSS()
changePlayerTurn()
end

function doPadTRIANGLE()
resetGame()
end

function startGameLoop()
while true do
pad = Controls.read()
if pad ~= oldPad then
clearScreen()

oldPad = pad

drawBoard()
selectSquare()

if quitGame() then
break
end

-- or not isDraw

if not won and not isDraw then
if pad:cross() then
doPadCROSS()
elseif pad:up() then
doPadUP()
elseif pad:down() then
doPadDOWN()
elseif pad:left() then
doPadLEFT()
elseif pad:right() then
doPadRIGHT()
end
end
if pad:triangle() then
doPadTRIANGLE()
end

flip()
end
end
end

function main()
activateUSB(true)
resetBoard()
oldPad = Controls.read()
clearScreen()
drawBoard()
flip()
startGameLoop()
end

-- Start program execution at main method
main()

- END CODE -


Last edited by Xs on Sat Sep 03, 2005 1:13 am; edited 1 time in total
Back to top
View user's profile Send private message
Xs



Joined: 02 Sep 2005
Posts: 4

PostPosted: Fri Sep 02, 2005 11:01 am    Post subject: Reply with quote

just noticed all the formatting was stripped from the code when i pasted it above so it kinda looks a mess. if anyone wants the formatted version of the source then ill emial you the file instead.

Best,
Xs.
Back to top
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Fri Sep 02, 2005 2:02 pm    Post subject: Reply with quote

You can use BBCode to display programs without any formatting by pressing "Code" button. When done with the program, press the "Code" again to close. You could re-edit your first post above.
_________________
Geo Massar
Retired Engineer
Back to top
View user's profile Send private message Send e-mail
MikeHaggar



Joined: 18 Jul 2005
Posts: 116

PostPosted: Sat Sep 03, 2005 12:42 am    Post subject: Reply with quote

Fix so you don't have to press a button to get it to display stuff when starting the first time.

Just replace the main function with this one:

Code:

function main()
 activateUSB(true)
 resetBoard()
 oldPad = Controls.read()
 clearScreen()
 drawBoard()
 selectSquare()
 flip()
 startGameLoop()
end


Last edited by MikeHaggar on Sat Sep 03, 2005 12:58 am; edited 1 time in total
Back to top
View user's profile Send private message
Wraggster



Joined: 26 Aug 2005
Posts: 121

PostPosted: Sat Sep 03, 2005 12:55 am    Post subject: Reply with quote

and the fixed version is here http://psp-news.dcemu.co.uk/tictactoe.shtml
_________________
Webmaster of http://www.dcemu.co.uk

DCEMU The Worlds Only Homebrew & Gaming Network of Sites.
Back to top
View user's profile Send private message Visit poster's website
Xs



Joined: 02 Sep 2005
Posts: 4

PostPosted: Sat Sep 03, 2005 1:09 am    Post subject: Reply with quote

thanks very much guys. really apriciate the advice. thanks for the mirror wraggster.


All the best,
Xs.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group