Class: GamesAndRpgParadise::Minesweeper::Terminal::Field

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

Overview

GamesAndRpgParadise::Minesweeper::Terminal::Field

Constant Summary collapse

BOMB =
#

BOMB

#
'*'
COVER =
#

COVER

#
''
EMPTY =
#

EMPTY

#
' '
FLAG =
#

FLAG

#
'F'
WRONG =
#

WRONG

#
'X'
MINE_COUNT_TO_COLOUR =
#

MINE_COUNT_TO_COLOUR

Mappings of mine counts to colour names.

#
{
  1 => :cyan,
  2 => :green,
  3 => :red,
  4 => :blue,
  5 => :magenta,
  6 => :yellow,
  7 => :bright_cyan,
  8 => :bright_green
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeField

#

initialize

Create a Field instance

#


74
75
76
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 74

def initialize
  reset
end

Instance Attribute Details

#mine_countObject

#

The number of mines in nearby fields

#


65
66
67
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 65

def mine_count
  @mine_count
end

Instance Method Details

#cover?Boolean

#

cover?

Whether or not the field has cover

#

Returns:

  • (Boolean)


146
147
148
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 146

def cover?
  @cover
end

#flagObject

#

flag.

Toggle flag for a covered field.

#


94
95
96
97
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 94

def flag
  return unless cover?
  @flag = !@flag
end

#flag?Boolean

#

flag?

Whether or not there is a flag placed

#

Returns:

  • (Boolean)


106
107
108
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 106

def flag?
  @flag
end

#is_wrongObject Also known as: wrong

#

is_wrong

Mark as having wrongly placed flag

#


155
156
157
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 155

def is_wrong
  @wrong = true
end

#mine!Object

#

mine!

Mark as having a mine

#


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

def mine!
  @mine = true
end

#mine?Boolean

#

mine?

Whether or not the field has mine

#

Returns:

  • (Boolean)


126
127
128
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 126

def mine?
  @mine
end

#render(decorator: ::GamesAndRpgParadise::Minesweeper::Terminal::DEFAULT_DECORATOR) ⇒ String

#

render

Render the field through this method.

#

Parameters:

  • decorator (Proc) (defaults to: ::GamesAndRpgParadise::Minesweeper::Terminal::DEFAULT_DECORATOR)

    apply style formatting

Returns:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 180

def render(
    decorator: ::GamesAndRpgParadise::Minesweeper::Terminal::DEFAULT_DECORATOR
  )
  if !cover?
    if mine?
      BOMB
    elsif flag? && wrong?
      decorator[WRONG, :on_red]
    elsif !mine_count.zero?
      decorator[mine_count.to_s, MINE_COUNT_TO_COLOUR[mine_count]]
    else
      EMPTY
    end
  elsif flag?
    FLAG
  else
    COVER
  end
end

#resetObject

#

reset

#


81
82
83
84
85
86
87
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 81

def reset
  @flag = false
  @mine = false
  @cover = true
  @wrong = false
  @mine_count = 0
end

#uncoverObject

#

uncover

Uncover this field.

#


135
136
137
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 135

def uncover
  @cover = false
end

#wrong?Boolean

#

wrong?

Whether or not a flag is placed wrongly

#

Returns:

  • (Boolean)


166
167
168
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 166

def wrong?
  @wrong
end