-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
72 lines (64 loc) · 1.54 KB
/
game.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
require_relative 'piece'
require_relative 'board'
class Game
def initialize
@board = Board.new
@turn = :red
end
def play
until cant_move(@turn)
@board.display
take_turn
end
end
def take_turn
puts "It's #{@turn.to_s}'s turn. Type in a move or move sequence to make"
moves = convert_move(gets.chomp)
if moves.flatten.include?(nil)
puts "can only move on the board"
take_turn
else
spot = moves.shift
if @board[spot] == nil
puts "no piece over there to move. Enter a valid starting position"
take_turn
elsif @board[spot].color != @turn
puts "You can only move your own piece. Enter a valid move"
take_turn
elsif !@board[spot].valid_move_seq?(moves)
puts "That's not a valid move sequence"
take_turn
else
(@board[spot]).perform_moves!(moves)
switch_turn
end
end
end
def cant_move(color)
it_cant = true
pawns = @board.pieces.select{|piece| piece.color == color}
pawns.each{ |pawn| it_cant = it_cant && !pawn.can_move?}
it_cant
end
def switch_turn
puts "switching the turn"
if @turn == :black
@turn = :red
else
@turn = :black
end
end
def convert_move(string)
move = []
string.split.each{ |spot| move += [convert_spot(spot)]}
move
end
def convert_spot(string)
letter_hash = ("a".."h").to_a.zip(0..7).to_a.to_h
[string[1].to_i-1] + [letter_hash[string[0]]]
end
end
if __FILE__ == $PROGRAM_NAME
g = Game.new
g.play
end