Skip to content

Instantly share code, notes, and snippets.

@angelxmoreno
Created May 21, 2016 19:26
Show Gist options
  • Save angelxmoreno/6821f54f2a28d59af2527175a5f5108e to your computer and use it in GitHub Desktop.
Save angelxmoreno/6821f54f2a28d59af2527175a5f5108e to your computer and use it in GitHub Desktop.
require 'tic_tac_toe_cn'
game = Tic_Tac_Toe.new
board = Array.new(3) {Array.new(3)}
game.initBoard(board)
puts "Starting Tic Tac Toe"
puts "Player 1 choose X or O"
playerOne = gets.chomp.upcase
if playerOne == "X"
playerTwo = "O"
else
playerOne = "O"
playerTwo = "X"
end
puts "Player 1: ##{playerOne}"
puts "Player 2: ##{playerTwo}"
puts "Who will go first? Type ##{playerOne} for Player 1 and ##{playerTwo} for Player 2"
player = gets.chomp.upcase
game.displayBoard(board)
gameOver = false
# main game loop
begin
# turnPlayed = false
begin
puts "Player ##{player}, choose a space"
##note: the player chooses between 1 and 9 but the computer counts from 0 to 8
playerChoice = (gets.chomp.to_i) - 1
turnPlayed = game.turnPlayed(board,player,playerChoice)
game.displayBoard(board)
end until turnPlayed
if game.winningRows(board) || game.winningCols(board) || game.winningDiagonals(board)
puts "Player ##{player} wins!"
gameOver = true
elsif game.fullBoard(board)
puts "It's a Draw!!"
gameOver = true
else
if player == playerOne
player = playerTwo
else
player = playerOne
end
end
end until gameOver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment