As usual, the video:
http://www.youtube.com/watch?v=0CNr76880Ic
And the source code:
2 players game, without AI right now.
Enjoy
As usual, the video:
http://www.youtube.com/watch?v=0CNr76880Ic
And the source code:
2 players game, without AI right now.
Enjoy
Thanks! Itβs wonderful!
Hi juaxix,
nice game, it showed me that I shamefully neglected proper ellipse rendering in loveCodea.
You made me find a bug in my code, now I show you one in yours.
Construct a pyramid like this with (r)ed and (y)ellow:
y
ry
rry
rrry
The winning state for yellow will not be detected. I found your checking algorithm quite difficult and took the opportunity to dumb it down. See here:
-- Returns the owner (player number 1 or 2) of a token.
-- Returns number 0 for empty tokens or out of board locations.
--
-- x : Column of piece (1...columns)
-- y : Row of piece (1...rows)
function Board:getTokenOwner(x, y)
if x < 1 or x > self.columns or y < 1 or y > self.rows then return 0 end
if self.tokens[x][y].state == TOKEN_EMPTY then return 0 end
return self.tokens[x][y].player
end
-- Scans the board and returns if there is a winner.
-- Player number of winner is stored in self.winner.
-- Matching token are set to SOLVED state.
function Board:checkWinState()
for y = 1, self.rows do
for x = 1, self.columns do
local owner = self:getTokenOwner(x, y)
if owner ~= 0 then
-- Match counters for 4 possible directions:
-- (h)orizontal, (v)ertical, (d)iagonal in 2 ways.
-- At the moment they all match the owner, therefore 1.
local h_match = 1
local v_match = 1
local d1_match = 1
local d2_match = 1
-- Check 3 more tokens in 4 directions
for i = 1, 3 do
if self:getTokenOwner(x + i, y) == owner then
h_match = h_match + 1
end
if self:getTokenOwner(x, y + i) == owner then
v_match = v_match + 1
end
if self:getTokenOwner(x + i, y + i) == owner then
d1_match = d1_match + 1
end
if self:getTokenOwner(x + i, y - i) == owner then
d2_match = d2_match + 1
end
end
-- Highlight tokens when there are 4 in a line
if h_match == 4 then
self.winner = owner
for i = 0, 3 do
self.tokens[x + i][y].state = TOKEN_SOLVED
end
end
if v_match == 4 then
self.winner = owner
for i = 0, 3 do
self.tokens[x][y + i].state = TOKEN_SOLVED
end
end
if d1_match == 4 then
self.winner = owner
for i = 0, 3 do
self.tokens[x + i][y + i].state = TOKEN_SOLVED
end
end
if d2_match == 4 then
self.winner = owner
for i = 0, 3 do
self.tokens[x + i][y - i].state = TOKEN_SOLVED
end
end
end
end
end
return self.winner ~= 0
end
Advantages:
Many readers may be able to understand it, maybe even programming novices.
It works, probably because of point 1.
It detects multiple matches.