-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.rb
More file actions
86 lines (69 loc) · 1.77 KB
/
game.rb
File metadata and controls
86 lines (69 loc) · 1.77 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
require './lib/position.rb'
require './lib/board.rb'
require './lib/game_piece.rb'
require './lib/move.rb'
# Remaining Items
# Implement mate condition
# Draw turn, check(mate) status, options to go back after piece selection
# Implement save / load game methods
# Pawn Promotion / En Passent
# Clean up - make more modular
class Game
@save_filename = './save.yaml'
def select_piece(pieces)
puts "Select pieces to move: "
input = gets.chomp
pieces[input.to_i]
end
def select_move(moves)
puts "Select space to move to"
input = gets.chomp
moves[input.to_i]
end
def save_game()
yaml = YAML::dump(self)
save_file = File.open(save_filename, "r") { |file| file.write(yaml) }
end
def load_game()
File.open(save_filename, "w") do |file|
yaml = file.read()
YAML::load(yaml)
end
end
def play
@board = Board.new
@board.init_pieces
@board.draw
@game = true
@turn = :white
@moved = false
while @game
@moved = false
unless @moved
#check_test(turn)
moveable_pieces = @board.list_pieces(@turn)
piece = select_piece(moveable_pieces)
available_moves = @board.available_moves(piece)
if available_moves.empty?
puts "No legal moves available. Hit enter to go back to piece selection"
gets.chomp
next
end
@board.list_moves(available_moves)
move = select_move(available_moves)
@board.remove_piece_at move.to if move.capture
piece.position = move.to
move.piece.mark_move if move.piece.class == Pawn
@moved = true
end
@board.draw
if @turn == :white
@turn = :black
else
@turn = :white
end
end
end
end
game = Game.new
game.play