-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise8.1.rb
More file actions
104 lines (81 loc) · 2.17 KB
/
Copy pathExercise8.1.rb
File metadata and controls
104 lines (81 loc) · 2.17 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
"""
Changes made
* Renamed the game_number variable in PlayGame to game_name to avoid confusion
* Realized that I can just get rid of that altogether
* Refactored the PlayGames class to only take a specific game object and play it
* Made sure all games had the same method names 'play_game' and 'get_results'
* Wondered why there was a GoPlayer class I didn't even see before
* Deleted GoPlayer
*
"""
class Poker
def initialize(players)
@players = players
@hands = []
players.length().times { |x| @hands.append(nil) }
end
def play_game()
puts "Players in the poker game:"
@players.length().times { |i| puts "#{self.get_player_name(i)}: #{self.get_player_hand(i)}" }
# [pretend there's code here]
end
def get_results()
return "[pretend these are poker results]"
end
def get_player_name(i)
return @players[i]
end
def get_player_hand(i)
return @hands[i]
end
end
class Chess
def initialize(players)
@players = players
end
def play_game()
puts "Players in the chess game:"
@players.length().times { |x| puts "#{self.get_player_name(x)}: #{@players[x][1]}" }
# [pretend there's code here]
end
def get_results()
return "[pretend these are chess results]"
end
def get_player_name(i)
@players[i][0]
end
end
class Go
def initialize(players)
@players = []
players.each { |x, y| @players.append(GoPlayer.new(x, y)) }
end
def play_game()
puts "Players in the go game:"
@players.each { |player| puts "#{player.name}: #{player.color}" }
# [pretend there's code here]
end
def get_results()
return "[pretend these are go results]"
end
end
class PlayGames
def initialize(game)
@game = game
end
def play()
play_game
puts @game.get_results
end
end
new_poker = Poker.new(["alice", "bob", "chris", "dave"])
pg = PlayGames.new(new_poker)
pg.play()
puts
new_chess = Chess.new([["alice", "white"], ["bob", "black"]])
pg = PlayGames.new(new_chess)
pg.play()
puts
new_go = Go.new([["alice", "white"], ["bob", "black"]])
pg = PlayGames.new(new_go)
pg.play()