Class: Battle::Game
- Inherits:
-
Object
- Object
- Battle::Game
- Defined in:
- lib/battle/game.rb
Constant Summary collapse
- STATUSES =
%w[start victory defeat]
Instance Attribute Summary collapse
-
#coords ⇒ Object
readonly
Returns the value of attribute coords.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#nuke_status ⇒ Object
readonly
Returns the value of attribute nuke_status.
-
#prize ⇒ Object
readonly
Returns the value of attribute prize.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#ships ⇒ Object
readonly
Returns the value of attribute ships.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#sunk ⇒ Object
readonly
Returns the value of attribute sunk.
Instance Method Summary collapse
- #finished? ⇒ Boolean
- #init? ⇒ Boolean
-
#initialize(name = nil, email = nil, id: nil) ⇒ Game
constructor
A new instance of Game.
- #nuke(x, y) ⇒ Object
- #register! ⇒ Object
Constructor Details
#initialize(name = nil, email = nil, id: nil) ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/battle/game.rb', line 8 def initialize(name = nil, email = nil, id: nil) @name = name @email = email @id = id @coords = [nil, nil] @status = id ? 'start': 'init' @ships = [] @response = {} Battle.ships.each { |name| @ships << Ship.new(name) } end |
Instance Attribute Details
#coords ⇒ Object (readonly)
Returns the value of attribute coords.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def coords @coords end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def email @email end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def name @name end |
#nuke_status ⇒ Object (readonly)
Returns the value of attribute nuke_status.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def nuke_status @nuke_status end |
#prize ⇒ Object (readonly)
Returns the value of attribute prize.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def prize @prize end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def response @response end |
#ships ⇒ Object (readonly)
Returns the value of attribute ships.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def ships @ships end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def status @status end |
#sunk ⇒ Object (readonly)
Returns the value of attribute sunk.
3 4 5 |
# File 'lib/battle/game.rb', line 3 def sunk @sunk end |
Instance Method Details
#finished? ⇒ Boolean
43 44 45 |
# File 'lib/battle/game.rb', line 43 def finished? status == "lost" || status == "victory" end |
#init? ⇒ Boolean
39 40 41 |
# File 'lib/battle/game.rb', line 39 def init? status == "init" end |
#nuke(x, y) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/battle/game.rb', line 29 def nuke(x, y) raise GameNotStartedYetError if init? raise GameAlreadyFinishedError if finished? do_request NUKE_URL, id: id, x: x, y: y handle_nuke response end |
#register! ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/battle/game.rb', line 19 def register! raise PlayerNameNotSpecified if name.nil? || name.empty? raise PlayerEmailNotSpecified if email.nil? || email.empty? do_request REGISTER_URL, "name" => name, "email" => email handle_register response end |