-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.coffee
160 lines (119 loc) · 3.67 KB
/
script.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Author: Ben Yarbrough
# Requires Underscore.js - http://underscorejs.org
# Assign global scope to the root variable
root = exports ? this
# Create global App object
root.App ?= {}
#------------------
# Models
#------------------
App.Models ?= {}
# Immutable singleton
class App.Models.Rank
constructor: (@value) ->
letter: ->
'A23456789TJQK'.charAt(@value)
nextLower: ->
if @value is 0 then null else App.Models.ranks[@value - 1]
nextHigher: ->
if @value is 12 then null else App.Models.ranks[@value + 1]
# Immutable singleton
class App.Models.Suit
constructor: (@value) ->
letter: ->
'CDHS'.charAt(@value) # clubs, diamonds, hearts, spades
color: ->
if @letter() is 'C' or @letter() is 'S' then 'black' else 'red'
name: ->
if @letter() is 'C'
'clubs'
else if @letter() is 'D'
'diams'
else if @letter() is 'H'
'hearts'
else
'spades'
# Do not instantiate Rank and Suit; instead, use these:
App.Models.ranks = (new App.Models.Rank(i) for i in [0...13])
App.Models.suits = (new App.Models.Suit(i) for i in [0...4])
_nextId = 0
class App.Models.Card
constructor: (@rank, @suit) ->
@id = "id#{_nextId++}"
class App.Models.Game
constructor: (@numberOfPlayers = 4) ->
# Setup empty player hands, defaults to 4 players
@players = ([] for i in [0...@numberOfPlayers])
# Create a shuffled deck
# Uses Fischer-Yates algorithm - http://bwy.me/4c
# via Underscore.js - http://underscorejs.org/#shuffle
@deck = _.shuffle(@createDeck())
deal: ->
deckCopy = @deck.slice(0)
# Determine how many cards will be left-over
spareCards = deckCopy.length % @numberOfPlayers
# Deal the cards evenly
while deckCopy.length - spareCards
for i in [0...@players.length]
@players[i].push(deckCopy.pop())
# Stash the left-over cards
@spareCards = deckCopy
# Return players/hands/cards
@players
createDeck: ->
# Underscore's flatten function: http://underscorejs.org/#flatten
_.flatten(new App.Models.Card(rank, suit) \
for rank in App.Models.ranks \
for suit in App.Models.suits)
#------------------
# Views
#------------------
App.Views ?= {}
# View(s) will be appended to this element
App.rootElement = '#card-table'
# Loop through players and hands for card display
# <% %> ERB-style delimiters
# underscore's _.each function: http://underscorejs.org/#each
App.Views.table = "
<% _.each(hands, function(hand, player) { %>
<div class='well span5'>
<h2>Player <%= player + 1 %></h2>
<div class='hand'>
<% _.each(hand, function(card) { %>
<span class='card <%= card.suit.name() %>'>
<span class='rank'><%= card.rank.letter() %></span>
<span class='suit'>&<%= card.suit.name() %>;</span>
</span>
<% }); %>
</div>
</div>
<% }); %>
"
#------------------
# Controllers
#------------------
App.Controllers ?= {}
class App.Controllers.Play
constructor: (@numberOfPlayers = 4) ->
@model = new App.Models.Game(@numberOfPlayers)
@hands = @model.deal()
@rootElement = $(App.rootElement)[0]
@view = App.Views.table
setupTable: ->
# Make sure the table is clear
$(@rootElement).empty()
# Underscore templating: http://underscorejs.org/#template
@table = _.template(@view, {hands : @hands})
# Go!
$(@rootElement).append(@table)
#------------------
# Events
#------------------
$ ->
# Play Button
$('#play').click ->
# Get number of players from select box
players = $('#choose-players').val()
# Shuffle, deal, show
game = new App.Controllers.Play(players)
game.setupTable()