Title | Solution assignment 4 |
---|---|
Course | ARTIFICIAL INTELLIGENCE I |
Institution | The University of Texas at Arlington |
Pages | 17 |
File Size | 66.7 KB |
File Type | |
Total Downloads | 36 |
Total Views | 122 |
Programming assignment 4 solution...
Maxconnect4.py
""" Name: Srihari Chandramouli UTA ID: 1001529776 Programming language: Python
CITATION Author name: Siddhant Gawsane Title: ArtificialIntelligence1 Link: www.github.com/siddhantgawsane/Artificialintelligence1/tree/master/Assignment2 """
import sys from MaxConnect4Game import *
def oneMoveGame(currentGame, depth): # This is the one-move mode!! if currentGame.pieceCount == 42: # Checking if the board full already! print 'BOARD FULL\n\nGame Over!\n' sys.exit(0)
move = currentGame.aiPlay(currentGame,depth) result = currentGame.playPiece(move) afterMoveOperations(currentGame, move) currentGame.gameFile.close()
def afterMoveOperations(currentGame, move): # This function tells us about all the operations that must be done after the moves
print('\n\nMove no. %d: Player %d, column %d\n' % (currentGame.pieceCount, currentGame.currentTurn, move + 1)) if currentGame.currentTurn == 1: currentGame.currentTurn = 2 elif currentGame.currentTurn == 2: currentGame.currentTurn = 1
print 'Game state after move:' currentGame.printGameBoard()
currentGame.countScore() print('Score: Player 1 = %d, Player 2 = %d\n' % (currentGame.player1Score, currentGame.player2Score))
currentGame.printGameBoardToFile()
def interactiveGame(currentGame, depth): # This is the interactive game mode
while not currentGame.pieceCount == 42: if currentGame.currentTurn == 1: userMove = input("Enter the column number [1-7] : ") if not 0 < userMove < 8: print "Invalid column number!" continue if not currentGame.playPiece(userMove - 1): print "This column is full!" continue try:
currentGame.gameFile = open("human.txt", 'w') except: sys.exit('Error opening output file.') afterMoveOperations(currentGame, userMove - 1)
elif not currentGame.pieceCount == 42: move = currentGame.aiPlay(currentGame,depth) result = currentGame.playPiece(move) try: currentGame.gameFile = open("comupter.txt", 'w') except: sys.exit('Error opening output file.') afterMoveOperations(currentGame, move)
currentGame.gameFile.close()
if currentGame.player1Score > currentGame.player2Score: print "Player 1 wins!!" elif currentGame.player2Score > currentGame.player1Score: print "Computer wins!!" else: print "It's a draw!!" print "Thank you for playing 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 Game mode unrecognized' % game_mode) sys.exit(2)
currentGame = maxConnect4Game() # Create a game
# Try to open the input file try: currentGame.gameFile = open(inFile, 'r') except IOError: sys.exit("\nCant open the input file.\nCheck the spelling of the file.\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))
if game_mode == 'interactive': if argv[3] == 'computer-next': # override current turn according to commandline arguments currentGame.currentTurn = 2 else: # human-next currentGame.currentTurn = 1 interactiveGame(currentGame, argv[4]) # 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('Cant open the output file.') oneMoveGame(currentGame, argv[4]) # Be sure to pass any other arguments from the command line you might need.
if __name__ == '__main__': main(sys.argv)
maxConnect4game.py #!/usr/bin/env python
# Written by Chris Conly based on C++ # code provided by Vassilis Athitsos # Written to be Python 2.4 compatible for omega
from copy import copy import copy import random import sys def possibleMoves(board): possibleMoves = [] for col, colVal in enumerate(board[0]): if colVal == 0: possibleMoves.append(col) return possibleMoves
def result(oldGame, column): newGame = maxConnect4Game()
try: newGame.nodeDepth = oldGame.nodeDepth + 1 except AttributeError: newGame.nodeDepth = 1
newGame.pieceCount = oldGame.pieceCount
newGame.gameBoard = copy.deepcopy(oldGame.gameBoard) if not newGame.gameBoard[0][column]: for i in range(5, -1, -1): if not newGame.gameBoard[i][column]: newGame.gameBoard[i][column] = oldGame.currentTurn newGame.pieceCount += 1 break if oldGame.currentTurn == 1: newGame.currentTurn = 2 elif oldGame.currentTurn == 2: newGame.currentTurn = 1
newGame.checkPieceCount() newGame.countScore()
return newGame
class Minimax: def __init__(self, game, depth): self.currentTurn = game.currentTurn self.game = game self.maxDepth = int(depth)
def makeDecision(self):
#Returns minimax's decision
minValues = [] possMoves = possibleMoves(self.game.gameBoard)
for move in possMoves: rslt = result(self.game, move) minValues.append(self.minVal(rslt, 99999, -99999))
chosen = possMoves[minValues.index(max(minValues))] return chosen
def minVal(self, state, alpha, beta):
# Performs minimizing operations
if state.pieceCount == 42 or state.nodeDepth == self.maxDepth: return self.utility(state) v = 99999
for move in possibleMoves(state.gameBoard): newState = result(state, move)
v = min(v, self.maxVal(newState, alpha, beta)) if v = beta: return v alpha = max(alpha, v) return v
def utility(self, state):
#Returns the utility that needs to be maximized or minimized
if self.currentTurn == 1: utility = state.player1Score * 2 - state.player2Score elif self.currentTurn == 2: utility = state.player2Score * 2 - state.player1Score
return utility
class maxConnect4Game: def __init__(self): self.gameBoard = [[0 for i in range(7)] for j in range(6)] self.currentTurn = 1 self.player1Score = 0 self.player2Score = 0 self.pieceCount = 0 self.gameFile = None random.seed()
# Count the number of pieces already played def checkPieceCount(self): self.pieceCount = sum(1 for row in self.gameBoard for piece in row if piece)
# Output current game status to console def printGameBoard(self): print ' -----------------' for i in range(6): print ' |', for j in range(7): print('%d' % self.gameBoard[i][j]), print '| ' print ' -----------------'
# Output current game status to file def printGameBoardToFile(self): for row in self.gameBoard: self.gameFile.write(''.join(str(col) for col in row) + '\r\n') self.gameFile.write('%s\r\n' % str(self.currentTurn))
# Place the current player's piece in the requested column def playPiece(self, column): if not self.gameBoard[0][column]: for i in range(5, -1, -1): if not self.gameBoard[i][column]: self.gameBoard[i][column] = self.currentTurn self.pieceCount += 1 return 1
# The AI section. Currently plays randomly. def aiPlay(self,currentGame,depth): searchTree = Minimax(currentGame, depth)
move = searchTree.makeDecision() return move # Calculate the number of 4-in-a-row each player has def countScore(self): self.player1Score = 0; self.player2Score = 0;
# Check horizontally for row in self.gameBoard: # Check player 1 if row[0:4] == [1]*4: self.player1Score += 1 if row[1:5] == [1]*4: self.player1Score += 1 if row[2:6] == [1]*4: self.player1Score += 1 if row[3:7] == [1]*4: self.player1Score += 1 # Check player 2 if row[0:4] == [2]*4: self.player2Score += 1 if row[1:5] == [2]*4: self.player2Score += 1 if row[2:6] == [2]*4: self.player2Score += 1 if row[3:7] == [2]*4: self.player2Score += 1
# Check vertically
for j in range(7): # Check player 1 if (self.gameBoard[0][j] == 1 and self.gameBoard[1][j] == 1 and self.gameBoard[2][j] == 1 and self.gameBoard[3][j] == 1): self.player1Score += 1 if (self.gameBoard[1][j] == 1 and self.gameBoard[2][j] == 1 and self.gameBoard[3][j] == 1 and self.gameBoard[4][j] == 1): self.player1Score += 1 if (self.gameBoard[2][j] == 1 and self.gameBoard[3][j] == 1 and self.gameBoard[4][j] == 1 and self.gameBoard[5][j] == 1): self.player1Score += 1 # Check player 2 if (self.gameBoard[0][j] == 2 and self.gameBoard[1][j] == 2 and self.gameBoard[2][j] == 2 and self.gameBoard[3][j] == 2): self.player2Score += 1 if (self.gameBoard[1][j] == 2 and self.gameBoard[2][j] == 2 and self.gameBoard[3][j] == 2 and self.gameBoard[4][j] == 2): self.player2Score += 1 if (self.gameBoard[2][j] == 2 and self.gameBoard[3][j] == 2 and self.gameBoard[4][j] == 2 and self.gameBoard[5][j] == 2): self.player2Score += 1
# Check diagonally
# Check player 1 if (self.gameBoard[2][0] == 1 and self.gameBoard[3][1] == 1 and self.gameBoard[4][2] == 1 and self.gameBoard[5][3] == 1): self.player1Score += 1 if (self.gameBoard[1][0] == 1 and self.gameBoard[2][1] == 1 and
self.gameBoard[3][2] == 1 and self.gameBoard[4][3] == 1): self.player1Score += 1 if (self.gameBoard[2][1] == 1 and self.gameBoard[3][2] == 1 and self.gameBoard[4][3] == 1 and self.gameBoard[5][4] == 1): self.player1Score += 1 if (self.gameBoard[0][0] == 1 and self.gameBoard[1][1] == 1 and self.gameBoard[2][2] == 1 and self.gameBoard[3][3] == 1): self.player1Score += 1 if (self.gameBoard[1][1] == 1 and self.gameBoard[2][2] == 1 and self.gameBoard[3][3] == 1 and self.gameBoard[4][4] == 1): self.player1Score += 1 if (self.gameBoard[2][2] == 1 and self.gameBoard[3][3] == 1 and self.gameBoard[4][4] == 1 and self.gameBoard[5][5] == 1): self.player1Score += 1 if (self.gameBoard[0][1] == 1 and self.gameBoard[1][2] == 1 and self.gameBoard[2][3] == 1 and self.gameBoard[3][4] == 1): self.player1Score += 1 if (self.gameBoard[1][2] == 1 and self.gameBoard[2][3] == 1 and self.gameBoard[3][4] == 1 and self.gameBoard[4][5] == 1): self.player1Score += 1 if (self.gameBoard[2][3] == 1 and self.gameBoard[3][4] == 1 and self.gameBoard[4][5] == 1 and self.gameBoard[5][6] == 1): self.player1Score += 1 if (self.gameBoard[0][2] == 1 and self.gameBoard[1][3] == 1 and self.gameBoard[2][4] == 1 and self.gameBoard[3][5] == 1): self.player1Score += 1 if (self.gameBoard[1][3] == 1 and self.gameBoard[2][4] == 1 and self.gameBoard[3][5] == 1 and self.gameBoard[4][6] == 1): self.player1Score += 1
if (self.gameBoard[0][3] == 1 and self.gameBoard[1][4] == 1 and self.gameBoard[2][5] == 1 and self.gameBoard[3][6] == 1): self.player1Score += 1
if (self.gameBoard[0][3] == 1 and self.gameBoard[1][2] == 1 and self.gameBoard[2][1] == 1 and self.gameBoard[3][0] == 1): self.player1Score += 1 if (self.gameBoard[0][4] == 1 and self.gameBoard[1][3] == 1 and self.gameBoard[2][2] == 1 and self.gameBoard[3][1] == 1): self.player1Score += 1 if (self.gameBoard[1][3] == 1 and self.gameBoard[2][2] == 1 and self.gameBoard[3][1] == 1 and self.gameBoard[4][0] == 1): self.player1Score += 1 if (self.gameBoard[0][5] == 1 and self.gameBoard[1][4] == 1 and self.gameBoard[2][3] == 1 and self.gameBoard[3][2] == 1): self.player1Score += 1 if (self.gameBoard[1][4] == 1 and self.gameBoard[2][3] == 1 and self.gameBoard[3][2] == 1 and self.gameBoard[4][1] == 1): self.player1Score += 1 if (self.gameBoard[2][3] == 1 and self.gameBoard[3][2] == 1 and self.gameBoard[4][1] == 1 and self.gameBoard[5][0] == 1): self.player1Score += 1 if (self.gameBoard[0][6] == 1 and self.gameBoard[1][5] == 1 and self.gameBoard[2][4] == 1 and self.gameBoard[3][3] == 1): self.player1Score += 1 if (self.gameBoard[1][5] == 1 and self.gameBoard[2][4] == 1 and self.gameBoard[3][3] == 1 and self.gameBoard[4][2] == 1): self.player1Score += 1 if (self.gameBoard[2][4] == 1 and self.gameBoard[3][3] == 1 and
self.gameBoard[4][2] == 1 and self.gameBoard[5][1] == 1): self.player1Score += 1 if (self.gameBoard[1][6] == 1 and self.gameBoard[2][5] == 1 and self.gameBoard[3][4] == 1 and self.gameBoard[4][3] == 1): self.player1Score += 1 if (self.gameBoard[2][5] == 1 and self.gameBoard[3][4] == 1 and self.gameBoard[4][3] == 1 and self.gameBoard[5][2] == 1): self.player1Score += 1 if (self.gameBoard[2][6] == 1 and self.gameBoard[3][5] == 1 and self.gameBoard[4][4] == 1 and self.gameBoard[5][3] == 1): self.player1Score += 1
# Check player 2 if (self.gameBoard[2][0] == 2 and self.gameBoard[3][1] == 2 and self.gameBoard[4][2] == 2 and self.gameBoard[5][3] == 2): self.player2Score += 1 if (self.gameBoard[1][0] == 2 and self.gameBoard[2][1] == 2 and self.gameBoard[3][2] == 2 and self.gameBoard[4][3] == 2): self.player2Score += 1 if (self.gameBoard[2][1] == 2 and self.gameBoard[3][2] == 2 and self.gameBoard[4][3] == 2 and self.gameBoard[5][4] == 2): self.player2Score += 1 if (self.gameBoard[0][0] == 2 and self.gameBoard[1][1] == 2 and self.gameBoard[2][2] == 2 and self.gameBoard[3][3] == 2): self.player2Score += 1 if (self.gameBoard[1][1] == 2 and self.gameBoard[2][2] == 2 and self.gameBoard[3][3] == 2 and self.gameBoard[4][4] == 2): self.player2Score += 1 if (self.gameBoard[2][2] == 2 and self.gameBoard[3][3] == 2 and
self.gameBoard[4][4] == 2 and self.gameBoard[5][5] == 2): self.player2Score += 1 if (self.gameBoard[0][1] == 2 and self.gameBoard[1][2] == 2 and self.gameBoard[2][3] == 2 and self.gameBoard[3][4] == 2): self.player2Score += 1 if (self.gameBoard[1][2] == 2 and self.gameBoard[2][3] == 2 and self.gameBoard[3][4] == 2 and self.gameBoard[4][5] == 2): self.player2Score += 1 if (self.gameBoard[2][3] == 2 and self.gameBoard[3][4] == 2 and self.gameBoard[4][5] == 2 and self.gameBoard[5][6] == 2): self.player2Score += 1 if (self.gameBoard[0][2] == 2 and self.gameBoard[1][3] == 2 and self.gameBoard[2][4] == 2 and self.gameBoard[3][5] == 2): self.player2Score += 1 if (self.gameBoard[1][3] == 2 and self.gameBoard[2][4] == 2 and self.gameBoard[3][5] == 2 and self.gameBoard[4][6] == 2): self.player2Score += 1 if (self.gameBoard[0][3] == 2 and self.gameBoard[1][4] == 2 and self.gameBoard[2][5] == 2 and self.gameBoard[3][6] == 2): self.player2Score += 1
if (self.gameBoard[0][3] == 2 and self.gameBoard[1][2] == 2 and self.gameBoard[2][1] == 2 and self.gameBoard[3][0] == 2): self.player2Score += 1 if (self.gameBoard[0][4] == 2 and self.gameBoard[1][3] == 2 and self.gameBoard[2][2] == 2 and self.gameBoard[3][1] == 2): self.player2Score += 1 if (self.gameBoard[1][3] == 2 and self.gameBoard[2][2] == 2 and self.gameBoard[3][1] == 2 and self.gameBoard[4][0] == 2):
self.player2Score += 1 if (self.gameBoard[0][5] == 2 and self.gameBoard[1][4] == 2 and self.gameBoard[2][3] == 2 and self.gameBoard[3][2] == 2): self.player2Score += 1 if (self.gameBoard[1][4] == 2 and self.gameBoard[2][3] == 2 and self.gameBoard[3][2] == 2 and self.gameBoard[4][1] == 2): self.player2Score += 1 if (self.gameBoard[2][3] == 2 and self.gameBoard[3][2] == 2 and self.gameBoard[4][1] == 2 and self.gameBoard[5][0] == 2): self.player2Score += 1 if (self.gameBoard[0][6] == 2 and self.gameBoard[1][5] == 2 and self.gameBoard[2][4] == 2 and self.gameBoard[3][3] == 2): self.player2Score += 1 if (self.gameBoard[1][5] == 2 and self.gameBoard[2][4] == 2 and self.gameBoard[3][3] == 2 and self.gameBoard[4][2] == 2): self.player2Score += 1 if (self.gameBoard[2][4] == 2 and self.gameBoard[3][3] == 2 and self.gameBoard[4][2] == 2 and self.gameBoard[5][1] == 2): self.player2Score += 1 if (self.gameBoard[1][6] == 2 and self.gameBoard[2][5] == 2 and self.gameBoard[3][4] == 2 and self.gameBoard[4][3] == 2): self.player2Score += 1 if (self.gameBoard[2][5] == 2 and self.gameBoard[3][4] == 2 and self.gameBoard[4][3] == 2 and self.gameBoard[5][2] == 2): self.player2Score += 1 if (self.gameBoard[2][6] == 2 and self.gameBoard[3][5] == 2 and self.gameBoard[4][4] == 2 and self.gameBoard[5][3] == 2): self.player2Score += 1...