Tic tac toe (tutorial)

Show off your games, demos and other (playable) creations.
Post Reply
User avatar
RPG
Party member
Posts: 157
Joined: Wed Mar 02, 2011 5:02 am
Location: Russia
Contact:

Tic tac toe (tutorial)

Post by RPG »

I really thought a long time about which game to create as an example for lQuery framework. Gems game is very difficult to understand, so I created a little game - tic tac toe, which takes advantages of lQuery. This game has a smallest size, and I hope it is very easy to understand.

Code: Select all

require('lib.lquery')

E:new(screen):draw(function() --draw field
	for i = 1, 4 do
		G.line(250, 50 + i*100, 550, 50 + i*100)
		G.line(150 + i*100, 150, 150 + i*100, 450)
	end
end)

G.setFont(30) --text ontop
text = E:new(screen):text('', 12, 'center'):size(800,16)

local move = true --current move: true - crossess, false - noughts
local win = false --if someone wins this becomes true
moves = 0 --moves count

local cell_draw = function(s) --cell drawing function
	if s.cell == true then --this is cross
		G.line(s.x + 20, s.y + 20, s.x + 80, s.y + 80)
		G.line(s.x + 80, s.y + 20, s.x + 20, s.y + 80)
	elseif s.cell == false then --this is nought
		G.circle('line', s.x + 50, s.y + 50, 32, 32)
	end
end

local check_win = function() --checks, if someone wins the game
	local a = cells._child
	local winner
	local function b(i) return a[i].cell end
	for i = 1, 3 do --check rows (three in row)
		if b(i) ~= nil and b(i) == b(i+3) and b(i) == b(i+6) then
			winner = b(i)
		elseif b((i-1)*3+1) ~= nil and b((i-1)*3+1) == b((i-1)*3+2) and b((i-1)*3+1) == b((i-1)*3+3) then
			winner = b((i-1)*3+1)
		end
	end
	--check two diagonals
	if b(1) ~= nil and b(1) == b(5) and b(1) == b(9) then
		winner = b(1)
	elseif b(3) ~= nil and b(3) == b(5) and b(3) == b(7) then
		winner = b(3)
	end
	if winner ~= nil then
		text.text = (winner and 'You win!' or 'Computer wins!')
		win = true
	end
	if win == false and moves == 9 then
		win = true
		text.text = 'Draw!'
	end
end

local ai = function() --simple artificial intelligence
	local n = 0
	repeat
		n = math.random(1,9) --just random:)
	until cells._child[n].cell == nil
	cell_click(cells._child[n])
end

cell_click = function(s) --click handler
	if win == false then
		if s.cell ~= true and s.cell ~= false then
			moves = moves + 1
			s.cell = move
			move = not move
			check_win() --checks, if someone wins
			if move == false and win == false then ai() end --run ai
		end
	else --restart game if someone wins and player clicks on the field
		for i = 1, 9 do cells._child[i].cell = nil end
		text.text = ''
		move = true
		win = false
		moves = 0
	end
end

cells = E:new(screen) --create cells
for i = 1, 3 do
	for j = 1, 3 do
		E:new(cells):move(150 + j * 100, 50 + i * 100):size(100, 100)
		:click(cell_click):draw(cell_draw) --click and draw callbacks
	end
end
Attachments
tic tac toe.love
(9.3 KiB) Downloaded 300 times
Last edited by RPG on Thu Aug 11, 2011 4:48 am, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Tic tac toe (tutorial)

Post by Robin »

Nice. Too bad it crashes on a draw: the AI keeps looking for an open space but it can't find any, so it gets stuck in an infinite loop.
Help us help you: attach a .love.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Tic tac toe (tutorial)

Post by TechnoCat »

It really really doesn't like cat's games. :neko:
User avatar
RPG
Party member
Posts: 157
Joined: Wed Mar 02, 2011 5:02 am
Location: Russia
Contact:

Re: Tic tac toe (tutorial)

Post by RPG »

Robin wrote:Nice. Too bad it crashes on a draw: the AI keeps looking for an open space but it can't find any, so it gets stuck in an infinite loop.
Fixed. I just forgot about draw situation.
TechnoCat wrote:It really really doesn't like cat's games. :neko:
Cat's game is hunting:-)
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Re: Tic tac toe (tutorial)

Post by LuaWeaver »

Nice, but the AI is a bad player. I thought I'd have a game of tic tac toe that was a bit challenging... :monocle:
"your actions cause me to infer your ego is the size of three houses" -finley
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Tic tac toe (tutorial)

Post by Jasoco »

I remember when I made Tic-Tac-Toe in Visual Basic way back in the 90's. I impressed myself. I gave it an AI so smart it had 3 difficulty levels. A dumb Easy mode where it just placed its circle randomly, a smart Hard mode where it actually checked for partial matches against each square to find the best way to either block you or win, and an in-between Normal mode where it randomly chose between dumb and smart. It even had a 2-player mode. Man, I'd love to still have that project. So long ago. So many computers ago.
User avatar
RPG
Party member
Posts: 157
Joined: Wed Mar 02, 2011 5:02 am
Location: Russia
Contact:

Re: Tic tac toe (tutorial)

Post by RPG »

LuaWeaver wrote:Nice, but the AI is a bad player. I thought I'd have a game of tic tac toe that was a bit challenging...
In this demo AI just places noughts randomly.
Jasoco wrote:I remember when I made Tic-Tac-Toe in Visual Basic way back in the 90's. I impressed myself. I gave it an AI so smart it had 3 difficulty levels. A dumb Easy mode where it just placed its circle randomly, a smart Hard mode where it actually checked for partial matches against each square to find the best way to either block you or win, and an in-between Normal mode where it randomly chose between dumb and smart. It even had a 2-player mode. Man, I'd love to still have that project. So long ago. So many computers ago.
This game is not meant to be a full game, but I hope it is a good example for beginners.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Tic tac toe (tutorial)

Post by Jasoco »

RPG wrote:
Jasoco wrote:I remember when I made Tic-Tac-Toe in Visual Basic way back in the 90's. I impressed myself. I gave it an AI so smart it had 3 difficulty levels. A dumb Easy mode where it just placed its circle randomly, a smart Hard mode where it actually checked for partial matches against each square to find the best way to either block you or win, and an in-between Normal mode where it randomly chose between dumb and smart. It even had a 2-player mode. Man, I'd love to still have that project. So long ago. So many computers ago.
This game is not meant to be a full game, but I hope it is a good example for beginners.
Hey, I was just beginning back then too. I was impressed with my work. It was the only project I ever finished to a polish. I never released it since it was 1998 and it was dial-up and just downloading the VB Runtime DLL's themselves took a half an hour or more.
Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests