-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfarkle.rb
More file actions
163 lines (146 loc) · 4 KB
/
Copy pathfarkle.rb
File metadata and controls
163 lines (146 loc) · 4 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require_relative 'dice.rb'
require_relative 'player.rb'
require_relative 'asides.rb'
require_relative 'yesorno.rb'
require_relative 'winner.rb'
require_relative 'score.rb'
require_relative 'hotdice.rb'
class Farkle
attr_reader :players, :player_num, :winner, :ans
attr_accessor :roll, :asides, :hot_asides, :hotdice, :score, :current_player
def initialize
@winner = Winner.new
@ans = YesOrNo.new
@players = []
@roll = Dice.new
@asides = Asides.new
@hot_asides = Asides.new
@hotdice = HotDice.new
@score = Score.new
@current_player
@turn_finished
end
def get_player_num
begin
puts "Enter the number of players (1 or more): "
@player_num = gets.to_i
end while @player_num < 1
# loop until user enters name of all players
end
def get_player_names
i = 0
while (i < @player_num)
puts "Enter the name of player #{(i+1)}: "
@players << Player.new(gets.chomp)
i += 1
end
end
def set_up_players
get_player_num
get_player_names
end
def show
roll.show_dice
asides.show_asides
puts "Your dice set aside from hot dice are: #{hot_asides.asides.to_s}" if hot_asides.has_asides?
score.score_dice(roll.dice + asides.asides + hot_asides.asides)
score.show_score
puts "Your total score will be: #{current_player.score + score.score}"
end
def scored
# if they set aside all but one die
if roll.dice.length == 1
roll.show_dice
asides.show_asides
end&& roll.dice.length > 1
puts "You scored #{score.score.to_s} points this round!"
end
def farkled
# show dice and asides so user can see that they farkled
asides.show_asides
roll.show_dice
puts "Farkle! You've lost all points for this round."
# set total score to 0
score.score = 0
turn_finished = true
end
def hot_diced
hotdice.hot_dice(roll, asides, hot_asides)
score.score_dice(roll.dice + hot_asides.asides + asides.asides)
end
def end_turn
# check if when turn ended they had points, to tell them
# how much they scored
if roll.has_points?
scored
# if no points then they farkled
else
farkled
end
puts "Press enter to continue: "
gets.chomp
puts "\n\n\n"
# add score to player's total
current_player.score += score.score
# clear asides and hot_asides for next turn
asides.clear
hot_asides.clear
end
def set_aside
# prompt user if they would like to set aside dice
puts "in asides"
asides.ask_user
if ans.yes?
asides.set_aside(roll.dice)
# roll remaining dice (dice that aren't set aside)
roll.roll(6-asides.asides.length)
else
@turn_finished = true
end
end
def turn
puts "It is now #{current_player.name}'s turn"
roll.roll(6)
@turn_finished = false
# while user has not farkled and has more than 1 die
while roll.has_points? and roll.dice.length > 1 and !@turn_finished
# show dice, asides, score, total score
show
# check for hot dice
if roll.hot_dice?
# run hot dice program
hot_diced
# if user wants to cash out, end turn, if not, continue normally
farkled unless roll.has_points?
if asides.has_asides? || hot_asides.has_asides?
set_aside
else
@turn_finished = true
end
else
set_aside
end
end
end_turn
end
def play
# play until user wants to stop
begin
# placeholder to output score of each turn
set_up_players
# loop until a user has won (gets over 10000 points)
while (winner.max_score(players) < 10000)
# iterate through all the players
players.each do |player|
@current_player = player
turn
end
end
# output congratulations message
puts "Congratulations, #{winner.winner(players)} has won!"
puts "Would you like to play a new game?"
end while (ans.yes?)
puts "Thanks for playing! I hope you enjoyed"
end
end
Farkle.new.play