40 lines
632 B
Ruby
40 lines
632 B
Ruby
#tiktaktoe
|
|
|
|
|
|
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
|
|
|
|
|
|
#if x is in lines then x wins
|
|
#if o is in lines then x wins
|
|
#if no nums in board then game over
|
|
|
|
player = "X"
|
|
loop do
|
|
print "Place an #{player} "
|
|
input = gets.to_i
|
|
p input
|
|
p board.include?(input)
|
|
board[input] = player
|
|
printBoard(board)
|
|
if player == "X"
|
|
player = "O"
|
|
else
|
|
player = "X"
|
|
end
|
|
end
|