×

Langue

Fermer
Atelier 801
  • Forums
  • Dev Tracker
  • Connexion
    • English Français
      Português do Brasil Español
      Türkçe Polski
      Magyar Română
      العربية Skandinavisk
      Nederlands Deutsch
      Bahasa Indonesia Русский
      中文 Filipino
      Lietuvių kalba 日本語
      Suomi עברית
      Italiano Česky
      Hrvatski Slovensky
      Български Latviešu
      Estonian
  • Langue
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • [Script] Tic Tac Toe
1 / 3 › »
[Script] Tic Tac Toe
Baasbase
« Citoyen »
1381160700000
    • Baasbase#0095
    • Profil
    • Derniers messages
#1
  0
Tic tac toe is finally available in TFM.
This script can be used as a tribe house script, it gets boring very fast though!

I hope you can learn from this, I have programmed it in a simplistic way and just looking at the functions should be enough to understand how this script works.

If you have any questions, feel free to ask them!

PS: It doesnt detect when there is a winner, it would be not too hard to implement that but I didn't feel like doing it, maybe one of you can add that?

PPS: !new starts a new game, !stop will quit the script and using !new playername will change your current opponent with the playername you specified (for example !new Tigrounette would put me against Tigrounette in a new match)

PPPS: When loading this script you have to manually change the players who will first participate, its at the top of the script :)


Baasbase a dit :

MAP = "<C><P /><Z><S><S X=\"400\" Y=\"390\" T=\"12\" H=\"30\" P=\"0,0,0.3,0.2,0,0,0,0\" L=\"800\" o=\"324650\" /><S X=\"400\" c=\"4\" Y=\"153\" T=\"12\" H=\"10\" P=\"0,0,0.3,0.2,0,0,0,0\" L=\"280\" o=\"324650\" /><S X=\"400\" c=\"4\" Y=\"246\" T=\"12\" H=\"10\" P=\"0,0,0.3,0.2,0,0,0,0\" L=\"280\" o=\"324650\" /><S X=\"353\" c=\"4\" Y=\"200\" T=\"12\" P=\"0,0,0.3,0.2,0,0,0,0\" H=\"280\" L=\"10\" o=\"324650\" /><S X=\"446\" c=\"4\" Y=\"200\" T=\"12\" H=\"280\" P=\"0,0,0.3,0.2,0,0,0,0\" L=\"10\" o=\"324650\" /></S><D /><O /></Z></C>"

PLAYERS = {
"Baasbase",
"Hobothehill"
}

GRID = {
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 }
}

turn = 1

tfm.exec.disableAutoNewGame(true)
tfm.exec.disableAutoShaman(true)
tfm.exec.newGame(MAP)

function eventMouse(p, x, y)
if x > 260 and x <= 540 and y > 60 and y <= 340 then
for i,n in pairs(PLAYERS) do
if i == turn then
if p == n then
if getField(x, y) == 0 then
setField(x, y, (PLAYERS[1] == p and 1 or 2))
turn = turn + (turn == 1 and 1 or -1)
end
end
end
end
end
end

function translateX(x)
if x < (260 + 280/3) then
return 1
elseif x < (260 + (280/3)*2) then
return 2
else
return 3
end
end

function translateY(x)
if x < (60 + 280/3) then
return 1
elseif x < (60 + (280/3)*2) then
return 2
else
return 3
end
end

function getField(x, y)
return GRID[translateY(y)][translateX(x)]
end

function setField(x, y, value)
local tx = translateX(x)
local ty = translateY(y)
GRID[ty][tx] = value
ui.addTextArea(tx-1 + ty*3, "<p align=\"center\"><font size=\"70\">" .. (value == 1 and "O" or "X") .. "</font></p>", nil, tx * (280/3) + 260 - (280/3), ty * (280/3) + 60 - (280/3), 280/3, 280/3, 0, 0, 0)
end

function eventNewPlayer(p)
tfm.exec.respawnPlayer(p)
end

function eventPlayerDied(p)
tfm.exec.respawnPlayer(p)
end

function eventPlayerWon(p)
tfm.exec.respawnPlayer(p)
end

function eventNewGame()
if tfm.get.room.currentMap == "@0" then
tfm.exec.setGameTime(5, true)
turn = 1
for y,xs in pairs(GRID) do
for x in pairs(xs) do
GRID[y][x] = 0
end
end
for p in pairs(tfm.get.room.playerList) do
tfm.exec.movePlayer(p, (math.random(1,2) == 1 and 100 or 700), 300)
system.bindMouse(p, false)
end
tfm.exec.movePlayer(PLAYERS[1], 200, 300)
tfm.exec.movePlayer(PLAYERS[2], 600, 300)
for i,p in pairs(PLAYERS) do
system.bindMouse(p, true)
end
ui.addTextArea(1, "<p align=\"center\"><font size=\"35\">" .. PLAYERS[1] .. "</font></p>", nil, 50, 50, 200, 50, 0, 0, 0)
ui.addTextArea(2, "<p align=\"center\"><font size=\"35\">" .. PLAYERS[2] .. "</font></p>", nil, 550, 50, 200, 50, 0, 0, 0)
for i=3,11 do
ui.removeTextArea(i)
end
end
end

function eventChatCommand(p, c)
for i=1,2 do
if p == PLAYERS then
if c == "stop" then
system.exit()
elseif string.sub(c, 1, 3) == "new" then
local pl = tostring(PLAYERS[1]) -- test
PLAYERS[1] = PLAYERS[2]
PLAYERS[2] = pl

for n in pairs(tfm.get.room.playerList) do
if string.find(c, n) then
PLAYERS = n
end
end

tfm.exec.newGame(MAP)
return
end
end
end
end

Enjoy!
Unicorm
« Citoyen »
1381161060000
    • Unicorm#0000
    • Profil
    • Derniers messages
    • Tribu
#2
  0
omg *-*
is it possible to have more than two players?

edit: is it possible to have a larger grid?

v that's too bad ;-;
Haruhitastic
« Censeur »
1381161300000
    • Haruhitastic#0000
    • Profil
    • Derniers messages
    • Tribu
#3
  0
have you ever played a round of tic tac toe where you've had more than two? that'd gonna be new to me if so. :P

(aka: no, likely not.)
Mikuhl
« Citoyen »
1381161840000
    • Mikuhl#3311
    • Profil
    • Derniers messages
    • Tribu
#4
  0
psst: you can use ' for the XML instead of using two characters to escape the quotation.
Foorero
« Citoyen »
1381162860000
    • Foorero#0000
    • Profil
    • Derniers messages
    • Tribu
#5
  0
http://i.imgur.com/kyUagSR.png
Topic-462937
xP
Good Game!
Ordboka
« Citoyen »
1381164660000
    • Ordboka#0000
    • Profil
    • Derniers messages
    • Tribu
#6
  0
Nice code. Figured I could give the winning mechanism a shot. Here it goes
Lua a dit :

MAP = "<C><P /><Z><S><S X=\"400\" Y=\"390\" T=\"12\" H=\"30\" P=\"0,0,0.3,0.2,0,0,0,0\" L=\"800\" o=\"324650\" /><S X=\"400\" c=\"4\" Y=\"153\" T=\"12\" H=\"10\" P=\"0,0,0.3,0.2,0,0,0,0\" L=\"280\" o=\"324650\" /><S X=\"400\" c=\"4\" Y=\"246\" T=\"12\" H=\"10\" P=\"0,0,0.3,0.2,0,0,0,0\" L=\"280\" o=\"324650\" /><S X=\"353\" c=\"4\" Y=\"200\" T=\"12\" P=\"0,0,0.3,0.2,0,0,0,0\" H=\"280\" L=\"10\" o=\"324650\" /><S X=\"446\" c=\"4\" Y=\"200\" T=\"12\" H=\"280\" P=\"0,0,0.3,0.2,0,0,0,0\" L=\"10\" o=\"324650\" /></S><D /><O /></Z></C>"

PLAYERS = {
"Ordboka",
"Ordboka"
}

GRID = {
{ 0, 0, 0},
{0,0,0},
{0,0,0}
}

turn = 1

tfm.exec.disableAutoNewGame(true)
tfm.exec.disableAutoShaman(true)
tfm.exec.newGame(MAP)

function eventMouse(p, x, y)
if x > 260 and x <= 540 and y > 60 and y <= 340 then
for i,n in pairs(PLAYERS) do
if i == turn then
if p == n then
if getField(x, y) == 0 then
setField(x, y, (PLAYERS[1] == p and 1 or 2))
checkForWin(turn, tx, ty)
turn = turn + (turn == 1 and 1 or -1)
end
end
end
end
end
end

function translateX(x)
if x < (260 + 280/3) then
return 1
elseif x < (260 + (280/3)*2) then
return 2
else
return 3
end
end

function translateY(x)
if x < (60 + 280/3) then
return 1
elseif x < (60 + (280/3)*2) then
return 2
else
return 3
end
end

function getField(x, y)
return GRID[translateY(y)][translateX(x)]
end

function setField(x, y, value)
tx = translateX(x)
ty = translateY(y)
GRID[ty][tx] = value
print(ty .. tx)
ui.addTextArea(tx-1 + ty*3, "<p align=\"center\"><font size=\"70\">" .. (value == 1 and "O" or "X") .. "</font></p>", nil, tx * (280/3) + 260 - (280/3), ty * (280/3) + 60 - (280/3), 280/3, 280/3, 0, 0, 0)
end

function eventNewPlayer(p)
tfm.exec.respawnPlayer(p)
end

function eventPlayerDied(p)
tfm.exec.respawnPlayer(p)
end

function eventPlayerWon(p)
tfm.exec.respawnPlayer(p)
end

function eventNewGame()
if tfm.get.room.currentMap == "@0" then
tfm.exec.setGameTime(5, true)
turn = 1
for y,xs in pairs(GRID) do
for x in pairs(xs) do
GRID[y][x] = 0
end
end
for p in pairs(tfm.get.room.playerList) do
tfm.exec.movePlayer(p, (math.random(1,2) == 1 and 100 or 700), 300)
system.bindMouse(p, false)
end
tfm.exec.movePlayer(PLAYERS[1], 200, 300)
tfm.exec.movePlayer(PLAYERS[2], 600, 300)
for i,p in pairs(PLAYERS) do
system.bindMouse(p, true)
end
ui.addTextArea(1, "<p align=\"center\"><font size=\"35\">" .. PLAYERS[1] .. "</font></p>", nil, 50, 50, 200, 50, 0, 0, 0)
ui.addTextArea(2, "<p align=\"center\"><font size=\"35\">" .. PLAYERS[2] .. "</font></p>", nil, 550, 50, 200, 50, 0, 0, 0)
for i=3,11 do
ui.removeTextArea(i)
end
end
end

function eventChatCommand(p, c)
for i=1,2 do
if p == PLAYERS then
if c == "stop" then
system.exit()
elseif string.sub(c, 1, 3) == "new" then
local pl = tostring(PLAYERS[1]) -- test
PLAYERS[1] = PLAYERS[2]
PLAYERS[2] = pl

for n in pairs(tfm.get.room.playerList) do
if string.find(c, n) then
PLAYERS = n
end
end

tfm.exec.newGame(MAP)
return
end
end
end
end

function checkForWin(playerTurn, yCord, xCord)
if yCord == 1 and xCord == 1 then
if GRID[1][2] == playerTurn and GRID[1][3] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[2][1] == playerTurn and GRID[3][1] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[2][2] == playerTurn and GRID[3][3] == playerTurn then
tfm.exec.newGame(MAP)
end
end
if yCord == 2 and xCord == 1 then
if GRID[1][1] == playerTurn and GRID[1][3] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[2][2] == playerTurn and GRID[3][2] == playerTurn then
tfm.exec.newGame(MAP)
end
end
if yCord == 3 and xCord == 1 then
if GRID[1][2] == playerTurn and GRID[1][1] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[2][3] == playerTurn and GRID[3][3] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[2][2] == playerTurn and GRID[3][1] == playerTurn then
tfm.exec.newGame(MAP)
end
end
if yCord == 1 and xCord == 2 then
if GRID[2][2] == playerTurn and GRID[2][3] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[1][1] == playerTurn and GRID[3][1] == playerTurn then
tfm.exec.newGame(MAP)
end
end
if yCord == 2 and xCord == 2 then
if GRID[1][2] == playerTurn and GRID[3][2] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[2][1] == playerTurn and GRID[2][3] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[1][1] == playerTurn and GRID[3][3] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[3][1] == playerTurn and GRID[1][3] == playerTurn then
tfm.exec.newGame(MAP)
end
end
if yCord == 3 and xCord == 2 then
if GRID[2][2] == playerTurn and GRID[2][1] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[1][3] == playerTurn and GRID[3][3] == playerTurn then
tfm.exec.newGame(MAP)
end
end
if yCord == 1 and xCord == 3 then
if GRID[2][1] == playerTurn and GRID[1][1] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[3][3] == playerTurn and GRID[3][2] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[2][2] == playerTurn and GRID[1][3] == playerTurn then
tfm.exec.newGame(MAP)
end
end
if yCord == 2 and xCord == 3 then
if GRID[2][2] == playerTurn and GRID[1][2] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[3][1] == playerTurn and GRID[3][3] == playerTurn then
tfm.exec.newGame(MAP)
end
end
if yCord == 3 and xCord == 3 then
if GRID[2][1] == playerTurn and GRID[1][1] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[3][1] == playerTurn and GRID[3][2] == playerTurn then
tfm.exec.newGame(MAP)
end
if GRID[2][2] == playerTurn and GRID[1][1] == playerTurn then
tfm.exec.newGame(MAP)
end
end
end

It's very simple, and could probably be improved. If you find any bugs please tell me or fix it yourself and post the code here for everyone to see!
Mezar
« Citoyen »
1381165140000
    • Mezar#9645
    • Profil
    • Derniers messages
#7
  0
edited
Shamousey
« Consul »
1381165260000
    • Shamousey#0095
    • Profil
    • Derniers messages
    • Tribu
#8
  0
I'm curious, why did you escape every single " instead of just using ' or [[...]]?

a dit :
MAP=[[<C><P /><Z><S><S X="400" Y="390" T="12" H="30" P="0,0,0.3,0.2,0,0,0,0" L="800" o="324650" /><S X="400" c="4" Y="153" T="12" H="10" P="0,0,0.3,0.2,0,0,0,0" L="280" o="324650" /><S X="400" c="4" Y="246" T="12" H="10" P="0,0,0.3,0.2,0,0,0,0" L="280" o="324650" /><S X="353" c="4" Y="200" T="12" P="0,0,0.3,0.2,0,0,0,0" H="280" L="10" o="324650" /><S X="446" c="4" Y="200" T="12" H="280" P="0,0,0.3,0.2,0,0,0,0" L="10" o="324650" /></S><D /><O /></Z></C>]]

 
Baasbase
« Citoyen »
1381165680000
    • Baasbase#0095
    • Profil
    • Derniers messages
#9
  0
matter of habit (programming background in java)
Gilcatmey
« Citoyen »
1381170900000
    • Gilcatmey#0000
    • Profil
    • Derniers messages
    • Tribu
#10
  0
I can't copy the full code from the forum :(
Tranna
« Citoyen »
1381171440000
    • Tranna#0000
    • Profil
    • Derniers messages
    • Tribu
#11
  0
How not? On the forum post, click CTRL+A then post in /lua tab, remove uneeded things like the end and the beginning and.. simple!
Gilcatmey
« Citoyen »
1381171800000
    • Gilcatmey#0000
    • Profil
    • Derniers messages
    • Tribu
#12
  0
Tranna a dit :
How not? On the forum post, click CTRL+A then post in /lua tab, remove uneeded things like the end and the beginning and.. simple!

Thanks! I was pressing CTRL+E instead xD

But now I pasted it but nothing happened, it happens always I try a script
Tranna
« Citoyen »
1381172220000
    • Tranna#0000
    • Profil
    • Derniers messages
    • Tribu
#13
  0
Do you have permission to use the /np command?
Gilcatmey
« Citoyen »
1381172340000
    • Gilcatmey#0000
    • Profil
    • Derniers messages
    • Tribu
#14
  0
Yes, I'm sub-leader actually
Haruhitastic
« Censeur »
1381177320000
    • Haruhitastic#0000
    • Profil
    • Derniers messages
    • Tribu
#15
  0
Is it possible that bogus hack ban is ruining it? :[ You can't use /lua if you've received a ban for hacking.
Xxninjazxx
« Citoyen »
1381181040000
    • Xxninjazxx#0000
    • Profil
    • Derniers messages
#16
  0
Now someone code celebrity squares with this.
Gilcatmey
« Citoyen »
1381191540000
    • Gilcatmey#0000
    • Profil
    • Derniers messages
    • Tribu
#17
  0
Haruhitastic a dit :
Is it possible that bogus hack ban is ruining it? :[ You can't use /lua if you've received a ban for hacking.

Wow, that'd be the reason...
Nathaan
« Citoyen »
1381245540000
    • Nathaan#0000
    • Profil
    • Derniers messages
    • Tribu
#18
  0
Good game for the code ! ;D
Zyx
« Censeur »
1381273560000
    • Zyx#8690
    • Profil
    • Derniers messages
#19
  0
I can't use it! I've copied it but the code is too long so I have to take piece to piece but I always get messed up! :(
Mikuhl
« Citoyen »
1381366260000
    • Mikuhl#3311
    • Profil
    • Derniers messages
    • Tribu
#20
  0
Swirlfire a dit :
I can't use it! I've copied it but the code is too long so I have to take piece to piece but I always get messed up! :(

You cant copy the whole thing? What computer are you using, a toaster?
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • [Script] Tic Tac Toe
1 / 3 › »
© Atelier801 2018

Equipe Conditions Générales d'Utilisation Politique de Confidentialité Contact

Version 1.27