-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
135 lines (99 loc) · 3.88 KB
/
app.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
133
134
135
require 'sinatra'
require 'rubygems'
require_relative 'board.rb'
require_relative 'human.rb'
require_relative 'random_ai.rb'
require_relative 'sequential_ai.rb'
require_relative 'unbeatable_ai.rb'
enable :sessions
ai = ""
get '/' do
session[:board] = Board.new
erb :home, :locals => { :board => session[:board].board_positions }
end
get '/player_1_name' do
erb :player_1_name, :locals => { :board => session[:board].board_positions }
end
post '/player_1_name' do
session[:player_1_name] = params[:player_1]
session[:player_1] = Human.new("x")
session[:current_player] = session[:player_1]
erb :opponent, :locals => { :board => session[:board].board_positions, :player_1_name => session[:player_1_name] }
# redirect '/choose_opponent'
end
post '/choose_opponent' do
player_2 = params[:player_2]
if player_2 == "human"
session[:player_2] = Human.new("o")
erb :player_2_name, :locals => { :board => session[:board].board_positions }
session[:player_2_name] = params[:player_2]
redirect 'player_2_name'
elsif player_2 == "sequential_ai"
session[:player_2] = SequentialAI.new("o")
session[:player_2_name] = "Easy Opponent"
redirect '/get_move'
elsif player_2 == "random_ai"
session[:player_2] = RandomAI.new("o")
session[:player_2_name] = "Medium Opponent"
redirect '/get_move'
else player_2 == "unbeatable_ai"
session[:player_2] = UnbeatableAI.new("o")
session[:player_2_name] = "Hard Opponent"
redirect '/get_move'
end
end
get '/player_2_name' do
erb :player_2_name, :locals => { :board => session[:board].board_positions }
end
post '/player_2_name' do
session[:current_player_name] = session[:player_1_name]
redirect '/get_move'
end
get '/get_move' do
move = session[:current_player].get_move(session[:board].board)
if move == "NO"
erb :get_move, :locals => { :current_player => session[:current_player], :current_player_name => session[:current_player_name], :board => session[:board].board_positions }
elsif session[:board].valid_space?(move)
redirect '/make_move' #+ move.to_s
else
redirect '/get_move'
end
end
post '/get_player_move' do
move = params[:square].to_i
# puts "move is #{move}"
if session[:board].valid_space?(move)
redirect '/make_move?move=' + move.to_s
else
redirect '/get_move'
end
end
get '/make_move' do
move = params[:move].to_i
puts "move is #{move}"
session[:board].update((move - 1), session[:current_player].marker)
erb :get_move, :locals => { :current_player => session[:current_player], :current_player_name => session[:current_player_name], :board => session[:board].board_positions }
if session[:board].winner?(session[:current_player].marker) == true
player_1 = session[:player_1_name]
player_2 = session[:player_2_name]
winner = session[:current_player_name]
erb :win, :locals => { :current_player => session[:current_player], :current_player_name => session[:current_player_name], :board => session[:board].board_positions }
elsif session[:board].full_board? == true
player_1 = session[:player_1_name]
player_2 = session[:player_2_name]
winner = "Tie"
erb :tie, :locals => { :board => session[:board].board_positions }
else redirect '/change_player'
end
end
get '/change_player' do
if session[:current_player].marker == "X"
session[:current_player] = session[:player_2]
session[:current_player_name] = session[:player_2_name]
else
session[:current_player] = session[:player_1]
session[:current_player].marker = "X"
session[:current_player_name] = session[:player_1_name]
end
erb :get_move, :locals => { :current_player => session[:current_player], :current_player_name => session[:current_player_name], :board => session[:board].board_positions }
end