Class: Turn

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ Turn

Returns a new instance of Turn.



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

def initialize(board)
  @board = board
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



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

def board
  @board
end

Instance Method Details

#computer_take_turnObject



32
33
34
35
# File 'lib/turn.rb', line 32

def computer_take_turn
    computer_input = board.columns.keys.shuffle.shuffle.first
    pc_place_piece(computer_input)
end

#pc_place_piece(column_inputted) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/turn.rb', line 52

def pc_place_piece(column_inputted)
  if board.columns[column_inputted.upcase][5].empty? == false
    computer_take_turn
  else
    board.columns.find do |column, row|
      if column_inputted.downcase == column.downcase
        row.find do |cell|
          cell.computer_add_piece if cell.empty?
        end
      end
    end
  end
end

#place_piece(column_inputted) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/turn.rb', line 66

def place_piece(column_inputted)
  if  board.columns[column_inputted.upcase][5].empty? == false
    puts "That column is full! Please select another."
    place_piece(gets.chomp)
  else
    board.columns.find do |column, row|
      if column_inputted.downcase == column.downcase
        row.find do |cell|
          cell.add_piece if cell.empty?
        end
      end
    end
  end
end

#two_player_take_turnObject



22
23
24
25
26
27
28
29
30
# File 'lib/turn.rb', line 22

def two_player_take_turn
  user_input = gets.chomp
  if "ABCDEFG".include? user_input.upcase
    user_place_piece(user_input)
  else
    puts "That is an invalid input! Please select a letter between A and G."
    two_player_take_turn
  end
end

#user_place_piece(column_inputted) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/turn.rb', line 37

def user_place_piece(column_inputted)
  if board.columns[column_inputted.upcase][5].empty? == false
    puts "That column is full! Please select another"
    user2_take_turn
  else
    board.columns.find do |column, row|
      if column_inputted.downcase == column.downcase
        row.find do |cell|
          cell.computer_add_piece if cell.empty?
        end
      end
    end
  end
end

#user_take_turnObject



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

def user_take_turn
  user_input = gets.chomp
  if "ABCDEFG".include? user_input.upcase
    place_piece(user_input)
  else
    puts "That is an invalid input! Please select a letter between A and G."
    user_take_turn
  end
end