Permalink
Browse files

Merge pull request #1 from mlainez/master

Add javascript version to run with node
  • Loading branch information...
2 parents 3a1d453 + ba5c9bc commit a03d8f782b9654d62f0526e0097324cf7f266156 @jbrains committed May 25, 2012
Showing with 199 additions and 0 deletions.
  1. +189 −0 javascript/game.js
  2. +10 −0 javascript/index.html
View
@@ -0,0 +1,189 @@
+function Game() {
+ var players = new Array();
+ var places = new Array(6);
+ var purses = new Array(6);
+ var inPenaltyBox = new Array(6);
+
+ var popQuestions = new Array();
+ var scienceQuestions = new Array();
+ var sportsQuestions = new Array();
+ var rockQuestions = new Array();
+
+ var currentPlayer = 0;
+ var isGettingOutOfPenaltyBox = false;
+
+ var didPlayerWin = function(){
+ return !(purses[currentPlayer] == 6)
+ };
+
+ var 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;
+ };
+
+ for(var i = 0; i < 50; i++){
+ popQuestions.push("Pop Question "+i);
+ scienceQuestions.push("Science Question "+i);
+ sportsQuestions.push("Sports Question "+i);
+ rockQuestions.push(this.createRockQuestion(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;
+ };
+
+
+ var 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')
+ 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());
+ askQuestion();
+ }else{
+ console.log(players[currentPlayer] + " is not getting out of the penalty box");
+ 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());
+ askQuestion();
+ }
+ };
+
+ this.wasCorrectlyAnswered = function(){
+ if(inPenaltyBox[currentPlayer]){
+ if(isGettingOutOfPenaltyBox){
+ console.log('Answer was correct!!!!');
+ purses[currentPlayer] += 1;
+ console.log(players[currentPlayer] + " now has " +
+ purses[currentPlayer] + " Gold Coins.");
+
+ var 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.");
+
+ var 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;
+ };
+};
+
+var notAWinner = false;
+
+var game = new Game();
+
+game.add('Chet');
+game.add('Pat');
+game.add('Sue');
+
+do{
+
+ game.roll(Math.floor(Math.random()*6) + 1);
+
+ if(Math.floor(Math.random()*10) == 7){
+ notAWinner = game.wrongAnswer();
+ }else{
+ notAWinner = game.wasCorrectlyAnswered();
+ }
+
+}while(notAWinner);
View
@@ -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>

0 comments on commit a03d8f7

Please sign in to comment.