rubtris 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/rubtris +1 -3
- data/lib/block.rb +1 -1
- data/lib/board.rb +29 -24
- data/lib/rubtris.rb +3 -140
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1249241ab00bf1ec7d0a107b099df5363fc721d2
|
4
|
+
data.tar.gz: ef04585863280c256655954a961719a24d395a12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15d5fd979d5665eaa68a538d922ddcfeb8c4395b79eec664ae37383504acbfddb06f9e7a2387eacb1a8ad2342826c6c362ebf406ba4c301cfb675c80f84a00e0
|
7
|
+
data.tar.gz: e7fba568c8e2f21fe8dcb552c568b810e8f0dc41f5952073ce1d4d3c82480604200e192c49ac0b646ffb7e96a10341c7e7610efd33bfcf4336903955e3ab5cdc
|
data/bin/rubtris
CHANGED
data/lib/block.rb
CHANGED
@@ -13,7 +13,7 @@ class Block
|
|
13
13
|
Pattern.new(:right_l, [[0, 2], [1, 2], [2, 2], [2, 1]], 3, :green),
|
14
14
|
Pattern.new(:t_block, [[1, 1], [2, 0], [2, 1], [2, 2]], 3, :red),
|
15
15
|
Pattern.new(:n_block_1, [[0, 0], [1, 0], [1, 1], [2, 1]], 3, :yellow),
|
16
|
-
Pattern.new(:n_block_2, [[0, 1], [1, 1], [1, 0], [2, 0]], 3, :
|
16
|
+
Pattern.new(:n_block_2, [[0, 1], [1, 1], [1, 0], [2, 0]], 3, :magenta)
|
17
17
|
]
|
18
18
|
|
19
19
|
|
data/lib/board.rb
CHANGED
@@ -14,7 +14,7 @@ class Board
|
|
14
14
|
def initialize
|
15
15
|
@grid, @over, @completed_lines = Array.new(HEIGHT) { Array.new(WIDTH) }, false, 0
|
16
16
|
@start_time, @time_limit, @line_limit = Time.now, nil, nil
|
17
|
-
@
|
17
|
+
@level = 0, 0
|
18
18
|
end
|
19
19
|
|
20
20
|
def [](coord)
|
@@ -29,7 +29,7 @@ class Board
|
|
29
29
|
str = ""
|
30
30
|
@grid.each_with_index do |row, i|
|
31
31
|
row.each do |pos|
|
32
|
-
str += pos.nil? ? " " : "
|
32
|
+
str += pos.nil? ? " " : "[]".colorize(pos.pattern.color)
|
33
33
|
end
|
34
34
|
str+= "*"
|
35
35
|
if i == 1
|
@@ -44,7 +44,7 @@ class Board
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def summary_s
|
47
|
-
"LINES: #{@completed_lines} LEVEL: #{level}
|
47
|
+
"LINES: #{@completed_lines} LEVEL: #{level}"
|
48
48
|
end
|
49
49
|
|
50
50
|
def level
|
@@ -85,20 +85,22 @@ class Board
|
|
85
85
|
true
|
86
86
|
end
|
87
87
|
|
88
|
+
def check_spaces(old_spaces, new_spaces)
|
89
|
+
new_territory = new_spaces - old_spaces
|
90
|
+
return false unless new_spaces.all? { |coord| on_board?(coord) }
|
91
|
+
return false if any_filled?(new_territory)
|
92
|
+
true
|
93
|
+
end
|
94
|
+
|
88
95
|
def change_direction(block, options = {})
|
89
96
|
i, j, turn = options[:i], options[:j], options[:turn]
|
90
97
|
new_upper_left = (i && j) ? [block.upper_left.first + i, block.upper_left.last + j] : block.upper_left
|
91
98
|
new_rotation = (turn) ? block.rotation + turn : block.rotation
|
92
99
|
old_spaces = block.spaces_occupied
|
93
100
|
new_spaces = block.spaces_occupied(pos: new_upper_left, rotation: new_rotation)
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
# p "old_spaces: #{old_spaces} new_spaces: #{new_spaces} old_territory: #{old_territory} new_territory: #{new_territory}"
|
98
|
-
return false unless new_spaces.all? { |coord| on_board?(coord) }
|
99
|
-
return false if any_filled?(new_territory)
|
100
|
-
remove_from_spaces(old_territory)
|
101
|
-
add_to_spaces(block, new_territory)
|
101
|
+
return false unless check_spaces(old_spaces, new_spaces)
|
102
|
+
remove_from_spaces(old_spaces)
|
103
|
+
add_to_spaces(block, new_spaces)
|
102
104
|
block.upper_left, block.rotation = new_upper_left, new_rotation
|
103
105
|
true
|
104
106
|
end
|
@@ -108,15 +110,15 @@ class Board
|
|
108
110
|
end
|
109
111
|
|
110
112
|
def push_selected_down
|
111
|
-
unless change_direction(@selected, i: 5, j: 0)
|
112
|
-
move_selected_down
|
113
|
-
end
|
113
|
+
move_selected_down unless change_direction(@selected, i: 5, j: 0)
|
114
114
|
end
|
115
115
|
|
116
116
|
def move_to_bottom
|
117
|
-
|
118
|
-
|
119
|
-
|
117
|
+
while change_direction(@selected, i: 1, j: 0)
|
118
|
+
old_spaces = @selected.spaces_occupied
|
119
|
+
new_upper_left = [@selected.upper_left.first + 1, @selected.upper_left.last]
|
120
|
+
new_spaces = @selected.spaces_occupied(pos: new_upper_left, rotation: @selected.rotation)
|
121
|
+
break unless check_spaces(old_spaces, new_spaces)
|
120
122
|
end
|
121
123
|
end
|
122
124
|
|
@@ -124,7 +126,9 @@ class Board
|
|
124
126
|
unless change_direction(@selected, i: 1, j: 0)
|
125
127
|
remove_completed_line_and_increase_score
|
126
128
|
@over = !add_block
|
129
|
+
return false
|
127
130
|
end
|
131
|
+
true
|
128
132
|
end
|
129
133
|
|
130
134
|
def move_selected_left
|
@@ -155,16 +159,17 @@ class Board
|
|
155
159
|
lines = 0
|
156
160
|
i = HEIGHT - 1
|
157
161
|
until i == 0
|
162
|
+
p i
|
158
163
|
spaces_on_row = [i].product((0...WIDTH).to_a)
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
164
|
+
|
165
|
+
if all_filled?(spaces_on_row)
|
166
|
+
remove_and_shift(i)
|
167
|
+
lines += 1
|
168
|
+
else
|
169
|
+
i -= 1
|
170
|
+
end
|
165
171
|
end
|
166
172
|
@completed_lines += lines
|
167
|
-
@score += (lines * SCORE_MULTIPLIER) ** 2
|
168
173
|
end
|
169
174
|
|
170
175
|
def over?
|
data/lib/rubtris.rb
CHANGED
@@ -1,141 +1,4 @@
|
|
1
|
-
require_relative '
|
2
|
-
require 'timeout'
|
3
|
-
require 'io/console'
|
4
|
-
require_relative 'menu'
|
1
|
+
require_relative 'game'
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
REFRESH_RATE = 0.1
|
9
|
-
BEGINNING_ADVANCE_RATE = 0.3
|
10
|
-
MINIMUM_ADVANCE_RATE = 0.05
|
11
|
-
DIF_BTWN_LEVEL = 0.04
|
12
|
-
|
13
|
-
|
14
|
-
def run
|
15
|
-
set_up_game
|
16
|
-
until over?
|
17
|
-
do_turn
|
18
|
-
end
|
19
|
-
end_game
|
20
|
-
run if !force_quit? && play_again
|
21
|
-
end
|
22
|
-
|
23
|
-
def get_mode
|
24
|
-
options = [
|
25
|
-
{title: "Unlimited", type: :none, value: true},
|
26
|
-
{title: "Timed", type: :increment, value: 3, unit: 'min.'},
|
27
|
-
{title: "Lines", type: :increment, value: 40}
|
28
|
-
]
|
29
|
-
prompt = "Welcome to Rubtris.\nSelect the mode you want to play!"
|
30
|
-
menu = Menu.new(options, prompt)
|
31
|
-
@game_mode = menu.open
|
32
|
-
end
|
33
|
-
|
34
|
-
def play_again
|
35
|
-
options = [
|
36
|
-
{title: "Yes", type: :none, value: true},
|
37
|
-
{title: "No", type: :none, value: false}
|
38
|
-
]
|
39
|
-
prompt = "#{@board.summary_s}\nPlay again?"
|
40
|
-
menu = Menu.new(options, prompt)
|
41
|
-
menu.open[:value]
|
42
|
-
end
|
43
|
-
|
44
|
-
def set_up_game
|
45
|
-
@board = Board.new
|
46
|
-
STDIN.echo = false
|
47
|
-
@force_quit = false
|
48
|
-
@board.add_block
|
49
|
-
@board.render
|
50
|
-
@start_time, @last_advanced, @level = Time.now, Time.now, 0
|
51
|
-
@time_limit, @line_limit = nil, nil
|
52
|
-
config = get_mode
|
53
|
-
add_win_condition(config[:title], config[:value])
|
54
|
-
end
|
55
|
-
|
56
|
-
def do_turn
|
57
|
-
auto_advance if auto_advance_needed?
|
58
|
-
system "clear" or system "cls"
|
59
|
-
@board.render
|
60
|
-
action = get_input
|
61
|
-
take_action(action) if action
|
62
|
-
end
|
63
|
-
|
64
|
-
def end_game
|
65
|
-
STDIN.echo = true
|
66
|
-
end
|
67
|
-
|
68
|
-
def current_advance_rate
|
69
|
-
[BEGINNING_ADVANCE_RATE - (@level * DIF_BTWN_LEVEL), MINIMUM_ADVANCE_RATE].max
|
70
|
-
end
|
71
|
-
|
72
|
-
def auto_advance_needed?
|
73
|
-
Time.now - @last_advanced > current_advance_rate
|
74
|
-
end
|
75
|
-
|
76
|
-
def auto_advance
|
77
|
-
@last_advanced = Time.now
|
78
|
-
@board.move_selected_down
|
79
|
-
end
|
80
|
-
|
81
|
-
def get_input
|
82
|
-
action = nil
|
83
|
-
begin
|
84
|
-
action = Timeout::timeout(REFRESH_RATE) { STDIN.getch }
|
85
|
-
rescue
|
86
|
-
end
|
87
|
-
action
|
88
|
-
end
|
89
|
-
|
90
|
-
def force_quit?
|
91
|
-
@force_quit
|
92
|
-
end
|
93
|
-
|
94
|
-
def take_action(action)
|
95
|
-
case action.downcase
|
96
|
-
when "w"
|
97
|
-
@board.move_to_bottom
|
98
|
-
when "a"
|
99
|
-
@board.move_selected_left
|
100
|
-
when "s"
|
101
|
-
@board.move_selected_down
|
102
|
-
when "d"
|
103
|
-
@board.move_selected_right
|
104
|
-
when "\r"
|
105
|
-
@board.push_selected_down
|
106
|
-
when "e"
|
107
|
-
@board.rotate_selected_right
|
108
|
-
when "q"
|
109
|
-
@board.rotate_selected_left
|
110
|
-
when "["
|
111
|
-
@board.rotate_selected_left
|
112
|
-
when "]"
|
113
|
-
@board.rotate_selected_right
|
114
|
-
when "\e"
|
115
|
-
@force_quit = true
|
116
|
-
end
|
117
|
-
nil
|
118
|
-
end
|
119
|
-
|
120
|
-
def over?
|
121
|
-
force_quit? || @board.over? || over_time? || over_lines?
|
122
|
-
end
|
123
|
-
|
124
|
-
def over_time?
|
125
|
-
@time_limit && (@last_advanced - @start_time) >= @time_limit
|
126
|
-
end
|
127
|
-
|
128
|
-
def over_lines?
|
129
|
-
@line_limit && @board.completed_lines >= @line_limit
|
130
|
-
end
|
131
|
-
|
132
|
-
def add_win_condition(condition, value)
|
133
|
-
case condition
|
134
|
-
when "Timed"
|
135
|
-
@time_limit = value * 60
|
136
|
-
when "Lines"
|
137
|
-
@line_limit = value
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
3
|
+
t = Game.new
|
4
|
+
t.run
|
metadata
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubtris
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Henderson
|
8
|
-
- Danny Burt
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
@@ -15,17 +14,19 @@ dependencies:
|
|
15
14
|
name: colorize
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
16
|
requirements:
|
18
|
-
- - "
|
17
|
+
- - "~>"
|
19
18
|
- !ruby/object:Gem::Version
|
20
19
|
version: '0'
|
21
20
|
type: :runtime
|
22
21
|
prerelease: false
|
23
22
|
version_requirements: !ruby/object:Gem::Requirement
|
24
23
|
requirements:
|
25
|
-
- - "
|
24
|
+
- - "~>"
|
26
25
|
- !ruby/object:Gem::Version
|
27
26
|
version: '0'
|
28
|
-
description:
|
27
|
+
description: Rubtris is a Ruby clone of everyone's favorite tetromino-based puzzle
|
28
|
+
game, played entirely on the command line. Install the Gem and simply run 'rubtris'
|
29
|
+
on the command line to play. Thanks to Danny Burt for his contributions.
|
29
30
|
email: benjamin.c.henderson@gmail.com
|
30
31
|
executables:
|
31
32
|
- rubtris
|
@@ -61,5 +62,5 @@ rubyforge_project:
|
|
61
62
|
rubygems_version: 2.2.2
|
62
63
|
signing_key:
|
63
64
|
specification_version: 4
|
64
|
-
summary: Command line
|
65
|
+
summary: Command line puzzle game action!
|
65
66
|
test_files: []
|