-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.rb
86 lines (66 loc) · 1.81 KB
/
chess.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
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
require './pieces'
require './board'
class ChessError < RuntimeError
end
class NotYourPiece < ChessError
end
class Game
attr_reader :board
def initialize(player1 = "NoName1", player2 = "NoName2")
@board = Board.new
@white_player = Player.new(player1, :white)
@black_player = Player.new(player2, :black)
@current_player = @white_player
play
end
def swap_player
@current_player = @current_player == @white_player ? @black_player : @white_player
end
def play
until @board.checkmate?(:white) || @board.checkmate?(:black)
@board.display
begin
if @board.in_check?(@current_player.color)
puts "#{@current_player.name} is in check!"
end
input = @current_player.gets_input
# @board[input[0]].valid_moves
if @board[input[0]] && @board[input[0]].color != @current_player.color
raise NotYourPiece.new("Pick your own piece")
end
@board.move(input[0], input[1])
rescue ChessError => e
puts "ErrorType: #{e.class}"
puts e.message
retry
end
swap_player
end
end
end
class Player
attr_reader :name, :color
def initialize(name, color)
@name = name
@color = color
end
def gets_input
begin
puts "#{@name} please select a #{color} piece, eg. 00 = row 0, col 0"
input1 = gets.chomp.split("").map(&:to_i) #[1, 1]
raise "Please choose 2 numbers" if input1.count != 2
rescue => e
puts e.message
retry
end
begin
puts "Please where the piece will go, eg. 00 = row 0, col 0"
input2 = gets.chomp.split("").map(&:to_i)
raise "Please choose 2 numbers" if input2.count != 2
rescue => e
puts e.message
retry
end
[input1, input2]
end
end