-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.rb
128 lines (116 loc) · 3.58 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
class Player
attr_reader :name, :symbol
def initialize(name, symbol)
@name = name
@symbol = symbol
end
end
class Board
def initialize
@current_state = ["", 1, 2, 3, 4, 5, 6, 7, 8, 9]
@moves = 0
end
def show_board
puts " #{@current_state[1]} | #{@current_state[2]} | #{@current_state[3]} "
puts "---|---|---"
puts " #{@current_state[4]} | #{@current_state[5]} | #{@current_state[6]} "
puts "---|---|---"
puts " #{@current_state[7]} | #{@current_state[8]} | #{@current_state[9]} "
end
def add_move(position, symbol)
@current_state[position.to_i] = symbol
@moves += 1
if winning_move?
return 1
elsif draw?
return -1
else
return 0
end
end
def is_move_valid?(position)
@current_state[position.to_i].is_a? Integer
end
private
def winning_move?
winning_row? || winning_column? || winning_diagonal?
end
def winning_row?
first_row = [@current_state[1], @current_state[2], @current_state[3]].uniq.length
second_row = [@current_state[4], @current_state[5], @current_state[6]].uniq.length
third_row = [@current_state[7], @current_state[8], @current_state[9]].uniq.length
if [first_row, second_row, third_row].include? 1
return true
else
return false
end
end
def winning_column?
first_column = [@current_state[1], @current_state[4], @current_state[7]].uniq.length
second_column = [@current_state[2], @current_state[5], @current_state[8]].uniq.length
third_column = [@current_state[3], @current_state[6], @current_state[9]].uniq.length
if [first_column, second_column, third_column].include? 1
return true
else
return false
end
end
def winning_diagonal?
downward_diag = [@current_state[1], @current_state[5], @current_state[9]].uniq.length
upward_diag = [@current_state[7], @current_state[5], @current_state[3]].uniq.length
if [downward_diag, upward_diag].include? 1
return true
else
return false
end
end
def draw?
@moves == 9
end
end
class Game
def initialize
puts "Player 1, enter your name:"
@player1 = Player.new(gets.chomp, "X")
puts "Player 2, enter your name:"
@player2 = Player.new(gets.chomp, "O")
@board = Board.new
end
def play
game_over = false
current_player = @player2
until game_over
if current_player == @player2
current_player = @player1
else
current_player = @player2
end
valid_move = false
until valid_move
puts
@board.show_board
puts
puts "It's #{current_player.name}'s turn."
puts "What is your move?"
position = gets.chomp
valid_move = true if @board.is_move_valid?(position)
end
result = @board.add_move(position, current_player.symbol)
if result == -1
puts
@board.show_board
puts
puts "It's a draw."
game_over = true
elsif result == 1
puts
@board.show_board
puts
puts "#{current_player.name} wins!"
game_over = true
end
end
end
end
#game = Game.new
#game.play