-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.rb
173 lines (125 loc) · 3.31 KB
/
tictactoe.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
class Player
attr_accessor :name, :symbol
def initialize(name, symbol)
@name = name
@symbol = symbol
end
end
class Board
WINS = [
[[0, 0], [1, 0],[2, 0]], [[0, 1],[1, 1], [2, 1]], [[0, 2], [1, 2], [2, 2]], # <-- Horizontal wins
[[0,0], [0, 1],[0,2]], [[1,0],[1,1],[1,2]], [[2,0],[2,1],[2,2]], # <-- Vertical wins
[[0,0], [1,1], [2,2]], [[2,0], [1,1], [0,2]], # <-- Diagonal wins
]
def initialize
@board = Array.new(3) { Array.new(3, ' ')}
end
def fill(horizontal, vertical, symbol)
@board[vertical][horizontal] = symbol
end
def get(horizontal, vertical)
return @board[vertical][horizontal]
end
def win?(player)
return WINS.any? { |line| line.all? { |square| @board[square[1]][square[0]] == player.symbol } }
end
def draw?
return @board.all? { |r| r.all? { |e| e != ' ' } }
end
def display
@board.each { |x|
puts x.join("|")
}
end
end
class Game
def play
intro
loop do
horizontal, vertical = receive_position
@board.fill(horizontal-1, vertical-1, @currentPlayer.symbol)
puts "Here is the board after #{@currentPlayer.name}'s turn."
@board.display
if @board.win?(@currentPlayer)
puts "#{@currentPlayer.name} wins!"
break
elsif @board.draw?
puts "It's a draw!"
break
end
@currentPlayer == @player1 ? @currentPlayer = @player2 : @currentPlayer = @player1
end
end
def intro
puts "Welcome to TicTacToe!"
player_one_input
player_two_input
puts "#{@player1.name} you are X, #{@player2.name} you are O."
puts "Here is the initial TicTacToe board."
@board = Board.new()
@board.display
@currentPlayer = @player1
end
def player_one_input
puts "Player 1, what is your name?:"
name = gets.chomp
while name.empty?
puts "Name can not be blank."
puts "Player 1, what is your name?:"
name = gets.chomp
end
@player1 = Player.new(name, 'X')
end
def player_two_input
puts "Player 2, what is your name?:"
name = gets.chomp
while name.empty?
puts "Name can not be blank."
puts "Player 2, what is your name?:"
name = gets.chomp
end
@player2 = Player.new(name, 'O')
end
def horizontal_input
puts "Where would you like to place your symbol horizontally (1-3), #{@currentPlayer.name}?"
horizontal = gets.chomp.to_i
verify_input(horizontal)
return horizontal
end
def vertical_input
puts "Where would you like to place your symbol vertically (1-3), #{@currentPlayer.name}?"
vertical = gets.chomp.to_i
verify_input(vertical)
return vertical
end
def position_taken?(horizontal, vertical)
return ['X', 'O'].include?(@board.get(horizontal-1, vertical-1))
end
def receive_position
horizontal = 0
vertical = 0
while (horizontal < 1 or horizontal > 3) and (vertical < 1 or vertical > 3)
while horizontal < 1 or horizontal > 3
horizontal = horizontal_input
end
while vertical < 1 or vertical > 3
vertical = vertical_input
end
if position_taken?(horizontal, vertical)
puts "There is already a symbol at horizontal: " + horizontal.to_s + " vertical: " + vertical.to_s
puts "Please choose a different location."
vertical = 0
horizontal = 0
end
end
return horizontal, vertical
end
private
def verify_input(input)
if input < 1 or input > 3
puts "Please choose a number between 1 and 3."
end
end
end
#game = Game.new
#game.play