Class: GameSetup
- Inherits:
-
Object
- Object
- GameSetup
- Defined in:
- lib/game_setup.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#player_one ⇒ Object
Returns the value of attribute player_one.
-
#player_two ⇒ Object
Returns the value of attribute player_two.
-
#ui ⇒ Object
Returns the value of attribute ui.
Instance Method Summary collapse
- #get_difficulty_level ⇒ Object
- #get_player_type(player) ⇒ Object
-
#initialize(board, player_one, player_two) ⇒ GameSetup
constructor
A new instance of GameSetup.
- #invalid_level(level) ⇒ Object
- #invalid_type(type, player) ⇒ Object
- #set_first_turn(player) ⇒ Object
- #set_opponents ⇒ Object
- #set_player_type(type, player) ⇒ Object
- #start! ⇒ Object
- #validate_level(level) ⇒ Object
- #validate_type(type, player) ⇒ Object
- #who_goes_first ⇒ Object
Constructor Details
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
6 7 8 |
# File 'lib/game_setup.rb', line 6 def board @board end |
#player_one ⇒ Object
Returns the value of attribute player_one.
6 7 8 |
# File 'lib/game_setup.rb', line 6 def player_one @player_one end |
#player_two ⇒ Object
Returns the value of attribute player_two.
6 7 8 |
# File 'lib/game_setup.rb', line 6 def player_two @player_two end |
#ui ⇒ Object
Returns the value of attribute ui.
6 7 8 |
# File 'lib/game_setup.rb', line 6 def ui @ui end |
Instance Method Details
#get_difficulty_level ⇒ Object
38 39 40 41 42 |
# File 'lib/game_setup.rb', line 38 def get_difficulty_level return nil unless player_one.player_type == COMPUTER_PLAYER || player_two.player_type == COMPUTER_PLAYER level = ui.request_difficulty_level validate_level(level) ? ui.(level) : invalid_level(level) end |
#get_player_type(player) ⇒ Object
33 34 35 36 |
# File 'lib/game_setup.rb', line 33 def get_player_type(player) type = ui.request_player_type(player.marker) validate_type(type, player) ? set_player_type(type, player) : invalid_type(type, player) end |
#invalid_level(level) ⇒ Object
62 63 64 65 |
# File 'lib/game_setup.rb', line 62 def invalid_level(level) ui.(level) get_difficulty_level end |
#invalid_type(type, player) ⇒ Object
53 54 55 56 |
# File 'lib/game_setup.rb', line 53 def invalid_type(type, player) ui.(type) get_player_type(player) end |
#set_first_turn(player) ⇒ Object
71 72 73 74 |
# File 'lib/game_setup.rb', line 71 def set_first_turn(player) player.turn = 1 ui.(player) end |
#set_opponents ⇒ Object
28 29 30 31 |
# File 'lib/game_setup.rb', line 28 def set_opponents player_one.opponent = player_two player_two.opponent = player_one end |
#set_player_type(type, player) ⇒ Object
48 49 50 51 |
# File 'lib/game_setup.rb', line 48 def set_player_type(type, player) player.player_type = type ui.(type, player.marker) end |
#start! ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/game_setup.rb', line 14 def start! begin set_opponents get_player_type(player_one) get_player_type(player_two) level = get_difficulty_level who_goes_first Game.new(board, player_one, player_two, level).play! rescue Interrupt ui. exit end end |
#validate_level(level) ⇒ Object
58 59 60 |
# File 'lib/game_setup.rb', line 58 def validate_level(level) (level == HARD_LEVEL) || (level == EASY_LEVEL) end |
#validate_type(type, player) ⇒ Object
44 45 46 |
# File 'lib/game_setup.rb', line 44 def validate_type(type, player) (type == HUMAN_PLAYER) || (type == COMPUTER_PLAYER) end |
#who_goes_first ⇒ Object
67 68 69 |
# File 'lib/game_setup.rb', line 67 def who_goes_first rand(0..1) == 1 ? set_first_turn(player_one) : set_first_turn(player_two) end |