-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckers.rb
More file actions
93 lines (76 loc) · 2 KB
/
checkers.rb
File metadata and controls
93 lines (76 loc) · 2 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require_relative 'board'
class Checkers
def initialize
@board = Board.new
@turn = :black
@players = {black: HumanPlayer.new(:black, 1), red: HumanPlayer.new(:red, 2)}
end
def play
puts "Welcome to Checkers!\n"
@board.set_board
loop do
@players[@turn].play_turn(@board)
break if game_over?
@turn = (@turn == :black ? :red : :black)
end
@board.render
if player_won?
puts "\nCongratulations Player #{@players[@turn].player_num}. You won!"
else
puts "\nThe match is a draw."
end
end
def player_won?
pieces = @board.board.flatten.compact
return true if pieces.none? { |piece| piece.color == :red } || pieces.none? { |piece| piece.color == :black }
end
def draw?
!@board.any_moves?
end
def game_over?
player_won? || draw?
end
end
class HumanPlayer
attr_reader :player_num
def initialize(color, player_num)
@color = color
@player_num = player_num
end
def play_turn(board)
if board.any_player_moves?(@turn)
begin
puts "\nPlayer #{self.player_num}, it's your turn.\n".colorize(@color)
board.render
move = gets_move
start_pos, end_pos = move[0], move[1]
if board.legal_move?(@color, start_pos, end_pos)
board.perform_move(start_pos, end_pos)
end
rescue StandardError => e
puts e.message
retry
end
else
puts "\nSorry Player #{self.player_num}, You have no moves."
end
end
def gets_move
puts "\nEnter Start and End Position".colorize(@color)
parse_position(gets.chomp.strip.downcase)
end
def parse_position(str)
strings = [str[/[a-h][1-8]/], str[/[a-h][1-8]$/]]
move = []
strings.each do |string|
pair = []
string.split("").each do |let_or_num|
pair << (let_or_num[/\d/] ? -(let_or_num.to_i - 8) : let_or_num.ord - 97)
end
move << pair.reverse #reverse because rows & cols != x & y
end
move
end
end
new_game = Checkers.new
new_game.play