TicTacToe.xml
snippets · 3.8 KB · 2010-03-30
Tic Tac Toe plugin for MUSHclient (Lua) by Soludra: play against another player.
Download « Back to the snippets list
<?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"
/>
<!-- Triggers -->
<triggers>
</triggers>
<!-- Aliases -->
<aliases>
<alias
match="^ttt=(.*)$"
enabled="y"
regexp="y"
send_to="12"
ignore_case="y"
sequence="100"
>
<send>print(%1)</send>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
require('tprint')
local CharacterGrid = require("MWidget.CharacterGrid")
-- Defines the default grid font.
local TICTACTOE_FONT = "Lucida Console"
local players = {
[1] = {
piece = 'X',
color = 0x00FF00,
lastmove = nil,
},
[2] = {
piece = 'O',
color = 0x0000FF,
lastmove = nil,
},
}
local current_player = 1
-- cell we're hovering over now
local hovered = nil
local 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
local CheckWin = function()
local 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))
if done then
for y = 1,3 do
for x = 1,3 do
local cell = grid:Cell(x, y)
cell.hotspot.mouseup = nil
cell.hotspot.mouseover = nil
cell.hotspot.cancelmouseover = nil
end
end
end
return done
end
local CellClick = function(win, flags, id)
local cell = win: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
local CellFocus = function(win, flags, id)
local cell = win: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
local CellBlur = function(win, flags, id)
if id ~= hovered then
return
end
local cell = win:HotspotToCell(hovered)
cell.forecolor = 0x000000
cell.char = ' '
hovered = nil
grid:Draw()
end
local Reset = function()
current_player = 1
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
cell.hotspot.mouseover = CellFocus
cell.hotspot.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
DoReset = Reset
]]>
</script>
</muclient>