Class: GameSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/game_setup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, player_one, player_two) ⇒ GameSetup

Returns a new instance of GameSetup.



7
8
9
10
11
12
# File 'lib/game_setup.rb', line 7

def initialize(board, player_one, player_two)
  @board = board
  @ui = UI.new(@board)
  @player_one = player_one
  @player_two = player_two
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



6
7
8
# File 'lib/game_setup.rb', line 6

def board
  @board
end

#player_oneObject

Returns the value of attribute player_one.



6
7
8
# File 'lib/game_setup.rb', line 6

def player_one
  @player_one
end

#player_twoObject

Returns the value of attribute player_two.



6
7
8
# File 'lib/game_setup.rb', line 6

def player_two
  @player_two
end

#uiObject

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_levelObject



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_assigned_message(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.invalid_input_message(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.invalid_input_message(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.first_move_message(player)
end

#set_opponentsObject



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_assigned_message(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.early_exit_message
    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_firstObject



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