def test(): state = ConnectFourBoard(7, 6) assert state.getBoard() == [[EMPTY] * 7] * 6 assert state.getPossiblePositionList(None) == [0, 1, 2, 3, 4, 5, 6] assert state.isWin() == None state.setMarker(RED, 0) assert state.getBoard() == [[EMPTY] * 7] * 5 + \ [[RED, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]] assert state.getPossiblePositionList(BLUE) == [0, 1, 2, 3, 4, 5, 6] assert state.getNextPlayer() == BLUE state.setMarker(BLUE, 1) assert state.getBoard() == [[EMPTY] * 7] * 5 + \ [[RED, BLUE, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]] assert state.getNextPlayer() == RED assert state.isFull() == False state.setMarker(RED, 0) state.setMarker(RED, 0) state.setMarker(RED, 0) assert state.isWin() == RED for x in range(7): for y in range(6): state.setMarker(BLUE, x) assert state.isFull() == True assert state.getPossiblePositionList(RED) == [] assert state.isWin() == BLUE print "Success"
def runGame(autoPlay=True): state = ConnectFourBoard(7, 6) maxPlayer = False minTime = None maxTime = None totalTime = 0 count = 0 if not autoPlay: human = input() if human in [0, 1]: human = human == 0 while True: marker = state.getNextPlayer() maxPlayer = not maxPlayer state.showBoard() if state.isWin(): break if not autoPlay and human == maxPlayer: while True: x = input() if state.setMarker(marker, x): break print marker print "Wrong position" else: start = time.time() if marker == RED: info = monteCarlo.get_play(state, marker) #info = minimax(state, 5, maxPlayer) #info = alphabeta(state, 6, -INFINITE, INFINITE, maxPlayer) pass else: info = monteCarlo.get_play(state, marker) #info = minimax(state, 5, maxPlayer) #info = alphabeta(state, 5, -INFINITE, INFINITE, maxPlayer) pass gap = time.time() - start if minTime == None or gap < minTime: minTime = gap if maxTime == None or gap > maxTime: maxTime = gap totalTime += gap count += 1 state.setMarker(marker, info[-1]) print marker print "Info:", info print "Gap :", gap print print "Win:", state.isWin() print print "MinTime :", minTime print "MaxTime :", maxTime print "MeanTime :", (totalTime / count) print "TotalTime:", totalTime print print "Count:", count