예제 #1
0
def Player_Move(Game):
    GameBoard.Game_Display(Game)
    print(" ")
    Check_Value1 = False

    #Loop Used To Ensure Player Inputs Valid Moves
    while (Check_Value1 == False):
        Heap_Chosen = input("Choose Heap: ")
        Pieces_Chosen = input("Choose Number of Pieces To Remove: ")

        #Check If Input Is Invalid (E.G. Not Integer)
        try:
            Heap_Chosen = int(Heap_Chosen)
            Pieces_Chosen = int(Pieces_Chosen)
        except ValueError:
            print("Input Error, Try Again")
            Check_Value1 = False
            break

        #Check If Input Is Invalid (E.G. Not With Heap and Pieces Range)
        if (Heap_Chosen < 0) or (Heap_Chosen > (len(Game) - 1)) or (
                Pieces_Chosen < 1) or (Pieces_Chosen > Game[Heap_Chosen]):
            print("Invalid Input, Try Again")
            Check_Value1 = False
        else:
            Game[Heap_Chosen] = Game[Heap_Chosen] - Pieces_Chosen
            Check_Value1 = True

    return Game
예제 #2
0
def Computer_Move(Game, Nim_Sum):
    print(" ")
    GameBoard.Game_Display(Game)

    # If Player Move Ends with Nim-Sum != 0, Attempt To Make Nim-Sum = 0
    if (Nim_Sum != 0):
        for i in range(len(Game)):
            #Searches For Best Valid Move, If Found Take Move
            if (Game[i] ^ Nim_Sum < Game[i]):
                #Remove the "nim-sum" number of pieces from heap if possible
                print(" ")
                print("Computer Removes", Game[i] - (Game[i] ^ Nim_Sum),
                      "Pieces From Heap", i)
                Game[i] = Game[i] ^ Nim_Sum
                return Game

    #If Player Move Ends with Nim-Sum = 0, Make Random Move (Hope Player Makes Error)
    else:
        Random_Heap = random.randint(0, len(Game) - 1)
        Game[Random_Heap] -= random.randint(1, Game[Random_Heap])
        return Game