Class: GridLayout
- Inherits:
-
Object
- Object
- GridLayout
- Includes:
- Colours
- Defined in:
- lib/games_paradise/schiffe_versenken/schiffe_versenken.rb
Overview
#
Ok das ist wirklich einfach. Unterstützt nur eine ein-mann Version von Schifferl versenken.
Um mit dem Spiel zu begonnen, sollte man so vorgehen:
1) randomly_place_ship
2) calculate_grid
Nun kann man jederzeit display_grid aufrufen, um die 2D Ascii Map anzusehen.
Die Idee des Spiel ist zur Zeit, das Mister Zufall den Grid Layout bestimmt. Das ist einmal Teil eins. Grid Layout ist übrigens 8x8 Kästchen
Damits leichter is, beginnen wir bei 0, sowohl bei Lines als auch bei Rows. Der erste Wert is also links oben, der zweite rechts eben die x-Achse.
0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7
Wenn da ein w steht, so is es wasser. das is der default Wert
Jetz wern wir 2 Schiffe platzieren. Eines das nur ein Feld gross is, das andere is 4 Felder gross.
#
Direct Known Subclasses
Constant Summary collapse
- N =
"\n"
Instance Attribute Summary collapse
-
#hash_grid ⇒ Object
Returns the value of attribute hash_grid.
-
#owner ⇒ Object
Returns the value of attribute owner.
Instance Method Summary collapse
-
#calculate_grid ⇒ Object
# === calculate_grid.
-
#debug ⇒ Object
# === debug.
-
#initialize ⇒ GridLayout
constructor
# === initialize ========================================================================= #.
-
#output_grid ⇒ Object
(also: #display_grid)
# === output_grid.
-
#owner? ⇒ Boolean
# === owner? ========================================================================= #.
-
#randomly_place_ship(space = 1) ⇒ Object
# === randomly_place_ship.
-
#randomly_shoot_at(target = self) ⇒ Object
# === randomly_shot_at.
-
#reset ⇒ Object
# === reset ========================================================================= #.
-
#shoot_at(location = '00', target = self) ⇒ Object
# === shot_at.
-
#win_the_game(location) ⇒ Object
# === win_the_game.
Constructor Details
#initialize ⇒ GridLayout
#
initialize
#
59 60 61 62 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 59 def initialize e reset end |
Instance Attribute Details
#hash_grid ⇒ Object
Returns the value of attribute hash_grid.
49 50 51 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 49 def hash_grid @hash_grid end |
#owner ⇒ Object
Returns the value of attribute owner.
50 51 52 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 50 def owner @owner end |
Instance Method Details
#calculate_grid ⇒ Object
#
calculate_grid
The method calculate_grid will calculate the grid-string
#
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 111 def calculate_grid @grid_string = ''.dup count = 0 @grid_string << ' 0 1 2 3 4 5 6 7 '+N lines_count = 0 0.upto(100) do |tmp| if tmp % 10 == 0 if tmp != 0 @grid_string << N end unless lines_count > 7 @grid_string << lines_count.to_s @grid_string << " " end lines_count += 1 end if tmp < 8 if @hash_grid['0'.dup<<tmp.to_s] == 's' @grid_string << sfancy(@hash_grid["0"<<tmp.to_s]) << ' '<< rev elsif @hash_grid['0'.dup<<tmp.to_s] == 'z' @grid_string << sfancy<< @hash_grid["0"<<tmp.to_s].to_s<< ' '<< rev else @grid_string << @hash_grid['0'.dup<< tmp.to_s].to_s << ' ' end elsif @hash_grid.has_key?(tmp.to_s) if @hash_grid[tmp.to_s] == 's' @grid_string << sfancy << @hash_grid[tmp.to_s]<< ' '<< rev elsif @hash_grid[tmp.to_s] == 'z' @grid_string << sfancy << @hash_grid[tmp.to_s].to_s<< ' '<< rev else @grid_string << @hash_grid[tmp.to_s].to_s<< ' ' end end end end |
#debug ⇒ Object
#
debug
DEBUG
#
205 206 207 208 209 210 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 205 def debug cliner { e "DEBUGGING" pp @hash_grid } end |
#output_grid ⇒ Object Also known as: display_grid
#
output_grid
output_grid will display the grid.
#
101 102 103 104 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 101 def output_grid e e @grid_string end |
#owner? ⇒ Boolean
#
owner?
#
67 68 69 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 67 def owner? return 'Besitzer is: '+@owner end |
#randomly_place_ship(space = 1) ⇒ Object
#
randomly_place_ship
With this method we can place a ship somewhere, randomly though.
#
152 153 154 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 152 def randomly_place_ship(space = 1) @hash_grid[@hash_grid.keys.sample] = 's' end |
#randomly_shoot_at(target = self) ⇒ Object
#
randomly_shot_at
With this method you just randomly shoot.
#
181 182 183 184 185 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 181 def randomly_shoot_at(target = self) erste_zahl, zweite_zahl = (0..7).to_a.shuffle[0], (0..7).to_a.shuffle[0] zahl = (erste_zahl.to_s+zweite_zahl.to_s) shoot_at(zahl,target) end |
#reset ⇒ Object
#
reset
#
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 74 def reset @owner = '' # kann man evtl brauchen # Bei @hash_grid sollte man aufpassen, das die gespeicherten # values in Reinform vorliegen, also ohne colour information. @hash_grid = { '00' => :w, '01' => :w, '02' => :w, '03' => :w, '04' => :w, '05' => :w, '06' => :w, '07' => :w, '10' => :w, '11' => :w, '12' => :w, '13' => :w, '14' => :w, '15' => :w, '16' => :w, '17' => :w, '20' => :w, '21' => :w, '22' => :w, '23' => :w, '24' => :w, '25' => :w, '26' => :w, '27' => :w, '30' => :w, '31' => :w, '32' => :w, '33' => :w, '34' => :w, '35' => :w, '36' => :w, '37' => :w, '40' => :w, '41' => :w, '42' => :w, '43' => :w, '44' => :w, '45' => :w, '46' => :w, '47' => :w, '50' => :w, '51' => :w, '52' => :w, '53' => :w, '54' => :w, '55' => :w, '56' => :w, '57' => :w, '60' => :w, '61' => :w, '62' => :w, '63' => :w, '64' => :w, '65' => :w, '66' => :w, '67' => :w, '70' => :w, '71' => :w, '72' => :w, '73' => :w, '74' => :w, '75' => :w, '76' => :w, '77' => :w } @grid_string = '' end |
#shoot_at(location = '00', target = self) ⇒ Object
#
shot_at
With this method you shoot at a given position. target must be an object.
#
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 162 def shoot_at( location = '00', target = self ) e 'oh oh eigenbeschuss! Hehe' if target == self if location.to_i > 77 raise ArgumentError, "Out of range! (Wrong input given, must be less than 77)" else e " You shoot at #{location}." win_the_game(location) if target.hash_grid[location] == 's' target.hash_grid[location] = 'x' calculate_grid # wir müssen den grid neu berechnen, der hat sich geändert end end |
#win_the_game(location) ⇒ Object
#
win_the_game
win_the_game
#
192 193 194 195 196 197 198 |
# File 'lib/games_paradise/schiffe_versenken/schiffe_versenken.rb', line 192 def win_the_game(location) e 'HIT!!!' e 'You won.' @hash_grid[location]= 'z' output_grid exit end |