I am messing with Ruby a little bit and would like to get some feedback about the Spaghettii below :)
Sides = ["X", "O"]
Msg_for_restart = " Starting all over again"
Msg_for_coordinates = "Enter coordinates in the form of x,y"
Msg_for_matrix = "This is how the matrix looks like"
Draw_msg = "The game is draw, try again please."
Rows, Columns = 3, 3
class Player
attr_reader :name, :side
def initialize(name, side)
@name, @side = name, side
end
def to_s
"#{@name} with #{@side} side"
end
end
def initialize_matrix(rows, columns)
Array.new(rows) { Array.new(columns) }
end
def print_matrix(matrix)
puts "----"
matrix.each_with_index do |row_value, i|
row_value.each_with_index do |column_value, j|
print matrix[i][j].to_s + " "
end
puts
end
puts "----"
end
def get_input
$stdin.gets.chomp
end
def get_players_info
puts "Welcome to Tic Tac Toe Game"
puts "Enter the name for the first player: "
first_player_name = get_input
puts "Enter a side to play with "
first_player_side = get_input
if not Sides.map { |s|
s.to_s
}.include? first_player_side
puts "Sorry, only X and O values are allowed" << Msg_for_restart
get_players_info
end
puts "Enter the name for the second player: "
second_player_name = get_input
puts "Enter a side for the second player: "
second_player_side = get_input
if first_player_side.to_s == second_player_side
puts "Sorry, sides can't be the same" << Msg_for_restart
get_players_info
end
[Player.new(first_player_name, first_player_side), Player.new(second_player_name, second_player_side)]
end
def get_x_player(players)
return players[0] if players[0].side == Sides[0].to_s
players[1]
end
def get_y_player(players)
return players[0] if players[0].side == Sides[1].to_s
players[1]
end
def are_elements_equal(array, element)
begin
array.each do |el|
return false if el != element
end
return true
rescue
return false
end
false
end
def is_player_winning_based_on_rows(side, matrix)
matrix.each_with_index do |x, xi|
row = []
x.each_with_index do |y, yi|
row << matrix[xi][yi]
end
return true if are_elements_equal(row, side)
end
false
end
def is_player_winning_based_on_columns(side, matrix)
matrix.each_with_index do |x, xi|
row = []
x.each_with_index do |y, yi|
row << matrix[yi][xi]
end
return true if are_elements_equal(row, side)
end
false
end
def is_player_winning_based_on_diagonals(side, matrix)
return true if matrix[0][0] == side && matrix[0][0] && matrix[1][1] && matrix[1][1] == matrix[2][2] || \
matrix[2][0] == side && matrix[2][0] == matrix[1][1] && matrix[1][1] == matrix[0][2]
false
end
matrix = initialize_matrix(Rows, Columns)
row_border = Rows - 1
column_border = Columns - 1
players = get_players_info
x_player = get_x_player(players)
y_player = get_y_player(players)
while true
print_matrix(matrix)
puts "Hello #{x_player.to_s}"
puts Msg_for_coordinates
x_player_input = get_input.split(",")
raise IndexError if x_player_input[0].to_i > row_border || x_player_input[1].to_i > column_border
matrix[x_player_input[0].to_i][x_player_input[1].to_i]= Sides[0]
if is_player_winning_based_on_rows(x_player.side, matrix) || is_player_winning_based_on_columns(x_player.side, matrix) || is_player_winning_based_on_diagonals(x_player.side, matrix)
puts "Congatulations #{x_player.to_s}, You Won!"
break
end
print_matrix(matrix)
puts "Hello #{y_player.to_s}"
puts Msg_for_coordinates
y_player_input = get_input.split(",")
raise IndexError if y_player_input[0].to_i > row_border || y_player_input[1].to_i > column_border
matrix[y_player_input[0].to_i][y_player_input[1].to_i]= Sides[1]
print_matrix(matrix)
if is_player_winning_based_on_rows(y_player.side, matrix) || is_player_winning_based_on_columns(y_player.side, matrix) || is_player_winning_based_on_diagonals(y_player.side, matrix)
puts "Congatulations #{y_player.to_s}, You Won!"
break
end
end