Class: GamesAndRpgParadise::GridLayout

Inherits:
Object
  • Object
show all
Includes:
Colours
Defined in:
lib/games_and_rpg_paradise/games/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

PlayerClass

Constant Summary collapse

N =
"\n"

Constants included from Colours

Colours::WHITE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colours

#brown, #cfile, #convert_colour, convert_colour, #fancy, #normal, #pink, #red, #yel

Constructor Details

#initializeGridLayout

#

initialize

#


61
62
63
64
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 61

def initialize
  e
  reset
end

Instance Attribute Details

#hash_gridObject

Returns the value of attribute hash_grid.



51
52
53
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 51

def hash_grid
  @hash_grid
end

#ownerObject

Returns the value of attribute owner.



52
53
54
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 52

def owner
  @owner
end

Instance Method Details

#calculate_gridObject

#

calculate_grid

The method calculate_grid will calculate the grid-string

#


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
146
147
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 113

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

#debugObject

#

debug

DEBUG

#


207
208
209
210
211
212
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 207

def debug
  cliner {
    e 'DEBUGGING'
    pp @hash_grid
  }
end

#output_gridObject Also known as: display_grid

#

output_grid

output_grid will display the grid.

#


103
104
105
106
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 103

def output_grid
  e
  e @grid_string
end

#owner?Boolean

#

owner?

#

Returns:

  • (Boolean)


69
70
71
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 69

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.

#


154
155
156
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 154

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.

#


183
184
185
186
187
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 183

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

#resetObject

#

reset

#


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 76

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.

#


164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 164

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

#


194
195
196
197
198
199
200
# File 'lib/games_and_rpg_paradise/games/schiffe_versenken/schiffe_versenken.rb', line 194

def win_the_game(location)
  e 'HIT!!!' 
  e 'You won.'
  @hash_grid[location]= 'z'
  output_grid
  exit
end