PtokaX forum

Archive => Archived 4.0 boards => Finished Lua 4 scripts => Topic started by: c h i l l a on 29 January, 2004, 13:12:37

Title: Tic Tac Toe
Post by: c h i l l a on 29 January, 2004, 13:12:37
Was fun coding, but still got stuff to optimise, for things I have no idea right now.

take one

--Tic Tac Toe V.01 by chill
--Best viewed with Ms Sans Serif, Size 9

tbot = "-TicTacToe-"

TPlayers = {}
taccept = {}
tturn = {}

function Main()
frmHub:RegBot(tbot)
SetTimer(10*1000)
end

function OnTimer()
for i,v in taccept do
taccept[i][1] = taccept[i][1] + 1
if taccept[i][1] >= 6 then
taccept[i] = nil
end
end
end


function DataArrival(curUser,data)
if strsub(data,1,4) == "$To:" then
if strsub(data,6,6+(strlen(tbot)-1)) == tbot then
pmmsg = strsub(data,(18+strlen(tbot)+2*strlen(curUser.sName)),(strlen(data)-1))
local _,_,word1 = strfind(pmmsg,"^(%S+)")
if TFUNCS[strlower(word1)] then
TFUNCS[strlower(word1)](curUser,pmmsg)
elseif TPlayers[strlower(curUser.sName)] then
Play3T(curUser,word1)
end


end
end
end

-------------------------------------------------------------------------
-- GAME FUNCTIONS
------------------------------------------------------------------------

function GetCurGame(A1,A2,A3,B1,B2,B3,C1,C2,C3)

local TicTacToeGame = "\r\n"..
"\t\t        1              2                  3\r\n\r\n"..
"\t\t                l                 l\r\n"..
"\tA\t        "..A1.."              "..A2.."               "..A3.."\r\n"..
"\t\t_______l_______l_______\r\n"..
"\t\t                l                 l\r\n"..
"\tB\t        "..B1.."              "..B2.."               "..B3.."\r\n"..
"\t\t_______l_______l_______\r\n"..
"\t\t                l                 l\r\n"..
"\tC\t        "..C1.."              "..C2.."               "..C3.."\r\n"..
"\t\t                l                 l\r\n"

return(TicTacToeGame)

end

function CreatePlayer(Player1,Player2)
TPlayers[Player1] = {
player = Player2,
game = {
A1 = { " ",0 },
A2 = { " ",0 },
A3 = { " ",0 },
B1 = { " ",0 },
B2 = { " ",0 },
B3 = { " ",0 },
C1 = { " ",0 },
C2 = { " ",0 },
C3 = { " ",0 },
},
}
end

function doCheckIfPlay(curGame)
if curGame.A1[2] == 1 and curGame.A2[2] == 1 and curGame.A3[2] == 1 and curGame.B1[2] == 1 and curGame.B2[2] == 1 and curGame.B3[2] == 1 and curGame.C1[2] == 1 and curGame.C2[2] == 1 and curGame.C3[2] == 1 then
return("STOP")
end
end

function doCheckForWin(curGame)
if curGame.A1[1] ~= " " and curGame.A1[1] == curGame.A2[1] and curGame.A1[1] == curGame.A3[1] then
return("WON")
elseif curGame.B1[1] ~= " " and curGame.B1[1] == curGame.B2[1] and curGame.B1[1] == curGame.B3[1] then
return("WON")
elseif curGame.C1[1] ~= " " and curGame.C1[1] == curGame.C2[1] and curGame.C1[1] == curGame.C3[1] then
return("WON")
elseif curGame.A1[1] ~= " " and curGame.A1[1] == curGame.B1[1] and curGame.A1[1] == curGame.C1[1] then
return("WON")
elseif curGame.A2[1] ~= " " and curGame.A2[1] == curGame.B2[1] and curGame.A2[1] == curGame.C2[1] then
return("WON")
elseif curGame.A3[1] ~= " " and curGame.A3[1] == curGame.B3[1] and curGame.A3[1]  == curGame.C3[1] then
return("WON")
elseif curGame.A1[1] ~= " " and curGame.A1[1] == curGame.B2[1] and curGame.A1[1] == curGame.C3[1] then
return("WON")
elseif curGame.A3[1] ~= " " and curGame.A3[1] == curGame.B2[1] and curGame.A3[1] == curGame.C1[1] then
return("WON")
end
end

function Play3T(curUser,word1)
if tturn[strlower(curUser.sName)] then
if TPlayers[strlower(curUser.sName)].game[word1][2] == 0 then
TPlayers[strlower(curUser.sName)].game[word1] = { tturn[strlower(curUser.sName)][2],1 }
if tturn[strlower(curUser.sName)][2] == "X" then
tturn[tturn[strlower(curUser.sName)][1]] = { strlower(curUser.sName),"O" }
else
tturn[tturn[strlower(curUser.sName)][1]] = { strlower(curUser.sName),"X" }
end
tturn[strlower(curUser.sName)] = nil
local lnick = strlower(curUser.sName)
curUser:SendPM(tbot,GetCurGame(TPlayers[lnick].game.A1[1],TPlayers[lnick].game.A2[1],TPlayers[lnick].game.A3[1],TPlayers[lnick].game.B1[1],TPlayers[lnick].game.B2[1],TPlayers[lnick].game.B3[1],TPlayers[lnick].game.C1[1],TPlayers[lnick].game.C2[1],TPlayers[lnick].game.C3[1]))
TPlayers[TPlayers[strlower(curUser.sName)].player].game = TPlayers[strlower(curUser.sName)].game
local curUser2 = GetItemByName(TPlayers[strlower(curUser.sName)].player)
local lnick = strlower(curUser2.sName)
curUser2:SendPM(tbot,curUser.sName.." Played, now its your turn."..GetCurGame(TPlayers[lnick].game.A1[1],TPlayers[lnick].game.A2[1],TPlayers[lnick].game.A3[1],TPlayers[lnick].game.B1[1],TPlayers[lnick].game.B2[1],TPlayers[lnick].game.B3[1],TPlayers[lnick].game.C1[1],TPlayers[lnick].game.C2[1],TPlayers[lnick].game.C3[1]))

if doCheckForWin(TPlayers[strlower(curUser.sName)].game) == "WON" then
curUser:SendPM(tbot,"******  YOU HAVE WON ********")
curUser2:SendPM(tbot,"****** YOU HAVE LOST ********")
TPlayers[strlower(curUser.sName)] = nil
TPlayers[strlower(curUser2.sName)] = nil
tturn[strlower(curUser2.sName)] = nil
elseif doCheckIfPlay(TPlayers[strlower(curUser.sName)].game) == "STOP" then
curUser:SendPM(tbot,"******  GAME ENDED EVEN ********")
curUser2:SendPM(tbot,"******  GAME ENDED EVEN ********")
TPlayers[strlower(curUser.sName)] = nil
TPlayers[strlower(curUser2.sName)] = nil
tturn[strlower(curUser2.sName)] = nil
end
end
end
end

TFUNCS = {
["invite"] = function(curUser,sdata)
local _,_,invited = strfind(sdata,"^%S+%s+(%S+)")
if invited and (not TPlayers[strlower(curUser.sName)]) and GetItemByName(invited) and (not TPlayers[strlower(invited)]) then
taccept[strlower(invited)] = { 0,curUser.sName }
curUser:SendPM(tbot,"Sending invitation to "..invited..".")
GetItemByName(invited):SendPM(tbot,curUser.sName.." invites you to a session of Tic,Tac Toe, reply: Accept or Deny.")

else
curUser:SendPM(tbot,"Either you already got a game going on or the user you wanted to invite is not online.")
end
end,
["quit"] = function(curUser,sdata)
if TPlayers[strlower(curUser.sName)] then
curUser:SendPM(tbot,"Game has been quit.")
if GetItemByName(TPlayers[strlower(curUser.sName)].player) then
GetItemByName(TPlayers[strlower(curUser.sName)].player):SendPM(tbot,curUser.sName.." has left the game.")
end
TPlayers[TPlayers[strlower(curUser.sName)].player] = nil
TPlayers[strlower(curUser.sName)] = nil
end
end,
["accept"] = function(curUser,sdata)
if taccept[strlower(curUser.sName)] and GetItemByName(taccept[strlower(curUser.sName)][2]) then
local curUser2 = GetItemByName(taccept[strlower(curUser.sName)][2])
taccept[strlower(curUser.sName)] = nil
CreatePlayer(strlower(curUser.sName),strlower(curUser2.sName))
CreatePlayer(strlower(curUser2.sName),strlower(curUser.sName))
curUser2:SendPM(tbot,curUser.sName.." has accepted your request.")
local lnick = strlower(curUser2.sName)
curUser2:SendPM(tbot,GetCurGame(TPlayers[lnick].game.A1[1],TPlayers[lnick].game.A2[1],TPlayers[lnick].game.A3[1],TPlayers[lnick].game.B1[1],TPlayers[lnick].game.B2[1],TPlayers[lnick].game.B3[1],TPlayers[lnick].game.C1[1],TPlayers[lnick].game.C2[1],TPlayers[lnick].game.C3[1]))
curUser:SendPM(tbot,"Waiting for "..curUser2.sName.." to start.")
tturn[strlower(curUser2.sName)] = { strlower(curUser.sName),"X" }
else
curUser:SendPM(tbot,"Other player went offline.")
taccept[strlower(curUser.sName)] = nil
end
end,
["deny"] = function(curUser,sdata)
if taccept[strlower(curUser.sName)] then
curUser:SendPM(tbot,"Request was denied.")
if GetItemByName(taccept[strlower(curUser.sName)][2]) then
GetItemByName(taccept[strlower(curUser.sName)][2]):SendPM(tbot,"Request was denied.")
end
taccept[strlower(curUser.sName)] = nil
end
end,
}
Title:
Post by: pHaTTy on 29 January, 2004, 13:15:51
nice one, eheh thats good idea, good luck adding more, will enjoy sitting back and enjoying some other scripts for a change now eheh
Title:
Post by: c h i l l a on 29 January, 2004, 13:33:33
thanks...

I hope I find better ways to, handle the Players and tables...  right now its quite wasty, and a better way of checking if one has won.
Title:
Post by: pHaTTy on 29 January, 2004, 14:17:03
for how bi*chy lua is, i think you've done a great job on keeping users status and the code looks clean, hope u find better ways, but looks fine to me :))
Title:
Post by: c h i l l a on 29 January, 2004, 19:54:10
Why is LUA so bitchy, with what can this be done a lot easier ?

Cause I am already thinking of  the Game 4 wins :))
( Dunno in German it 4 Gewinnt) no idea what it is in English.

here is take 2.

--Tic Tac Toe V.02 by chill
--Best viewed with Ms Sans Serif, Size 9

tbot = "-TicTacToe-"

-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-- ************* MAIN SCRIPT *************
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
TPlayers = {}
curGames = {}
tInvite = {}

function Main()
frmHub:RegBot(tbot)
SetTimer(10*1000)
end

function OnTimer()
foreach(tInvite, function(i,_)
tInvite[i][1] = tInvite[i][1] + 1
if tInvite[i][1] >= 6 then
tInvite[i] = nil
end
end)
end

function DataArrival(curUser,data)
if strsub(data,1,4) == "$To:" then
if strsub(data,6,5+(strlen(tbot))) == tbot then
pmmsg = strsub(data,(18+strlen(tbot)+2*strlen(curUser.sName)),(strlen(data)-1))
local _,_,word1 = strfind(pmmsg,"^(%S+)")
if TFUNCS[strlower(word1)] then
TFUNCS[strlower(word1)](curUser,pmmsg)
elseif (TPlayers[strlower(curUser.sName)]) then
Play3T(curUser,word1)
end
end
end
end
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
-- ************* GAME FUNCTIONS *************
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------
-- SHOW CURRENT GAME
-------------------------------------------------------------------------
function GetCurGame(A1,A2,A3,B1,B2,B3,C1,C2,C3)

local TicTacToeGame = "\r\n\r\n"..
"\t\t        1              2                  3\r\n"..
"\r\n"..
"\tA\t        "..A1.."       l       "..A2.."        l       "..A3.."\r\n"..
"\t\t_______________________\r\n"..
"\r\n"..
"\tB\t        "..B1.."       l       "..B2.."        l       "..B3.."\r\n"..
"\t\t_______________________\r\n"..
"\r\n"..
"\tC\t        "..C1.."       l       "..C2.."        l       "..C3.."\r\n"..
"\r\n"

return(TicTacToeGame)

end

-------------------------------------------------------------------------
-- CREATE A GAME
-------------------------------------------------------------------------
function CreateGame(curUser1,curUser2)
curGames[strlower(curUser1.sName).."$"..strlower(curUser2.sName)] = {
toPlay = { curUser2.sName,"X" },
game = {
A1 = { "  ",0 },
A2 = { "  ",0 },
A3 = { "  ",0 },
B1 = { "  ",0 },
B2 = { "  ",0 },
B3 = { "  ",0 },
C1 = { "  ",0 },
C2 = { "  ",0 },
C3 = { "  ",0 },
},
}
TPlayers[strlower(curUser1.sName)] = {
PlaysWith = curUser2,
curGame = strlower(curUser1.sName).."$"..strlower(curUser2.sName),
}
TPlayers[strlower(curUser2.sName)] = {
PlaysWith = curUser1,
curGame = strlower(curUser1.sName).."$"..strlower(curUser2.sName),
}
end

-------------------------------------------------------------------------
-- DELETE A GAME
-------------------------------------------------------------------------
function DelGame(curUser1,curUser2)
curGames[TPlayers[strlower(curUser1.sName)].curGame] = nil
TPlayers[strlower(curUser1.sName)] = nil
TPlayers[strlower(curUser2.sName)] = nil
end

-------------------------------------------------------------------------
-- CHECK IF GAME HAS ENDED
-------------------------------------------------------------------------
function doCheckIfPlay(curGame)
count = 0
foreach(curGame, function(i,_)
count = count + %curGame[i][2]
end)
if count == 9 then
count = nil
return("STOP")
end
count = nil
end

-------------------------------------------------------------------------
-- CHECK IF SOMEONE HAS WON
-------------------------------------------------------------------------
function doCheckForWin(curGame)
if curGame.A1[1] ~= "  " and curGame.A1[1] == curGame.A2[1] and curGame.A1[1] == curGame.A3[1] then
return("WON")
elseif curGame.B1[1] ~= "  " and curGame.B1[1] == curGame.B2[1] and curGame.B1[1] == curGame.B3[1] then
return("WON")
elseif curGame.C1[1] ~= "  " and curGame.C1[1] == curGame.C2[1] and curGame.C1[1] == curGame.C3[1] then
return("WON")
elseif curGame.A1[1] ~= "  " and curGame.A1[1] == curGame.B1[1] and curGame.A1[1] == curGame.C1[1] then
return("WON")
elseif curGame.A2[1] ~= "  " and curGame.A2[1] == curGame.B2[1] and curGame.A2[1] == curGame.C2[1] then
return("WON")
elseif curGame.A3[1] ~= "  " and curGame.A3[1] == curGame.B3[1] and curGame.A3[1]  == curGame.C3[1] then
return("WON")
elseif curGame.A1[1] ~= "  " and curGame.A1[1] == curGame.B2[1] and curGame.A1[1] == curGame.C3[1] then
return("WON")
elseif curGame.A3[1] ~= "  " and curGame.A3[1] == curGame.B2[1] and curGame.A3[1] == curGame.C1[1] then
return("WON")
end
end

-------------------------------------------------------------------------
-- PLAY ONE ROUND
-------------------------------------------------------------------------
function Play3T(curUser,word1)
local sGame = curGames[TPlayers[strlower(curUser.sName)].curGame].game
if (curUser.sName == curGames[TPlayers[strlower(curUser.sName)].curGame].toPlay[1]) and sGame[word1] and (sGame[word1][2] == 0) then
local curUser1 = TPlayers[strlower(curUser.sName)].PlaysWith
if GetItemByName(TPlayers[strlower(curUser.sName)].PlaysWith.sName) then
local sGame = curGames[TPlayers[strlower(curUser.sName)].curGame].game
sGame[word1] = { curGames[TPlayers[strlower(curUser.sName)].curGame].toPlay[2],1 }
curGames[TPlayers[strlower(curUser.sName)].curGame].game = sGame
if curGames[TPlayers[strlower(curUser.sName)].curGame].toPlay[2] == "X" then
curGames[TPlayers[strlower(curUser.sName)].curGame].toPlay = { TPlayers[strlower(curUser.sName)].PlaysWith.sName,"O" }
else
curGames[TPlayers[strlower(curUser.sName)].curGame].toPlay = { TPlayers[strlower(curUser.sName)].PlaysWith.sName,"X" }
end
curUser:SendPM(tbot,GetCurGame(sGame.A1[1],sGame.A2[1],sGame.A3[1],sGame.B1[1],sGame.B2[1],sGame.B3[1],sGame.C1[1],sGame.C2[1],sGame.C3[1]))
curUser1:SendPM(tbot,curUser.sName.." Played, now its your turn."..GetCurGame(sGame.A1[1],sGame.A2[1],sGame.A3[1],sGame.B1[1],sGame.B2[1],sGame.B3[1],sGame.C1[1],sGame.C2[1],sGame.C3[1]))
if doCheckForWin(sGame) == "WON" then
curUser:SendPM(tbot,"******  YOU HAVE WON ********")
curUser1:SendPM(tbot,"****** YOU HAVE LOST ********")
DelGame(curUser,curUser1)
elseif doCheckIfPlay(sGame) == "STOP" then
curUser:SendPM(tbot,"******  GAME ENDED EVEN ********")
curUser1:SendPM(tbot,"******  GAME ENDED EVEN ********")
DelGame(curUser,curUser1)
end
else
curUser:SendPM(tbot,"Other Player went offline.")
DelGame(curUser,curUser1)
end
end
end

-------------------------------------------------------------------------
-- TTT TABLE FUNCS
-------------------------------------------------------------------------
TFUNCS = {
["invite"] = function(curUser,sdata)
local _,_,invited = strfind(sdata,"^%S+%s+(%S+)")
if invited and (not TPlayers[strlower(curUser.sName)]) and GetItemByName(invited) and (not TPlayers[strlower(invited)]) then
tInvite[strlower(invited)] = { 0,curUser }
curUser:SendPM(tbot,"Sending invitation to "..invited..".")
GetItemByName(invited):SendPM(tbot,curUser.sName.." invites you to a session of Tic,Tac Toe, Type: Accept or Deny.")
else
curUser:SendPM(tbot,"Either you already got a game going on or the user you wanted to invite is not online.")
end
end,
["quit"] = function(curUser,sdata)
if TPlayers[strlower(curUser.sName)] then
curUser:SendPM(tbot,"Game has been quit.")
TPlayers[strlower(curUser.sName)].PlaysWith:SendPM(tbot,curUser.sName.." has left the game.")
DelGame(curUser,TPlayers[strlower(curUser.sName)].PlaysWith)
end
end,
["accept"] = function(curUser,sdata)
if tInvite[strlower(curUser.sName)] then
if GetItemByName(tInvite[strlower(curUser.sName)][2].sName) then
local curUser2 = tInvite[strlower(curUser.sName)][2]
CreateGame(curUser,curUser2)
curUser2:SendPM(tbot,curUser.sName.." has accepted your request.")
curUser2:SendPM(tbot,GetCurGame("  ","  ","  ","  ","  ","  ","  ","  ","  "))
curUser:SendPM(tbot,"Waiting for "..curUser2.sName.." to start.")
else
curUser:SendPM(tbot,"Other player went offline.")
end
tInvite[strlower(curUser.sName)] = nil
end
end,
["deny"] = function(curUser,sdata)
if tInvite[strlower(curUser.sName)] then
curUser:SendPM(tbot,"Invitation was denied.")
tInvite[strlower(curUser.sName)][2]:SendPM(tbot,"You invitation was denied.")
tInvite[strlower(curUser.sName)] = nil
end
end,
}

Dunno...  if the curUser holding is really correct, but it works fine.
Title:
Post by: HaL on 29 January, 2004, 20:43:54
real nice script chilla
very nice....

can you tell me where can i get the
4 Gewinnt script? :-)

gru? HaL

oh sorry i saw now you are thinking about it
sorry :-)
Title:
Post by: c h i l l a on 29 January, 2004, 20:46:42
Jope I hope I will get it, but theoretically it should work,

but.. Only problem would be, the different text styles.
Title:
Post by: HaL on 30 January, 2004, 23:33:36
hi chilla

is it possible that the script was not regged in hub(not visible)?

and if some one write in mainchat !ttt-invite
the private chat with the tictactoe script pops up?
the same would be great 4 the 4Gewinnt script
!4g-invite ?

HaL
Title:
Post by: c h i l l a on 31 January, 2004, 00:50:59
klaro Hal ;).

Its a good idea, also found some mistakes in Vier Gewinnt. Connect4
Title: Can I have a rundown on how it works :-S
Post by: jsjen on 14 February, 2004, 09:12:07
I dont understand how to use it sorry.
Title:
Post by: Masterload on 14 February, 2004, 11:37:56
how it works :)

pm to the both

invite
who will    ,     Accept/not Accept

try it en see :)
*****************************
chill :)
nice job m8y
tic tac.. is a hot game here