Tuesday, October 4
Shadow

A TicTacToe game

import random
board = ["-","-","-","-","-","-","-","-","-"]
pl_in = {"a1":1,"a2":2,"a3":3,"b1":4,"b2":5,"b3":6,"c1":7,"c2":8,"c3":9}
winner = None
game_run = True

def printBoard(board):
    print(board[0]+" | "+board[1]+" | "+board[2])
    print("----------")
    print(board[3]+" | "+board[4]+" | "+board[5])
    print("----------")
    print(board[6]+" | "+board[7]+" | "+board[8])
    print("----------")

def playerFirstSelect():
    player = random.choice(["X","O"])
    return player

def playerInput():
    inp_int = input("a1 a2 a3 \t b1 b2 b3  \t c1 c2 c3 : ")
    inp = pl_in[inp_int]
    if(board[inp-1] == "-"):
        board[inp-1] = player
    else:
        print("Sorry, it's already taken.You can choose any other ")
        inp_int = input("a1 a2 a3 \t b1 b2 b3  \t c1 c2 c3")
        inp = pl_in[inp_int]
        board[inp-1] = player

def playerSelect(play):
    if (play == "X"):
        return "O"
    else:
        return "X"

def chkHtl(board):
    global winner
    if board[0] == board[1] == board[2] != "-":
        winner = board[0]
        return True
    if board[3] == board[4] == board[5] != "-":
        winner = board[3]
        return True
    if board[6] == board[7] == board[8] != "-":
        winner = board[6]
        return True

def chkVtl(board):
    global winner
    if board[0] == board[3] == board[6] != "-":
        winner = board[0]
        return True
    if board[1] == board[4] == board[7] != "-":
        winner = board[1]
        return True
    if board[2] == board[5] == board[8] != "-":
        winner = board[2]
        return True

def chkDgl(board):
    global winner
    if board[0] == board[4] == board[8] != "-":
        winner = board[0]
        return True
    if board[2] == board[4] == board[6] != "-":
        winner = board[4]
        return True

def tie(board):
    if "-" not in board:
        print("Oh No,Here's a tie.Start Over")
        printBoard(board)
player = playerFirstSelect()

while game_run:
    printBoard(board)
    print("       ")
    playerInput()
    print("__________________________________________________________________________________")
    print("       ")
    player = playerSelect(player)
    print(player,"'s turn")

    if chkDgl(board) or chkVtl(board) or chkHtl(board):
        print("The winner is ",winner)
        game_run = False

Leave a Reply