-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.rb
89 lines (73 loc) · 1.76 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
87
88
89
# encoding: utf-8
require "./chessboard.rb"
require "./chesspieces.rb"
require "./player.rb"
require "colorize"
require "yaml"
class Chess
attr_reader :board, :players
def initialize
@board = Chessboard.new
player1 = HumanPlayer.new('W', 'Player 1', board)
puts "Player 1, you play the white pieces."
player2 = HumanPlayer.new('B', 'Player 2', board)
puts "Player 2, you play the black pieces."
@players = [player1, player2]
end
def play
until checkmate? || stalemate?
move = players[0].move
if move == "S"
save_game
next
elsif move == "CL"
castle('left')
elsif move == "CR"
castle('right')
else
normal_move(move)
end
players.reverse!
end
end
def checkmate?
result = board.checkmate?(players[0].color)
if result
board.print_board
puts "#{player.name} wins!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
end
result
end
def stalemate?
result = board.checkmate?(players[0].color, true)
if result
board.print_board
puts "Stalemate!!!!!!!!!!!!1!!!!!!!!1!!!one!!!"
end
result
end
def normal_move(move)
board.update(move)
end
def castle(direction)
column = (direction == 'left' ? [0, 3, 4, 2] : [7, 5, 4, 6])
row = (players[0].color == "W" ? 0 : 7)
move_one = [[column[0], row], [column[1], row]]
move_two = [[column[2], row], [column[3], row]]
board.update(move_one)
board.update(move_two)
end
def save_game
print "Please provide filename (without file extension): "
filename = $stdin.gets.chomp + ".txt"
File.open(filename, 'w') do |file|
file.puts self.to_yaml
end
end
end
if ARGV[0]
p ARGV[0]
YAML::load_file(ARGV[0]).play
else
Chess.new.play
end