#################################################################
##  CS 3200 (Fall 2025), Assignment #2                         ##
##  Program File Name: altNim.py                               ##
##       Student Name: Todd Wareham                            ##
##         Login Name: harold                                  ##
##              MUN #: 8008765                                 ##
#################################################################

## Main program for playing a game of altNim against a computer.

import sys
from myAltNimClasses import *
from myAltNimFunctions import *


def main():
   if (len(sys.argv) != 5):
      print("format: altNim.py humanFirst? startOdd? maxDepth(>0) givenSticks(>1)")
      exit(0)

   if (sys.argv[1] == "True"):
      humanFirst = True
   elif (sys.argv[1] == "False"):
      humanFirst = False
   else:
      humanFirst = None

   if (sys.argv[2] == "True"):
      startOdd = True
   elif (sys.argv[2] == "False"):
      startOdd = False
   else:
      startOdd = None

   maxDepth = int(sys.argv[3])
   givenSticks = int(sys.argv[4])

   print("\nWelcome to altNim!\n")
   print("   " + ("Human" if humanFirst else "Machine") + " plays first")
   print("   " + ("Odd" if startOdd else "Even") + " move first")
   print("   " + str(maxDepth) + " move lookahead")
   print("   " + str(givenSticks) + " initial matchsticks\n")

   gameMoveNum = 1
   sticksRemaining = givenSticks

   while (True):
      if (((gameMoveNum % 2 == 1) and humanFirst) or
          ((gameMoveNum % 2 == 0) and not humanFirst)):
         print("Game Move # " + str(gameMoveNum) + " (Human):")
         print("   " + str(sticksRemaining) + " matchsticks remaining")
         startState = State(startOdd, maxDepth, ((gameMoveNum - 1) % 4), 
                                                None, sticksRemaining)
         bestVal = MiniMax(startState, 0, True)
         if (bestVal == 1000):
            print("   Human wins on any move\n")
            break
         elif (bestVal == -1000):
            print("   Human loses on any move\n")
            break
         else:
            print("   Best value of " + str(bestVal) + 
                  " obtained by recommended action " +
                  str(startState.getCurrentBestAction()) 
                  + ": Action? ", end = "")
            reply = input()
            if (reply == ""):
               action = startState.getCurrentBestAction()
            else:
               action = int(reply)
         sticksRemaining = sticksRemaining + action
         gameMoveNum = gameMoveNum + 1
      else:
         print("Game Move # " + str(gameMoveNum) + " (Machine):")
         print("   " + str(sticksRemaining) + " matchsticks remaining")
         startState = State(startOdd, maxDepth, ((gameMoveNum - 1) % 4), 
                                                None, sticksRemaining)
         bestVal = MiniMax(startState, 0, True)
         if (bestVal == 1000):
            print("   Machine wins on any move\n")
            break
         elif (bestVal == -1000):
            print("   Machine loses on any move\n")
            break
         else:
            print("   Best value of " + str(bestVal) + 
                  " obtained by machine with action " +
                  str(startState.getCurrentBestAction()))
         sticksRemaining = sticksRemaining + \
                           startState.getCurrentBestAction()
         gameMoveNum = gameMoveNum + 1

main()
