def main(argv): # Make sure we have enough command-line arguments if len(argv) != 5: print('Four command-line arguments are needed:') print( 'Usage: \'python3 %s interactive [input_file] [computer-next/human-next] [depth]\'' % argv[0]) print( 'or: \'python3 %s one-move [input_file] [output_file] [depth]\'' % argv[0]) sys.exit(-1) game_mode, inFile, nextPlayer, depth = argv[1:5] if game_mode != 'interactive' and game_mode != 'one-move': print('%s is an unrecognized game mode' % game_mode) sys.exit(-1) currentGame = MaxConnect4Game() # Create a game # Try to open the input file try: currentGame.gameFile = open(inFile, 'r') except IOError: sys.exit("\nError opening input file.\nCheck file name.\n") # Read the initial game state from the file and save in a 2D list file_lines = currentGame.gameFile.readlines() currentGame.gameBoard = [[int(char) for char in line[0:7]] for line in file_lines[0:-1]] currentGame.currentTurn = int(file_lines[-1][0]) currentGame.gameFile.close() print('\nWelcome to MaxConnect-4\n\n') input("Press ENTER to continue...") print(chr(27) + "[2J") print('Game state before move:') currentGame.printGameBoard() # Update a few game variables based on initial state and print the score currentGame.countScore() print('Score: Player 1 = %d, Player 2 = %d\n' % (currentGame.player1Score, currentGame.player2Score)) if game_mode == 'interactive': interactiveGame(currentGame, depth, nextPlayer, inFile) else: outFile = argv[3] try: currentGame.outFile = open(outFile, 'w') except: sys.exit('Error opening output file.') oneMoveGame(currentGame, depth)
def setInitState(self, inFile, gameMode, nextPlayer): self.initialGameboard = maxconnect.MaxConnect4Game() self.initialGameboard.setBoardFromFile(inFile, gameMode, nextPlayer) self.initialGameboard.printGameBoard() return self.initialGameboard
def __init__(self): initialGameboard = maxconnect.MaxConnect4Game() depth_limit = 0
def __init__(self): initialgameBoard = M.MaxConnect4Game() depth_limit = 0
def initState(self, inputFile, gameMode, nextPlayer): self.initialgameBoard = M.MaxConnect4Game() self.initialgameBoard.setBoard(inputFile, gameMode, nextPlayer) self.initialgameBoard.printGameBoard() return self.initialgameBoard
def setInitState(self,inputFile,game_mode,nextPlayer): self.initialGameboard = m.MaxConnect4Game() self.initialGameboard.setBoard(inputFile,game_mode,nextPlayer) self.initialGameboard.printBoard() return self.initialGameboard
def minimaxdecision(state,depth): state = MaxConnect4Game() current_state = state.gameBoard
def maxvalue(state): state = MaxConnect4Game()
def main(argv): # Make sure we have enough command-line arguments if len(argv) != 5: print 'Four command-line arguments are needed:' print( 'Usage: %s interactive [input_file] [computer-next/human-next] [depth]' % argv[0]) print('or: %s one-move [input_file] [output_file] [depth]' % argv[0]) sys.exit(2) game_mode, inFile = argv[1:3] if not game_mode == 'interactive' and not game_mode == 'one-move': print('%s is an unrecognized game mode' % game_mode) sys.exit(2) currentGame = MaxConnect4Game.maxConnect4Game() # Create a game # Try to open the input file try: currentGame.gameFile = open(inFile, 'r') except IOError: sys.exit("\nError opening input file.\nCheck file name.\n") # Read the initial game state from the file and save in a 2D list file_lines = currentGame.gameFile.readlines() currentGame.gameBoard = [[int(char) for char in line[0:7]] for line in file_lines[0:-1]] currentGame.currentTurn = int(file_lines[-1][0]) currentGame.gameFile.close() print '\nMaxConnect-4 game\n' print 'Game state before move:' currentGame.printGameBoard() # Update a few game variables based on initial state and print the score currentGame.checkPieceCount() currentGame.countScore() print('Score: Player 1 = %d, Player 2 = %d\n' % (currentGame.player1Score, currentGame.player2Score)) # Taking currentGame.maxPlayer as the one who plays first so the algo for comp will always start with MaxVal call if currentGame.currentTurn == 1: currentGame.maxPlayer = 1 currentGame.minPlayer = 2 elif currentGame.currentTurn == 2: currentGame.maxPlayer = 2 currentGame.minPlayer = 1 else: print 'Check input file. Current Turn row.' sys.exit() # Checking if input board is not full if currentGame.pieceCount == 42: print 'Input Board state is full. No more possible moves.' sys.exit() currentGame.argvList = argv currentGame.depthLimit = int(argv[4]) if game_mode == 'interactive': interactiveGame( currentGame ) # Be sure to pass whatever else you need from the command line else: # game_mode == 'one-move' # Set up the output file outFile = argv[3] try: currentGame.gameFile = open(outFile, 'w') except: sys.exit('Error opening output file.') oneMoveGame( currentGame ) # Be sure to pass any other arguments from the command line you might need.