-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrop7.rb
69 lines (61 loc) · 1.24 KB
/
drop7.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
class Board
WALL = :WALL
STONE = :STONE
BROKEN_STONE = :BROKEN_STONE
PIECE = {1 => "1", 2 => "2", 3 => "3", 4 => "4", 5 => "5", 6 => "6", 7 => "7", nil => " ", WALL => "=", STONE => 'O', BROKEN_STONE => '*'}
attr_reader :current_piece
def initialize
@board = []
((7 * 7) .. (7 * 7 + 6)).each do |i|
@board[i] = WALL
end
srand
next_piece
end
def next_piece
@current_piece = 1 + rand(7)
end
def show
puts("abcdefg")
8.times do |y|
7.times do |x|
putc(PIECE[@board[y * 7 + x]])
end
puts
end
end
def put_piece(pos)
raise "bas pos" unless pos and puttable?(pos)
pos_ = pos
while not @board[pos_ + 7]
pos_ += 7
end
@board[pos_] = @current_piece
end
def puttable?(pos)
@board[pos].nil?
end
def game_over?
7.times do |x|
return false if puttable?(x)
end
true
end
end
if __FILE__ == $0
board = Board.new
loop do
board.show
break if board.game_over?
print("#{Board::PIECE[board.current_piece]}> ")
STDOUT.flush
pos = gets
begin
board.put_piece("abcdefg".index(pos[0]))
board.next_piece
rescue RuntimeError => e
puts("Error: #{e}")
end
end
puts("Game Over...")
end