Class: GameController

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGameController

Returns a new instance of GameController.



14
15
16
17
# File 'lib/game_controller.rb', line 14

def initialize
	@console = ConsoleUI.new
       @config = Factory.new(@console)
end

Instance Attribute Details

#aiObject (readonly)

Returns the value of attribute ai.



12
13
14
# File 'lib/game_controller.rb', line 12

def ai
  @ai
end

#consoleObject (readonly)

Returns the value of attribute console.



12
13
14
# File 'lib/game_controller.rb', line 12

def console
  @console
end

Instance Method Details

#display_boardObject



52
53
54
# File 'lib/game_controller.rb', line 52

def display_board
	@board_printer.print(@board.get_print_results)
end

#opponent(player) ⇒ Object



70
71
72
# File 'lib/game_controller.rb', line 70

def opponent player
	if player == @player1 then @player2 else @player1 end
end

#play_again?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
# File 'lib/game_controller.rb', line 60

def play_again?
       response = @console.query_user_for_new_game

	if response then true
	else
		@console.say_goodbye
		false
	end
end

#present_game_resultObject



56
57
58
# File 'lib/game_controller.rb', line 56

def present_game_result
	@console.present_game_result(@board.check_board_status)
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/game_controller.rb', line 19

def run

	@console.greet_user
	new_game = true
	while new_game
	
           setup_game
		player = @player1 
		while @board.check_board_status == :continue_game
		    display_board
               move = player.make_move(@board)
               @console.print_player_move(move,player)
	        player = opponent(player)
		end

           display_board
		present_game_result
		new_game = play_again?
	end
end

#set_playersObject



46
47
48
49
50
# File 'lib/game_controller.rb', line 46

def set_players
    players = @config.get_players
    @player1 = players[:player1]
    @player2 = players[:player2]
end

#setup_gameObject



40
41
42
43
44
# File 'lib/game_controller.rb', line 40

def setup_game
    set_players
    @board = @config.get_board
    @board_printer = @config.get_board_printer(@board)
end