Created
November 4, 2011 06:10
-
-
Save zbowling/1338765 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dieMap = ['A','B','C','D','E','F'] | |
class BuddhaGame | |
constructor: (@robot,@player) -> | |
@diceLeft = 6 | |
@diceTaken = [] | |
@lastDice = [] | |
@gameover = no | |
rollRemainingDice: -> | |
@lastDice = [] | |
printlines = ['','','','','','',''] | |
for dieIdx in [0...@diceLeft] | |
die = @randomDice() | |
diestr = @diceString(die,dieMap[dieIdx],die) | |
dielines = diestr.split "\n" | |
for i in [0...printlines.length] | |
printlines[i] += dielines[i] | |
@lastDice.push die | |
printlines | |
roll: -> | |
"new game!\n" + | |
@rollRemainingDice().join("\n") | |
take: (diceToTake) -> | |
dieLetters = diceToTake.toUpperCase().replace(" ","").split("") | |
lastDiceLeftCount = @diceLeft | |
for dieLetter in dieLetters | |
dieIndex = dieMap.indexOf dieLetter | |
if dieIndex != -1 and dieIndex < @lastDice.length | |
die_val = @lastDice[dieIndex] | |
if die_val != -1 | |
@diceTaken.push die_val | |
@lastDice[dieIndex] = -1 #marking it invalid so you can pick it twice | |
@diceLeft -= 1 | |
if @diceLeft == lastDiceLeftCount | |
"(you didn't pick any dice.)" | |
printlines = ['','','','','','',''] | |
if @diceLeft > 1 | |
printlines = @rollRemainingDice() | |
for i in [0...printlines.length] | |
printlines[i] += "\t > " | |
else if @diceLeft == 1 #only one left so lets just end the suffering | |
die = @randomDice() | |
@diceTaken.push die | |
@diceLeft = 0 | |
takenString = @diceTakenStringArray() | |
for i in [0...printlines.length] | |
printlines[i] += takenString[i] | |
return_str = printlines.join("\n") + "\n" + @calculateScoreString() | |
if @diceLeft <= 0 | |
gameover = yes | |
return_str += "\nNice job!" | |
return_str | |
score: -> | |
score = 0 | |
has_one = no | |
has_four = no | |
for dieIdx in [[email protected]] | |
dice_val = @diceTaken[dieIdx] | |
if dice_val == 4 and has_four == no | |
has_four = yes | |
else if dice_val == 1 and has_one == no | |
has_one = yes | |
else | |
score += dice_val | |
if has_hour and has_one | |
score | |
else | |
0 | |
calculateScoreString: -> | |
score = 0 | |
has_one = no | |
has_four = no | |
for dieIdx in [[email protected]] | |
dice_val = @diceTaken[dieIdx] | |
if dice_val == 4 and has_four == no | |
has_four = yes | |
else if dice_val == 1 and has_one == no | |
has_one = yes | |
else | |
score += dice_val | |
if @diceLeft <= 0 and (has_one == no or has_four == no) | |
"score: 0 // bummer..." | |
else if has_one == no and has_four == no | |
"score: #{score} (you still need to take a 1 and a 4)" | |
else if has_one == no and has_four == yes | |
"score: #{score} (you still need to take a 1)" | |
else if has_four == no and has_one == yes | |
"score: #{score} (you still need to take a 4)" | |
else | |
"score: #{score}" | |
diceTakenStringArray: -> | |
printlines = ['','','','','','',''] | |
has_one = no | |
has_four = no | |
for dieIdx in [[email protected]] | |
dice_val = @diceTaken[dieIdx] | |
dice_score = "-" | |
if dice_val == 4 and has_four == no | |
has_four = yes | |
else if dice_val == 1 and has_one == no | |
has_one = yes | |
else | |
dice_score = dice_val | |
diestr = @diceString(dice_val,"*",dice_score) | |
dielines = diestr.split "\n" | |
for i in [0...printlines.length] | |
printlines[i] += dielines[i] | |
printlines | |
randomDice: -> | |
1 + Math.floor(Math.random() * 6) | |
diceString: (value,i,x) -> | |
switch value | |
when 1 | |
" | |
#{x} \n | |
--------- \n | |
| | \n | |
| o | \n | |
| | \n | |
--------- \n | |
#{i} \n | |
" | |
when 2 | |
" | |
#{x} \n | |
--------- \n | |
| o | \n | |
| | \n | |
| o | \n | |
--------- \n | |
#{i} \n | |
" | |
when 3 | |
" | |
#{x} \n | |
--------- \n | |
| o | \n | |
| o | \n | |
| o | \n | |
--------- \n | |
#{i} \n | |
" | |
when 4 | |
" | |
#{x} \n | |
--------- \n | |
| o o | \n | |
| | \n | |
| o o | \n | |
--------- \n | |
#{i} \n | |
" | |
when 5 | |
" | |
#{x} \n | |
--------- \n | |
| o o | \n | |
| o | \n | |
| o o | \n | |
--------- \n | |
#{i} \n | |
" | |
when 6 | |
" | |
#{x} \n | |
--------- \n | |
| o o | \n | |
| o o | \n | |
| o o | \n | |
--------- \n | |
#{i} \n | |
" | |
class BuddhaLounge | |
constructor: (@robot) -> | |
@games = [] | |
@playerdata = [] | |
@robot.brain.on 'loaded', => | |
if @robot.brain.data.buddhagames? | |
@games = @robot.brain.data.buddhagames | |
if @robot.brain.data.playerdata? | |
@playerdata = @robot.brain.data.playerdata | |
startGame: (msg, player) -> | |
@games[player] = new BuddhaGame @robot, player | |
msg.reply "\n" + @games[player].roll() | |
@robot.brain.data.buddhagames = @games | |
if not @playerdata[player]? | |
@playerdata[player] = { totalScore: 0, totalGamesStarted: 0, totalGamesFinished: 0, lastScore:0} | |
@playerdata[player].totalGamesStarted += 1; | |
@robot.brain.data.playerdata = @playerdata | |
take: (msg, player, take) -> | |
if @games[player]? | |
msg.reply "\n" + @games[player].take(take) | |
@robot.brain.data.buddhagames = @games | |
if @games[player].gameover | |
if not @playerdata[player]? | |
@playerdata[player] = { totalScore: 0, totalGamesStarted: 0, totalGamesFinished: 0, lastScore:0} | |
@playerdata[player].lastScore = @games[player].score() | |
@playerdata[player].totalScore += @games[player].score() | |
@playerdata[player].totalGamesFinished += 1 | |
@robot.brain.data.playerdata = @playerdata | |
else | |
msg.reply "you aren't playing a game." | |
stats: (msg) -> | |
return_str = "" | |
for playerIdx in [[email protected]] | |
player = @playerdata[playerIdx] | |
return_str += "\n | |
#{player}: \n | |
\tlast score: #{@playerdata[player].lastScore}\n | |
\taverage score: #{@playerdata[player].totalScore/@playerdata[player].totalGamesFinished}\n | |
\ttotal games finished: #{@playerdata[player].totalGamesFinished}\n | |
\ttotal games started: #{@playerdata[player].totalGamesStarted}\n | |
" | |
msg.reply return_str | |
### | |
b = new BuddhaGame null,null | |
console.log b.roll() | |
console.log "taking A, B, C, D" | |
console.log b.take("A A A D") | |
console.log "taking the last 2" | |
console.log b.take("A B") | |
### | |
module.exports = (robot) -> | |
buddha = new BuddhaLounge robot | |
robot.hear /(buddha|dice) start/i, (msg) -> | |
buddha.startGame msg, msg.message.user | |
robot.hear /(buddha|dice) stats/i, (msg) -> | |
buddha.stats msg | |
robot.hear /(buddha|dice) take ([\w .-]+)/i, (msg) -> | |
buddha.take msg, msg.message.user, msg.match[2] | |
Yeah! I added a newer version.
multi or single player? Looks like single player...
it tracks users separately that are playing concurrently but it doesn't wrap them into a single game. It's harder because I have to verify they are in the same room.
Good point, though it would be extremely cool to include a join feature. Didn't know you guys were using Campfire?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This a chat bot? Like the idea... ;)