forked from caradojo/trivia
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from emiguez/coffeescript_version
coffescript version
- Loading branch information
Showing
3 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
Game = -> | ||
players = new Array() | ||
places = new Array(6) | ||
purses = new Array(6) | ||
inPenaltyBox = new Array(6) | ||
popQuestions = new Array() | ||
scienceQuestions = new Array() | ||
sportsQuestions = new Array() | ||
rockQuestions = new Array() | ||
currentPlayer = 0 | ||
isGettingOutOfPenaltyBox = false | ||
didPlayerWin = -> | ||
(purses[currentPlayer] isnt 6) | ||
|
||
currentCategory = -> | ||
return "Pop" if places[currentPlayer] is 0 | ||
return "Pop" if places[currentPlayer] is 4 | ||
return "Pop" if places[currentPlayer] is 8 | ||
return "Science" if places[currentPlayer] is 1 | ||
return "Science" if places[currentPlayer] is 5 | ||
return "Science" if places[currentPlayer] is 9 | ||
return "Sports" if places[currentPlayer] is 2 | ||
return "Sports" if places[currentPlayer] is 6 | ||
return "Sports" if places[currentPlayer] is 10 | ||
"Rock" | ||
|
||
@createRockQuestion = (index) -> | ||
"Rock Question " + index | ||
|
||
i = 0 | ||
|
||
while i < 50 | ||
popQuestions.push "Pop Question " + i | ||
scienceQuestions.push "Science Question " + i | ||
sportsQuestions.push "Sports Question " + i | ||
rockQuestions.push @createRockQuestion(i) | ||
i++ | ||
@isPlayable = (howManyPlayers) -> | ||
howManyPlayers >= 2 | ||
|
||
@add = (playerName) -> | ||
players.push playerName | ||
places[@howManyPlayers() - 1] = 0 | ||
purses[@howManyPlayers() - 1] = 0 | ||
inPenaltyBox[@howManyPlayers() - 1] = false | ||
console.log playerName + " was added" | ||
console.log "They are player number " + players.length | ||
true | ||
|
||
@howManyPlayers = -> | ||
players.length | ||
|
||
askQuestion = -> | ||
console.log popQuestions.shift() if currentCategory() is "Pop" | ||
console.log scienceQuestions.shift() if currentCategory() is "Science" | ||
console.log sportsQuestions.shift() if currentCategory() is "Sports" | ||
console.log rockQuestions.shift() if currentCategory() is "Rock" | ||
|
||
@roll = (roll) -> | ||
console.log players[currentPlayer] + " is the current player" | ||
console.log "They have rolled a " + roll | ||
if inPenaltyBox[currentPlayer] | ||
unless roll % 2 is 0 | ||
isGettingOutOfPenaltyBox = true | ||
console.log players[currentPlayer] + " is getting out of the penalty box" | ||
places[currentPlayer] = places[currentPlayer] + roll | ||
places[currentPlayer] = places[currentPlayer] - 12 if places[currentPlayer] > 11 | ||
console.log players[currentPlayer] + "'s new location is " + places[currentPlayer] | ||
console.log "The category is " + currentCategory() | ||
askQuestion() | ||
else | ||
console.log players[currentPlayer] + " is not getting out of the penalty box" | ||
isGettingOutOfPenaltyBox = false | ||
else | ||
places[currentPlayer] = places[currentPlayer] + roll | ||
places[currentPlayer] = places[currentPlayer] - 12 if places[currentPlayer] > 11 | ||
console.log players[currentPlayer] + "'s new location is " + places[currentPlayer] | ||
console.log "The category is " + currentCategory() | ||
askQuestion() | ||
|
||
@wasCorrectlyAnswered = -> | ||
if inPenaltyBox[currentPlayer] | ||
if isGettingOutOfPenaltyBox | ||
console.log "Answer was correct!!!!" | ||
purses[currentPlayer] += 1 | ||
console.log players[currentPlayer] + " now has " + purses[currentPlayer] + " Gold Coins." | ||
winner = didPlayerWin() | ||
currentPlayer += 1 | ||
currentPlayer = 0 if currentPlayer is players.length | ||
winner | ||
else | ||
currentPlayer += 1 | ||
currentPlayer = 0 if currentPlayer is players.length | ||
true | ||
else | ||
console.log "Answer was correct!!!!" | ||
purses[currentPlayer] += 1 | ||
console.log players[currentPlayer] + " now has " + purses[currentPlayer] + " Gold Coins." | ||
winner = didPlayerWin() | ||
currentPlayer += 1 | ||
currentPlayer = 0 if currentPlayer is players.length | ||
winner | ||
|
||
@wrongAnswer = -> | ||
console.log "Question was incorrectly answered" | ||
console.log players[currentPlayer] + " was sent to the penalty box" | ||
inPenaltyBox[currentPlayer] = true | ||
currentPlayer += 1 | ||
currentPlayer = 0 if currentPlayer is players.length | ||
true | ||
|
||
@ | ||
|
||
notAWinner = false | ||
game = new Game() | ||
game.add "Chet" | ||
game.add "Pat" | ||
game.add "Sue" | ||
loop | ||
game.roll Math.floor(Math.random() * 6) + 1 | ||
if Math.floor(Math.random() * 10) is 7 | ||
notAWinner = game.wrongAnswer() | ||
else | ||
notAWinner = game.wasCorrectlyAnswered() | ||
break unless notAWinner |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
var Game, game, notAWinner; | ||
Game = function() { | ||
var askQuestion, currentCategory, currentPlayer, didPlayerWin, i, inPenaltyBox, isGettingOutOfPenaltyBox, places, players, popQuestions, purses, rockQuestions, scienceQuestions, sportsQuestions; | ||
players = new Array(); | ||
places = new Array(6); | ||
purses = new Array(6); | ||
inPenaltyBox = new Array(6); | ||
popQuestions = new Array(); | ||
scienceQuestions = new Array(); | ||
sportsQuestions = new Array(); | ||
rockQuestions = new Array(); | ||
currentPlayer = 0; | ||
isGettingOutOfPenaltyBox = false; | ||
didPlayerWin = function() { | ||
return purses[currentPlayer] !== 6; | ||
}; | ||
currentCategory = function() { | ||
if (places[currentPlayer] === 0) { | ||
return "Pop"; | ||
} | ||
if (places[currentPlayer] === 4) { | ||
return "Pop"; | ||
} | ||
if (places[currentPlayer] === 8) { | ||
return "Pop"; | ||
} | ||
if (places[currentPlayer] === 1) { | ||
return "Science"; | ||
} | ||
if (places[currentPlayer] === 5) { | ||
return "Science"; | ||
} | ||
if (places[currentPlayer] === 9) { | ||
return "Science"; | ||
} | ||
if (places[currentPlayer] === 2) { | ||
return "Sports"; | ||
} | ||
if (places[currentPlayer] === 6) { | ||
return "Sports"; | ||
} | ||
if (places[currentPlayer] === 10) { | ||
return "Sports"; | ||
} | ||
return "Rock"; | ||
}; | ||
this.createRockQuestion = function(index) { | ||
return "Rock Question " + index; | ||
}; | ||
i = 0; | ||
while (i < 50) { | ||
popQuestions.push("Pop Question " + i); | ||
scienceQuestions.push("Science Question " + i); | ||
sportsQuestions.push("Sports Question " + i); | ||
rockQuestions.push(this.createRockQuestion(i)); | ||
i++; | ||
} | ||
this.isPlayable = function(howManyPlayers) { | ||
return howManyPlayers >= 2; | ||
}; | ||
this.add = function(playerName) { | ||
players.push(playerName); | ||
places[this.howManyPlayers() - 1] = 0; | ||
purses[this.howManyPlayers() - 1] = 0; | ||
inPenaltyBox[this.howManyPlayers() - 1] = false; | ||
console.log(playerName + " was added"); | ||
console.log("They are player number " + players.length); | ||
return true; | ||
}; | ||
this.howManyPlayers = function() { | ||
return players.length; | ||
}; | ||
askQuestion = function() { | ||
if (currentCategory() === "Pop") { | ||
console.log(popQuestions.shift()); | ||
} | ||
if (currentCategory() === "Science") { | ||
console.log(scienceQuestions.shift()); | ||
} | ||
if (currentCategory() === "Sports") { | ||
console.log(sportsQuestions.shift()); | ||
} | ||
if (currentCategory() === "Rock") { | ||
return console.log(rockQuestions.shift()); | ||
} | ||
}; | ||
this.roll = function(roll) { | ||
console.log(players[currentPlayer] + " is the current player"); | ||
console.log("They have rolled a " + roll); | ||
if (inPenaltyBox[currentPlayer]) { | ||
if (roll % 2 !== 0) { | ||
isGettingOutOfPenaltyBox = true; | ||
console.log(players[currentPlayer] + " is getting out of the penalty box"); | ||
places[currentPlayer] = places[currentPlayer] + roll; | ||
if (places[currentPlayer] > 11) { | ||
places[currentPlayer] = places[currentPlayer] - 12; | ||
} | ||
console.log(players[currentPlayer] + "'s new location is " + places[currentPlayer]); | ||
console.log("The category is " + currentCategory()); | ||
return askQuestion(); | ||
} else { | ||
console.log(players[currentPlayer] + " is not getting out of the penalty box"); | ||
return isGettingOutOfPenaltyBox = false; | ||
} | ||
} else { | ||
places[currentPlayer] = places[currentPlayer] + roll; | ||
if (places[currentPlayer] > 11) { | ||
places[currentPlayer] = places[currentPlayer] - 12; | ||
} | ||
console.log(players[currentPlayer] + "'s new location is " + places[currentPlayer]); | ||
console.log("The category is " + currentCategory()); | ||
return askQuestion(); | ||
} | ||
}; | ||
this.wasCorrectlyAnswered = function() { | ||
var winner; | ||
if (inPenaltyBox[currentPlayer]) { | ||
if (isGettingOutOfPenaltyBox) { | ||
console.log("Answer was correct!!!!"); | ||
purses[currentPlayer] += 1; | ||
console.log(players[currentPlayer] + " now has " + purses[currentPlayer] + " Gold Coins."); | ||
winner = didPlayerWin(); | ||
currentPlayer += 1; | ||
if (currentPlayer === players.length) { | ||
currentPlayer = 0; | ||
} | ||
return winner; | ||
} else { | ||
currentPlayer += 1; | ||
if (currentPlayer === players.length) { | ||
currentPlayer = 0; | ||
} | ||
return true; | ||
} | ||
} else { | ||
console.log("Answer was correct!!!!"); | ||
purses[currentPlayer] += 1; | ||
console.log(players[currentPlayer] + " now has " + purses[currentPlayer] + " Gold Coins."); | ||
winner = didPlayerWin(); | ||
currentPlayer += 1; | ||
if (currentPlayer === players.length) { | ||
currentPlayer = 0; | ||
} | ||
return winner; | ||
} | ||
}; | ||
this.wrongAnswer = function() { | ||
console.log("Question was incorrectly answered"); | ||
console.log(players[currentPlayer] + " was sent to the penalty box"); | ||
inPenaltyBox[currentPlayer] = true; | ||
currentPlayer += 1; | ||
if (currentPlayer === players.length) { | ||
currentPlayer = 0; | ||
} | ||
return true; | ||
}; | ||
return this; | ||
}; | ||
notAWinner = false; | ||
game = new Game(); | ||
game.add("Chet"); | ||
game.add("Pat"); | ||
game.add("Sue"); | ||
while (true) { | ||
game.roll(Math.floor(Math.random() * 6) + 1); | ||
if (Math.floor(Math.random() * 10) === 7) { | ||
notAWinner = game.wrongAnswer(); | ||
} else { | ||
notAWinner = game.wasCorrectlyAnswered(); | ||
} | ||
if (!notAWinner) { | ||
break; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head> | ||
<script type="text/javascript" src='./game.js'></script> | ||
</head> | ||
<body> | ||
<h1>JBrains Legacy Code Retreat</h1> | ||
<h3>Javascript implementation</h3> | ||
<p>Use Chrome console or Firefox's firebug</p> | ||
</body> | ||
</html> |