Connect 4 implemented with python AI alpha

I'm trying to create game called Connect Four with AI which use alpha-beta pruning algorithm in python. Here is the code what I have managed to do:

    # -*- coding: utf-8 -*-

import sys

class ConnectFour:
    def __init__(self):
        self.board  = [[],[],[],[],[],[]]
        for i in range(7):
            for j in range(6):
                self.board[j].append(" ")
        self.moves = 0
        self.colstack = [0,0,0,0,0,0,0]
        self.node = 0
        self.move = 0

    def PrintGameBoard(self):
        print('  0   1   2   3   4   5   6')
        for i in range(5, -1, -1):
            print('|---|---|---|---|---|---|---|')
            print("| ",end="")
            for j in range(7):
                print(self.board[i][j],end="")
                if j != 6:
                    print(" | ",end="")
                else:
                    print(" |")
        print('`---------------------------´')

    def CanPlay(self, col):
        return self.colstack[col] < 6

    def Play(self, col, board):
        board[self.colstack[col]][col] = 2
        self.colstack[col] += 1
        self.moves += 1
        return board

    def IsWinning(self, currentplayer):
        for i in range(6):
            for j in range(4):
                if self.board[i][j] == currentplayer and self.board[i][j+1] == currentplayer and self.board[i][j+2] == currentplayer and self.board[i][j+3] == currentplayer:
                    return True
        for i in range(3):
            for j in range(7):
                if self.board[i][j] == currentplayer and self.board[i+1][j] == currentplayer and self.board[i+2][j] == currentplayer and self.board[i+3][j] == currentplayer:
                    return True     
        for i in range(3):
            for j in range(4):
                if self.board[i][j] == currentplayer and self.board[i+1][j+1] == currentplayer and self.board[i+2][j+2] == currentplayer and self.board[i+3][j+3] == currentplayer:
                    return True
        for i in range(3,6):
            for j in range(4):
                if self.board[i][j] == currentplayer and self.board[i-1][j+1] == currentplayer and self.board[i-2][j+2] == currentplayer and self.board[i-3][j+3] == currentplayer:
                    return True
        return False

    def AlphaBeta(self, alpha, beta):
        self.node += 1
        if self.moves == 42:
            return 0
        for col in range(7):
            if self.CanPlay(col) and self.IsWinning(2):
                return (43 - self.moves)/2  
        max = (41 - self.moves)/2
        if beta > max:
            beta = max
            if alpha >= beta:
                return beta
        for col in range(7):
            if self.CanPlay(col):
                self.board[self.colstack[col]][col] = 2
                self.move = col
                score = -self.AlphaBeta(-alpha, -beta)
                if score >= beta:
                    return score
                elif score > alpha:
                    alpha = score
                self.board[self.colstack[col]][col] = " "

    def Solve(self, table, week=False):
        self.node = 0
        self.board = table
        if week:
            self.AlphaBeta(-1, 1)
            self.board = self.Play(self.move, table)
            return self.board
        else:
            self.AlphaBeta(-21, 21)
            self.board = self.Play(self.move, table)
            return self.board

    def PlayerMove(self, table):
        self.board  = table
        try:
            allowedmove = False
            while not allowedmove:
                print("Choose a column where you want to make your move (0-6): ",end="")
                col =input()
                if self.CanPlay(int(col)):
                    self.board[self.colstack[int(col)]][int(col)] = 1
                    self.moves += 1
                    self.colstack[int(col)] += 1
                    allowedmove = True
                else:
                    print("This column is full")
        except (NameError, ValueError, IndexError, TypeError, SyntaxError) as e:
            print("Give a number as an integer between 0-6!")
        else:
            return self.board

def PlayerMark():
    print("Player 1 starts the game")
    mark = ''
    while not (mark == "1" or mark == "2"):
        print('Do you want to be 1 or 2: ',end="")
        mark = input()
    if mark == "1":
        return 1
    else:
        return 2

def PlayAgain():
    print('Do you want to play again? (yes or no) :',end="")
    return input().lower().startswith('y')

def main():
    sys.setrecursionlimit(2000)
    print("Connect4")
    while True:
        mark = PlayerMark()
        connectfour = ConnectFour()
        if mark==1:
            print("You are going to start the gamernrn")
        else:
            print("Computer (negamax) starts the game")
        gameisgoing = True
        table  = [[],[],[],[],[],[]]
        for i in range(7):
            for j in range(6):
                table[j].append(" ")
        while gameisgoing:
            connectfour.PrintGameBoard()
            if mark == 1:
                table = connectfour.PlayerMove(table)
                if connectfour.IsWinning(1):
                    connectfour.PrintGameBoard()
                    print('You won the game!')
                    gameisgoing = False
                else:
                    if connectfour.moves==42:
                        connectfour.PrintGameBoard()
                        print('Game is tie')
                        break
                    else:
                        mark = 2
            else:
                move = connectfour.Solve(table)
                if connectfour.IsWinning(2):
                    connectfour.PrintGameBoard()
                    print('Computer won the game')
                    gameisgoing = False
                else:
                    if connectfour.moves==42:
                        connectfour.PrintGameBoard()
                        print('Game is tie')
                        break
                    else:
                        mark = 1
        if not PlayAgain():
            print("Game ended")
            break

if __name__ == '__main__':
    main()

Im not sure if the algorithm is working proberly but the problem is that I get RecursionError: maximum recirsion depth exceeded in comparison. If I increase recursionlimit I get in around 10000 MemoryError: Stack Overflow. I think that the problem is that there is too many states to handle with to my computer. Thats why I changed negamax algorithm to alpha-beta pruning but there seem to be still to many states. Is there a effective technique that algorithm search for example max depth 10 and still computer is almost invinciple. Im waiting your solutions.

链接地址: http://www.djcxy.com/p/56398.html

上一篇: 使用minimax搜索不完美信息的纸牌游戏

下一篇: 用python AI alpha实现连接4