Here is a demo plugin that lets you play TicTacToe in a miniwindow. My implementation of the game itself is undoubtedly rather bad (note the CheckWin/CheckRow functions), but the purpose of the plugin is just to showcase the CharacterGrid widget.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
name="TicTacToe"
author="Soludra"
id="03829336f30e9f2dcace86e9"
language="Lua"
purpose="Play a game of Tic Tac Toe!"
date_written="2009-11-22 07:46:00"
requires="4.43"
version="1.0"
/>
<!-- Script -->
<script>
<![CDATA[
CharacterGrid = require("MWidget.CharacterGrid")
-- Defines the default grid font.
TICTACTOE_FONT = "Lucida Console"
players = {
[1] = {
piece = 'X',
color = 0x00FF00,
lastmove = nil,
},
[2] = {
piece = 'O',
color = 0x0000FF,
lastmove = nil,
},
}
current_player = 1
-- cell we're hovering over now
hovered = nil
-- has the game been finished?
done = false
CheckRow = function(cell1, cell2, cell3)
if cell1.char ~= ' ' and
cell1.char == cell2.char and
cell2.char == cell3.char then
cell1.forecolor, cell2.forecolor, cell3.forecolor = 0xFF0000, 0xFF0000, 0xFF0000
return true
else
return false
end
end
CheckWin = function()
done = CheckRow(grid:Cell(1, 1), grid:Cell(1, 2), grid:Cell(1, 3)) or
CheckRow(grid:Cell(2, 1), grid:Cell(2, 2), grid:Cell(2, 3)) or
CheckRow(grid:Cell(3, 1), grid:Cell(3, 2), grid:Cell(3, 3)) or
CheckRow(grid:Cell(1, 1), grid:Cell(2, 1), grid:Cell(3, 1)) or
CheckRow(grid:Cell(1, 2), grid:Cell(2, 2), grid:Cell(3, 2)) or
CheckRow(grid:Cell(1, 3), grid:Cell(2, 3), grid:Cell(3, 3)) or
CheckRow(grid:Cell(1, 1), grid:Cell(2, 2), grid:Cell(3, 3)) or
CheckRow(grid:Cell(3, 1), grid:Cell(2, 2), grid:Cell(1, 3))
return done
end
CellClick = function(flags, id)
if done then
return
end
local cell = grid:HotspotToCell(id)
if cell.char ~= " " then
return
end
local player = players[current_player]
if player.lastmove then
player.lastmove.backcolor = 0xFFFFFF
end
cell.char = player.piece
cell.backcolor = player.color
CheckWin()
player.lastmove = cell
current_player = (current_player%2) + 1
grid:Draw()
end
CellFocus = function(flags, id)
if done then
return
end
local cell = grid:HotspotToCell(id)
if cell.char ~= " " then
return
end
cell.forecolor = players[current_player].color
cell.char = players[current_player].piece
hovered = id
grid:Draw()
end
CellBlur = function(flags, id)
if done then
return
end
if id ~= hovered then
return
end
local cell = grid:HotspotToCell(hovered)
cell.forecolor = 0x000000
cell.char = ' '
hovered = nil
grid:Draw()
end
Reset = function()
current_player = 1
done = false
grid:ResetGrid()
for y = 1, 3 do
for x = 1, 3 do
local cell = grid:Cell(x, y)
cell.forecolor = 0x000000
cell.hotspot = {
mouseup = "CellClick",
mouseover = "CellFocus",
cancelmouseover = "CellBlur",
}
end
end
grid:Draw()
end
OnPluginInstall = function()
grid = CharacterGrid.new(3, 3)
grid:Anchor(12)
grid:Font(TICTACTOE_FONT, 15)
grid.backcolor = 0xFFFFFF
Reset()
grid:Show()
end
OnPluginClose = function()
grid:Destroy()
end
OnPluginEnable = OnPluginInstall
OnPluginDisable = OnPluginClose
]]>
</script>
</muclient>
The grid appears by default in the center of the screen. Hovering over any available space will display the current player's color/piece. It highlights each player's last move, and highlights a winning line in blue (and also locks the grid). |