-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
148 lines (123 loc) · 3.46 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require "./board.rb"
class Game
attr_accessor :board, :current_player, :moves
TURN = {:red => :black, :black => :red}
NAV = { 'j' => [-1, 0], 'k' => [0, 1], 'l' => [1, 0], 'i' => [0, -1] }
def initialize
@board = Board.new
@current_player = :red
@move = {:coordinates => [0,0], :crunch => false}
@moves = []
end
def run
until @board.over?
system("clear")
@board.render
begin
start_pos, move_sequence = get_input
@board[start_pos.first].perform_moves(move_sequence, current_player)
@current_player = TURN[@current_player]
rescue InvalidMoveError
puts "You can't do that"
retry
rescue NachYoPeaceError
puts "That's nach yo peaccce!"
retry
end
end
end
def get_input
puts "What piece would you like to move you silly user?"
start_pos = gets.chomp
puts "Where would you like to move it? (you may put in multiple spots for multi jumps)"
end_pos = gets.chomp
[parse_input(start_pos), parse_input(end_pos)]
end
def parse_input(move_sequence)
move_sequence = move_sequence.gsub(/\D/, '').split("")
moves = []
move_sequence.each_index do |idx|
next if idx.odd?
moves << [move_sequence[idx].to_i, move_sequence[idx + 1].to_i]
end
moves
end
def run_new
until @board.over?
begin
system("clear")
@board.render
puts "#{@current_player}, do ya dayam turn!"
if @move[:crunch] && moves.length > 1
do_the_move(moves)
reset_defaults
system("clear")
@board.render
end
get_move
rescue PickSpace
@moves << @board.cursor_pos
rescue EndSpace
@moves << @board.cursor_pos
@move[:crunch] = true
rescue InvalidMoveError
puts "You can't do that"
reset_defaults
retry
rescue NachYoPeaceError
puts "That's nach yo peaccce!"
reset_defaults
retry
end
end
end
def do_the_move(moves)
start_pos = moves.shift
start_pos = [start_pos[1], start_pos[0]]
seq_moves = []
@moves.each do |move|
seq_moves << [move[1], move[0]]
end
raise InvalidMoveError if @board[start_pos].nil?
@board[start_pos].perform_moves(seq_moves, @current_player)
@current_player = TURN[@current_player]
end
def reset_defaults
@move[:crunch] = false
@moves = []
end
def get_char
# Thanks to Jeff Fidler who thanked --> http://stackoverflow.com/questions/8142901/ruby-stdin-getc-does-not-read-char-on-reception
begin
system("stty raw -echo")
str = STDIN.getc
ensure
system("stty -raw echo")
end
str.chr
end
def get_move
print "\nUse i,j,k,l to steer the cursor. Use z to pick the checker you would like to move."
print"\nUse the space bar to select a position to move your piece."
print"\nYou can use z to select multiple spaces, and then space to multi jump!"
print"\nIf you do something you are not allowed it is the next player's turn. Unforgivable!"
command = get_char
case command
when ' '
raise EndSpace
when 'z'
raise PickSpace
when 'q'
else
@board.move_cursor(command)
return nil
end
move[:coordinates] = @board.cursor_pos
end
end
class PickSpace < RuntimeError
end
class EndSpace < RuntimeError
end
new_game = Game.new
new_game.run_new