Created
August 22, 2018 10:15
-
-
Save ladislas/013abbed2fad54fad2b077ec48e5963d to your computer and use it in GitHub Desktop.
TicTacToe - Machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
source "https://rubygems.org" | |
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | |
gem "colorize" | |
gem "terminal-table" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GEM | |
remote: https://rubygems.org/ | |
specs: | |
colorize (0.8.1) | |
terminal-table (1.8.0) | |
unicode-display_width (~> 1.1, >= 1.1.1) | |
unicode-display_width (1.3.2) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
colorize | |
terminal-table | |
BUNDLED WITH | |
1.16.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'colorize' | |
$DEGUG = true | |
# $DEGUG = false | |
def debug (string) | |
if ARGV[0] == "--debug" | |
puts string | |
end | |
end | |
first_move_boards = [ | |
[ "0" , "." , "." , "." , "." , "." , "." , "." , "." ], # type 1 | |
[ "." , "0" , "." , "." , "." , "." , "." , "." , "." ], # type 2 | |
[ "." , "." , "0" , "." , "." , "." , "." , "." , "." ], # type 1 | |
[ "." , "." , "." , "0" , "." , "." , "." , "." , "." ], # type 2 | |
[ "." , "." , "." , "." , "0" , "." , "." , "." , "." ], # type 3 | |
[ "." , "." , "." , "." , "." , "0" , "." , "." , "." ], # type 2 | |
[ "." , "." , "." , "." , "." , "." , "0" , "." , "." ], # type 1 | |
[ "." , "." , "." , "." , "." , "." , "." , "0" , "." ], # type 2 | |
[ "." , "." , "." , "." , "." , "." , "." , "." , "0" ] # type 1 | |
] | |
first_move_boards_ordered_by_types = [ | |
[ "0" , "." , "." , "." , "." , "." , "." , "." , "." ], # type 1 / 0 % 3 = 0 | |
[ "." , "." , "0" , "." , "." , "." , "." , "." , "." ], # type 1 / 2 % 3 = 2 | |
[ "." , "." , "." , "." , "." , "." , "0" , "." , "." ], # type 1 / 6 % 3 = 0 | |
[ "." , "." , "." , "." , "." , "." , "." , "." , "0" ], # type 1 / 8 % 3 = 2 | |
[ "." , "0" , "." , "." , "." , "." , "." , "." , "." ], # type 2 / 1 % 3 = 1 | |
[ "." , "." , "." , "0" , "." , "." , "." , "." , "." ], # type 2 / 3 % 3 = 0 | |
[ "." , "." , "." , "." , "." , "0" , "." , "." , "." ], # type 2 / 5 % 3 = 2 | |
[ "." , "." , "." , "." , "." , "." , "." , "0" , "." ], # type 2 / 7 % 3 = 1 | |
[ "." , "." , "." , "." , "0" , "." , "." , "." , "." ] # type 3 / 4 % 3 = 1 | |
] | |
def print_board (board) | |
board.each_with_index { |value, index| | |
if index.modulo(3) == 0 | |
print "\n" | |
end | |
print value | |
print " " | |
} | |
puts "\n\n" | |
end | |
abc_array = [ | |
["a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i"], | |
["1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9"], | |
["a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i"], | |
["1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9"], | |
["a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i"], | |
["1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9"] | |
] | |
def print_boards_array (boards, boards_per_line = 5) | |
puts "\n" | |
if boards_per_line > boards.length | |
boards_per_line = boards.length | |
end | |
number_of_full_lines = boards.length / boards_per_line | |
number_of_remaining_boards = boards.length % boards_per_line | |
(0..number_of_full_lines - 1).each do |print_line| | |
[(0..2), (3..5), (6..8)].each do |board_line| | |
lower_bound = print_line * boards_per_line | |
upper_bound = (print_line + 1) * boards_per_line - 1 | |
(lower_bound..upper_bound).each do |board_index| | |
board_line.each do |position| | |
print boards[board_index][position] | |
print " " | |
end | |
print " " | |
end | |
print "\n" | |
end | |
print "\n" | |
end | |
if number_of_remaining_boards != 0 | |
upper_bound = -1 | |
lower_bound = - number_of_remaining_boards | |
[(0..2), (3..5), (6..8)].each do |board_line| | |
(lower_bound..upper_bound).each do |board_index| | |
board_line.each do |position| | |
print boards[board_index][position] | |
print " " | |
end | |
print " " | |
end | |
print "\n" | |
end | |
end | |
# [(0..2), (3..5), (6..8)].each do |line| | |
# (boards_per_line..boards.length - 1).each do |board_index| | |
# line.each do |position| | |
# print boards[board_index][position] | |
# print " " | |
# end | |
# print " " | |
# end | |
# print "\n" | |
# end | |
# [(0..2), (3..5), (6..8)].each do |line| | |
# boards.each_with_index do |board, index| | |
# if index > 0 && index % 8 == 0 | |
# puts "\n" | |
# end | |
# line.each do |i| | |
# print board[i] | |
# print " " | |
# end | |
# print " " | |
# end | |
# puts "\n" | |
# end | |
# puts "\n" | |
end | |
def print_boards (*boards) | |
puts "\n" | |
[(0..2), (3..5), (6..8)].each do |range| | |
boards.each do |board| | |
range.each do |index| | |
print board[index] | |
print " " | |
end | |
print " " | |
end | |
puts "\n" | |
end | |
puts "\n" | |
end | |
def same_value_at_position? (b1, p1, b2, p2) | |
if b1[p1] == b2[p2] | |
debug "first_board(#{p1}) = #{b1[p1]} == #{b2[p2]} = second_board(#{p2})" | |
return true | |
else | |
debug "first_board(#{p1}) = #{b1[p1]} =! #{b2[p2]} = second_board(#{p2})" | |
return false | |
end | |
end | |
def identity? (first_board, second_board) | |
(0..8).each do |i| | |
unless same_value_at_position?(first_board, i, second_board, i) | |
puts "Identity - " + "no".red | |
return false | |
end | |
end | |
puts "Identity - " + "yes".green | |
return true | |
end | |
def vertical_symmetry? (first_board, second_board) | |
[0, 3, 6].each do |i| | |
f_prog = i | |
s_prog = i + 2 | |
unless same_value_at_position?(first_board, f_prog, second_board, s_prog) | |
puts "Vertical symmetry - " + "no".red | |
return false | |
end | |
end | |
[1, 4, 7].each do |i| | |
f_prog = i | |
s_prog = i | |
unless same_value_at_position?(first_board, f_prog, second_board, s_prog) | |
puts "Vertical symmetry - " + "no".red | |
return false | |
end | |
end | |
[2, 5, 8].each do |i| | |
f_prog = i | |
s_prog = i - 2 | |
unless same_value_at_position?(first_board, f_prog, second_board, s_prog) | |
puts "Vertical symmetry - " + "no".red | |
return false | |
end | |
end | |
puts "Vertical symmetry - " + "yes".green | |
return true | |
end | |
def horizontal_symmetry? (first_board, second_board) | |
[0, 1, 2].each do |i| | |
f_prog = i | |
s_prog = i + 6 | |
unless same_value_at_position?(first_board, f_prog, second_board, s_prog) | |
puts "Vertical symmetry - " + "no".red | |
return false | |
end | |
end | |
[3, 4, 5].each do |i| | |
f_prog = i | |
s_prog = i | |
unless same_value_at_position?(first_board, f_prog, second_board, s_prog) | |
puts "Vertical symmetry - " + "no".red | |
return false | |
end | |
end | |
[6, 7, 8].each do |i| | |
f_prog = i | |
s_prog = i - 6 | |
unless same_value_at_position?(first_board, f_prog, second_board, s_prog) | |
puts "Vertical symmetry - " + "no".red | |
return false | |
end | |
end | |
puts "Horizontal symmetry - " + "yes".green | |
return true | |
end | |
first_move_board_types = [] | |
first_move_boards.each do |board| | |
if first_move_board_types.empty? | |
first_move_board_types.append(board) | |
end | |
first_move_board_types.each do |type| | |
print_boards(board, type) | |
if vertical_symmetry?(board, type) || horizontal_symmetry?(board, type) || identity?(board, type) | |
puts "same board type, skipping" | |
break | |
else | |
first_move_board_types.append(board) | |
end | |
end | |
end | |
print_boards_array(first_move_board_types) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment