Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Simple text-based Tic Tac Toe - Python Code Bank


Simple text-based Tic Tac Toe
A simple tic tac toe game. The code might not be the most elegant but it's readable.
                X = 1
O = 2

X_input = 0
O_input = 0

#########
# 0 1 2 #
# 3 4 5 #
# 6 7 8 #
#########

board = \
{
    0:".", 1:".", 2:".",
    3:".", 4:".", 5:".",
    6:".", 7:".", 8:"."
}

def winner(arg):
    if arg == X:
        print "X wins!"
    if arg == O:
        print "O wins!"
    if arg == 0:
		print "Draw!"

def draw_board():
    print board[0], board[1], board[2]
    print board[3], board[4], board[5]
    print board[6], board[7], board[8]

def check_if_won(arg):
    if arg == X:
        # Horizontals
        if board[0] == "X" and board[1] == "X" and board[2] == "X":
            winner(X)
            return 1
        if board[3] == "X" and board[4] == "X" and board[5] == "X":
            winner(X)
            return 1
        if board[6] == "X" and board[7] == "X" and board[8] == "X":
            winner(X)
            return 1
        # Diagonals
        if board[0] == "X" and board[4] == "X" and board[8] == "X":
            winner(X)
            return 1
        if board[2] == "X" and board[4] == "X" and board[6] == "X":
            winner(X)
            return 1
        # Verticals
        if board[0] == "X" and board[3] == "X" and board[6] == "X":
            winner(X)
            return 1
        if board[1] == "X" and board[4] == "X" and board[7] == "X":
            winner(X)
            return 1
        if board[2] == "X" and board[5] == "X" and board[8] == "X":
            winner(X)
            return 1
            
    if arg == O:
        # Horizontals
        if board[0] == "O" and board[1] == "O" and board[2] == "O":
            winner(O)
            return 1
        if board[3] == "O" and board[4] == "O" and board[5] == "O":
            winner(O)
            return 1
        if board[6] == "O" and board[7] == "O" and board[8] == "O":
            winner(O)
            return 1
        # Diagonals
        if board[0] == "O" and board[4] == "O" and board[8] == "O":
            winner(O)
            return 1
        if board[2] == "O" and board[4] == "O" and board[6] == "O":
            winner(O)
            return 1
        # Verticals
        if board[0] == "O" and board[3] == "O" and board[6] == "O":
            winner(O)
            return 1
        if board[1] == "O" and board[4] == "O" and board[7] == "O":
            winner(O)
            return 1
        if board[2] == "O" and board[5] == "O" and board[8] == "O":
            winner(O)
            return 1
        if board[0] != "." and board[1] != "." and board[2] != "."\
        and board[3] != "." and board[4] != "." and board[5] != "."\
        and board[6] != "." and board[1] != "." and board[1] != ".":
			winner(0)
			return 1

def game_loop():
    draw_board()
    while True:
        X_input = raw_input("X: ")
        if int(X_input) > 8:
            return 1
        if board[int(X_input)] != X and board[int(X_input)] != O:
            board[int(X_input)] = "X"
            if check_if_won(X) == 1:
                return 1
            break
    draw_board()
    while True:
        O_input = raw_input("O: ")
        if int(O_input) > 8:
            return 1
        if board[int(O_input)] != X and board[int(O_input)] != O:
            board[int(O_input)] = "O"
            if check_if_won(O) == 1:
                return 1
            break
            
while 1:
    if game_loop() == 1:
        break

            
Comments
Sorry but there are no comments to display