-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw6assignment.rb
104 lines (82 loc) · 2.16 KB
/
hw6assignment.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
# University of Washington, Programming Languages, Homework 6, hw6runner.rb
# This is the only file you turn in, so do not modify the other files as
# part of your solution.
class MyPiece < Piece
# The constant All_My_Pieces should be declared here
def initialize(point_array, board)
super(point_array, board)
end
def self.next_piece(board)
if board.set_check
board.set_check = false
piece = [[[0,0]]]
else
piece = All_Pieces.sample
end
MyPiece.new(piece, board)
end
# your enhancements here
#
All_My_Pieces = All_Pieces
All_Pieces << [ [[0,0], [-1, 0], [-2, 0], [1, 0], [2, 0]],
[[0,0], [0, 1], [0, 2], [0, -1], [0, -2]]]
All_Pieces << rotations([[0, 0], [0, 1], [1,1]])
All_Pieces << rotations([[0,0], [-1,0], [-1,1], [0, 1], [1, 1]])
end
class MyBoard < Board
# your enhancements here
attr_accessor :set_check
def initialize(game)
super(game)
@set_check = false
end
def rotate180
if !game_over? and @game.is_running?
@current_block.move(0, 0, 1)
@current_block.move(0, 0, 1)
end
draw
end
def store_current
locations = @current_block.current_rotation
displacement = @current_block.position
locations.each_index{|index|
current = locations[index];
@grid[current[1]+displacement[1]][current[0]+displacement[0]] =
@current_pos[index]
}
remove_filled
@delay = [@delay - 2, 80].max
end
def check
return if @set_check or score < 100
@set_check = true
@score -= 100
end
def next_piece
@current_block = MyPiece.next_piece(self)
@current_pos = nil
end
end
class MyTetris < Tetris
# your enhancements here
def initialize
super
end
def set_board
@canvas = TetrisCanvas.new
@board = MyBoard.new(self)
@canvas.place(@board.block_size * @board.num_rows + 3,
@board.block_size * @board.num_columns + 6, 24, 80)
@board.draw
end
def check
@board.check
update_score
end
def key_bindings
super
@root.bind('c', proc {self.check})
@root.bind('u', proc {@board.rotate180})
end
end