forresty
5/10/2017 - 10:20 AM

interface.rb

require_relative 'tic_tac_toe'

def print_board(board)
  puts board[0..2].join
  puts board[3..5].join
  puts board[6..8].join
end

def prompt_player_input(player)
  puts "Player #{player}, now is your turn. enter position (0..8)"
  print '> '
  gets.chomp.to_i
end

def take_turn!(board, player)
  position = prompt_player_input(player)

  update_board!(board, position, player)

  print_board(board)

  if player_win?(board, player)
    puts "#{player} won!"
    exit
  end
end

board = ['_'] * 9
# board = %w{ x o _ _ x _ o x _ }

  ##############
  #
  #  _xo
  #  _x_
  #  x_o
  #
  ##############

until game_over?(board)
  take_turn!(board, 'x')
  take_turn!(board, 'o')
end
def player_win?(board, player)
  #########
  # 0 1 2
  # 3 4 5
  # 6 7 8
  #########

  winning_combinations = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
  ]

  winning_combinations.each do |combination|
    return true if combination.all? { |pos| board[pos] == player }
  end

  false
end

def game_over?(board)
  ## how do we implement this????
end

def update_board!(board, position, piece)
  board[position] = piece
end