Class: TTT::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Game

Returns a new instance of Game.



11
12
13
14
15
16
17
18
19
20
# File 'lib/tictactoe/game.rb', line 11

def initialize(args = {})
  @io = args.fetch(:io, IOTerminal.new)
  @board_presenter = args.fetch(:board_presenter, BoardPresenterTerminal.new)
  @input_helper = InputHelper.new(@io)
  @number_of_turns_taken = 0
  # below finds the user needed input needed for initial players and board configuration and stores in config object
  @config = GameConfig.new(@input_helper)
  @players = generate_players
  @board = generate_empty_board
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



8
9
10
# File 'lib/tictactoe/game.rb', line 8

def board
  @board
end

#board_presenterObject

Returns the value of attribute board_presenter.



8
9
10
# File 'lib/tictactoe/game.rb', line 8

def board_presenter
  @board_presenter
end

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/tictactoe/game.rb', line 8

def config
  @config
end

#input_helperObject (readonly)

Returns the value of attribute input_helper.



9
10
11
# File 'lib/tictactoe/game.rb', line 9

def input_helper
  @input_helper
end

#ioObject

Returns the value of attribute io.



9
10
11
# File 'lib/tictactoe/game.rb', line 9

def io
  @io
end

#number_of_turns_takenObject

Returns the value of attribute number_of_turns_taken.



8
9
10
# File 'lib/tictactoe/game.rb', line 8

def number_of_turns_taken
  @number_of_turns_taken
end

#playersObject

Returns the value of attribute players.



8
9
10
# File 'lib/tictactoe/game.rb', line 8

def players
  @players
end

Instance Method Details

#available_choicesObject



126
127
128
# File 'lib/tictactoe/game.rb', line 126

def available_choices
  board.available_choices
end

#change_square(display_value, new_value) ⇒ Object



89
90
91
92
93
94
# File 'lib/tictactoe/game.rb', line 89

def change_square(display_value, new_value)
  board.change_square(display_value, new_value)
  if !won?
    move_forward_one_turn
  end
end

#current_playerObject



134
135
136
# File 'lib/tictactoe/game.rb', line 134

def current_player
  players[current_turn_player_index]
end

#draw?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/tictactoe/game.rb', line 100

def draw?
  board.full? && !board.won?
end

#generate_empty_boardObject



34
35
36
37
# File 'lib/tictactoe/game.rb', line 34

def generate_empty_board
  number_of_rows_cols = config.number_of_rows_cols
  Board.new(rows_and_cols: number_of_rows_cols, squares: SquaresFactory.build_empty_squares(number_of_rows_cols) )
end

#generate_playersObject



28
29
30
31
32
# File 'lib/tictactoe/game.rb', line 28

def generate_players
  player_1 = Player.new(value: config.player_1_value, name: config.player_1_name, type: :human)
  player_2 = Player.new(value: config.player_2_value, name: config.player_2_name, type: config.player_2_type, difficulty_level: config.computer_difficulty_level)
  [player_1, player_2]
end

#get_square_choiceObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tictactoe/game.rb', line 76

def get_square_choice
  if current_player.type == :human
    input_helper.get_square_to_change(current_player.name, available_choices)
  elsif current_player.type == :computer
    input_helper.computer_choosing_graphic
    if current_player.difficulty_level == :difficult
      perfect_move
    elsif current_player.difficulty_level == :easy
      random_move
    end
  end
end

#move_forward_one_turnObject



138
139
140
# File 'lib/tictactoe/game.rb', line 138

def move_forward_one_turn
  self.number_of_turns_taken += 1
end

#over?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/tictactoe/game.rb', line 96

def over?
  board.full? || board.won?
end

#perfect_moveObject



116
117
118
119
120
# File 'lib/tictactoe/game.rb', line 116

def perfect_move
  move_generator = MoveGenerator.new(self)
  move_generator.minimax
  move_generator.choice
end

#playObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tictactoe/game.rb', line 48

def play
  while true
    while !over?
      print_board
      square_choice = get_square_choice
      #note that change_square moves current_player forward only if game is not won
      if square_choice == :exit
        break
      else
        change_square(square_choice, current_player.value)
      end
    end

    #need to print_final_iteration of board
    print_board

    if won?
      winning_prompt
    elsif draw?
      draw_prompt
    end

    reset_game
    new_game_starting_graphic
  end
end


130
131
132
# File 'lib/tictactoe/game.rb', line 130

def print_board
  board_presenter.present_board(board)
end

#random_moveObject



122
123
124
# File 'lib/tictactoe/game.rb', line 122

def random_move
  available_choices.sample
end

#reset_boardObject



39
40
41
# File 'lib/tictactoe/game.rb', line 39

def reset_board
  self.board = generate_empty_board
end

#reset_gameObject



43
44
45
46
# File 'lib/tictactoe/game.rb', line 43

def reset_game
  reset_board
  self.number_of_turns_taken = 0
end

#winnerObject



108
109
110
111
112
113
114
# File 'lib/tictactoe/game.rb', line 108

def winner
  #each move is immediately proceeded by an increment to number_of_selections_made; therefore, need to rewind won to find winner
  if !won?
    return nil
  end
  current_player
end

#won?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/tictactoe/game.rb', line 104

def won?
  board.won?
end