-
Notifications
You must be signed in to change notification settings - Fork 0
/
ooptictactoe.rb
191 lines (153 loc) · 3.92 KB
/
ooptictactoe.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
require 'pry'
class Board
WINNING_LINES = [[1,2,3], [4,5,6], [7,8,9]] +
[[1,4,7], [2,5,8], [3,6,9]] +
[[1,5,9], [3,5,7]]
def initialize
@squares = {}
reset
end
def get_square_at(key)
@squares[key]
end
def set_square_at(key, marker)
@squares[key].marker = marker
end
def unmarked_keys
@squares.keys.select {|key| @squares[key].unmarked? }
end
def full?
unmarked_keys.empty?
end
def someone_won?
!!detect_winner
end
def count_human_marker(squares)
squares.collect(&:marker).count(TTTGame::HUMAN_MARKER)
end
def count_computer_marker(squares)
squares.collect(&:marker).count(TTTGame::COMPUTER_MARKER)
end
def detect_winner
WINNING_LINES.each do |line|
if count_human_marker(@squares.values_at(*line)) == 3
return TTTGame::HUMAN_MARKER
elsif count_computer_marker(@squares.values_at(*line)) == 3
return TTTGame::COMPUTER_MARKER
end
end
nil
end
def reset
(1..9).each {|key| @squares[key] = Square.new}
end
end
class Square
INITIAL_MARKER = " "
attr_accessor :marker
def initialize(marker=INITIAL_MARKER)
@marker = marker
end
def to_s
@marker
end
def unmarked?
marker == INITIAL_MARKER
end
end
class Player
attr_reader :marker
def initialize(marker)
@marker = marker
end
end
class TTTGame
HUMAN_MARKER = "X"
COMPUTER_MARKER = "O"
attr_reader :board, :human, :computer
def initialize
@board = Board.new
@human = Player.new(HUMAN_MARKER)
@computer = Player.new(COMPUTER_MARKER)
end
def display_welcome_message
puts "Welcome to Tic Tac Toe!"
puts ""
end
def display_goodbye_message
puts "Thanks for playing Tic Tac Toe! Goodbye!"
end
def display_board(clear = true)
system 'clear' if clear
puts "You're a #{human.marker}. Computer is a #{computer.marker}."
puts ""
puts " | |"
puts " #{board.get_square_at(1)} | #{board.get_square_at(2)} | #{board.get_square_at(3)}"
puts " | |"
puts "-----+-----+-----"
puts " | | "
puts " #{board.get_square_at(4)} | #{board.get_square_at(5)} | #{board.get_square_at(6)} "
puts " | | "
puts "-----+-----+-----"
puts " | |"
puts " #{board.get_square_at(7)} | #{board.get_square_at(8)} | #{board.get_square_at(9)}"
puts " | |"
puts ""
end
def human_moves
puts "Choose a square: #{board.unmarked_keys.join(', ')}:"
square = nil
loop do
square = gets.chomp.to_i
break if board.unmarked_keys.include?(square)
puts "Sorry, that's not a valid choice."
end
board.set_square_at(square, human.marker)
end
def computer_moves
board.set_square_at(board.unmarked_keys.sample, computer.marker)
end
def display_result
display_board
case board.detect_winner
when human.marker
puts "You won!"
when computer.marker
puts "Computer won!"
else
puts "It's a tie!"
end
end
def play_again?
answer = nil
loop do
puts "Would you like to play again? (Y/N)"
answer = gets.chomp.downcase
break if %w(y n).include? answer
puts "Sorry, must be y or n."
end
answer == 'y'
end
def play
system 'clear'
display_welcome_message
loop do
display_board(false)
loop do
human_moves
break if board.someone_won? || board.full?
computer_moves
break if board.someone_won? || board.full?
display_board
end
display_result
break unless play_again?
board.reset
system 'clear'
puts "Let's play again!"
end
display_goodbye_message
end
end
game = TTTGame.new
game.play