Class: GamesAndRpgParadise::Minesweeper::GridPlacer

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb

Overview

GamesAndRpgParadise::Minesweeper::GridPlacer

Constant Summary collapse

N =
"\n"
DEFAULT_GRID_SIZE =
#

DEFAULT_GRID_SIZE

#
18
DEFAULT_N_MINES_TO_PLACE =
#

DEFAULT_N_MINES_TO_PLACE

#
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grid_dimension = DEFAULT_GRID_SIZE, how_many_mines_to_place = DEFAULT_N_MINES_TO_PLACE, use_minesweeper_layout = true, run_already = true) ⇒ GridPlacer

#

initialize

If the third argument, ‘use_minesweeper_layout`, is true, we call set_minesweeper_grid to transform our map to a Minesweeper grid layout.

#


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
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 64

def initialize(
    grid_dimension          = DEFAULT_GRID_SIZE, 
    how_many_mines_to_place = DEFAULT_N_MINES_TO_PLACE, # ← How many mines to place.
    use_minesweeper_layout  = true,
    run_already             = true
  )
  reset
  set_grid_dimension(
    grid_dimension
  )
  set_n_mines(
    how_many_mines_to_place
  )
  @use_minesweeper_layout = use_minesweeper_layout
  # ========================================================================= #
  # === Handle blocks next
  # ========================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ======================================================================= #
    # === :do_not_run_yet
    # ======================================================================= #
    when :do_not_run_yet
      run_already = false
    end
  end
  run if run_already
end

Instance Attribute Details

#grid_dimensionObject

Returns the value of attribute grid_dimension.



45
46
47
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 45

def grid_dimension
  @grid_dimension
end

Instance Method Details

#be_verbose?Boolean

#

be_verbose?

#

Returns:

  • (Boolean)


115
116
117
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 115

def be_verbose?
  @be_verbose
end

#count_n_placed_minesObject

#

count_n_placed_mines

Gives back how many mines are already on the game map in total.

#


184
185
186
187
188
189
190
191
192
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 184

def count_n_placed_mines
  n_mines = 0
  @hash_grid.each_pair { |key, value|
    value.each_pair { |inner_key, inner_value|
      n_mines += 1 if inner_value == 'm'
    }
  }
  return n_mines
end

#count_surrounding_mines(around_this_field) ⇒ Object

#

count_surrounding_mines

Will tell you how many mines surround a given field.

Pass it the position of the field in question. Note that this method will use return_adjacent_grids

#


319
320
321
322
323
324
325
326
327
328
329
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 319

def count_surrounding_mines(around_this_field)
  unless around_this_field.is_a? Array
    e 'We require an Array, not a '+around_this_field.class.to_s+'. Exiting.'
    exit
  end 
  counter = 0 # default
  return_adjacent_grids(around_this_field).each { |val|
    counter += 1 if @hash_grid[val.first.to_s][val[1].to_s] == 'm'
  }
  return counter
end

#display_game_mapObject Also known as: show

#

display_game_map

This method will show the game-map, onto the commandline.

#


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 239

def display_game_map
  @grid_dimension.downto(1) { |outer_counter|
    ee '  '
    result = '%2d' % outer_counter.to_s
    if @use_colours
      result = Colours.simp(result)
    end
    ee result
    ee ' |  '
    1.upto(@grid_dimension) { |inner_counter|
      _ = @hash_grid[inner_counter.to_s][outer_counter.to_s].dup
      if (_ == 'm' ) and @use_colours
        _ = Colours.steelblue(_)
      end
      result = _.dup
      result << ' '
      ee result
    }
    e # Newline is nice,
  }
  e  '     |  '
  ee '     |  '
  @grid_dimension.times { |i|
    result = (i+1).to_s+' '
    if @use_colours
      result = Colours.simp(result)
    end
    ee result
  }
  e
end

#hash_grid=(i) ⇒ Object

#

hash_grid=

#


348
349
350
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 348

def hash_grid=(i)
  @hash_grid = i
end

#hash_grid?Boolean Also known as: hash_grid

#

hash_grid?

#

Returns:

  • (Boolean)


341
342
343
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 341

def hash_grid?
  @hash_grid
end

#initialize_the_game_mapObject

The keys are all strings. No exception to this rule!

#


223
224
225
226
227
228
229
230
231
232
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 223

def initialize_the_game_map
  @grid_dimension.times { |outer_loop|
    key1 = (outer_loop+1).to_s
    @hash_grid[key1] = {}
    @grid_dimension.times { |inner_loop|
      key2 = (inner_loop+1).to_s
      @hash_grid[key1][key2] = '0' # 0 für empty. Default.
    }
  }
end

#n_mines?Boolean Also known as: n_mines

#

n_mines?

#

Returns:

  • (Boolean)


334
335
336
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 334

def n_mines? # How many mines are on the game field.
  @n_mines
end

#place_minesObject

#

place_mines

This method will randomly place the available mines.

#


170
171
172
173
174
175
176
177
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 170

def place_mines
  loop {
    rand_key1 = (rand(@grid_dimension)+1).to_s
    rand_key2 = (rand(@grid_dimension)+1).to_s
    @hash_grid[rand_key1][rand_key2] = 'm'
    break if count_n_placed_mines == @n_mines
  }
end

#resetObject

#

reset (reset tag)

#


97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 97

def reset
  # ========================================================================= #
  # === @hash_grid
  # ========================================================================= #
  @hash_grid = {} # stores multi-index
  # ========================================================================= #
  # === @be_verbose
  # ========================================================================= #
  @be_verbose = true
  # ========================================================================= #
  # === @use_colours
  # ========================================================================= #
  @use_colours = true
end

#return_adjacent_grids(start_grid = [3,3]) ⇒ Object

#

return_adjacent_grids

This helper method simply returns the grids which touch a given grid. It will give you an array as its result. If you want to get the info from a specific field, you must pass an array of its x-y coordinates.

We only return non-nil entries, thus ONLY entries which are part of the game grid layout! (If it’d do otherwise, it’d be a bug.)

Example:

ff 33 is passed, we will return:

   24,34,44
   23, x 43
   22,32,42

If we are at 11, the situation would be this:

12 22
x  21
#


294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 294

def return_adjacent_grids(
    start_grid = [3,3]
  )
  x = start_grid[0].to_i # x achse
  y = start_grid[1].to_i
  array = []
  array << [x-1,y+1] unless x == 1 || y == @grid_dimension
  array << [x,y+1]   unless           y == @grid_dimension
  array << [x+1,y+1] unless x == @grid_dimension || y == @grid_dimension
  array << [x-1,y]   unless x == 1
  array << [x+1,y]   unless x == @grid_dimension
  array << [x-1,y-1] unless x == 1 || y == 1
  array << [x,y-1]   unless y == 1
  array << [x+1,y-1] unless x == @grid_dimension || y == 1
  return array
end

#return_random_field(be_verbose = be_verbose?) ) ⇒ Object

#

return_random_field

This method will return a random field. Or better said, it will return the content of it.

#


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 200

def return_random_field(be_verbose = be_verbose?)
  key1 = rand(@grid_dimension)+1 
  key2 = rand(@grid_dimension)+1
  key1 = key1.to_s
  key2 = key2.to_s
  if be_verbose
    result = @hash_grid[key1][key2]
    if result == 'm'
      e key1+', '+key2+' is a mine!'
    else
      e key1+', '+key2+' has '+result+' nearby mines.'
    end
  end
  return @hash_grid[key1][key2]
end

#runObject

#

run

#


355
356
357
358
359
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 355

def run
  initialize_the_game_map
  place_mines
  set_minesweeper_grid if @use_minesweeper_layout
end

#set_grid_dimension(i = DEFAULT_GRID_SIZE) ⇒ Object

#

set_grid_dimension

#


129
130
131
132
133
134
135
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 129

def set_grid_dimension(
    i = DEFAULT_GRID_SIZE
  )
  i = i.first if i.is_a? Array
  i = DEFAULT_GRID_SIZE if i.nil?
  @grid_dimension = i.to_i # Must be Integer.
end

#set_minesweeper_grid(down_to_n = 1) ⇒ Object

#

set_minesweeper_grid

This will transform the map to match up a minesweeper gameplay. Do this only on startup of the application. To do this, we need to place a number around the fields next to a minefield, and thus can invoke the method only after mines were placed.

#


154
155
156
157
158
159
160
161
162
163
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 154

def set_minesweeper_grid(down_to_n = 1)
  @grid_dimension.downto(down_to_n) { |outer_counter|
    1.upto(@grid_dimension) { |inner_counter| 
      new_value = count_surrounding_mines( [outer_counter, inner_counter] )
      unless @hash_grid[outer_counter.to_s][inner_counter.to_s] == 'm'
        @hash_grid[outer_counter.to_s][inner_counter.to_s] = new_value.to_s
      end
    }
  }
end

#set_n_fields(i) ⇒ Object

#

set_n_fields

#


140
141
142
143
144
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 140

def set_n_fields(i)
  set_grid_dimension(
    Math.sqrt(i)
  )
end

#set_n_mines(i) ⇒ Object

#

set_n_mines

#


122
123
124
# File 'lib/games_and_rpg_paradise/games/minesweeper/grid_placer.rb', line 122

def set_n_mines(i)
  @n_mines = i
end