-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
67 lines (57 loc) · 1.42 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
require_relative 'checkers_board'
class Game
attr_reader :board
ROWS = [nil, 7, 6, 5, 4, 3, 2, 1, 0]
COLUMNS = { a: 0, b: 1, c: 2, d: 3, e: 4, f: 5, g: 6, h: 7 }
def initialize
@board = Board.new
@board.place_red_checkers
@board.place_black_checkers
end
def play
until game_over?
display_board
do_move
end
end_game
end
def do_move
puts "Select piece"
start_square = parse_input(gets.chomp)
puts "Where do you want to move it to?"
end_square = parse_input(gets.chomp)
try_slide = board[start_square].perform_slide(start_square, end_square)
board[start_square].perform_jump(start_square, end_square) unless try_slide
end
def game_over?
false
end
def end_game
display_board
puts "Somebody wins!"
end
def parse_input(input)
row = ROWS[input[1].to_i]
column = COLUMNS[input[0].to_sym]
[row, column]
end
def display_board
puts " ________________________"
board.grid.each_with_index do |row, i|
print "#{ -1 * (i - 8) } |"
row.each_with_index do |square, j|
if square.nil? && (i + j).odd?
print "|||"
elsif square.nil?
print " "
else
print "(#{square.token})"
end
end
print "|\n"
end
puts " ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾"
puts " a b c d e f g h"
puts ""
end
end