forked from nickb/yahtzee
-
Notifications
You must be signed in to change notification settings - Fork 1
/
yahtzee.rb
executable file
·77 lines (67 loc) · 2.31 KB
/
yahtzee.rb
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
#!/usr/bin/ruby
require_relative 'die.rb'
require_relative 'player.rb'
require_relative 'round.rb'
class YahtzeeGame
def initialize(players = 1)
@dice = Array.new(5) {Die.new}
@no_of_players = players
@curr_player_no = 0
@players = Array.new(@no_of_players) { Player.new(self) }
@rolls = 0
end
def roll(reroll_dice = [0,1,2,3,4])
if (@rolls < 3)
reroll_dice.map {|n| @dice[n] }.each do | die | die.roll end
@rolls = @rolls + 1
else
raise "You've had 3 rolls - choose a category to score this turn"
end
end
def end_turn
@rolls = 0
@curr_player_no = (@curr_player_no + 1) % @no_of_players
end
def curr_player
@players[@curr_player_no]
end
def curr_player_no
@curr_player_no + 1
end
def dice
@dice.map {|d| d.num}
end
def over?
[email protected]? {|p| !p.finished?}
end
end
key_move_mappings = {"1" => :ones, "2" => :twos, "3" => :threes,
"4" => :fours, "5" => :fives, "6" => :sixes,
"s" => :small_straight, "l" => :large_straight,
"t" => :three_of_a_kind, "f" => :four_of_a_kind,
"h" => :full_house, "y" => :yahtzee, "?" => :chance};
players = ARGV.first.to_i || 1
players = 1 if (players <= 0)
game = YahtzeeGame.new(players)
puts "Beginning Yahtzee game with #{players} player#{players != 1 ? "s" : ""}"
until (game.over?)
puts "Player #{game.curr_player_no}'s turn"
player = game.curr_player
player.start_turn
puts "Your first roll is #{game.dice.join(', ')}.\nEnter the numbers of the dice you want to reroll (e.g. \"12345\" for all dice)"
command = $stdin.gets
reroll_dice = Array.new
command.each_byte {|s| if (s >= 49 && s < 54) then reroll_dice << s-49 end }
player.refine(reroll_dice)
puts "Your second roll is #{game.dice.join(', ')}.\nEnter the numbers of the dice you want to reroll (e.g. \"12345\" for all dice)"
command = $stdin.gets
reroll_dice.clear
command.each_byte {|s| if (s >= 49 && s < 54) then reroll_dice << s-49 end }
player.refine(reroll_dice)
puts "Your final roll is #{game.dice.join(', ')}.\nEnter your move - one of: 123456tfslh?y"
score = -1
score = player.score(key_move_mappings[$stdin.gets.strip]) until (score != -1)
puts "You scored #{score} in that move"
player.print_board
end
puts "The winner is player #{game.curr_player_no} with a score of #{player.total_score}"