Class: ConnectFour::Game

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

Overview

Game

Preview: README

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



12
13
14
15
# File 'lib/connect_four/game.rb', line 12

def initialize
  @winner = nil
  @total_moves = 0
end

Instance Attribute Details

#winnerObject (readonly)

Returns the value of attribute winner.



10
11
12
# File 'lib/connect_four/game.rb', line 10

def winner
  @winner
end

Instance Method Details

#check_win(board) ⇒ Object



64
65
66
# File 'lib/connect_four/game.rb', line 64

def check_win(board)
  board.four_connected?
end

#game_loop(player1, player2, board) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/connect_four/game.rb', line 32

def game_loop(player1, player2, board)
  puts 'Game starts!'
  board.display_board
  loop do
    run_loop(player1, board)
    break if won?

    run_loop(player2, board)
    break if won?
  end
end

#input_names(player1, player2) ⇒ Object



25
26
27
28
29
30
# File 'lib/connect_four/game.rb', line 25

def input_names(player1, player2)
  puts "Enter player1's name :"
  player1.input_name
  puts "Enter player2's name :"
  player2.input_name
end

#run_loop(player, board) ⇒ Void

Parameters:

Returns:

  • (Void)


47
48
49
50
51
52
53
54
55
56
# File 'lib/connect_four/game.rb', line 47

def run_loop(player, board)
  print "#{player.name}'s turn :"
  move = player.input_move(board)
  @total_moves += 1
  board.insert_at(move, player.sign)
  remove_invalid_warning_output
  board.redraw
  announce_winner(player) if check_win(board)
  tie if @total_moves == 42
end

#startObject



17
18
19
20
21
22
23
# File 'lib/connect_four/game.rb', line 17

def start
  player1 = Player.new("\u25CF")
  player2 = Player.new("\u25CB")
  board = Board.new
  intro_messages(player1, player2)
  game_loop(player1, player2, board)
end

#won?Boolean

checks if #winner is not nil.

Returns:

  • (Boolean)


60
61
62
# File 'lib/connect_four/game.rb', line 60

def won?
  @winner != nil
end