Created
December 8, 2014 14:08
-
-
Save fholgado/459eee79d73812029089 to your computer and use it in GitHub Desktop.
Pong Score Tracker
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
var pong = {}; | |
// Flags | |
// Player 0, Player 1 scores :) | |
pong.score = [0, 0]; | |
// We'll use this flag to set first serve | |
pong.gameStart = false; | |
pong.currentlyServing = 0; | |
// Scoring functions | |
pong.increaseScore = function(player) { | |
// If game hasn't started, first button press sets serve | |
if (pong.gameStart === false) { | |
pong.currentlyServing = player; | |
pong.score = [0,0]; | |
pong.gameStart = true; | |
console.log('Starting game and setting serve to player ' + player); | |
} else { | |
pong.score[player] = pong.score[player] + 1; | |
console.log('point for player ' + player); | |
} | |
// Update current server upon every score | |
pong.currentlyServing = pong.calculateCurrentServer(pong.score, pong.currentlyServing); | |
// Let the game continue if we're in deuce | |
if (pong.scoreIsDeuce(pong.score)) { | |
console.log('Deuces!'); | |
if (Math.abs(pong.score[0] - pong.score[1]) === 2) { | |
pong.gameStart = false; | |
pong.announceGameWinner(pong.score); | |
return false; | |
} | |
} else { | |
// If not in deuce, let's end the game on 11 | |
if (pong.score[0] === 11 || pong.score[1] === 11) { | |
pong.gameStart = false; | |
pong.announceGameWinner(pong.score); | |
return false; | |
} | |
} | |
var data = { | |
score: pong.score, | |
serving: pong.currentlyServing | |
} | |
console.log('score: ' + pong.score[0] + ' - ' + pong.score[1]); | |
console.log('serving: Player ' + pong.currentlyServing); | |
// return data; | |
} | |
pong.scoreIsDeuce = function(score) { | |
if (score[0] + score[1] >= 21) return true; | |
} | |
pong.calculateGameWinner = function(score) { | |
return score.indexOf(Math.max.apply(Math, score)); | |
} | |
pong.calculateGameLoser = function(score) { | |
return score.indexOf(Math.min.apply(Math, score)); | |
} | |
pong.announceGameWinner = function(score) { | |
console.log('game over! Player ' + pong.calculateGameWinner(pong.score) + ' won ' + pong.score[pong.calculateGameWinner(pong.score)] + ' to ' + pong.score[pong.calculateGameLoser(pong.score)] + '.'); | |
} | |
pong.calculateCurrentServer = function(score, currentlyServing) { | |
// Otherwise, we have to flip first server to trick this | |
if (score[0] + score[1] === 0) { | |
console.log('starting game, set current server'); | |
return currentlyServing; | |
} | |
// Gotta change servers every point on deuce | |
if (pong.scoreIsDeuce(score)) { | |
if (currentlyServing === 0) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} else { | |
// Alternate serve every 2 points | |
if ((score[0] + score[1]) % 2 === 0) { | |
console.log('serve change detected!'); | |
if (currentlyServing === 0) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} else { | |
return currentlyServing; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment