-
Notifications
You must be signed in to change notification settings - Fork 156
/
reversi.rb
58 lines (45 loc) · 1.26 KB
/
reversi.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true
require_relative './lib/reversi_methods'
class Reversi
include ReversiMethods
QUIT_COMMANDS = %w[quit exit q].freeze
def initialize
@board = build_initial_board
@current_stone = BLACK_STONE
end
def run
loop do
output(@board)
if finished?(@board)
puts '試合終了'
puts "白○:#{count_stone(@board, WHITE_STONE)}"
puts "黒●:#{count_stone(@board, BLACK_STONE)}"
break
end
unless placeable?(@board, @current_stone)
puts '詰みのためターンを切り替えます'
toggle_stone
next
end
print "command? (#{@current_stone == WHITE_STONE ? '白○' : '黒●'}) > "
command = gets.chomp
break if QUIT_COMMANDS.include?(command)
begin
if put_stone(@board, command, @current_stone)
puts '配置成功、次のターン'
toggle_stone
else
puts '配置失敗、ターン据え置き'
end
rescue StandardError => e
puts "ERROR: #{e.message}"
end
end
puts 'finished!'
end
private
def toggle_stone
@current_stone = @current_stone == WHITE_STONE ? BLACK_STONE : WHITE_STONE
end
end
Reversi.new.run if __FILE__ == $PROGRAM_NAME