[go: nahoru, domu]

Skip to content

Commit

Permalink
Implement diagonal win & draw logic
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayShade committed Feb 13, 2023
1 parent a410ef3 commit dd87639
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 22 deletions.
8 changes: 4 additions & 4 deletions board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create_board
# Assigning array of given size & default value to each row
board_hash[row_name] = Array.new(size, ' ')
end
puts board_hash
# puts board_hash
end

def update_board(plyr_move, plyr_icon)
Expand All @@ -34,7 +34,7 @@ def update_board(plyr_move, plyr_icon)
end
end

def display_board(calculation_result)
def display_board
board_hash.each do |row_name, arr|
arr.each_with_index do |ele, idx|
row_ele = idx == arr.length - 1 ? ele : "#{ele} | "
Expand All @@ -46,7 +46,7 @@ def display_board(calculation_result)
end
end

def win?

def announce_round_result(calculation_result)
puts calculation_result
end
end
87 changes: 70 additions & 17 deletions game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,40 @@ def create_board_objct

def play_round
make_turn(p1_obj.name, p1_obj.input_move, p1_obj.icon)
# return if there is no empty square left:
return unless any_square_empty?
# # return if there is no empty square left:
# return unless any_square_empty?

make_turn(p2_obj.name, p2_obj.input_move, p2_obj.icon)
end

def make_turn(player_name, input_move, icon)
check_turn_legality
board_obj.update_board(input_move, icon)
calculation_result = do_calculations(player_name)
calculation_result = return_winner_or_draw(player_name)
# Default arguments if tried to passed to arguments instead of parameters
# cause assignment. So be careful in that instance.
board_obj.display_board(calculation_result)
end_game unless calculation_result.nil?
board_obj.display_board
end_game(calculation_result) unless calculation_result.nil?
end

def check_turn_legality; end

def do_calculations(player_name)
return "#{player_name} wins" if check_win_horizental
return "#{player_name} wins" if check_win_vertical
# check_win_diagonal
# draw_check
def return_winner_or_draw(player_name)
win_message = "#{player_name} wins"
return win_message if check_horizental_win || check_vertical_win || check_diagonal_win

'Match Draw!!!' unless any_square_empty?
end

def check_win_horizental
def check_horizental_win
board_obj.board_hash.values.any? do |array|
array.all? { |ele| ele == 'x' || array.all? | ele | ele == 'o' }
array.count('x') == array.length || array.count('o') == array.length
end
end

def check_win_vertical
numeric_hash = {}
board_obj.board_hash.values.each_with_index { |ele, idx| numeric_hash[idx] = ele }
def check_vertical_win
numeric_hash = convert_to_numeric_hash

# initial value against each icon is 0. This will be incremented on each
# vertical match.
iconic_hash = Hash.new(0)
Expand All @@ -90,8 +90,61 @@ def check_win_vertical
iconic_hash.values.any? { |val| val == board_obj.size }
end

def end_game
puts 'here in end game'
def check_diagonal_win
num_hash = convert_to_numeric_hash
bord_size = board_obj.size
chek_left_diagnl_win(num_hash, bord_size) || chek_right_diagnl_win(num_hash, bord_size)
end

def chek_left_diagnl_win(numeric_hash, board_size)
left_diag_hash = {}
# board_size = board_obj.size
# Assigning array of given size & default value to each key of hash:
left_diag_hash['x'] = Array.new(board_size, ' ')
left_diag_hash['o'] = Array.new(board_size, ' ')
# left_diag_hash['o'] = Array.new(board_obj.size, ' ')
numeric_hash.each do |key, array|
array.each_with_index do |icon, idx|
# if diagonal row index & column index are same(idx == key), then its a
# left diagonal match :
left_diag_hash[icon][idx] = icon if key == idx && icon != ' '
end
end
# if any array of diag containing diagonal values have only x or only o,
# return true:
left_diag_hash.values.any? do |arr|
arr.count('x') == board_size || arr.count('o') == board_size
end
end

def chek_right_diagnl_win(numeric_hash, board_size)
right_diag_hash = {}
# Assigning array of given size & default value to each key of hash:
right_diag_hash['x'] = Array.new(board_size, ' ')
right_diag_hash['o'] = Array.new(board_size, ' ')

numeric_hash.each do |key, array|
array.each_with_index do |icon, idx|
# if diagonal row index & column index addition result in size of
# array - 1 are same(idx == key), then its a generic right diagonal match :
right_diag_hash[icon][idx] = icon if key + idx == board_size - 1 && icon != ' '
end
end
right_diag_hash.values.any? do |arr|
arr.count('x') == board_size || arr.count('o') == board_size
end
end

def convert_to_numeric_hash
numeric_hash = {}
board_obj.board_hash.values.each_with_index do |ele, idx|
numeric_hash[idx] = ele
end
numeric_hash
end

def end_game(calculation_result)
board_obj.announce_round_result(calculation_result)
exit
end
end
Expand Down
2 changes: 1 addition & 1 deletion player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(name, icon)
end

def input_move
puts "#{name} turn:"
print "#{name} turn: "
input = gets.chomp
end
end

0 comments on commit dd87639

Please sign in to comment.