# Visual Basic > Games and Graphics Programming > Game Demos >  [Python] Tic-Tac-Toe

## dday9

This is just the source code.

Notes:
The rows and columns are 0 based which means that 00 is the top-left piece.

Source:

Python Code:
cells = [" ",    " ",    " ",    " ",    " ",    " ",    " ",    " ",    " "]    turn = "X"    def set_cell(x, y):    cell = None    if x == 0:        if y == 0:            cell = 0        elif y == 1:            cell = 3        elif y == 2:            cell = 6    elif x == 1:        if y == 0:            cell = 1        elif y == 1:            cell = 4        elif y == 2:            cell = 7    elif x == 2:        if y == 0:            cell = 2        elif y == 1:            cell = 5        elif y == 2:            cell = 8            return int(cell)            def set_turn(change):    if change == "X":        change = "O"    else:        change = "X"            return change    def get_turn(turn):    if turn == "X":        print ("It is player1's turn.")    else:        print ("It is player2's turn.")        def draw_grid(cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9):    print (" " + cell1 + " | " + cell2 + " | " + cell3 + " ")    print ("---|---|---")    print (" " + cell4 + " | " + cell5 + " | " + cell6 + " ")    print ("---|---|---")    print (" " + cell7 + " | " + cell8 + " | " + cell9 + " ")    def instructions():    print ("To play, simply type the column followed by the row.")    print ("For example, if I wanted to play the top-middle piece, I would enter: 10")    print ("To find out who's turn it is, simply type: t")    def check_win():     #Player1 - Horizontal    if cells[0] == "X" and cells[1] == "X" and cells[2] == "X":        print ("Player1 wins")        return True    elif cells[3] == "X" and cells[4] == "X" and cells[5] == "X":        print ("Player1 wins")        return True    elif cells[6] == "X" and cells[7] == "X" and cells[8] == "X":        print ("Player1 wins")        return True    #Player1 - Vertical    elif cells[0] == "X" and cells[3] == "X" and cells[6] == "X":        print ("Player1 wins")        return True    elif cells[1] == "X" and cells[4] == "X" and cells[7] == "X":        print ("Player1 wins")        return True    elif cells[2] == "X" and cells[5] == "X" and cells[8] == "X":        print ("Player1 wins")        return True            #Player1 - Diagonal    elif cells[0] == "X" and cells[4] == "X" and cells[8] == "X":        print ("Player1 wins")        return True    elif cells[2] == "X" and cells[4] == "X" and cells[6] == "X":        print ("Player1 wins")        return True            #Player2 - Horizontal    elif cells[0] == "O" and cells[1] == "O" and cells[2] == "O":        print ("Player1 wins")        return True    elif cells[3] == "O" and cells[4] == "O" and cells[5] == "O":        print ("Player1 wins")        return True    elif cells[6] == "O" and cells[7] == "O" and cells[8] == "O":        print ("Player1 wins")        return True    #Player2 - Vertical    elif cells[0] == "O" and cells[3] == "O" and cells[6] == "O":        print ("Player1 wins")        return True    elif cells[1] == "O" and cells[4] == "O" and cells[7] == "O":        print ("Player1 wins")        return True    elif cells[2] == "O" and cells[5] == "O" and cells[8] == "O":        print ("Player1 wins")        return True            #Player2 - Diagonal    elif cells[0] == "O" and cells[4] == "O" and cells[8] == "O":        print ("Player1 wins")        return True    elif cells[2] == "O" and cells[4] == "O" and cells[6] == "O":        print ("Player1 wins")        return True        #No winners...    else:                blank = False        for itms in cells:            if itms == " ":                blank = True            if blank == False:            return True        else:            return False     def set_cells():    return [" ", " ", " ", " ", " ", " ", " ", " ", " "]    def reset_turn():    return "X"    def new_game():    turn = reset_turn()    cells = set_cells()    instructions()        return [turn, cells]    new_game()while True:    response = input()    if response == "t":        get_turn(turn)    else:        cell = set_cell(int(response[0]), int(response[1]))                cells[cell] = turn                draw_grid(cells[0], cells[1], cells[2], cells[3], cells[4], cells[5], cells[6], cells[7], cells[8])                win = check_win()        if win == True:            end = new_game()            turn = end[0]            cells = end[1]        else:            turn = set_turn(turn)

----------

