Skip to content

Instantly share code, notes, and snippets.

@nalabjp
Created February 25, 2016 00:56
Show Gist options
  • Save nalabjp/4a8bfb9ac2228ac4700c to your computer and use it in GitHub Desktop.
Save nalabjp/4a8bfb9ac2228ac4700c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class AlreadyExistsStone < StandardError; end
def init_board(num = 19)
@board = Array.new(num){ Array.new(num) }
end
def init_narabe(num = 5)
@narabe = num
end
def init_stone
@stone = :black
end
def change_stone
@stone = @stone.eql?(:black) ? :white : :black
end
def show_board
puts @board.inspect
end
def show_next_stone
puts "next -> #{@stone.to_s}"
end
def parse_stdin
gets.chomp.split(' ').map {|str| str.to_i - 1 }
end
def put_stone(pos)
res = @board[pos.first][pos.last]
if res.nil?
@board[pos.first][pos.last] = @stone
else
raise AlreadyExistsStone
end
end
def finish?
check_horizon(@board)
check_horizon(@board.transpose)
check_slanting(@board)
check_slanting(@board.transpose)
end
def check_horizon(board)
continuing = 0
board.each do |arr|
arr.each do |square|
if square == @stone
continuing += 1
else
continuing = 0
end
return true if continuing == @narabe
end
end
false
end
def check_slanting(board)
continuing = 0
# TODO
false
end
def main
loop do
begin
show_next_stone
put_stone(parse_stdin)
change_stone
show_board
finish?
rescue
# TODO: error message
retry
end
end
end
def prepare
init_board
init_narabe
init_stone
end
prepare
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment