Last active
February 24, 2016 10:34
-
-
Save ppworks/91b2ade78eca1154a2ee to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
class Gomoku | |
STONES = %w(1 0) | |
def initialize(row: 19, col: 19, connectivity: 5) | |
@row = row | |
@col = col | |
@connectivity = connectivity | |
@stones = STONES.dup | |
reset | |
print_boards | |
hello | |
run | |
end | |
def reset | |
@boards1 = [] # 横判定用 | |
@boards2 = [] # 縦判定用 | |
@boards3 = [] # 右肩上がり斜め判定用 | |
@boards4 = [] # 右肩下がり斜め判定用 | |
@col.times do |c| | |
@row.times do |r| | |
@boards1 << Array.new(@col, '-') | |
@boards2 << Array.new(@col, '-') | |
@boards3 << Array.new(@col, '-') | |
@boards4 << Array.new(@col, '-') | |
end | |
end | |
end | |
def print_boards | |
@col.times do |c| | |
@row.times do |r| | |
print @boards1[c][r] | |
end | |
print "\n" | |
end | |
end | |
def hello | |
puts '五目並べへヨウコソ' | |
puts "#{STONES.join('と')}を交互に並べてタテ・ヨコ・ナナメに先に#{@connectivity}並べた方の勝ちです" | |
end | |
def current_stone | |
@stones.first | |
end | |
def run | |
puts "#{current_stone} を置く場所を入力して下さい(例: 0, 1)" | |
inputs = gets | |
x, y = inputs.split(',') | |
set_to_board(x.to_i, y.to_i) | |
end | |
def rotate_stones | |
@stones << @stones.shift | |
end | |
def set_to_board(x, y) | |
if @boards1[x][y] == '-' | |
@boards1[x][y] = current_stone | |
@boards2[y][x] = current_stone | |
@boards3[x-y][y] = current_stone | |
@boards4[x+y][y] = current_stone | |
rotate_stones | |
else | |
puts "すでに石が置かれていますよ!" | |
end | |
print_boards | |
if judge | |
else | |
run | |
end | |
end | |
def judge | |
[@boards1, @boards2, @boards3, @boards4].each do |boards| | |
boards.each do |col| | |
STONES.each do |stone| | |
if col.join('').match /#{stone}{#{@connectivity}}/ | |
puts "#{stone}の勝ちです" | |
return true | |
end | |
end | |
end | |
end | |
false | |
end | |
end | |
Gomoku.new(row: 19, col: 19) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment