-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
122 lines (100 loc) · 2.65 KB
/
server.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
require "sinatra"
require 'pry'
use Rack::Session::Cookie, {
secret: "keep_it_secret_keep_it_safe"
}
def game(move)
human_choice = move
computer_choice = rand(3)
@player_score = 0
@computer_score = 0
# 0 = rock
# 1 = paper
# 2 = scissors
if computer_choice == 0 && human_choice == 'r'
return 'Player chose rock. Computer chose rock. Tie, choose again.'
end
if computer_choice == 0 && human_choice == 'p'
@player_score += 1
return 'Player chose paper. Computer chose rock. Paper beats rock,
Player wins the round.'
end
if computer_choice == 0 && human_choice == 's'
@computer_score += 1
return 'Player chose scissors. Computer chose rock.
Rock beats scissors, Computer wins the round.'
end
if computer_choice == 1 && human_choice == 'r'
@computer_score += 1
return 'Player chose rock. Computer chose paper.
Paper beats rock, Computer wins the round.'
end
if computer_choice == 1 && human_choice == 'p'
return 'Player chose paper. Computer chose paper. Tie, choose again.'
end
if computer_choice == 1 && human_choice == 's'
@player_score += 1
return 'Player chose scissors. Computer chose paper.
Scissors beats paper, Player wins the round.'
end
if computer_choice == 2 && human_choice == 'r'
@player_score += 1
return 'Player chose rock. Computer chose scissors.
Rock beats scissors, Player wins the round.'
end
if computer_choice == 2 && human_choice == 'p'
@computer_score += 1
return 'Player chose paper. Computer chose scissors.
Scissors beats paper, Computer wins the round.'
end
if computer_choice == 2 && human_choice == 's'
return 'Player chose scissors. Computer chose scissors. Tie, choose again.'
end
end
def computer_count
return @computer_score
end
def human_count
return @player_score
end
def winner(human,computer)
if human > computer
return "Player wins the game!"
else
return "Computer wins the game!"
end
end
get '/game' do
session[:gameplay]
session[:human]
session[:computer]
if session[:winner] != nil
redirect '/winner'
end
erb :index
end
get '/winner' do
session[:gameplay]
session[:human]
session[:computer]
session[:winner]
erb :show
end
post '/game' do
session[:gameplay] = game(params[:choice].downcase[0])
session[:human] ||= 0
session[:computer] ||= 0
session[:human] += human_count
session[:computer] += computer_count
if session[:computer] == 2 || session[:human] == 2
session[:winner] = winner(session[:human],session[:computer])
end
redirect '/game'
end
post '/winner' do
replay = params
if replay != nil
session.clear
redirect '/game'
end
end