/
tictactoe_class.rb
191 lines (177 loc) · 3.66 KB
/
tictactoe_class.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# The general class for the game
class TicTacToe
# Possibilities of winning
WIN = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]
# Introduces the game and begins the first round
def initialize(input_given = nil)
@row1 = " | | "
@row2 = " | | "
@row3 = " | | "
@array_X = []
@array_O = []
@input_given = input_given
introduction unless input_given
end
def introduction
puts "You have initiated a game of Tic-Tac-Toe!"
puts "When choosing a spot on the board, use the"
puts "numbers 1 through 9 like so:"
puts "\n"
puts " 1 | 2 | 3 \n-----------"
puts " 4 | 5 | 6 \n-----------"
puts " 7 | 8 | 9 \n\n"
puts "X goes first."
turn("X")
end
# Initiates a player's turn
def turn(piece)
puts "\nChoose position for #{piece}, or ask to 'display'"
puts "the board, or ask for 'help', or ask to 'exit':"
position = STDIN.gets.chomp.strip.downcase
if position == "help"
help
turn(piece)
elsif position == "display"
display
turn(piece)
elsif position == "exit"
puts "Goodbye!"
elsif /\d/ =~ position
transition(position,piece)
display
if piece == "X"
piece = "O" unless win?(piece)
elsif piece == "O"
piece = "X" unless win?(piece)
end
if win?(piece)
puts "#{piece}'s win!"
play_again_input
elsif all_spots_full?
puts "It's a draw!"
play_again_input
else
turn(piece)
end
else
puts "\nNot a valid entry. Try again."
turn(piece)
end
end
def transition(position,piece)
position = position.to_i
if spot_empty?(position)
place_piece(position,piece)
puts "\nYour #{piece} piece was placed in position #{position}."
end
end
# Checks if the spot chosen on the gameboard is empty
def spot_empty?(position)
case position
when 1
spot = @row1[1]
when 2
spot = @row1[5]
when 3
spot = @row1[9]
when 4
spot = @row2[1]
when 5
spot = @row2[5]
when 6
spot = @row2[9]
when 7
spot = @row3[1]
when 8
spot = @row3[5]
when 9
spot = @row3[9]
end
if spot =~ /\s/
true
else
false
end
end
# Places a piece in the chosen position
def place_piece(position,piece)
case position
when 1
@row1[1] = piece
when 2
@row1[5] = piece
when 3
@row1[9] = piece
when 4
@row2[1] = piece
when 5
@row2[5] = piece
when 6
@row2[9] = piece
when 7
@row3[1] = piece
when 8
@row3[5] = piece
when 9
@row3[9] = piece
end
if piece == "X"
@array_X.push(position)
elsif piece == "O"
@array_O.push(position)
end
end
# Displays the game board
def display
puts "\n"
puts @row1
puts "-----------"
puts @row2
puts "-----------"
puts @row3
puts "\n"
end
# When player calls for help, displays guide gameboard
def help
puts "\n"
puts " 1 | 2 | 3 \n-----------"
puts " 4 | 5 | 6 \n-----------"
puts " 7 | 8 | 9 \n\n"
end
# Checks if the current player has won
def win?(piece)
if piece == "X"
current_array = @array_X
elsif piece == "O"
current_array = @array_O
end
WIN.each do |possibility|
if possibility - current_array == []
return true
end
end
false
end
# Gives the option of playing again
def play_again_input
puts "Play again? (type 'yes' or 'no')"
response = STDIN.gets.chomp.strip.downcase
unless response == "yes" || response == "no"
puts "\nNot a valid entry. Try again."
play_again_input
end
play_again(response)
end
def play_again(response)
if response == "yes"
puts "\n\n"
initialize(@input_given)
elsif response == "no"
puts "Goodbye!"
end
end
# Checks if all the spots on the gameboard have been filled
def all_spots_full?
true if @array_X.size == 5
end
end