Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

import base as Base
import copy, random, time, math
from PIL import Image, ImageDraw
 
 
# Player template for HIVE --- ALP semestral work
# Vojta Vonasek, 2023
 
 
# PUT ALL YOUR IMPLEMENTATION INTO THIS FILE
 
class Spider:
    def __init__(self, p,q, parent = None):
        self.p = p
        self.q = q
        self.parent = parent
        self.path = []
 
 
 
 
 
class Player(Base.Board):
    def __init__(self, playerName, myIsUpper, size, myPieces, rivalPieces):  #do not change this line
        Base.Board.__init__(self, myIsUpper, size, myPieces, rivalPieces)    #do not change this line
        self.playerName = playerName
        self.algorithmName = "myGreatMethod"
 
 
    def getAllEmptyCells(self):
        result = []
        for p in self.board:
            for q in self.board[p]:
                if self.isEmpty(p,q, self.board):
                    result.append( [p,q] )                        
        return result
 
 
    def getAllNonemptyCells(self):                    
        result = []
        for p in self.board:
            for q in self.board[p]:
                if not self.isEmpty(p,q, self.board):
                    result.append( [p,q] )                        
        return result
 
 
    def move(self):
        """ return [animal, oldP, oldQ, newP, newQ], or [animal, None, None, newP, newQ] or [] """
 
        #the following code just randomly places (ignoring all the rules) some random figure at the board
        emptyCells = self.getAllEmptyCells()
 
        if len(emptyCells) == 0:
            return []
 
        randomCell = emptyCells[ random.randint(0, len(emptyCells)-1) ]
        randomP, randomQ = randomCell
 
        for animal in self.myPieces:
            if self.myPieces[animal] > 0: #is this animal still available? if so, let's place it
                return [ animal, None, None, randomP, randomQ ]
 
 
 
        #all animals are places, let's move some randomly (again, while ignoring all rules)
        allFigures = self.getAllNonemptyCells()
        randomCell = allFigures[ random.randint(0, len(allFigures)-1) ]
        randomFigureP, randomFigureQ = randomCell
        #determine which animal is at randomFigureP, randomFigureQ
        animal = self.board[ randomFigureP ][ randomFigureQ ][-1]   # [-1] means the last letter
        return [animal, randomFigureP, randomFigureQ, randomP, randomQ ]
 
    def canSlide(self, board, p,q, lp, lq):
        #p,q is abs, lp,lq is local
        slidingNeighbors = {}
        slidingNeighbors[ (1,0) ] = [ [1,-1],[0,1] ]
        slidingNeighbors[ (1,-1) ] = [ [1,0],[0,-1] ]
        slidingNeighbors[ (0,-1) ] = [ [1,-1],[-1,0] ]
        slidingNeighbors[ (-1,0) ] = [ [0,-1],[-1,1] ]
        slidingNeighbors[ (-1,1) ] = [ [-1,0],[0,1] ]
        slidingNeighbors[ (0,1) ] = [ [-1,1],[1,0] ]
 
        newp, newq = p+lp, q+lq
        if self.inBoard(newp, newq) and self.isEmpty(newp, newq, board):
            for neighbor in slidingNeighbors[ (lp,lq) ]:
                np, nq = neighbor
                if self.inBoard(p+np, q+nq) and \
                   self.isEmpty(p+np, q+nq, board):
                    return True
        return False
 
 
    def spiderMoves(self, p,q, board):
        result = []
        N = [ [1,-1], [1,0],[0,1],[-1,1],[-1,0],[0,-1] ]
        start = Spider(p,q)
        start.path = [ [p,q ] ]
        queue = [ start ]
        while len(queue) > 0:
            actual = queue.pop()
            #actual.p, actual.q
            if len(actual.path) == 4:
                result.append( actual.path )
                continue
 
            for neighbor in N:
                lp, lq = neighbor
                if self.canSlide(self.board, actual.p,actual.q, lp, lq):
                    newp, newq = actual.p+lp,actual.q + lq
                    if not [newp, newq] in actual.path:
                        newState = Spider(newp, newq, actual)
                        newState.path = copy.deepcopy(actual.path) + [[newp, newq ]]
                        queue.insert(0, newState)
        return result
 
    def testSpider(self):
        self.board[1][6] = "S"
 
        moves = self.spiderMoves(1,6, self.board)
        frame = 0
        for move in moves:
            HL = {}
            for pos in move:
                p,q = pos
                HL[ (p,q) ] = "#000F00"
            self.saveImage("spider"+str(frame)+".png", HL=HL)
            frame += 1
 
 
 
 
    def test(self):
        self.board[1][6] = "Q"
        self.board[2][5] = "S"
        self.board[1][7] = "S"
        N = [ [1,-1], [1,0],[0,1],[-1,1],[-1,0],[0,-1] ]
        frame = 0
        p,q = 1,6
        HL = {}
 
        for neighbor in N:
            lp, lq = neighbor
            if self.canSlide(self.board, p,q, lp, lq):
                self.board[ p+lp][ q+lq] = "q"
                HL[ (p+lp,q+lq) ] = "#00FF00"
                self.saveImage("canSlide" + str(frame) + ".png", HL=HL)
                self.board[ p+lp][ q+lq] = ""
                frame += 1
 
 
 
def updatePlayers(move, activePlayer, passivePlayer):
    """ write move made by activePlayer player
        this method assumes that all moves are correct, no checking is made
    """
    if len(move) == 0:
        return
 
    animal, p,q, newp, newq = move
    if p == None and q == None:
        #placing new animal
        activePlayer.myPieces[animal]-=1
        passivePlayer.rivalPieces = activePlayer.myPieces.copy()
    else:
        #just moving animal
        #delete its old position
        activePlayer.board[p][q] = activePlayer.board[p][q][:-1]
        passivePlayer.board[p][q] = passivePlayer.board[p][q][:-1]
 
    activePlayer.board[newp][newq] += animal
    passivePlayer.board[newp][newq] += animal
 
 
 
 
if __name__ == "__main__":
    boardSize = 13
    smallFigures = { "q":1, "a":2, "b":2, "s":2, "g":2 }  #key is animal, value is how many is available for placing
    bigFigures = { figure.upper(): smallFigures[figure] for figure in smallFigures } #same, but with upper case
 
    P1 = Player("player1", False, 13, smallFigures, bigFigures)
    P2 = Player("player2", True, 13, bigFigures, smallFigures)
 
    filename = "begin.png"
    P1.saveImage(filename)
 
    P1.testSpider()
    print("end")
 
 
    moveIdx = 0
    while True:
        break
 
        move = P1.move()
        print("P1 returned", move)
        updatePlayers(move, P1, P2)  #update P1 and P2 according to the move
        filename = "move-{:03d}-player1.png".format(moveIdx) 
        P1.saveImage(filename)     
 
 
        move = P2.move()
        print("P2 returned", move)
        updatePlayers(move, P2, P1) #update P2 and P1 according to the move
        filename = "move-{:03d}-player2.png".format(moveIdx)
        P1.saveImage(filename)
 
        moveIdx += 1
        P1.myMove = moveIdx
        P2.myMove = moveIdx
 
        if moveIdx > 50:
            print("End of the test game")
            break

courses/b3b33alp/cviceni/faq/spiderdraft.txt · Last modified: 2023/12/18 16:05 by vonasvoj