Class: Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



9
10
11
12
13
14
15
# File 'lib/game.rb', line 9

def initialize
  @board = Board.new
  @player = Player.new(@board)
  @computer = Computer.new(@board)
  @start = nil
  @finish = nil
end

Instance Attribute Details

#finishObject (readonly)

Returns the value of attribute finish.



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

def finish
  @finish
end

#playerObject

Returns the value of attribute player.



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

def player
  @player
end

#startObject (readonly)

Returns the value of attribute start.



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

def start
  @start
end

Instance Method Details

#playObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/game.rb', line 19

def play
  @board.default_board
  puts "Press 'P' to play, or press 'Q' to quit"
  wanna_play = gets.chomp.downcase
  puts " "
  while wanna_play != 'p' && wanna_play != 'q'
    puts "Press 'P' to play, or press 'Q' to quit"
    wanna_play = gets.chomp.downcase
  end
  if wanna_play == 'q'
    exit()
  end
  puts "--------------------------------"
  puts "Great! Here's the board you'll be
        playing on."
  puts " "
  @board.display_board
  puts "Please enter your player name below"
  name = gets.chomp.capitalize
  @player.assign_player_name(name)
  @start = Time.now
  loop do
    puts "#{@player.name}, it's now your turn!"
    @player.player_turn(@board)
    # require'pry';binding.pry
    @board.display_board
    if @board.check_winner == 'x'
      puts " "
      puts "You Won!"
      puts " "
      break
    end
    if @board.check_full == true
      puts " "
      puts "Draw!"
      puts " "
      break
    end

    ###Computers turn###
    @computer.computer_turn(@board)
    @board.display_board
    if @board.check_winner == 'o'
      puts " "
      puts "You Lost!"
      puts " "
      break
    end
    if @board.check_full == true
      puts " "
      puts "Draw!"
      puts " "
      break
    end
    @finish = Time.now
  end
  total_time = @finish.to_i - @start.to_i
  puts "It took you #{total_time} seconds to play this game."
  # puts " "
  puts "------------------------------"
  puts "Don't disconnect, Quad Connectâ„¢!"
  puts "------------------------------"
  puts "Play again?"
  puts " "
  play
end