Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
atonevski committed Nov 1, 2017
0 parents commit 83b3ae2
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
@@ -0,0 +1,7 @@
[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
/doc/
/lib/
/bin/
/.shards/
1 change: 1 addition & 0 deletions .travis.yml
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Andreja Tonevski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
@@ -0,0 +1,27 @@
# knight

TODO: Write a description here

## Installation

TODO: Write installation instructions here

## Usage

TODO: Write usage instructions here

## Development

TODO: Write development instructions here

## Contributing

1. Fork it ( https://github.com/[your-github-name]/knight/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request

## Contributors

- [[your-github-name]](https://github.com/[your-github-name]) Andreja Tonevski - creator, maintainer
Binary file added knight
Binary file not shown.
13 changes: 13 additions & 0 deletions shard.yml
@@ -0,0 +1,13 @@
name: knight
version: 0.1.0

authors:
- Andreja Tonevski <atonevski@gmail.com>

targets:
knight:
main: src/knight.cr

crystal: 0.23.1

license: MIT
9 changes: 9 additions & 0 deletions spec/knight_spec.cr
@@ -0,0 +1,9 @@
require "./spec_helper"

describe Knight do
# TODO: Write tests

it "works" do
false.should eq(true)
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
@@ -0,0 +1,2 @@
require "spec"
require "../src/knight"
153 changes: 153 additions & 0 deletions src/knight.cr
@@ -0,0 +1,153 @@
require "./knight/*"

module Knight
# TODO Put your code here

def self.print_syntax_and_exit
puts <<-EOS
usage knight [[size] col row]
\twhere:
\t\tcol/row start position: col: A|B|C..., row 1|2|3...
EOS
exit 1
end

class Board
MAX = 10
@@alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@diffs = [
{ x: -1, y: -2 },
{ x: 1, y: -2 },
{ x: 2, y: -1 },
{ x: 2, y: 1 },
{ x: 1, y: 2 },
{ x: -1, y: 2 },
{ x: -2, y: -1 },
{ x: -2, y: 1 },
]

@one_halve : Int32
@two_thirds : Int32
@three_fourths : Int32

def initialize(@size : Int32, x, y : Int32)
raise "Size #{ @size } too big" if @size > MAX
if x < 0 || y < 0 || x >= @size || y >= @size
raise "Invalid start position (#{x}, #{y})"
end

@table = [] of Array(Int32)
@size.times {|i| @table << Array.new(@size, 0)}
@table[x][y] = 1

@one_halve = @size*@size/2
@two_thirds = @size*@size*2/3
@three_fourths = @size*@size*3/4
end

def try(level, x, y) : Bool
if level == @size * @size
return true
end

# at this depth we check if some positions
# are unreachable
if level == @two_thirds || level == @three_fourths ||
level == @one_halve
@size.times do |r|
@size.times do |c|
if @table[r][c] == 0
return false unless is_reachable? r, c
end
end
end
end

@@diffs.each do |d|
xdx = x + d[:x]
ydy = y + d[:y]
if xdx >= 0 && xdx < @size &&
ydy >= 0 && ydy < @size &&
@table[xdx][ydy] == 0

# set this position to next level
# and try again
@table[xdx][ydy] = level + 1
return true if self.try(level+1, xdx, ydy)
@table[xdx][ydy] = 0
end
end
false
end

def is_reachable?(x, y)
return true if @table[x][y] != 0

@@diffs.each do |d|
xdx = x + d[:x]
ydy = y + d[:y]
if xdx >= 0 && xdx < @size &&
ydy >= 0 && ydy < @size &&
@table[xdx][ydy] == 0

return true
end
end
false
end

# prints the arbitrary chess board
def print
print "r\\c"
@size.times {|c| printf " %s", @@alpha[c]}
print "\n"

@size.times do |r|
printf "%-3d", r + 1
@size.times do |c|
printf "%3d", @table[r][c]
end
print "\n"
end
end
end

# default board size and knight's start position
x = 6
y = 6
n = 8
case ARGV.size
when 0
when 2
raise "Columns must be letters A|B|..." unless ('A'..'H').includes? ARGV[0][0].upcase
y = ARGV[0][0].upcase.ord - 'A'.ord
x = ARGV[1].to_i - 1
raise "Invalid row #{x}" unless (0..n-1).includes? x
when 3
n = ARGV[0].to_i
raise "Board size must be between 5..10" unless (5..10).includes? n
raise "Columns must be letters A|B|..." unless ('A'..'H').includes? ARGV[1][0].upcase
y = ARGV[1][0].upcase.ord - 'A'.ord
x = ARGV[2].to_i - 1
raise "Invalid row #{x}" unless (0..n-1).includes? x
else
print_syntax_and_exit
end
start = Time.new
board = Board.new n, x, y
board.try 1, x, y
board.print
puts "elapsed time: #{ Time.now - start }"
end

# nn: nn sec, nm: n min, nM: n0 min, nh: n hour, nH: n0 hours
#
# r\c A B C D E F G H
# 1
# 2 6 1m 2 22
# 3 27 12
# 4 12m21
# 5
# 6 4
# 7 3m 1
# 8
3 changes: 3 additions & 0 deletions src/knight/version.cr
@@ -0,0 +1,3 @@
module Knight
VERSION = "0.1.0"
end

0 comments on commit 83b3ae2

Please sign in to comment.