Skip to content

Instantly share code, notes, and snippets.

@ZornTaov
Created January 24, 2014 04:14
Show Gist options
  • Save ZornTaov/8591998 to your computer and use it in GitHub Desktop.
Save ZornTaov/8591998 to your computer and use it in GitHub Desktop.
Bingo game for TinyIrc's TinyBot
math.randomseed(os.time())
local gameplaying = false
local debugbingo = false
function round(val, decimal)
local exp = decimal and 10^decimal or 1
return math.ceil(val * exp - 0.5) / exp
end
function shuffle(t)
assert(t, "table.shuffle() expected a table, got nil")
local iterations = table.getn(t)
local j
for i = iterations, 2, -1 do
j = math.random(i)
t[i], t[j] = t[j], t[i]
end
end
local function findMe(sometable, searchMe)
local found = false
for i = 1,table.getn(sometable) do
if sometable[i] == searchMe then
found = true
break
end
end
return found -- (true/false)
end
local bingoSheet = {}
local called = {}
local a = {"B", "I", "N", "G", "O"}
local players = {}
function fillSheet()
print("________________________")
for i=1, 75 do
bingoSheet[i] = a[round(i/15+.5)] .. i
end
shuffle(bingoSheet)
print(table.concat( bingoSheet, ", " ))
if debugbingo then
for i=1, 73 do
print(bingoSheet[1])
table.remove(bingoSheet, 1)
end
end
end
local cardTable = {}
for i=1,5 do
cardTable[i] = {}
for j=1,15 do
cardTable[i][j] = j + 15*(i-1)
end
end
function addPlayer(nick)
mt = {}
cardTableDummy = cardTable
for i=1,5 do
mt[i] = {}
shuffle(cardTableDummy[i])
for j=1,5 do
mt[i][j] = a[i] .. cardTableDummy[i][1]
table.remove(cardTableDummy[i], 1)
end
end
mt[3][3] = "free"
players[nick] = mt
for i=1,5 do
print(table.concat(players[nick][i], ", ") .. "\n")
end
end
function getPlayers()
return players
end
function getNumPlayers()
return table.getn(players)
end
function checkcard(nick)
win = false
print(table.concat(called, ", "))
--check rows
for i=1,5 do
ding = 0
for j=1,5 do
if findMe(called, players[nick][i][j]) or players[nick][i][j] == "free" then
ding = ding + 1
end
end
if ding == 5 then
win = true
break
end
end
--check columns
for i=1,5 do
if win then break end
ding = 0
for j=1,5 do
if findMe(called, players[nick][j][i]) or players[nick][j][i] == "free" then
ding = ding + 1
end
end
if ding == 5 then
win = true
break
end
end
--check cross
if win then return win end
ding = 0
for i=1,5 do
if findMe(called, players[nick][i][i]) or players[nick][i][i] == "free" then
ding = ding + 1
end
end
if ding == 5 then
win = true
end
if win then return win end
ding = 0
for i=1,5 do
if findMe(called, players[nick][i][6-i]) or players[nick][i][6-i] == "free" then
ding = ding + 1
end
end
if ding == 5 then
win = true
end
return win
end
function showcard(nick)
local card = players[nick]
for i=1,5 do
for j=1,5 do
if findMe(called, card[i][j]) or card[i][j] == "free" then
card[i][j] = "(" .. card[i][j] .. ")"
end
end
end
return card
end
function draw()
if table.getn(bingoSheet)<1 then
print("game over!")
return false
end
print(bingoSheet[1])
table.insert(called, bingoSheet[1])
--print(table.concat(called, ", ") .. " called table")
call = bingoSheet[1]
table.remove(bingoSheet, 1)
return call
end
function endGame()
gameplaying = false
bingoSheet = {}
called = {}
players = {}
end
----ADD THIS TO THE END OF CommandTab.lua
require('TinyBot/Bingo')
function onBingoStart(server, to, cmd, argv, nuh)
if string.sub(to, 1, 1) == "#" then
if not gameplaying then
gameplaying = true
fillSheet()
bot_sendreply(server, to, nuh.nick, "A game has started, use !bingo_join to join!")
else
bot_sendreply(server, to, nuh.nick, "in a game!")
end
end
end
function onBingoJoin(server, to, cmd, argv, nuh)
if string.sub(to, 1, 1) == "#" then
if gameplaying then
addPlayer(nuh.nick)
bot_sendreply(server, to, nuh.nick, nuh.nick .. " has joined the game!")
player = getPlayers()[nuh.nick]
for i=1,5 do
bot_sendreply(server, nuh.nick, nuh.nick, table.concat(player[i], " "))
end
else
bot_sendreply(server, to, nuh.nick, "No game has started! Use !bingo_start then !bingo_join")
end
end
end
function onBingoDraw(server, to, cmd, argv, nuh)
if string.sub(to, 1, 1) == "#" then
if gameplaying then
call = draw()
if call ~= false then
bot_sendreply(server, to, nuh.nick, call .. "!")
end
else
bot_sendreply(server, to, nuh.nick, "No game has started! Use !bingo_start then !bingo_join")
end
end
end
function onBingo(server, to, cmd, argv, nuh)
if string.sub(to, 1, 1) == "#" then
if gameplaying then
if checkcard(nuh.nick) then
bot_sendreply(server, to, nuh.nick, nuh.nick .. " is a WINNER!")
endGame()
gameplaying = false
else
bot_sendreply(server, to, nuh.nick, nuh.nick .. " is not a winner, keep playing!")
end
else
bot_sendreply(server, to, nuh.nick, "No game has started! Use !bingo_start then !bingo_join")
end
end
end
function onBingoCheck(server, to, cmd, argv, nuh)
if string.sub(to, 1, 1) == "#" then
if gameplaying then
bot_sendreply(server, nuh.nick, nuh.nick, "---------------")
card = showcard(nuh.nick)
for i=1,5 do
bot_sendreply(server, nuh.nick, nuh.nick, table.concat(card[i], " "))
end
else
bot_sendreply(server, to, nuh.nick, "No game has started! Use !bingo_start then !bingo_join")
end
end
end
bot_addcommand("bingo_start", 5, "Start a Bingo game!", nil, nil, onBingoStart)
bot_addcommand("bingo_join", 5, "Join a Bingo game!", nil, nil, onBingoJoin)
bot_addcommand("draw", 5, "Draw!", nil, nil, onBingoDraw)
bot_addcommand("bingo", 5, "Bingo!", nil, nil, onBingo)
bot_addcommand("check", 5, "check your card!", nil, nil, onBingoCheck)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment