-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaying_greed.rb
More file actions
144 lines (121 loc) · 3.55 KB
/
playing_greed.rb
File metadata and controls
144 lines (121 loc) · 3.55 KB
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
#require 'highline/import'
$LOAD_PATH << File.dirname(__FILE__)
require 'about_scoring_project'
require 'about_dice_project'
class Player
attr_accessor :score
def initialize(score)
@score = score
self
end
end
class Turn
attr_accessor :accumulated_score, :player
attr_reader :player_index
def initialize(player, player_index)
@player = player
@accumulated_score = 0
@player_index = player_index
self
end
def accumulate(current_score)
@accumulated_score = (current_score == 0) ? current_score : @accumulated_score + current_score
return current_score == 0 ? false : true
end
def reset_accumulated_score
@accumulated_score = 0
end
def print_scores
puts "Score in this round:", @accumulated_score
puts "Total Score:", @player.score + @accumulated_score
end
def compute_player_score
@player.score = @player.score + @accumulated_score
end
def initial_minimum_score_unsatisfied?
if @player.score == 0 and @accumulated_score < 300
@accumulated_score = 0
return true
end
return false
end
def max_score_limit_reached?
@accumulated_score + @player.score >= 3000
end
end
class PlayingGreed
attr_accessor :players
def initialize
@players = []
end
def play_and_set_score(turn, n)
dice = DiceSet.new
dice.roll(n)
player_index = turn.player_index
puts "Player #{player_index + 1} rolls: ", dice.values.join(', ')
current_score = score(dice)
turn.accumulate(current_score)
if turn.max_score_limit_reached?
turn.print_scores
turn.compute_player_score
return true
end
scored_count = n - unscored_count(dice)
# Exit if the user scores 0 in current turn
if current_score == 0
turn.reset_accumulated_score
turn.print_scores
return false
end
turn.print_scores
if n == scored_count
puts "Do you want to roll the dices again?(y/n):"
roll_again = gets.chomp
return play_and_set_score(turn,5) if roll_again.strip.downcase == 'y'
elsif scored_count > 0 and n == 5
puts "Do you want to roll the non-scoring #{n - scored_count} dices?(y/n):"
roll_again = gets.chomp
return play_and_set_score(turn, n - scored_count) if roll_again.strip.downcase == 'y'
end
turn.print_scores if turn.initial_minimum_score_unsatisfied?
turn.compute_player_score
return false
end
def play
puts 'Enter number of players:'
no_of_players = gets.chomp.to_i
no_of_players.times do
players << Player.new(0)
end
ctr = 0
final_turn = false
reached_player = nil
max_scored_player = nil
while true
ctr += 1
puts "Turn #{ctr}\n----------"
for i in (0..no_of_players-1) do
player = players[i]
turn = Turn.new(player, i)
final_turn = play_and_set_score(turn, 5)
if final_turn
reached_player = i
max_scored_player = i
break
end
end
break if final_turn
end
puts "------------------\n------------------\n"
puts "Final turn begins now, all except player #{reached_player + 1} are going to roll dice !!\n"
puts "------------------\n------------------\n"
for i in (0..no_of_players-1) do
next if i == reached_player
player = players[i]
turn = Turn.new(player, i)
play_and_set_score(turn, 5)
max_scored_player = i if player.score > players[max_scored_player].score
end
puts "Player #{max_scored_player + 1} wins !!!, Player #{max_scored_player + 1}'s Score: ", players[max_scored_player].score
end
end