Skip to content

Commit 6ea950e

Browse files
committed
refactor - a lot
1 parent e53d60c commit 6ea950e

File tree

1 file changed

+72
-79
lines changed

1 file changed

+72
-79
lines changed

Diff for: tictactoe.rb

+72-79
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,61 @@
11
class TTT
2-
def initialize
3-
@three_board = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
4-
@flat_3_board = @three_board.flatten
5-
6-
@four_board = [[1, 2, 3, 4], [5, 6, 7, 8],
7-
[9, 10, 11, 12], [13, 14, 15, 16]]
8-
@flat_4_board = @four_board.flatten
9-
10-
@p1, @p2, @computer, @human, @turn, @choice = nil
11-
@board = @three_board
2+
def initialize
3+
@player_1, @player_2, @computer, @human, @turn, @dimension, @board = nil
124
end
135

146
def print_three_board
15-
puts "\n #{@flat_3_board[0]} | #{@flat_3_board[1]} | #{@flat_3_board[2]} ",
7+
puts "\n #{@board[0]} | #{@board[1]} | #{@board[2]} ",
168
"----------",
17-
" #{@flat_3_board[3]} | #{@flat_3_board[4]} | #{@flat_3_board[5]} ",
9+
" #{@board[3]} | #{@board[4]} | #{@board[5]} ",
1810
"----------",
19-
" #{@flat_3_board[6]} | #{@flat_3_board[7]} | #{@flat_3_board[8]} "
11+
" #{@board[6]} | #{@board[7]} | #{@board[8]} "
2012
end
2113

2214
def print_four_board
23-
puts "\n #{@flat_4_board[0]} | #{@flat_4_board[1]} | #{@flat_4_board[2]} | #{@flat_4_board[3]} ",
15+
puts "\n #{@board[0]} | #{@board[1]} | #{@board[2]} | #{@board[3]}",
2416
"-------------------",
25-
" #{@flat_4_board[4]} | #{@flat_4_board[5]} | #{@flat_4_board[6]} | #{@flat_4_board[7]} ",
17+
" #{@board[4]} | #{@board[5]} | #{@board[6]} | #{@board[7]}",
2618
"-------------------",
27-
" #{@flat_4_board[8]} | #{@flat_4_board[9]} | #{@flat_4_board[10]} | #{@flat_4_board[11]} ",
19+
" #{@board[8]} | #{@board[9]} | #{@board[10]} | #{@board[11]} ",
2820
"-------------------",
29-
" #{@flat_4_board[12]} | #{@flat_4_board[13]} | #{@flat_4_board[14]} | #{@flat_4_board[15]} "
21+
" #{@board[12]} | #{@board[13]} | #{@board[14]} | #{@board[15]} "
22+
end
23+
24+
def available?(box)
25+
box.class == Fixnum
3026
end
3127

32-
update_board = lambda do |flat_board, player, place|
33-
if flat_board[place - 1].class == Fixnum
34-
flat_board[place - 1] = player
35-
@board = flat_board.each_slice(@choice).to_a
28+
def update_board(player, place)
29+
if available?(@board[place - 1])
30+
@board[place - 1] = player.rjust(place.to_s.length)
3631
else
3732
prompt_new_box(player)
3833
end
39-
4034
determine_game_over(player)
4135
end
4236

37+
def yes?
38+
["Y","y"].include?(gets.chomp)
39+
end
40+
4341
def display_menu
44-
# choose board size
4542
print "Would you like to play on a 3 x 3 board or a 4 x 4 board?\n"
4643
print "Enter '3' for the former or '4' for the latter: "
47-
@choice = gets.chomp.to_i
48-
if @choice == 4
49-
@board = @four_board
50-
elsif @choice != 3 && @choice != 4
51-
@choice == 3
44+
@dimension = gets.chomp.to_i
45+
if @dimension == 3
46+
@board = (1..9).to_a
47+
elsif @dimension == 4
48+
@board = (1..16).to_a
49+
else
50+
@dimension = 3
51+
@board = (1..9).to_a
5252
print "That's not a valid choice, so we'll just default to a 3 x 3 board.\n"
5353
end
5454

55-
# choose whether to play against computer
5655
print "Would you like to play against the computer? (Y/N) "
57-
if ["Y","y"].include?(gets.chomp)
56+
if yes?
5857
print "Would you like to play first? (Y/N) "
59-
["Y","y"].include?(gets.chomp) ? @computer = 0 : @computer = 1
58+
yes? ? @computer = 0 : @computer = 1
6059
relay_computer_human_turns
6160
else
6261
print "OK, let's proceed with two human players...\n"
@@ -65,37 +64,37 @@ def display_menu
6564
end
6665

6766
def computer_move
67+
computer_think
6868
# sample of all available spaces within @board
69-
@place = @board.flatten.select{ |i| i.class == Fixnum }.sample
70-
@choice == 3 ? update_board.call(@flat_3_board, @computer, @place) :
71-
update_board.call(@flat_4_board, @computer, @place)
69+
@place = @board.flatten.select{ |i| available?(i) }.sample
70+
update_board(@computer, @place)
7271
end
7372

7473
def computer_think
7574
print "\nTHE ALMIGHTY COMPUTER IS CONTEMPLATING ITS MOVE...\n"
7675
3.times do
77-
sleep(1) # sleep for 1 second
76+
sleep(1)
7877
print "\nCOMPUTING...\n"
7978
end
8079
print "\nWHAM!\n"
8180
sleep(1)
8281
end
8382

8483
def relay_computer_human_turns
85-
@choice == 3 ? print_three_board : print_four_board
84+
@dimension == 3 ? print_three_board : print_four_board
8685

8786
if @computer == 0 && @human.nil? # human plays first move of the game
8887
print "\nHuman, do you want to play an X or an O? "
8988
confirm_x_or_o
9089
elsif @computer == 1 # computer plays first move of the game
9190
@computer = ["X","O"].sample
9291
computer_move
93-
elsif @human.nil? # human plays second (human's first move)
92+
elsif @human.nil? # human plays second (this is human's first move)
9493
@computer == "O" ? @human = "X" : @human = "O"
9594
print "\nWatch out, Human! Computer has made its first move!"
9695
print "\nIn which box would you like to place your #{@human}? "
9796
mark(@human)
98-
elsif @computer == 0 # computer plays second (computer's first move)
97+
elsif @computer == 0 # computer plays second (this is computer's first move)
9998
@human == "O" ? @computer = "X" : @computer = "O"
10099
computer_move
101100
elsif @turn == @human # human's subsequent move
@@ -107,28 +106,28 @@ def relay_computer_human_turns
107106
end
108107

109108
def relay_player_turns
110-
@choice == 3 ? print_three_board : print_four_board
109+
@dimension == 3 ? print_three_board : print_four_board
111110

112-
if @p1.nil?
111+
if @player_1.nil?
113112
print "\nPlayer 1, make your move!\nDo you want to play an X or an O? "
114113
confirm_x_or_o
115-
elsif @p2.nil?
116-
@p1 == "O" ? @p2 = "X" : @p2 = "O"
117-
print "\nPlayer 2, make your move! In which box would you like to place your #{@p2}? "
118-
mark(@p2)
119-
elsif @turn == @p1
120-
print "\nPlayer 1, back to you! In which box would you like to place your #{@p1}? "
121-
mark(@p1)
114+
elsif @player_2.nil?
115+
@player_1 == "O" ? @player_2 = "X" : @player_2 = "O"
116+
print "\nPlayer 2, make your move! In which box would you like to place your #{@player_2}? "
117+
mark(@player_2)
118+
elsif @turn == @player_1
119+
print "\nPlayer 1, back to you! In which box would you like to place your #{@player_1}? "
120+
mark(@player_1)
122121
else
123-
print "\nPlayer 2, back to you! In which box would you like to place your #{@p2}? "
124-
mark(@p2)
122+
print "\nPlayer 2, back to you! In which box would you like to place your #{@player_2}? "
123+
mark(@player_2)
125124
end
126125
end
127126

128127
def confirm_x_or_o
129128
if @computer.nil?
130-
@p1 = gets.chomp
131-
player = @p1
129+
@player_1 = gets.chomp
130+
player = @player_1
132131
else
133132
@human = gets.chomp
134133
player = @human
@@ -146,73 +145,67 @@ def confirm_x_or_o
146145

147146
def mark(player)
148147
@place = gets.chomp.to_i
149-
if (@choice == 3 && (1..9).include?(@place)) ||
150-
(@choice == 4 && (1..16).include?(@place))
151-
@choice == 3 ? update_board.call(@flat_3_board, player, @place) :
152-
update_board.call(@flat_4_board, player, @place)
148+
if (@dimension == 3 && (1..9).include?(@place)) ||
149+
(@dimension == 4 && (1..16).include?(@place))
150+
update_board(player, @place)
153151
else
154152
print "Sir / Madam, please choose a valid number: "
155153
mark(player)
156154
end
157155
end
158156

159157
def prompt_new_box(player)
160-
@choice == 3 ? print_three_board : print_four_board
158+
@dimension == 3 ? print_three_board : print_four_board
161159
print "\nSTOP in the name of the law! That box is occupied - please choose another one. "
162160
mark(player)
163161
end
164162

165163
def determine_game_over(player)
166164
if victory?
167-
if @turn == @computer
168-
computer_think
169-
end
170-
171-
@choice == 3 ? print_three_board : print_four_board
165+
@dimension == 3 ? print_three_board : print_four_board
172166

173-
if @turn == @p1 || @turn == @p2 || @turn == @human
167+
if @turn == @player_1 || @turn == @player_2 || @turn == @human
174168
puts "\nCONGRATULATIONS! You have vanquished your foe!\n\n(╯°□°)╯︵ ┻━┻\n\n"
175169
else
176170
puts "\nPUNY HUMAN, ACCEPT DEFEAT!"
177171
end
178172
restart_request
179-
elsif (@board.all?{|row| row.all? {|i| i.class == String} } && !victory?)
173+
elsif (@board.none?{|box| available?(box)} && !victory?)
180174
puts "Messieurs, mesdames - you have come to a draw."
181175
restart_request
182176
else
183177
if @computer.nil?
184-
player == @p1 ? @turn = @p2 : @turn = @p1
178+
player == @player_1 ? @turn = @player_2 : @turn = @player_1
185179
relay_player_turns
186180
else
187181
player == @computer ? @turn = @human : @turn = @computer
188-
if @turn == @human
189-
computer_think
190-
end
191182
relay_computer_human_turns
192183
end
193184
end
194185
end
195186

196187
def victory?
197-
reverse_board = @board.map{ |a| a.reverse }
198-
@board.any?{ |row| row.uniq.size == 1 } ||
199-
@board.transpose.any?{ |row| row.uniq.size == 1 } ||
200-
(@choice == 3 && (0..2).map{ |i| @board[i][i] }.uniq.size == 1) ||
201-
(@choice == 3 && (0..2).map{ |i| reverse_board[i][i] }.uniq.size == 1) ||
202-
203-
(@choice == 4 && (0..3).map{ |i| @board[i][i] }.uniq.size == 1) ||
204-
(@choice == 4 && (0..3).map{ |i| reverse_board[i][i] }.uniq.size == 1)
188+
sliced_board = @board.each_slice(@dimension).to_a
189+
reversed_sliced_board = sliced_board.map{ |a| a.reverse }
190+
191+
sliced_board.any?{ |row| row.uniq.size == 1 } ||
192+
sliced_board.transpose.any?{ |row| row.uniq.size == 1 } ||
193+
(@dimension == 3 && (0..2).map{ |i| sliced_board[i][i] }.uniq.size == 1) ||
194+
(@dimension == 3 && (0..2).map{ |i| reversed_sliced_board[i][i] }.uniq.size == 1) ||
195+
(@dimension == 4 && (0..3).map{ |i| sliced_board[i][i] }.uniq.size == 1) ||
196+
(@dimension == 4 && (0..3).map{ |i| reversed_sliced_board[i][i] }.uniq.size == 1)
205197
end
206198

207199
def restart_request
208200
print "Would you like to play again? (Y/N) "
209-
if ["Y","y"].include?(gets.chomp)
210-
initialize
211-
display_menu
201+
if ["N","n"].include?(gets.chomp)
202+
exit(0)
212203
end
213204
end
214205
end
215206

216207
if __FILE__ == $0
217-
TTT.new.display_menu
208+
loop do
209+
TTT.new.display_menu
210+
end
218211
end

0 commit comments

Comments
 (0)