-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic.rb
More file actions
151 lines (128 loc) · 3.16 KB
/
tic.rb
File metadata and controls
151 lines (128 loc) · 3.16 KB
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
class Player
attr_reader :name, :color
def initialize(input)
@name = input.fetch(:name)
@color = input.fetch(:color)
end
end
class Cell
attr_accessor :value
def initialize(value = "")
@value = value
end
end
class Array
def all_same?
self.all? {|element| element == self[0]}
end
def all_string?
self.all? {|element| element == String}
end
end
class Board
attr_reader :board
def initialize(input = {})
@board = input.fetch(:grid, default_grid)
end
def get_position(row_index, column_index)
return (row_index * @board.size) + column_index + 1
end
def format_board
board.each_with_index do |row, row_index|
row.each_with_index.map do |column, column_index|
if column.value == "O" || column.value == "X"
print column.value
else
column.value = get_position(row_index, column_index)
print column.value
end
print "|" if column_index < 2
end
print "\n"
end
end
def make_move(move, color)
choices = {"1" => [0,0],
"2" => [0,1],
"3" => [0,2],
"4" => [1,0],
"5" => [1,1],
"6" => [1,2],
"7" => [2,0],
"8" => [2,1],
"9" => [2,2]
}
x, y = choices[move]
change_piece(x, y, color)
end
def change_piece(x, y, color)
board[x][y].value = color
end
def winning_position_values(winning_position)
winning_position.map do|cell|
cell.value
end
end
def get_diagonals
[
[board[0][0], board[1][1], board[2][2]],
[board[0][2], board[1][1], board[2][0]]
]
end
def winner?
winning_solutions = board + board.transpose + get_diagonals
winning_solutions.each do |winning_position|
return true if winning_position_values(winning_position).all_same? == true
end
end
def draw?
storage = []
board.flatten.each do |cell|
storage << cell.value.class
end
storage.all_string? ? true : false
end
private
def default_grid
Array.new(3) {Array.new(3) {Cell.new}}
end
end
class TicTacToe
attr_accessor :players, :board, :current_player, :other_player
def initialize(players, board = Board.new)
@board = board
@current_player, @other_player = players
end
def switch_players
@current_player, @other_player = @other_player, @current_player
end
def play
board.format_board
print "\n"
puts "#{@current_player.name}, please pick a position?"
print "\n"
board.make_move(gets.chomp, @current_player.color)
if board.winner? == true
puts "#{@current_player.name} is the winner!!"
board.format_board
return false
elsif board.draw? == true
puts "Nobody won! It is a draw game!"
board.format_board
return false
else
switch_players
end
end
end
puts "What's your name Player 1?"
name1 = gets.chomp
player1 = Player.new(:name => name1, :color => "O")
puts "What's your name Player 2?"
name2 = gets.chomp
player2 = Player.new(:name => name2, :color => "X")
players = [player1, player2]
game = TicTacToe.new(players)
while(game.play != false) do
game.play
end