Jessica Willemain
Jessica Willemain

Reputation: 41

Very new at Ruby and trying to compare an array with a nested array

So I am building a tic tac toe game and I am trying to pull keys from my board hash and then compare that with arrays within an array. The issue is that the winning combos all have 3 values, but it may take a player 4 or 5 moves to win, so I want to see if those 4 or 5 values include one of the winning combos (hopefully that makes sense). Specifically the issue is with the winner_check method, is there a way to get this method working?

def initialize_board
  board = {}
  (1..9).each {|position| board[position] = ' ' }
  board
end

def empty_positions(board)
  board.keys.select {|position| board[position] != 'X' && 'O'}
end

def player_turn(board)
  puts "Let's play Tic Tac Toe! Pick a position 1-9 and try to beat me."
  position = gets.chomp.to_i
  board[position] = 'X'
end

def computer_turn(board)
  position = empty_positions(board).sample
  board[position] = 'O'
end

def winner_check(board)
  winning_combos = [[1, 2, 3], 
    [4, 5, 6], 
    [7, 8, 9], 
    [1, 4, 7], 
    [2, 5, 8], 
    [3, 6, 9], 
    [1, 5, 9],
    [3, 5, 7]]
    winning_combos.each {|combo|}
      if board.select { |k,v| v == 'X' }.keys.include?(combo)
        puts "You've won! Great job!"
      if board.select { |k,v| v == 'O' }.keys.include?(combo)
        puts "Looks like the computer outsmarted you, try again!"
      elsif
        puts "Looks like you've tied, try again!"
      end
    nil
  nil
end

def game_over?(board)
  empty_positions(board) == []
end

def draw_board(board)
  puts
  puts " #{board[1]} | #{board[2]} | #{board[3]} "
  puts "---+---+---"
  puts " #{board[4]} | #{board[5]} | #{board[6]} "
  puts "---+---+---"
  puts " #{board[7]} | #{board[8]} | #{board[9]} "
  puts
end

board = initialize_board
draw_board(board)

until winner_check(board) || game_over?(board)
  player_turn(board)
  draw_board(board)
  winner_check(board)
  computer_turn(board)
  draw_board(board)
  winner_check(board)
end

Upvotes: 4

Views: 99

Answers (1)

Jared Beck
Jared Beck

Reputation: 17528

I think you're on the right track, but you have some syntax errors:

winning_combos.each {|combo|}
  if board.select { |k,v| v == 'X' }.keys.include?(combo)
    puts "You've won! Great job!"
  if board.select { |k,v| v == 'O' }.keys.include?(combo)
    puts "Looks like the computer outsmarted you, try again!"
  elsif
    puts "Looks like you've tied, try again!"
  end
nil

On the first line, you close your block too early. The } at the end of the line closes the block.

On the last line, where you have a literal nil, I think that's where your closing } should go.

You also have two ifs in a row, should be if, elsif.

Syntax errors corrected:

winning_combos.each { |combo|

  if board.select { |k,v| v == 'X' }.keys.include?(combo)
    puts "You've won! Great job!"
  elsif board.select { |k,v| v == 'O' }.keys.include?(combo)
    puts "Looks like the computer outsmarted you, try again!"
  elsif
    puts "Looks like you've tied, try again!"
  end
}

Upvotes: 1

Related Questions