-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rb
91 lines (82 loc) · 2.22 KB
/
main.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
90
91
require "bundler/inline"
gemfile do
source 'https://rubygems.org'
gem 'colorize'
end
require "./lib/modules/solicit_player_moves.rb"
require "./lib/modules/serialize.rb"
require "./lib/classes/board"
require "./lib/classes/player"
def play_game
$board = Board.new
player_1 = Player.new(:white)
player_2 = Player.new(:black)
puts `clear`
puts "Welcome to chess.\n".center(120)
puts "Enter 1 to play a new game or enter 2 to load a game"
reply = gets.chomp.to_i
if reply == 1
play
elsif reply == 2
load_game
play
end
end
def clear_screen_display_board
puts `clear`
puts $board
end
def solicit_move
begin
reply = gets.chomp[0..1]
if SolicitPlayerMoves::save_game?(reply)
save_game
end
SolicitPlayerMoves::ensure_reply_was_given(reply)
x_coordinate = SolicitPlayerMoves::validate_x_coordinate(reply[0])
y_coordinate = SolicitPlayerMoves::validate_y_coordinate(reply[1])
rescue StandardError => e
# puts e.backtrace
clear_screen_display_board
puts e.message
retry
end
[x_coordinate,y_coordinate]
end
def play
loop do
clear_screen_display_board
puts "#{$board.current_turn_color.capitalize}, you are in check." if $board.in_check?
puts "#{$board.current_turn_color.capitalize}, choose a piece to move.\nTo save type `sv`"
begin
$delta_1 = solicit_move
$board.select_piece($delta_1)
rescue => e
# puts e.backtrace
clear_screen_display_board
puts e.message
retry
end
clear_screen_display_board
puts "Choose where to set it down"
begin
$delta_2 = solicit_move
$board.move($delta_1, $delta_2)
rescue => e
# puts e.backtrace
clear_screen_display_board
puts e.message
retry
end
break if $board.check_mate?
end
clear_screen_display_board
puts "#{$board.toggle_turn.capitalize} wins!"
end
def save_game
Serialize::write_to_file(Marshal.dump($board))
end
def load_game
$board = Marshal.load(Serialize::select_file)
end
play_game