WORLD NAME Simple Dice Game IMAGESFOLDER_PUBLIC http://www.dimensionex.net/boardgames/ IMAGESFOLDER_LOCAL /dimx/boardgames/ ' Uncomment the following if images don't show up 'IMAGESFOLDER /dimx/boardgames/ VERSION 1.2 'Tested on DimX 5.7.1b AUTHOR Cris SITE http://www.dimensionex.net/turnbased/simpledice.htm HELP http://www.dimensionex.net/turnbased/help.htm GUI LOGOSRC greentable.jpg PANEL default BUTTON help BUTTON logout TEXTBOX BUTTON say PANEL pre VERSION OF default CR CR BUTTON startgame, "Start Game!", "Start Game!", onStartGame PANEL play VERSION OF default CR CR BUTTON rolldice, "Roll Dice", "Roll Dice!", onRollDice PANEL pause VERSION OF default PANEL end VERSION OF default CR CR BUTTON resetgame, "Reset", "Reset", onReset END_GUI ROOMS ROOM board NAME Game Board IMAGE greentable.jpg END_ROOMS CHARACTERS ATTRLIST showmode=0 END_CHARACTERS ITEMS ITEM dice1 NAME dice IMAGE 64x64 blank.gif POSITION board SHOW ONSCREEN 60,100 ITEM dice2 NAME dice IMAGE 64x64 blank.gif POSITION board SHOW ONSCREEN 210,100 END_ITEMS SETS ARRAY players (empty),(empty) ARRAY turnscore 0,0 ARRAY score 0,0 END_SETS SCRIPTS Sub config() target_players = 2 target_turns = 3 auto_start = false End_Sub EVENT onStart Call config() mode = "pre" ' modes: pre / play / pause / end nplayers = 0 cycles = 0 SetPanel $WORLD,"pre" Dim i For i=1 to target_players score(i)=0 Next End_EVENT EVENT onNew If (mode = "pre" Or mode = "pause") And nplayers < target_players Dim slot = addPlayer($AGENT) If nplayers = target_players If mode = "pre" If Not(auto_start) Speak SYS,$WORLD,"You can now start the game by clicking the [START GAME] button." Else Call play() End_If Else If mode="pause" Speak SYS,$AGENT,"You have joined a running game as player #" + slot Call play() End_If End_If End_If End_If board.description = getSituation() RefreshView board End_EVENT Function addPlayer(person) Dim i For i=1 To SetLen(players) If Not(Exists(players(i))) players(i) = person nplayers = nplayers+1 Return i End_If Next End_Function Function getSituation() Dim i Dim x Dim string string = "-- current players: " + nplayers + " (target is: " + target_players + ") --" string = string + "
"
If mode <> "pre"
string = string + "Play cycles: " + cycles + "
"
End_If
If mode = "play"
string = string + "Next player: " + players(nowplays).name
End_If
If mode = "pre" Or mode = "pause"
If nplayers < target_players
string = string + "Waiting for " + (target_players - nplayers) + " more player(s)... Invite friends!"
Else
If nplayers > target_players
string = string + "There is " + (nplayers - target_players) + " person too much..."
Else
string = string + "Ready to start!"
End_If
End_If
End_If
If mode = "end"
string = string + "GAME OVER - Click [RESET] button for another game."
End_If
Return string
End_Function
EVENT onStartGame
If mode = "pre"
If findUser($AGENT) > 0
If nplayers < target_players
Speak "Cannot start a game before at least " + target_players + " players are connected."
Else
Call play()
started_playing = findUser($AGENT)
nowplays = started_playing
Speak SYS,$WORLD,"Now plays: " + players(nowplays).name
End_If
Else
' somebody is trying to play but not in the list of players
If nplayers < target_players
' OK one place is available - join
Call onNew()
Speak "OK you now have joined - click [START GAME] again to start"
Else
Speak "You are not one of the players - sorry. You are welcome to stay as a watcher."
End_If
End_If
Else
Speak "Cannot start game now (mode=" + mode + ")"
End_If
board.description = getSituation()
END_EVENT
Function findUser(user)
For i=1 To SetLen(players)
If user = players(i)
Return i
End_If
Next
Return 0
End_Function
EVENT onRollDice
If $AGENT = players(nowplays)
Dim roll1 = RndInt(6)
Dim roll2 = RndInt(6)
dice1.image = NewImage("d" + roll1 + ".gif",64,64)
dice2.image = NewImage("d" + roll2 + ".gif",64,64)
turnscore(nowplays) = roll1 + roll2
Speak SYS,$WORLD,"" + players(nowplays).name + " has scored: " + roll1 + " + " + roll2 + " = " + turnscore(nowplays)
nowplays = nowplays + 1
If nowplays > nplayers
nowplays = 1
End_If
If nowplays = started_playing
cycles = cycles + 1
Call updScore()
If checkEnd()
mode = "end"
SetPanel $WORLD,"end"
Else
dummy=0
'Speak SYS,$WORLD,"Another turn. Now plays: " + players(nowplays).name
End_If
Else
dummy=0
'Speak SYS,$WORLD,"Now plays: " + players(nowplays).name
End_If
Else
Speak "WAIT! It's " + players(nowplays).name + " turn."
End_If
board.description = getSituation()
RefreshView board
End_EVENT
Sub updScore()
If turnscore(1) > turnscore(2)
Speak SYS,$WORLD,"O.K. then " + players(1).name + " wins this turn"
score(1) = score(1) + 1
Else
If turnscore(2) > turnscore(1)
Speak SYS,$WORLD,"Look: " + players(2).name + " wins this turn"
score(2) = score(2) + 1
Else
Speak SYS,$WORLD,"Hey - this cycle was even!"
End_If
End_If
board.description = getSituation()
RefreshView board
End_Sub
Function checkEnd()
Return (cycles = target_turns)
End_Function
EVENT board.onLoose
If $AGENT=null
' User logs off
Dim loggingout = findUser($TARGET)
If loggingout > 0
players(loggingout) = "(empty)"
nplayers = nplayers - 1
If mode = "play"
mode = "pause"
SetPanel $WORLD,"pause"
End_If
End_If
board.description = getSituation()
End_If
End_EVENT
Sub play()
Speak SYS,$WORLD,"Game is running... GOOD LUCK!"
mode = "play"
SetPanel $WORLD,"play"
End_Sub
EVENT onReset
Reset
END_EVENT
END_SCRIPTS
END_WORLD