-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgame.rb
171 lines (141 loc) · 3.76 KB
/
game.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
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
164
165
166
167
168
169
require 'pry'
class Game
attr_accessor :player, :computer_opponent, :board, :current_player, :other_player
@@greeting_message = 'Welcome, the instruction to Tic-Tac-Toe are as followed:
To answer please use 1-9, which corresponds to a space of
the board.'
@@goodbye_message = "Thank you for playing!"
def initialize #X
@board = Board.new
@player = nil
@computer_opponent = nil
@current_player = nil
@other_player = nil
end
def self.greeting_message #X
@@greeting_message
end
def self.goodbye_message #X
@@goodbye_message
end
def prompt_for_name #X
puts "Enter name: "
end
def prompt_for_token #X
puts "X or O?"
end
def get_player #X
prompt_for_name
@player = Player.find_or_create_by_name(gets.chomp)
@current_player = @player
prompt_for_token
@player.set_player_token
end
def get_opponent #X
@opponent = ComputerPlayer.new
@other_player = @opponent#can change this to add 2 players
@opponent.set_computer_token(@player)
end
def get_player_and_opponent #X
get_player
get_opponent
end
def switch_turns #X
@current_player, @other_player = @other_player, @current_player
end
def prompt_player_for_move
puts "Enter a number from 1 to 9"
move = (gets.chomp.to_i - 1)
while !self.board.empty_square?(move) && ![0..8].include?(move)
#repeat input process
puts "Enter a number from 1 to 9"
move = gets.chomp.to_i
end
self.player.add_move(move)
end
def set_move #X
if @current_player.is_a?(Player)
prompt_player_for_move
elsif @current_player.is_a?(ComputerPlayer)
move = @current_player.computer_move
while !check_computer_move(move)
move = @current_player.computer_move
end
@current_player.moves << move
end
end
def check_computer_move(move) #X
self.board.empty_square?(move)
end
def win_message #X
puts "#{@current_player.name} has won!"
end
def tie_message #X
puts "Tie Game!"
end
def game_board_status #X
@board.status #=> open or closed
end
def win_sequence
@current_player.update_record_win
@other_player.update_record_loss
self.board.close_board
self.board.print_board
self.win_message
end
def tie_sequence
@current_player.update_record_tie
@other_player.update_record_tie
self.board.close_board
self.board.print_board
tie_message
end
def check_for_win_or_tie
#binding.pry
if self.board.check_for_win
self.win_sequence
elsif self.board.check_for_tie
self.tie_sequence
end
end
def one_round #X
self.board.print_board
self.set_move
self.board.check_and_set_square(@current_player.moves.last, @current_player)
self.check_for_win_or_tie
#binding.pry
self.switch_turns
#binding.pry
end
def game_loop #X
while game_board_status == "open"
self.one_round
end
end
end
######
# def self.play_again?
# puts "Would you like to play again? (Y/N)"
# input = gets.upcase.chomp
# until (input == "Y") || (input == "N")
# puts "Invalid entry. Please select 'Y' or 'N'"
# input = gets.upcase.chomp
# end
# input == "Y" ? @play_again = true : @play_again = false
# end
######
# def valid_move?
# [0..8].include?(@move) && self.board.empty_square?(@move) ? true : false
# end
# if @current_player.class == Player
# @move = prompt_player_for_move # => returns 8
# #prompt_player should have a built in loop
# #to ensure value is between 1-9 or 0-8
# else
# until self.board.empty_square?(@move) ###need to refactor?
# @move = @current_player.computer_move
# #SHOULD keep randomly selecting a number
# #until that number passes empty_square?
# end ##UNTIL
# end ## IF
# @move