Class: GamesAndRpgParadise::Minesweeper::Terminal::Field
- Inherits:
-
Object
- Object
- GamesAndRpgParadise::Minesweeper::Terminal::Field
- 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
-
#mine_count ⇒ Object
# The number of mines in nearby fields.
Instance Method Summary collapse
-
#cover? ⇒ Boolean
# === cover?.
-
#flag ⇒ Object
# === flag.
-
#flag? ⇒ Boolean
# === flag?.
-
#initialize ⇒ Field
constructor
# === initialize.
-
#is_wrong ⇒ Object
(also: #wrong)
# === is_wrong.
-
#mine! ⇒ Object
# === mine!.
-
#mine? ⇒ Boolean
# === mine?.
-
#render(decorator: ::GamesAndRpgParadise::Minesweeper::Terminal::DEFAULT_DECORATOR) ⇒ String
# === render.
-
#reset ⇒ Object
# === reset ========================================================================= #.
-
#uncover ⇒ Object
# === uncover.
-
#wrong? ⇒ Boolean
# === wrong?.
Constructor Details
#initialize ⇒ Field
#
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_count ⇒ Object
#
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
#
146 147 148 |
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 146 def cover? @cover end |
#flag ⇒ Object
#
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
#
106 107 108 |
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 106 def flag? @flag end |
#is_wrong ⇒ Object 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
#
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.
#
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 |
#reset ⇒ Object
#
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 |
#uncover ⇒ Object
#
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
#
166 167 168 |
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/field.rb', line 166 def wrong? @wrong end |