Tic Tac Toe(v0.1)

[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
For the last couple of days i’ve been playing around with making a basic game and settled on trying to make tic tac toe.
The idea being that i’ll set out with an idea and document the progress as I go along, figuring out the roadblocks I come across.

I started off strong, and got all the basic board drawing tools and other helper tools in place.
The first problem that i’ve come across is the getting the program to scan the lines to check if there is a winner.
This has to obviously be a function that is defined so it can be checked multiple times whilst in the running game loop.
From what i can see, this is a problem stemming from the way I initially set the board ( using a function that takes a list of 9 inputs and converts the numerical value 0-2 into a “X” or a “O” in the place of that input on the board.
After watching a few other people solution to this problem, I can now see that its better to store the board in lists with separate lists within to hold the row data.
This method makes it easier when you come to access the data.

gb = [[0,0,0],[0,0,0],[0,0,0]]
Here is the before and after for the base function this draws the current board to the screen when called and then checks if anyone has won.

I’ve also gotten a little far ahead of myself, I’ve added lots of functions that I thought might be useful.
The most interesting is a Coin Flip function.

It literally selects a random number ( 0 – 1 ) and assigns a heads or tails value to it.
This is for when the player is selecting who they want to go first, they get three options (player,npc,flip) it will then randomly select who starts.
Thought it was a bit of fun.

This is as far as i have gotten for now, here is the full code.
Its very messy and alot is redundant, I’m still very much a beginner.
I get a little mad with nested if statements too.

import random
playerPiece = ""
npcPiece = ""
running = False
playerGamesWon = 0 
npcGamesWon = 0
row1 = ""
row2 = ""
row3 = ""
gb = [[0,0,0],[0,0,0],[0,0,0]]


def XorO(input):
#returns the type version either X, Y or null depending on the the input 0-2
    if input == 0:
        return " "
    else:
        if input == 1: 
            return "X"
        else:
            return "O"   

def XorO2(input):
#returns the number version of the X or Y depending on the input
    if input == "X":
            return 1
    else:
        if input == "O": 
            return 2
        else:
            return 0

def flip():
    #flipping a coin to decide who goes first
    result = random.randrange(2)
    if result == 0:
        return "Tails"
    else:
        return "Heads"

def wincheck(type):
    if gb[0][0] == type and gb[0][1] == type and gb[0][2] == type:
        #print (type + " won the game")
        return True
    else:
        if gb[1][0] == type and gb[1][1] == type and gb[1][2] == type:
            #print (type + " won the game")
            return True
        else:
            if gb[2][0] == type and gb[2][1] == type and gb[2][2] == type:
                #print (type + " won the game")
                return True
            else:
                if gb[0][0] == type and gb[1][0] == type and gb[2][0] == type:
                    #print (type + " won the game")
                    return True
                else:
                    if gb[0][1] == type and gb[1][1] == type and gb[2][1] == type:
                        #print (type + " won the game")
                        return True
                    else:
                        if gb[0][2] == type and gb[1][2] == type and gb[2][2] == type:
                            #print (type + " won the game")
                            return True
                        else:
                            #print ("no win")
                            return False
    
##def base(b0,b1,b2,b3,b4,b5,b6,b7,b8):
##    global row1
##    global row2
##    global row3
###builds the base that can accept the player inputs 
##    row1 = XorO(b0) + "|" + XorO(b1) + "|" + XorO(b2)
##    row2 = XorO(b3) + "|" + XorO(b4) + "|" + XorO(b5)    
##    row3 = XorO(b6) + "|" + XorO(b7) + "|" + XorO(b8)
##    print (row1)
##    print (row2)
##    print (row3)

def base():
    global gb
    for row in gb:
        print (row)
    if wincheck("X") or wincheck("O"):
        print ("theres a winner")
        
        

def player(x,y,type):
#trying to build the X,Y coordinates system to plot numbers in the base input.  
    t = XorO2(type)
    print (t)

def score(npc,player):
    playerScore = "you've won : " + player + " games"
    npaScore = "the computer has won : " + npc + " games"
    return playerScore
    return npcScore

def whoGoesFirst():
    #player picking who should go first ( npc, or playing)
    #three option (me, you, flip)
    #flip flips coin
    while True: 
        choice = input("who goes first : player, NPC, flip : ")
        if choice == "player" or choice == "NPC" or choice == "flip":
            if choice == "player":
                print ("Player Goes First \n")
                break
            else:
                if choice == "NPC":
                    print ("NPC Goes First \n")
                    break
                else:
                    if choice == "flip":
                        flipChoice = input("Heads or Tails? : ")
                        coin = flip()
                        if flipChoice == coin:
                            ##player wins and goes first 
                            print ("you won the coin flip \n")
                            break
                        else:
                            ##player looses and goes second
                            print ("you lost the coin flip \n")
                            break
                         
        else:
            print ("invalid choice, please select correctly")  
    

def playerSelect():
    global playerPiece
    global npcPiece
    #asks the player which piece they want, and sets it to global playerPiece
    #also sets the npcPiece to the opposite
    while True:
        playerPiece = input("please select a piece, X/O : ")
        if playerPiece == "X" or playerPiece == "O":
            print ("you have chosen : " + playerPiece)
            if playerPiece == "X":
                npcPiece = "O"
            else: 
                npcPiece = "X"
            break
        print ("invalid selection")

def plotmove(input,type):
    while True: 
        if input <= 3:
            #row 1
            gb[0][input-1] = type                
        else:
            if input > 3 and input <= 6:
                #row 2
                gb[1][input-4] = type
                
            else:
                if input > 6 and input <= 9:
                    #row 3
                    gb[2][input-7] = type
                    
                    
        base()
        return

def run():
    ################################# main game loop
    global running
    running = True
    while running:
        print ("Welcome to tic tac toe (V1) \n") 
        playerSelect()
        whoGoesFirst()
        base()



        ##End Plate
        running = False
        print ("Coded by Ash Marshall 31/03/2019")

def test():
    sp = []
    tester = str(row1),str(row2),str(row3)
    
    print (tester.split())