예제 #1
0
 def init_board(self):
     # Put all the chess pieces into the board
     pieces = Pieces()
     self.pieces = pieces.return_exist_pieces()
     for key in self.pieces:
         if key in self.board_pieces:
             self.board_pieces[key] = self.pieces[key]
예제 #2
0
class Board:
    """Class displays the game board"""
    def __init__(self, WIDTH, BOXES):
        self.BOXES = BOXES
        self.box_size = WIDTH / self.BOXES
        self.pieces = Pieces(WIDTH, BOXES)

    def set_up_board(self):
        """Place desired number of cells on the board"""
        for i in range(self.BOXES):
            for j in range(self.BOXES):
                x = i * self.box_size
                y = j * self.box_size
                stroke(0)
                fill(0, 0.6, 0)
                rect(x, y, self.box_size, self.box_size)
        self.pieces.starting_pieces()

    def play(self, x, y, player):
        """Returns pieces"""
        return self.pieces.play(x, y, player)

    def no_more_moves(self, player):
        return self.pieces.no_more_moves(player)

    def determine_winner(self):
        self.pieces.determine_winner()

    def computer_move(self, player):
        self.pieces.computer_move(player)
예제 #3
0
 def test_rotation(self):
     random_piece = Piece(Pieces.random_shape(), (0, 0), rotation=0)
     self.assertEqual(random_piece.rotation, 0)
     random_piece.rotate(2)
     self.assertEqual(random_piece.rotation, 2)
     random_piece.rotate(-3)
     self.assertEqual(random_piece.rotation, 3)
예제 #4
0
def test_constructor():
    pieces = Pieces(600, 8)
    single_piece = SinglePiece

    assert pieces.WIDTH == 600
    assert pieces.BOXES == 8
    assert pieces.box_width == 75
예제 #5
0
async def download():

    print(
        "Proceso de descarga inicializado. \n\nEs posible que se deba esperar un tiempo prologando para la obtencion de pieces para la descarga.\n"
    )

    peer_addresses = await Tracker(torrent).begin_connection()

    # asignacion de la clase encargada de gestionar las piezas, como la descarga que sera utilizada por cada
    # valido para hacer la descarga

    pieces = Pieces(torrent, torrent_writer.get_received_pieces_queue())

    # lista de peers intanciados con su parametro de torrent, puerto, host
    # y descarga previamente creada.

    peers = []

    for host, port in peer_addresses:
        peers.append(Peer(torrent, host, port, pieces, torrent_writer))

    # lista de ejecucion de la descarga realizada para cada peer,
    # con su espera hasta que el mismo responda.

    for peer in peers:
        peer.pieces.peerList = peers
        await peer.download()
예제 #6
0
 def __init__(self, height=16, width=16, fileName='./state'):
     self.board = Board(height, width)
     self.pieces = Pieces()
     self.moves = []
     self.boardPath = []
     self.step = 0
     self.stepNo = 0
     self.stepMax = 0
     self.boardMax = []
     self.stepPath = []
     if fileName[-6:] == '.state':
         self.fileNameState = fileName
         self.fileNameBoard = fileName.replace('.state', '.board')
         self.fileNameMax = fileName.replace('.state', '.max')
     else:
         self.fileNameState = fileName + '.state'
         self.fileNameBoard = fileName + '.board'
         self.fileNameMax = fileName + '.max'
예제 #7
0
파일: board.py 프로젝트: dlink/chess
    def setup(self):
        '''Set up the pieces on the board based self.setup_data'''

        # read setup data
        for row in open(self.setup_data).readlines():
            # ignore comments, skip blank lines
            row = re.sub('#.*', '', row).strip()
            if not row:
                continue

            # eq.: w:Ke1
            color, piece_position = row.split(':')
            char = piece_position[0]
            position = piece_position[1:]
            piece = Pieces.create(char, color)

            self.placePiece(piece, position)
            self.pieces.append(piece)
예제 #8
0
def test_constructor():
    board = Board(600, 8)
    pieces = Pieces(600, 8)
    pass
예제 #9
0
 def __init__(self, WIDTH, BOXES):
     self.BOXES = BOXES
     self.box_size = WIDTH / self.BOXES
     self.pieces = Pieces(WIDTH, BOXES)
예제 #10
0
class Eternity:
    def __init__(self, height=16, width=16, fileName='./state'):
        self.board = Board(height, width)
        self.pieces = Pieces()
        self.moves = []
        self.boardPath = []
        self.step = 0
        self.stepNo = 0
        self.stepMax = 0
        self.boardMax = []
        self.stepPath = []
        if fileName[-6:] == '.state':
            self.fileNameState = fileName
            self.fileNameBoard = fileName.replace('.state', '.board')
            self.fileNameMax = fileName.replace('.state', '.max')
        else:
            self.fileNameState = fileName + '.state'
            self.fileNameBoard = fileName + '.board'
            self.fileNameMax = fileName + '.max'

    @staticmethod
    def readPath(fileName='./path/path.csv'):
        with open(fileName) as csvfile:
            readCSV = csv.reader(csvfile,
                                 delimiter=',',
                                 quoting=csv.QUOTE_NONE)

            boardPath = []
            next(readCSV, None)
            for row in readCSV:
                boardPath.append([int(row[0]), int(row[1])])

        return boardPath

    def __repr__(self):
        r = len(self.board.board)
        c = len(self.board.board[0])
        rpr = ""

        for i in range(r):
            for j in range(c):
                if j == 0:
                    rpr += self.reprCell(i + 1, j + 1)
                else:
                    rpr += "|" + self.reprCell(i + 1, j + 1)

            rpr += "\n"

        return rpr

    def reprCell(self, r, c):
        cell = '{:>3}'.format(str(self.board.getPiece(r, c)))

        fnd = False
        i = 0
        for i in range(len(self.boardPath)):
            if self.boardPath[i] == [r, c]:
                fnd = True
                break

        if fnd:
            if i < len(self.moves):
                cell += ';' + '{:>2}'.format(str(len(self.moves[i])))
            else:
                cell += '; -'

            cell += ';' + '{:>3}'.format(str(i + 1))
        else:
            cell += '; -; - '

        return cell

    def initPiece(self):
        self.boardPath = self.readPath()

        r = len(self.board.board)
        c = len(self.board.board[0])

        if c == 16 and c == r:
            self.pieces[139].turn180()
            self.board.addPiece(9, 8, self.pieces[139].id)
            self.pieces[139].used = 1
            self.boardPath.remove([9, 8])
        else:
            raise Exception("Initial piece only for 16x16")

    def addClues(self, no=3):
        r = len(self.board.board)
        c = len(self.board.board[0])

        if c == 16 and c == r:
            if no >= 1:
                self.pieces[181].turn270()
                self.board.addPiece(14, 3, self.pieces[181].id)
                self.pieces[181].used = 1
                self.boardPath.remove([14, 3])

            if no >= 2:
                self.pieces[255].turn270()
                self.board.addPiece(3, 14, self.pieces[255].id)
                self.pieces[255].used = 1
                self.boardPath.remove([3, 14])

            if no >= 3:
                self.board.addPiece(14, 14, self.pieces[249].id)
                self.pieces[249].used = 1
                self.boardPath.remove([14, 14])
        else:
            raise Exception("Clues only for 16x16")

    def gnrtPattern(self, r, c):
        if c > 1:
            tmp = self.board.getPiece(r, c - 1)
            if tmp != 0:
                left = self.pieces[tmp].right
            else:
                left = -1
        else:
            left = 0

        if c < 16:
            tmp = self.board.getPiece(r, c + 1)
            if tmp != 0:
                right = self.pieces[tmp].left
            else:
                right = -1
        else:
            right = 0

        if r > 1:
            tmp = self.board.getPiece(r - 1, c)
            if tmp != 0:
                up = self.pieces[tmp].down
            else:
                up = -1
        else:
            up = 0

        if r < 16:
            tmp = self.board.getPiece(r + 1, c)
            if tmp != 0:
                down = self.pieces[tmp].up
            else:
                down = -1
        else:
            down = 0

        if c == 1 or c == 16:
            if r == 1 or r == 16:
                pType = 3
            else:
                pType = 2
        else:
            if r == 1 or r == 16:
                pType = 2
            else:
                pType = 1

        return Piece(0, up, right, down, left, pType, 0, 0)

    def cmprPieces(self, i, ptrn):
        if ptrn.type != self.pieces[i].type:
            return False

        if ptrn.up != -1 and ptrn.up != self.pieces[i].up:
            return False

        if ptrn.right != -1 and ptrn.right != self.pieces[i].right:
            return False

        if ptrn.down != -1 and ptrn.down != self.pieces[i].down:
            return False

        if ptrn.left != -1 and ptrn.left != self.pieces[i].left:
            return False

        return True

    def fndMoves(self, r, c):
        ptrn = self.gnrtPattern(r, c)
        mvs = []

        if ptrn.type == 1:
            pcs = self.pieces.getPieces()

            for i in pcs:
                if self.cmprPieces(i, ptrn):
                    mvs.append([i, self.pieces[i].rot])

                self.pieces[i].turn90()
                if self.cmprPieces(i, ptrn):
                    mvs.append([i, self.pieces[i].rot])

                self.pieces[i].turn90()
                if self.cmprPieces(i, ptrn):
                    mvs.append([i, self.pieces[i].rot])

                self.pieces[i].turn90()
                if self.cmprPieces(i, ptrn):
                    mvs.append([i, self.pieces[i].rot])
        elif ptrn.type == 2:
            pcs = self.pieces.getBorders()

            for i in pcs:
                if c == 1:
                    if self.pieces[i].up == 0:
                        self.pieces[i].turn270()
                    elif self.pieces[i].right == 0:
                        self.pieces[i].turn180()
                    elif self.pieces[i].down == 0:
                        self.pieces[i].turn90()
                elif c == 16:
                    if self.pieces[i].up == 0:
                        self.pieces[i].turn90()
                    elif self.pieces[i].left == 0:
                        self.pieces[i].turn180()
                    elif self.pieces[i].down == 0:
                        self.pieces[i].turn270()
                elif r == 1:
                    if self.pieces[i].right == 0:
                        self.pieces[i].turn270()
                    elif self.pieces[i].down == 0:
                        self.pieces[i].turn180()
                    elif self.pieces[i].left == 0:
                        self.pieces[i].turn90()
                else:
                    if self.pieces[i].right == 0:
                        self.pieces[i].turn90()
                    elif self.pieces[i].up == 0:
                        self.pieces[i].turn180()
                    elif self.pieces[i].left == 0:
                        self.pieces[i].turn270()

                if self.cmprPieces(i, ptrn):
                    mvs.append([i, self.pieces[i].rot])
        else:
            pcs = self.pieces.getCorners()

            for i in pcs:
                if c == 1:
                    if r == 1:
                        if self.pieces[i].up == 0 and self.pieces[i].right == 0:
                            self.pieces[i].turn270()
                        elif self.pieces[i].right == 0 and self.pieces[
                                i].down == 0:
                            self.pieces[i].turn180()
                        elif self.pieces[i].down == 0 and self.pieces[
                                i].left == 0:
                            self.pieces[i].turn90()
                    else:
                        if self.pieces[i].up == 0 and self.pieces[i].right == 0:
                            self.pieces[i].turn180()
                        elif self.pieces[i].right == 0 and self.pieces[
                                i].down == 0:
                            self.pieces[i].turn90()
                        elif self.pieces[i].up == 0 and self.pieces[
                                i].left == 0:
                            self.pieces[i].turn270()
                else:
                    if r == 1:
                        if self.pieces[i].up == 0 and self.pieces[i].left == 0:
                            self.pieces[i].turn90()
                        elif self.pieces[i].right == 0 and self.pieces[
                                i].down == 0:
                            self.pieces[i].turn270()
                        elif self.pieces[i].down == 0 and self.pieces[
                                i].left == 0:
                            self.pieces[i].turn180()
                    else:
                        if self.pieces[i].up == 0 and self.pieces[i].left == 0:
                            self.pieces[i].turn180()
                        elif self.pieces[i].right == 0 and self.pieces[
                                i].up == 0:
                            self.pieces[i].turn90()
                        elif self.pieces[i].down == 0 and self.pieces[
                                i].left == 0:
                            self.pieces[i].turn270()

                if self.cmprPieces(i, ptrn):
                    mvs.append([i, self.pieces[i].rot])

        return mvs

    def mkMove(self, moveNo=-1):
        r = self.boardPath[self.step][0]
        c = self.boardPath[self.step][1]
        self.addMoveNo(self.step)
        self.stepNo += 1

        if len(self.moves) < self.step + 1:
            mvs = self.fndMoves(r, c)
            self.moves.append(mvs)

        if len(self.moves[self.step]) > 0:
            if moveNo == -1:
                mv = choice(self.moves[self.step])
            else:
                mv = self.moves[self.step][moveNo]

            self.moves[self.step].remove(mv)

            self.pieces[mv[0]].rotate(mv[1])
            self.pieces[mv[0]].used = 1
            self.board.addPiece(r, c, mv[0])
            self.step += 1
        else:
            del self.moves[-1]
            self.step -= 1

            if self.step == -1:
                if path.exists(self.fileNameState):
                    rename(self.fileNameState,
                           self.fileNameState.replace(".calc", ".fin"))

                raise DeadEnd

            r = self.boardPath[self.step][0]
            c = self.boardPath[self.step][1]

            self.pieces[self.board.getPiece(r, c)].used = 0
            self.board.rmPiece(r, c)

    def bruteForce(self, t=180):
        s = time.time()

        while time.time() - s < t:
            if self.step < len(self.boardPath):
                self.mkMove()
            else:
                self.saveState()
                rename(self.fileNameState,
                       self.fileNameState.replace(".calc", ".fin"))
                finalDir = path.dirname(self.fileNameState)
                rename(finalDir, finalDir.replace("part", "finito"))

                raise FinalSolution

            if self.step > self.stepMax:
                self.stepMax = self.step
                self.boardMax = copy.deepcopy(self.board)
                # self.saveState(1)

    def addMoveNo(self, s):
        fnd = False

        for i in range(len(self.stepPath)):
            if s == self.stepPath[i][0]:
                self.stepPath[i][1] += 1
                fnd = True
                break

        if not fnd:
            self.stepPath.append([s, 1])

    def stepMin(self):
        minNo = self.step

        for i in range(len(self.stepPath)):
            if minNo > self.stepPath[i][0]:
                minNo = self.stepPath[i][0]

        return minNo

    def stepQ(self, q):
        q = q * 0.01 * self.stepNo

        qnt = 0
        minNo = self.stepMin()

        i = 0
        for i in range(len(self.stepPath)):
            qnt += self.stepQnt(minNo + i)

            if qnt >= q:
                break

        return minNo + i

    def stepQnt(self, n):
        i = 0
        for i in range(len(self.stepPath)):
            if n == self.stepPath[i][0]:
                break

        return self.stepPath[i][1]

    def board2file(self):
        fileObj = open(self.fileNameBoard, "w")
        fileObj.write(repr(self))
        fileObj.close()

    def saveState(self, fileType=0):
        if fileType == 0:
            with open(self.fileNameState, "wb") as f:
                pickle.dump(self, f, 4)
        else:
            with open(self.fileNameMax, "wb") as f:
                pickle.dump(self, f, 4)

    def loadState(self):
        with open(self.fileNameState, "rb") as f:
            tmp = pickle.load(f)

            self.board = tmp.board
            self.pieces = tmp.pieces
            self.moves = tmp.moves
            self.boardPath = tmp.boardPath
            self.step = tmp.step
            self.stepNo = 0
            self.stepMax = tmp.stepMax
            self.boardMax = tmp.boardMax
            self.stepPath = []
예제 #11
0
 def random_piece(cls):
     shape = Pieces.random_shape()
     color = Colors.random_color()
     return cls(shape, color)
예제 #12
0
def test_constructor():
    board = Board(600, 8)
    pieces = Pieces(600, 8)

    assert board.BOXES == 8
    assert board.box_size == 75
예제 #13
0
 def __init__(self):
     self.name = ''
     self.board = Board()
     self.pieces = Pieces()
     self.score = []
예제 #14
0
 def __init__(self, WIDTH, BOXES):
     self.pieces = Pieces(WIDTH, BOXES)
     self.board = Board(WIDTH, BOXES)
     self.player = self.BLACK