-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe.rb
133 lines (116 loc) · 3.55 KB
/
tic-tac-toe.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
class Game
@@turn_count = 1
@@winner = ''
def initialize
puts 'Player 1 - enter your name'
@player_one_name = gets.chomp
puts 'Player 2 - enter your name'
@player_two_name = gets.chomp
@board = Array.new(3) { Array.new(3, '_') }
end
def display_board(board)
puts "\r"
puts "#{board[0][0]} | #{board[0][1]} | #{board [0][2]}"
puts "#{board[1][0]} | #{board[1][1]} | #{board [1][2]}"
puts "#{board[2][0]} | #{board[2][1]} | #{board [2][2]}"
end
def player_turn(turn)
if turn.odd?
player_choice(@player_one_name, 'O')
else
player_choice(@player_two_name, 'X')
end
end
def player_choice(player, symbol)
puts "#{player} please enter your coordinates separated by a space"
input = gets.chomp
input_array = input.split
coord_one = input_array[0].to_i
coord_two = input_array[1].to_i
until input.match(/\s/) && coord_one.between?(0, 2) && coord_two.between?(0, 2) && @board[coord_one][coord_two] == '_'
puts "Please enter valid coordinates"
input = gets.chomp
input_array = input.split
coord_one = input_array[0].to_i
coord_two = input_array[1].to_i
end
add_to_board(coord_one, coord_two, symbol)
end
def add_to_board(coord_one, coord_two, symbol)
@board[coord_one][coord_two] = symbol
@@turn_count += 1
end
def three_across
@board.each do |i|
if i.all? { |j| j == 'X' }
@@winner = 'X'
@@turn_count = 10
elsif i.all? { |j| j == 'O' }
@@winner = 'O'
@@turn_count = 10
else
nil
end
end
end
def three_down
flat = @board.flatten
flat.each_with_index do |v, i|
if v == 'X' && flat[i + 3] == 'X' && flat[i + 6] == 'X'
@@winner = 'X'
@@turn_count = 10
elsif v == 'O' && flat[i + 3] == 'O' && flat[i + 6] == 'O'
@@winner = 'O'
@@turn_count = 10
else
nil
end
end
end
def three_diagonal
center_val = @board[1][1]
if center_val == 'X' || center_val == 'O'
if @board[0][0] && @board[2][2] == center_val
@@winner = center_val
@@turn_count = 10
elsif @board[2][0] && @board[0][2] == center_val
@@winner = center_val
@@turn_count = 10
end
else
nil
end
end
def declare_result(symbol)
case symbol
when 'O'
puts "#{@player_one_name} wins the game!"
when 'X'
puts "#{@player_two_name} wins the game!"
else
puts "It's a tie!"
end
end
def play_game
puts "\r\n"
puts 'Here is the board'
display_board(@board)
until @@turn_count >= 10 do
player_turn(@@turn_count)
three_across
three_diagonal
three_down
display_board(@board)
end
declare_result(@@winner)
end
end
puts 'Welcome to tic-tac-toe. The rules are as expected, but choosing placement requires coordinates'
puts 'Each turn, enter two numbers separated by a space, per the grid layout below:'
puts "\r\n"
puts '0 0 | 0 1 | 0 2'
puts '1 0 | 1 1 | 1 2'
puts '2 0 | 2 1 | 2 2'
puts "\r\n"
game = Game.new
game.play_game