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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/bin/rubtris +1 -3
  3. data/lib/block.rb +1 -1
  4. data/lib/board.rb +29 -24
  5. data/lib/rubtris.rb +3 -140
  6. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78200817740afc610e6c602f5930fc1cea1cf46f
4
- data.tar.gz: 298413daeec207e1dae84d8f3444648e33c57413
3
+ metadata.gz: 1249241ab00bf1ec7d0a107b099df5363fc721d2
4
+ data.tar.gz: ef04585863280c256655954a961719a24d395a12
5
5
  SHA512:
6
- metadata.gz: a732126696a3ce05eb1567dc755e9a8901ee78f056ffd6cf0955c4c9a5b5a50ef223885cc156506c8295df06183e7699c17644dcb3ffc7f9e60422373aa29d55
7
- data.tar.gz: d54ed54432f0e9c2ce6afb5328cee4e50ad97596b8bbec9376ddfa29589c0e53664061e52cbca5f22ca8f206a1df83a918fda9a1438916d9e02a8a31829fbd9b
6
+ metadata.gz: 15d5fd979d5665eaa68a538d922ddcfeb8c4395b79eec664ae37383504acbfddb06f9e7a2387eacb1a8ad2342826c6c362ebf406ba4c301cfb675c80f84a00e0
7
+ data.tar.gz: e7fba568c8e2f21fe8dcb552c568b810e8f0dc41f5952073ce1d4d3c82480604200e192c49ac0b646ffb7e96a10341c7e7610efd33bfcf4336903955e3ab5cdc
data/bin/rubtris CHANGED
@@ -1,5 +1,3 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rubtris'
4
- t = Rubtris.new
5
- t.run
3
+ require 'rubtris'
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, :black)
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
- @score, @level = 0, 0
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? ? " " : " ".colorize(background: pos.pattern.color)
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} SCORE: #{@score}"
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
- old_territory = old_spaces - new_spaces
95
- new_territory = new_spaces - old_spaces
96
- # p "i: #{i} j: #{j} turn: #{turn} new_upper_left: #{new_upper_left} new_rotation: #{new_rotation}"
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
- id = @selected.object_id
118
- while change_direction(@selected, i: 1, j: 0) && id == @selected.object_id
119
- move_selected_down
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
- if all_filled?(spaces_on_row)
160
- remove_and_shift(i)
161
- lines += 1
162
- else
163
- i -= 1
164
- end
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 'board'
2
- require 'timeout'
3
- require 'io/console'
4
- require_relative 'menu'
1
+ require_relative 'game'
5
2
 
6
- class Rubtris
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.5
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: Command line Tetrino based puzzle game!
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 Tetrino based puzzle game!
65
+ summary: Command line puzzle game action!
65
66
  test_files: []