Class: Minesweeprb::Game
- Inherits:
-
Object
- Object
- Minesweeprb::Game
- Defined in:
- lib/minesweeprb/game.rb
Constant Summary collapse
- SPRITES =
{ clock: '◷', clues: '◻➊➋➌➍➎➏➐➑'.chars.freeze, flag: '✖', lose_face: '☹', mark: '⍰', mine: '☀', play_face: '☺', square: '◼', win_face: '☻', }.freeze
Instance Attribute Summary collapse
-
#active_square ⇒ Object
Returns the value of attribute active_square.
-
#flagged_squares ⇒ Object
readonly
Returns the value of attribute flagged_squares.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#marked_squares ⇒ Object
readonly
Returns the value of attribute marked_squares.
-
#mines ⇒ Object
readonly
Returns the value of attribute mines.
-
#revealed_squares ⇒ Object
readonly
Returns the value of attribute revealed_squares.
-
#sprites ⇒ Object
readonly
Returns the value of attribute sprites.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #center ⇒ Object
- #cycle_flag ⇒ Object
- #end_time ⇒ Object
- #face ⇒ Object
- #game_over_message ⇒ Object
- #header ⇒ Object
- #header_segments ⇒ Object
-
#initialize(width:, height:, mines:, label: nil, sprites: SPRITES) ⇒ Game
constructor
rubocop:disable Lint/UnusedMethodArgument.
- #lost? ⇒ Boolean
- #move(direction) ⇒ Object
- #over? ⇒ Boolean
- #play_grid ⇒ Object
- #remaining_mines ⇒ Object
- #restart ⇒ Object
- #reveal_active_square ⇒ Object
- #started? ⇒ Boolean
- #time ⇒ Object
- #won? ⇒ Boolean
Constructor Details
#initialize(width:, height:, mines:, label: nil, sprites: SPRITES) ⇒ Game
rubocop:disable Lint/UnusedMethodArgument
27 28 29 30 31 32 33 |
# File 'lib/minesweeprb/game.rb', line 27 def initialize(width:, height:, mines:, label: nil, sprites: SPRITES) # rubocop:disable Lint/UnusedMethodArgument @width = width @height = height @mines = mines @sprites = sprites restart end |
Instance Attribute Details
#active_square ⇒ Object
Returns the value of attribute active_square.
17 18 19 |
# File 'lib/minesweeprb/game.rb', line 17 def active_square @active_square end |
#flagged_squares ⇒ Object (readonly)
Returns the value of attribute flagged_squares.
17 18 19 |
# File 'lib/minesweeprb/game.rb', line 17 def flagged_squares @flagged_squares end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
17 18 19 |
# File 'lib/minesweeprb/game.rb', line 17 def height @height end |
#marked_squares ⇒ Object (readonly)
Returns the value of attribute marked_squares.
17 18 19 |
# File 'lib/minesweeprb/game.rb', line 17 def marked_squares @marked_squares end |
#mines ⇒ Object (readonly)
Returns the value of attribute mines.
17 18 19 |
# File 'lib/minesweeprb/game.rb', line 17 def mines @mines end |
#revealed_squares ⇒ Object (readonly)
Returns the value of attribute revealed_squares.
17 18 19 |
# File 'lib/minesweeprb/game.rb', line 17 def revealed_squares @revealed_squares end |
#sprites ⇒ Object (readonly)
Returns the value of attribute sprites.
17 18 19 |
# File 'lib/minesweeprb/game.rb', line 17 def sprites @sprites end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
17 18 19 |
# File 'lib/minesweeprb/game.rb', line 17 def start_time @start_time end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
17 18 19 |
# File 'lib/minesweeprb/game.rb', line 17 def width @width end |
Instance Method Details
#center ⇒ Object
50 51 52 |
# File 'lib/minesweeprb/game.rb', line 50 def center [(width / 2).floor, (height / 2).floor] end |
#cycle_flag ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/minesweeprb/game.rb', line 116 def cycle_flag return if over? || revealed_squares.empty? || revealed_squares.include?(active_square) if flagged_squares.include?(active_square) @flagged_squares -= [active_square] @marked_squares += [active_square] elsif marked_squares.include?(active_square) @marked_squares -= [active_square] elsif flagged_squares.length < mines @flagged_squares += [active_square] end end |
#end_time ⇒ Object
54 55 56 |
# File 'lib/minesweeprb/game.rb', line 54 def end_time @end_time || now end |
#face ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/minesweeprb/game.rb', line 89 def face if won? sprites[:win_face] elsif lost? sprites[:lose_face] else sprites[:play_face] end end |
#game_over_message ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/minesweeprb/game.rb', line 173 def if won? "#{sprites[:win_face]} YOU WON #{sprites[:win_face]}" else "#{sprites[:lose_face]} GAME OVER #{sprites[:lose_face]}" end end |
#header ⇒ Object
99 100 101 102 103 |
# File 'lib/minesweeprb/game.rb', line 99 def header "#{sprites[:mine]} #{remaining_mines.to_s.rjust(3, '0')} " \ "#{face} " \ "#{sprites[:clock]} #{time.round.to_s.rjust(3, '0')}" end |
#header_segments ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/minesweeprb/game.rb', line 105 def header_segments [ [:mine, sprites[:mine]], [nil, " #{remaining_mines.to_s.rjust(3, '0')} "], [:face, face], [nil, ' '], [:clock, sprites[:clock]], [nil, " #{time.round.to_s.rjust(3, '0')}"], ] end |
#lost? ⇒ Boolean
165 166 167 |
# File 'lib/minesweeprb/game.rb', line 165 def lost? revealed_squares.intersect?(@mined_squares) end |
#move(direction) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/minesweeprb/game.rb', line 64 def move(direction) return if over? x, y = active_square case direction when :up then y -= 1 when :down then y += 1 when :left then x -= 1 when :right then x += 1 end self.active_square = [x, y] end |
#over? ⇒ Boolean
169 170 171 |
# File 'lib/minesweeprb/game.rb', line 169 def over? won? || lost? end |
#play_grid ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/minesweeprb/game.rb', line 137 def play_grid height.times.map do |y| width.times.map do |x| square = [x, y] if @mined_squares.include?(square) && (revealed_squares.include?(square) || over?) sprites[:mine] elsif revealed_squares.include?(square) || over? sprites[:clues][@grid[y][x]] elsif flagged_squares.include?(square) sprites[:flag] elsif marked_squares.include?(square) sprites[:mark] else sprites[:square] end end end end |
#remaining_mines ⇒ Object
46 47 48 |
# File 'lib/minesweeprb/game.rb', line 46 def remaining_mines mines - flagged_squares.length end |
#restart ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/minesweeprb/game.rb', line 35 def restart @active_square = center @flagged_squares = [] @marked_squares = [] @mined_squares = [] @revealed_squares = [] @grid = Array.new(height) { Array.new(width) } @start_time = nil @end_time = nil end |
#reveal_active_square ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/minesweeprb/game.rb', line 129 def reveal_active_square return if over? || flagged_squares.include?(active_square) x, y = active_square reveal_square(x, y) @end_time = now if over? end |
#started? ⇒ Boolean
157 158 159 |
# File 'lib/minesweeprb/game.rb', line 157 def started? !over? && revealed_squares.any? end |
#time ⇒ Object
58 59 60 61 62 |
# File 'lib/minesweeprb/game.rb', line 58 def time return 0 unless start_time end_time - start_time end |
#won? ⇒ Boolean
161 162 163 |
# File 'lib/minesweeprb/game.rb', line 161 def won? !lost? && revealed_squares.count == (width * height) - mines end |