Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/reversi_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def put_stone(board, cell_ref, stone_color, dry_run: false)

# コピーした盤面にて石の配置を試みて、成功すれば反映する
copied_board = Marshal.load(Marshal.dump(board))
copied_board[pos.col][pos.row] = stone_color
copied_board[pos.row][pos.col] = stone_color

turn_succeed = false
Position::DIRECTIONS.each do |direction|
Expand All @@ -61,8 +61,9 @@ def put_stone(board, cell_ref, stone_color, dry_run: false)
end

def turn(board, target_pos, attack_stone_color, direction)
return false if target_pos.out_of_board?
return false if target_pos.stone_color(board) == attack_stone_color
if target_pos.out_of_board? || target_pos.stone_color(board) == attack_stone_color || target_pos.stone_color(board) == BLANK_CELL

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

横に並べる方法もありますが、横に長いコードはそれはそれで可読性が悪いので、一貫して縦に並べる、というアプローチもあります。(好みの問題ですが)

    return false if target_pos.out_of_board?
    return false if target_pos.stone_color(board) == attack_stone_color
    return false if target_pos.stone_color(board) == BLANK_CELL

return false
end

next_pos = target_pos.next_position(direction)
if (next_pos.stone_color(board) == attack_stone_color) || turn(board, next_pos, attack_stone_color, direction)
Expand All @@ -86,6 +87,7 @@ def placeable?(board, attack_stone_color)
return true if put_stone(board, position.to_cell_ref, attack_stone_color, dry_run: true)
end
end
false
end

def count_stone(board, stone_color)
Expand Down