Last active
March 2, 2017 19:18
-
-
Save dustMason/4514528 to your computer and use it in GitHub Desktop.
Simple console-based Letters and Numbers game – only the "Numbers" version.
Requires node.js, coffeescript module and prompt (`npm install -g prompt`)
Run with `coffee game.coffee`
This file contains hidden or 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
prompt = require('prompt') | |
class Player | |
constructor: (@name,@deck)-> | |
@cards = [] | |
@ops = ["+","-","*","/"] | |
@chooseCards() | |
chooseCards: (slots=6)-> | |
for slot in [0..slots-1] | |
i = Math.floor(Math.random()*@deck.length) | |
card = @deck[i] | |
@cards.push card | |
@deck.splice(i,1) | |
playNumber: (num)-> | |
num = parseInt(num) | |
if @cards.indexOf(num) > -1 | |
@equation.addCard(num) | |
@cards.splice(@cards.indexOf(num),1) | |
true | |
else | |
false | |
playOperator: (op)-> | |
if @ops.indexOf(op) > -1 | |
@equation.addCard(op) | |
true | |
else | |
false | |
playCard: (card,type)-> | |
if type == "number" then @playNumber(card) else @playOperator(card) | |
printCards: -> @printArray(@cards) | |
printOps: -> @printArray(@ops) | |
printArray: (array)-> | |
out = "" | |
out += "[#{item}] " for item in array | |
console.log out | |
class NumberRound | |
constructor: -> | |
@big = [25,50,75,100] | |
@small = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10] | |
@goalNumber = Math.floor(Math.random() * 999) + 1 | |
@players = [] | |
@currentPlayer = 0 | |
addPlayer: (name)-> | |
deck = @big.concat @small | |
player = new Player(name, deck) | |
player.equation = new MathEquation() | |
@players.push(player) | |
turn: -> | |
player = @players[@currentPlayer] | |
console.log "Your turn, #{player.name}. Try to reach #{@goalNumber}" | |
console.log player.equation.cards.join(" ") + " = " + player.equation.total | |
if player.equation.length() % 2 == 0 | |
q = "Choose a number:" | |
type = "number" | |
player.printCards() | |
else | |
q = "Choose an operation:" | |
type = "operation" | |
player.printOps() | |
console.log q | |
prompt.start() | |
prompt.get([type], (err, result) => | |
player.playCard(result[type],type) | |
if @currentPlayer+1 == @players.length then @currentPlayer = 0 else @currentPlayer += 1 | |
@turn() | |
) | |
class MathEquation | |
constructor: -> | |
@cards = [] | |
@total = 0 | |
@eq = "" | |
addCard: (card)-> | |
@cards.push card | |
@calculate() | |
length: -> @cards.length | |
calculate: -> | |
if @cards.length > 1 && @cards.length % 2 == 1 | |
@eq = "" | |
parensCount = Math.floor(@cards.length/2) - 1 | |
@eq += "(" for i in [0..parensCount] | |
@eq += @cards[0] | |
for card,i in @cards[1..-1] | |
@eq += card | |
@eq += ")" if i % 2 == 1 | |
@total = eval(@eq) | |
r = new NumberRound() | |
r.addPlayer("Jordan") | |
r.addPlayer("Ruben") | |
r.addPlayer("Cyrelle") | |
r.turn() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment