예제 #1
0
파일: trace.py 프로젝트: endvroy/hybrid_npi
    def __init__(self, in1, in2, debug=False):
        """
        Instantiates a trace object, and builds the exact execution pipeline for adding the given
        parameters.

        """
        self.in1, self.in2, self.debug = in1, in2, debug
        self.trace, self.scratch = [], ScratchPad(in1, in2, CONFIG["ENVIRONMENT_ROW"], CONFIG["ENVIRONMENT_COL"])

        # Build Execution Trace
        self.build()

        # Check answer
        true_ans = self.in1 + self.in2
        trace_ans = int("".join(map(str, map(int, self.scratch[3]))))

        assert(true_ans == trace_ans)
예제 #2
0
        ax2.set_facecolor((0.0, 0.0, 0.0))
        pix_range = np.max(arr) - np.min(arr)
        ax2.imshow((arr - np.min(arr)) / (pix_range + 1e-6), vmin=0, vmax=1)
    else:
        fig = plt.figure()
        fig.canvas.set_window_title(title)
        ax = fig.add_subplot("111")
        ax.set_facecolor((0.0, 0.0, 0.0))
        ax.imshow(array)


pad_size = (1023, 1001)

if __name__ == "__main__":
    set_omp_max_threads(4)
    p = ScratchPad()
    p.load_brush(get_brushes()[0])
    p.reset_pad(*pad_size, 1)
    assert p.get_brush_num() == 1
    assert p.get_pad_size() == pad_size
    assert p.get_layer_num() == 1
    p.set_opacity(0, 1.0)
    p.add_layer()
    p.pop_layer(1)
    assert p.get_layer_num() == 1

    # draw rectangle, only works when dtime is set to max
    points = [
        Point(0.3, 0.3),
        Point(0.3, 0.6),
        Point(0.6, 0.6),
예제 #3
0
from scratchpad import (Setting, Point, ScratchPad, get_brushes,
                        set_omp_max_threads)
from time import time
import numpy as np

# 11ms per frame, per layer at 1024 * 1024, using 4 threads, float
# 4.5ms ~ 7.5ms per frame, per layer at 1024 * 1024, using 4 threads, uint8
pad_size = (1024, 1024)

if __name__ == "__main__":
    #set_omp_max_threads(4)
    p = ScratchPad()
    p.load_brush(get_brushes()[0])
    p.reset_pad(*pad_size, 1)

    # draw rectangle, only works when dtime is set to max
    points = [
        Point(0.3, 0.3),
        Point(0.3, 0.6),
        Point(0.6, 0.6),
        Point(0.6, 0.3),
        Point(0.3, 0.3)
    ]
    p.draw(0, 0, Setting(1.0, 0.1, 0.5, 0.5, 0.5, 0.5), points)

    begin = time()
    for i in range(2000):
        p.render(np.float32)
    end = time()
    print((end - begin) / 2000)
예제 #4
0
파일: ClueBoard.py 프로젝트: rmills2/Coral
 def create_scratchPad(self):
     ############## Scratch Pad Integration ################
     self.scratchPad = ScratchPad(self.display, [], [])
     self.entries = self.scratchPad.runScratchPad()
예제 #5
0
파일: ClueBoard.py 프로젝트: rmills2/Coral
class CluelessGamePlayer(ConnectionListener):
    """ This class begins the gameplay by drawing the board """

    def __init__(self, host="198.61.134.123", port=8080):

        self.display = None
        self.roomFont = None

        self.scratchPad = None
        self.entries = None
        self.playerArea = None
        self.cardArea = None
        self.gameBoard = None

        self.turn = -1
        self.youAre = 0
        self.myCards = []
        self.numplayers = 0

        self.game_process = None
        self.running = False
        self.need_initializing = False
        self.gameid = None
        self.Connect((host, int(port)))
        self.card_deck = CardDeck()
        self.suggestionDisproved = []

        self.need_to_notify = False
        self.newPosition = 0

    def Network(self, data):
        # print 'network data:', data
        pass

    def set_turn(self, turn):
        self.turn = turn
        # self.turn = 0

    def set_player_id(self, id):
        self.youAre = id
        # self.youAre = 0

    def set_player_cards(self, cards):
        self.myCards = cards

    def Network_hello(self, data):
        print "SERVER SAYS HELLO!"

    def Network_close(self, data):
        sys.exit()

    def set_total_num_players(self, numplayers):
        self.numplayers = numplayers

    def Network_startgame(self, data):
        player_character = data["character"]
        player_cards = data["cards"]

        player_id = data["youAre"]
        turn = data["turn"]
        numplayers = data["numplayers"]

        self.set_turn(turn)
        self.set_player_id(player_id)
        self.set_total_num_players(numplayers)
        self.set_player_cards(player_cards)

        success, cards = create_message("start")

        print "player_cards: ", player_cards
        print "start success?: ", success
        self.running = True
        self.initialize_game_board()
        if success:
            connection.Send({"action": "success", "ID": self.gameid})
        else:
            connection.Send({"action": "fail", "ID": self.gameid})

    def Network_updateTurn(self, data):
        """ This function is to update the player's server turn index """
        self.turn = data["turn"]

    def Network_endgame(self, data):
        create_message("end")
        sys.exit()

    def Network_winner(self, data):
        create_message("win")
        sys.exit()

    def Network_loser(self, data):
        create_message("lose")
        sys.exit()

    def updateTurn(self, turn):
        self.turn = turn

    def create_board(self):
        ############## Game and Font Initialization ###############
        pygame.init()
        self.display = pygame.display.set_mode((900, 800), HWSURFACE | DOUBLEBUF)
        pygame.display.set_caption("Board")
        myfont = pygame.font.SysFont("Times New Roman", 50)
        self.roomFont = pygame.font.SysFont("Times New Roman", 15)
        self.roomFont.set_bold(True)
        self.display.fill(TAN)

    def create_scratchPad(self):
        ############## Scratch Pad Integration ################
        self.scratchPad = ScratchPad(self.display, [], [])
        self.entries = self.scratchPad.runScratchPad()

    def create_playerArea(self):
        ############## Player Area Integration ################
        self.playerArea = PlayerArea([], self.display, 600, 320)

    def create_cardArea(self):
        ############## Card Area Initialization ##############
        self.cardArea = cardArea(self.display)
        self.cardArea.placeCards(self.myCards)

    def create_gameBoard(self):
        ############## Create the GameBoard #####################
        self.gameBoard = GameBoard(self.myCards, [], 1)
        self.gameBoard.drawGameBoard(self.display, self.roomFont)

    def draw_border(self):
        ############### Draw Border ###################
        pygame.draw.rect(self.display, BLACK, (0, 0, 600, 500), 4)

    def execute(self):

        self.game_process = Process(target=self.run)
        self.game_process.start()

    def initialize_game_board(self):

        self.create_board()
        self.create_scratchPad()
        self.create_playerArea()
        self.create_cardArea()
        self.create_gameBoard()
        self.draw_border()

    def Network_updateTurn(self, data):
        turn = data["turn"]
        self.turn = turn

    def Network_movePlayer(self, data):
        spotArrayIndex = data["spotArrayIndex"]
        self.update_player_position(spotArrayIndex)

    def update_player_position(self, spotArrayIndex):
        print "Updating Player position!"
        currentCharacter = characterArray[self.turn % len(characterArray)]
        currentCharacter.moveCharacter(self.display, self.roomFont, spotArray[spotArrayIndex])
        ##### Update the player area
        previousPlayer = self.playerArea.players[self.turn % len(self.playerArea.players)]
        if self.turn % len(self.playerArea.players) == 2:
            previousPlayer.drawPlayerArrow(self.display, TAN, False)
        elif self.turn % len(self.playerArea.players) == 5:
            previousPlayer.drawPlayerArrow(self.display, TAN, True)

        self.turn = self.turn + 1 if self.turn < self.numplayers - 1 else 0

        currentPlayer = self.playerArea.players[self.turn % len(self.playerArea.players)]

        if self.turn % len(self.playerArea.players) > 2:
            previousPlayer.drawPlayerArrow(self.display, TAN, True)
            currentPlayer.drawPlayerArrow(self.display, currentPlayer.getColor(), True)
        else:
            previousPlayer.drawPlayerArrow(self.display, TAN, False)
            currentPlayer.drawPlayerArrow(self.display, currentPlayer.getColor(), False)

        currentCharacter.draw(self.display, self.roomFont)
        pygame.display.update()

    def notify_move(self, spotArrayIndex):
        connection.Send({"action": "playerMove", "spotArrayIndex": spotArrayIndex, "playerId": self.youAre})
        self.need_to_notify = False

    def notify_suggestion(self, cards, spotArrayIndex):
        print "Notifying new suggestion: ", cards
        print "WITH SPOT ARRAY INDEX: ", spotArrayIndex
        connection.Send(
            {"action": "newSuggestion", "cards": cards, "playerId": self.youAre, "spotArrayIndex": spotArrayIndex}
        )

    def notify_finalaccusation(self, cards):
        connection.Send({"action": "finalAccusation", "cards": cards, "playerId": self.youAre})

    def Network_newSuggestion(self, data):
        cards = data["cards"]
        card_names = cards.values()
        card_name_text = ",".join(card_names)
        print "card_name_text: ", card_name_text
        create_message("accuse", card_name_text)

    def Network_forceDisproval(self, data):
        print "FORCING DISPROVAL WITH SUGGESTION CARDS: ", data["cards"]
        cards = data["cards"]
        card_names = cards.values()
        card_name_text = ",".join(card_names)
        print "card_name_text: ", card_name_text
        success, disproved_cards = create_message("mustDisprove", card_name_text, self.myCards)
        print "success: ", success
        print "disproved cards: ", disproved_cards
        if disproved_cards:
            connection.Send({"action": "disproved", "cards": disproved_cards})
        else:
            connection.Send({"action": "disproved", "cards": False})

    def Network_isDisproved(self, data):
        card_names = data["cards"]
        card_name_text = ",".join(card_names)
        print "card_name_text: ", card_name_text
        create_message("disprove", card_name_text)
        self.notify_move(self.newPosition)

    def Network_notDisproved(self, data):
        create_message("notdisproved")
        self.notify_move(self.newPosition)

    def run(self):

        if not self.running:
            return

        if self.need_to_notify:
            self.notify_move(self.newPosition)

        self.playerArea.players[self.turn].drawPlayerArrow(
            self.display, self.playerArea.players[self.turn].getColor(), False
        )
        self.playerArea.players[self.youAre].drawBox(self.display)

        for event in pygame.event.get():
            # if self.turn == self.youAre:
            # create_message("move")
            if event.type == pygame.MOUSEBUTTONUP and self.turn == self.youAre:

                # MOVE A PLAYER
                for i in range(len(spotArray)):
                    rect = pygame.Rect(spotArray[i].x, spotArray[i].y, 120, 100)
                    if rect.collidepoint(event.pos):
                        currentCharacter = characterArray[self.turn % len(characterArray)]
                        characterRect = pygame.Rect(
                            currentCharacter.currentArea.x, currentCharacter.currentArea.y, 120, 100
                        )
                        if (
                            isValidSecretPassage(spotArray[i], currentCharacter.currentArea) == False
                            and spotArray[i].isAdjacent(spotArray, currentCharacter.currentArea) == False
                            or spotArray[i].maxOccupancy - len(spotArray[i].currentOccupants) <= 0
                        ):
                            create_message("invalidMove")
                            break
                        else:
                            suggestionMade = False
                            # self.update_player_position(i)
                            if isinstance(spotArray[i], Room):
                                currentCharacter.draw(self.display, self.roomFont)
                                pygame.display.update()
                                success, cards = create_message("newSuggestion", spotArray[i].name)
                                print "SUCCESS: ", success
                                print "CARDS: ", cards
                                if success and cards:
                                    suggestionMade = True
                                    self.notify_suggestion(cards, i)
                                    self.need_to_notify = False
                                else:
                                    self.need_to_notify = True
                            else:
                                self.need_to_notify = True
                            self.newPosition = i

                ##### Checks for making the final accusation #####
                rect = pygame.Rect(770, 460, 130, 40)
                if rect.collidepoint(event.pos):
                    print "MAKING FINAL ACCUSATION"
                    success, cards = create_message("newSuggestion", "None")
                    if success and cards:
                        self.notify_finalaccusation(cards)

            # UPDATE SCRATCH PAD
            if event.type == pygame.MOUSEBUTTONUP:
                yVal = 0
                ##### Checks for scratch pad #####
                for i in range(len(self.entries)):
                    r = self.entries[i].getRect()
                    if r.collidepoint(event.pos):
                        if self.scratchPad.scratchColorsArray[i] == True:
                            pygame.draw.line(self.display, RED, (r.x, r.y + 10), (r.x + 120, r.y + 10), 3)
                            self.scratchPad.scratchColorsArray[i] = False
                        else:
                            self.scratchPad.redrawEntryArea(self.display, r)
                            self.scratchPad.blitText(self.entries[i].getName(), r, self.display)
                            self.scratchPad.scratchColorsArray[i] = True
                    yVal += 20

            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        pygame.display.update()
예제 #6
0
TAN= (247, 231, 160)


############## Game and Font Initialization ###############
pygame.init()
display = pygame.display.set_mode((900, 600), 0, 32)
pygame.display.set_caption('Board')
myfont = pygame.font.SysFont("Times New Roman", 50)
display.fill(WHITE)


############## Scratch Pad Integration ################

scratchColorsArray = []
allArray = []
scratchPad = ScratchPad(display, scratchColorsArray, allArray)
allArray = scratchPad.runScratchPad()

############## Internal Class Declarations ##############
class Character:
    def __init__(self, name, color, currentSpace):
        self.name = name
        self.color = color
        self.currentSpace = currentSpace
    def name(self):
        return self.name
    def color(self):
        return self.color
    def draw(self):
        pygame.draw.circle(display, self.color, (self.currentSpace.x + 60, self.currentSpace.y + 60), 15)
    def currentSpace(self):