65 lines
1.2 KiB
Ruby
65 lines
1.2 KiB
Ruby
#tiktaktoe 01/17/2022
|
|
|
|
LINES = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
|
|
|
|
board = Array.new(9)
|
|
9.times do |i|
|
|
board[i] = i
|
|
end
|
|
|
|
def printBoard(board)
|
|
9.times do |i|
|
|
if i % 3 == 0
|
|
print "\n -------------\n | "
|
|
end
|
|
print "#{board[i]} | "
|
|
end
|
|
print "\n -------------\n"
|
|
end
|
|
|
|
def checkWin(board, x, o)
|
|
for line in LINES do
|
|
if board[line[0]] == "X" and board[line[1]] == "X" and board[line[2]] == "X"
|
|
puts "X WINS!!!!!!"
|
|
exit
|
|
elsif board[line[0]] == "O" and board[line[1]] == "O" and board[line[2]] == "O"
|
|
puts "O WINS!!!!!!"
|
|
exit
|
|
end
|
|
end
|
|
end
|
|
|
|
def isBoardFull(board)
|
|
if not board.any?(0..9)
|
|
puts "GAME OVER"
|
|
exit
|
|
end
|
|
end
|
|
|
|
player = "X"
|
|
x = Array.new
|
|
o = Array.new
|
|
def play(board, player, x, o)
|
|
isBoardFull(board)
|
|
printBoard(board)
|
|
print "Place an #{player} "
|
|
input = gets.to_i
|
|
if board.include?(input)
|
|
else
|
|
puts "Bad input, pick a valid position"
|
|
play(board, player, x, o)
|
|
end
|
|
board[input] = player
|
|
if player == "X"
|
|
x.push(input)
|
|
player = "O"
|
|
else
|
|
o.push(input)
|
|
player = "X"
|
|
end
|
|
checkWin(board, x, o)
|
|
play(board, player, x, o)
|
|
end
|
|
|
|
play(board, player, x, o)
|