-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework2_java.rb
132 lines (115 loc) · 2.71 KB
/
homework2_java.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
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
require 'pry'
def ErrorMessage
puts "Sorry, unexpected case occurred."
end
class Player
# attr_accessor :name, :gesture
@name = nil
@gesture = nil
def get_gesture
@gesture
end
end
class Human < Player
def set_name
puts "What's your name?"
print "> "
@name = gets.chomp.capitalize
puts ""
end
def set_gesture
begin
puts "Please enter (R/P/S)."
print "#{@name}> "
@gesture = gets.chomp.upcase
puts ""
end while !["R", "P", "S"].include? @gesture
end
end
class Computer < Player
def set_name
@name ||= ["Amanda", "Bella", "Chloe"].shuffle.first
end
def set_gesture
@gesture = ["R", "P", "S"].shuffle.last
puts "#{@name}> #{@gesture}"
puts ""
end
end
class Game
@score
def initialize
@score = {"win" => 0, "deuce" => 0, "lose" => 0}
end
def introduce
puts "************************************"
puts "* *"
puts "* Let's play Rock Paper Scissors *"
puts "* *"
puts "************************************"
end
def judge(player1, player2)
case player1.get_gesture
when "R"
case player2.get_gesture
when "R" then result = "deuce"
when "P" then result = "lose"
when "S" then result = "win"
else ErrorMessage end
when "P"
case player2.get_gesture
when "R" then result = "win"
when "P" then result = "deuce"
when "S" then result = "lose"
else ErrorMessage end
when "S"
case player2.get_gesture
when "R" then result = "lose"
when "P" then result = "win"
when "S" then result = "deuce"
else ErrorMessage end
else
ErrorMessage
end
case result
when "win" then puts "You Win;)"
when "deuce" then puts "Deuse:o"
when "lose" then puts "You lose:("
else ErrorMessage end
puts ""
@score[result] += 1
end
def continue?
puts "===================================="
puts "Do you want to play again? (Y/N)"
print "> "
answer = gets.chomp.upcase
puts ""
answer == "Y"
end
def gameover
puts "Total:"
print " Win: #{@score["win"]}"
print " Deuce: #{@score["deuce"]}"
print " Lose: #{@score["lose"]}\n"
puts ""
puts "Bye, Have a nice day!"
puts ""
end
def play(player1, player2)
self.introduce
player1.set_name
player2.set_name
begin
player1.set_gesture
player2.set_gesture
self.judge(player1, player2)
end while self.continue?
self.gameover
end
end
############### main block ###############
shinohana_natsuki = Human.new
love_machine = Computer.new
game = Game.new
game.play(shinohana_natsuki, love_machine)