Class: GameManager

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

Instance Method Summary collapse

Instance Method Details

#check_re_render(board, checker) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/game_manager.rb', line 55

def check_re_render(board, checker)
  if checker.check_win_conditions(board)
    board.render_board
    board.clear_board
    puts "Game Over: #{checker.winner} wins"
    return true
  end
  false
end

#game_loop(board, game, cpu) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/game_manager.rb', line 25

def game_loop(board, game, cpu)
  checker = CheckerCriteria.new
  loop do
    game.start_turn
    break if check_re_render(board, checker)
    game.cpu_turn
    break if check_re_render(board, checker)
    if board.draw?
      puts "It's a draw"
      board.render_board
      board.clear_board
      break
    end
  end
  start_game(board)
end

#select_cpu_type(player) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/game_manager.rb', line 42

def select_cpu_type(player)
  puts "s for smart computer, r for random computer"
    choice = gets.chomp
    if choice == 's'
      cpu = ComputerSmart.new(player.type)
    elsif choice == 'r'
      cpu = ComputerRandom.new(player.type)
    else
      puts "invalid selection"
      select_cpu_type(player)
    end
end

#start_game(board) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/game_manager.rb', line 5

def start_game(board)
  puts "Welcome to CONNECT FOUR"
  puts "Enter p to play. Enter q to quit"
  choice = gets.chomp

  if choice.downcase == "p"
    selection = ''
    player_1 = x_or_o
    cpu = select_cpu_type(player_1)
    board.setup_game
    game = TurnManager.new(board, player_1, cpu)
    game_loop(board, game, cpu)
  elsif choice.downcase == "q"
    return false
  else
    puts "Invalid choice"
    start_game(board)
  end
end

#x_or_oObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/game_manager.rb', line 65

def x_or_o
  puts "would you like X's or O's?('x/o')"
  selection = gets.chomp
  if selection == "x"
    return player = Player.new("X")
  elsif selection == "o"
    return player = Player.new("O")
  else
    puts "invalid selecton"
    x_or_o
  end
end