Last active
December 2, 2015 14:41
-
-
Save Drowze/bbdc6df7f047676da41f to your computer and use it in GitHub Desktop.
mim ajuda
This file contains 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 'matrix' | |
class Matrix | |
def []=(i, j, x) | |
@rows[i][j] = x | |
end | |
end | |
class String # Only works for single letters; could be extended to uppercase letters too | |
def step(c, delta=1) | |
if c.ord + delta > 122 then | |
return [c.ord + delta - 26].pack 'U' | |
elsif c.ord + delta < 97 then | |
return [c.ord + delta + 26].pack 'U' | |
else | |
return [c.ord + delta].pack 'U' | |
end | |
end | |
def increment(c) | |
return nil if self.size > 1 | |
c %= 26 | |
step self, c | |
end | |
def decrement(c) | |
return nil if self.size > 1 | |
c %= 26 | |
step self, -c | |
end | |
end | |
class Board | |
attr_reader :cell_pairs | |
MOVES = { | |
'nw' => [-1, 0], | |
'ne' => [-1, +1], | |
'sw' => [+1, -1], | |
'se' => [+1, 0] | |
} | |
CELL_CODES = Matrix[ | |
[0,0,0,0,0,1,3,3,3,2], | |
[0,0,0,0,5,7,7,7,7,2], | |
[0,0,0,5,7,7,7,7,7,2], | |
[0,0,5,7,7,7,7,7,7,2], | |
[0,5,7,7,7,7,7,7,7,2], | |
[4,7,7,7,7,7,7,7,7,0], | |
[4,7,7,7,7,7,7,7,0,0], | |
[4,7,7,7,7,7,7,0,0,0], | |
[4,7,7,7,7,7,0,0,0,0], | |
[4,7,7,7,7,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0] | |
] | |
CELL_PAIRS = module Cell_pairs # "Function" to prepare the cell_pairs | |
cell_pairs = Hash.new | |
start_letter = 'a' | |
increment = 0 | |
checker = 0 | |
CELL_CODES.each_with_index do |element, row, col| | |
if row <= 5 | |
if checker != row then # I get here everytime it goes to another row | |
increment = -row + 5 | |
checker = row | |
end | |
if(element == 7) then | |
cell_pairs["#{row} #{col}"] = "#{row.to_s}#{start_letter.increment(increment)}" | |
increment += 2 | |
end | |
else | |
if checker != row then # I get here everytime it goes to another row | |
increment = row - 5 | |
checker = row | |
end | |
if(element == 7) then | |
cell_pairs["#{row} #{col}"] = "#{row.to_s}#{start_letter.increment(increment)}" | |
increment += 2 | |
end | |
end | |
end | |
cell_pairs | |
end | |
def self.prepare_board positions | |
seq = [' ',1,2,3,4,5,6,7,8,9,' '] | |
counter = 0 | |
indentation = 0 | |
ret = '' | |
CELL_CODES.to_a.map.with_index do |m, i| | |
if i != 10 then | |
ret += seq[counter].to_s + ' ' * indentation | |
m.each_with_index do |n, j| | |
n == 7 ? ret += ("#{positions[i,j]} ") : ret += (' ') # piece occupying the cell | |
[4,5,6,7].include?(n) ? ret += ('| ') : ret += (' ') | |
end | |
ret += "\n" + ' ' * indentation unless i == CELL_CODES.to_a.map.size-1 | |
m.each_with_index do |n| | |
[2,3,6,7].include?(n) ? ret += ('\\ ') : ret += (' ') | |
[1,3,5,7].include?(n) ? ret += ('/ ') : ret += (' ') | |
end | |
ret += "\n" | |
counter+=1 | |
indentation+=2 | |
end | |
end | |
ret += ' ' * 15 + 'a b c d e f g h i j k l m n o' + "\n" | |
ret | |
end | |
end | |
print Board::CELL_PAIRS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment