def test_set_twice(my_board,): test_b = Block("test", [[1] for i in range(2)]) test_b_2 = Block("test2", [[1]]) after_set_board = [[1 if i==3 and j<3 else 0 for i in range(board_size)] for j in range(board_size)] my_board.set_block(test_b,3,0) my_board.set_block(test_b_2,3,2) my_board.set_block(test_b_2,3,2) assert after_set_board == my_board.now
def test_board_update_delete_check(my_board): b1 = Block("test1", Blocks.pattern["bar2_v"]) b2 = Block("test2", Blocks.pattern["corner5_ru"]) after = set_tst_board_block(my_board.now,2,1,b1.form) after = set_tst_board_block(after,2,0,b2.form) my_board.set_block(b1,2,1) my_board.set_block(b2,2,0) print("\n" + my_board.out_state()) print("\n", after) assert after == my_board.now
def test_set_cant_set(my_board,x,y,b): test_b = Block("test", b) taikakusen = lambda n:[[1 if i==j else 0 for i in range(n)] for j in range(n)] now_board = taikakusen(board_size) setting_block = Block("setting",now_board) my_board.set_block(setting_block,0,0) #じゃまブロックセット my_board.set_block(test_b,x,y) assert now_board == my_board.now
def test_search_blocks_of_a_permutation_setting_cant(my_brain,my_board,cantset_points): my_board.now = cantset_points tst_blk=[] tst_blk.append(Block("test1",[[1,1]])) tst_blk.append(Block("test2",[[1,1]])) tst_blk.append(Block("test3",[[1],[1]])) results,eval = my_brain.search_blocks_of_a_permutation_setting(my_board,tst_blk,[0,1,2]) for i in range(len(results)): assert not results[i].playable assert results[i].block.name == tst_blk[i].name
def test_search_blocks_of_a_permutation_setting(my_brain,my_board,tst_block,cantset_points): for i,j in cantset_points: my_board.now[i][j] = 1 tst_block.append(Block(("test2",[[1,1]]))) tst_block.append(Block(("test3",[[1],[1]]))) results,eval = my_brain.search_blocks_of_a_permutation_setting(my_board,tst_block,[0,1,2]) for i in range(len(results)): assert results[i].playable assert 0 <= results[i].x < board_size assert 0 <= results[i].y < board_size assert results[i].block.name == tst_block[i].name
def test_board_can_not_set_here(my_board): blocks = Blocks() my_board.now = [[1 for i in range(board_size)] for j in range(board_size)] for n,f in blocks.pattern.items(): b = Block(n,f) assert not my_board.can_set(b,0,0)
def next_block(self, last_block, list_transaction): this_index = last_block.index + 1 this_timestamp = time.time() this_data = list_transaction this_hash = last_block.hash return Block(this_index, this_timestamp, this_data, this_hash)
def ParseLevel(self, file): result=[] img = Image.open("src/res/img/levels/"+file) self.width, self.height = img.size pixels=img.load() result = list() for x in range(self.width): for y in range(self.height): block_type = self.formatPixel(pixels[x,y]) if block_type is not None: for block in range(len(self.blocks_arr)): if block_type is -1: # The player has to start less than 10 blocks in assert (x < 10), ("The player must start less than 10 blocks into the level! ({0}, {1})".format(x, y)) self.player_pos = [x*30,(self.height*30)-30-(y*30)] # X: current x * block_height # Y: Max height (height*30) - 30 - (current y * block_height) since it starts bottom down, and -30 since everything is off by one block up break elif block_type.number is self.blocks_arr[block].number: res=Block(block_type.file, block_type.color) res.pos=(x*res.size[0], self.height * res.size[1]-res.size[1]-(y*res.size[1])) result.append(res) break return result
def addBlock(self, type, argN=0, methodType=None): if type == "class": blocks.append( Block(type + str(self.classCount), type, "Add Caption", self.classCount, 0)) self.classCount += 1 d = class_Block(self) d.set_name(blocks[len(blocks) - 1]) elif type == "method": blocks.append( Block(methodType + str(self.methodCount), type, "Add Caption", self.methodCount, 0)) self.methodCount += 1 d = method_Block(self) d.set_name(blocks[len(blocks) - 1]) elif type == "variable": blocks.append( Block(type + str(self.variableCount), type, "Add Caption", self.variableCount, 0)) self.variableCount += 1 d = variable_Block(self) d.set_name(blocks[len(blocks) - 1]) elif type == "output": blocks.append( Block(type + str(self.outputCount), type, "Add Caption", self.outputCount, 0)) self.outputCount += 1 d = output_Block(self) d.set_name(blocks[len(blocks) - 1]) elif type == "probe": blocks.append(Probe()) self.probeCount += 1 d = probe_Block(self) else: print("Error with request") pass s = Scatterer() self.add_widget(s) s.set_name(blocks[len(blocks) - 1]) if type == "method": d.tailorMethod(argN, methodType) elif type == "probe": d.spawnProbe() scatterStack.append(s) widgetStack.append(d) s.add_widget(d)
def test_start_stop(my_player): board_size = 5 full_set_board = [[1 for i in range(board_size)] for j in range(board_size)] setting_block = Block("setting", full_set_board) my_player.set_block(setting_block, 0, 0) result_num = my_player.start() assert result_num == 0
def LoadBlocks(self): result = [] with open("src/res/blocks.json") as file: data = json.load(file) for dat in data: result.append(Block(dat["file"], dat["color"])) return result
def test_search_settable_point3(my_brain,my_board,expect_point,cantset_points,b): for i,j in cantset_points: my_board.now[i][j] = 1 pts = my_brain.search_settable_point(my_board, Block("test",b)) assert len(pts) > 0 for p in pts: assert expect_point in pts
def test_block_set(): blocks = Blocks() name = "square_3" b = Block(name, blocks.pattern[name]) assert b.name == name assert b.form == [[1,1,1], [1,1,1], [1,1,1]]
def __init__(self): self.genesis_block = Block(0, time.time(), [{'from' : 'GOD', 'to' : 'Jesus', 'amount' : 1000000}], '0') self.chain = [self.genesis_block] self.wallets = self.gen_wallets() self.agents = self.wallet_to_agents(self.wallets) self.wallets_keys = [wallet.public_key for wallet in self.wallets]
def test_search_all_permutation_blocks(my_brain,my_board,tst_block): tst_block.append(Block(("test2",[[1 for i in range(board_size)]]))) tst_block.append(Block(("test3",[[1] for i in range(board_size)]))) result,eval = my_brain.search_all_permutation_blocks(my_board,tst_block) assert_res = [] block_num = len(tst_block) for p in itertools.permutations(range(block_num),block_num): res,eval = my_brain.search_blocks_of_a_permutation_setting(my_board,tst_block,list(p)) assert_res.append([res,eval]) zipped = list(zip(*assert_res)) evals = list(zipped[1]) assert_res_max = max(assert_res, key=lambda r : r[1] == max(evals))[0] assert eval == max(evals) for i in range(len(result)): assert result[i].playable assert result[i].block.name in [ast.block.name for ast in assert_res_max]
def test_set_block_init(my_board,x,y,b): test_b = Block("test", b) after_set_board = \ set_tst_board_block( [[0 for i in range(board_size)] for j in range(board_size)] , x,y,b) my_board.set_block(test_b,x,y) assert after_set_board == my_board.now
def addBlock(self, type): print(type) s = Scatterer() if type == "class": d = class_Block() self.blocks.append( Block(type + str(self.classCount), type, "Add Caption", 0, 0, 0, 0, 0)) self.classCount += 1 elif type == "method": d = method_Block() self.blocks.append( Block(type + str(self.methodCount), type, "Add Caption", 0, 0, 0, 0, 0)) self.methodCount += 1 elif type == "variable": d = variable_Block() self.blocks.append( Block(type + str(self.variableCount), type, "Add Caption", 0, 0, 0, 0, 0)) self.variableCount += 1 elif type == "output": d = output_Block() self.blocks.append( Block(type + str(self.outputCount), type, "Add Caption", 0, 0, 0, 0, 0)) self.outputCount += 1 else: print("Error with request") print("Status:") for i in self.blocks: print(i.Name) print("___") self.add_widget(s) s.add_widget(d) print("button is pressed")
def save(gameobj): gameobj.blocklist = [] for block in gameobj.blocksetlist: tempBlock = Block(block.x, block.y, block.size, block.blockType) if tempBlock.blockType == "tower": tempBlock.rofMax = block.rofMax tempBlock.range = block.range gameobj.blocklist.append(tempBlock) tempEntList = [(EmptyNPC(ent.startX, ent.startY, ent.size, ent.blockType, ent.range, ent.rofMax)) for ent in gameobj.entlist] name = "levels/%s.lvl" % gameobj.savename print(name) with open(name, "wb") as file: pickle.dump((gameobj.blocklist, tempEntList, mapDict["XMAX_GRID"], mapDict["YMAX_GRID"], gameobj.player.pos.x, gameobj.player.pos.y, globDict["scoregoal"]), file)
def main(): pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Colliding Blocks") BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) timesteps = 10000 digits = 5 block1 = Block() block1.x, block1.y = 300, 575 block1.w = 25 block1.v_x, block1.m = 0, 1 block1.xmin, block1.xmax = 0, 10000 block2 = Block() block2.x, block2.y = 500, 450 block2.w = 150 block2.v_x, block2.m = -1 / timesteps, pow(100, digits - 1) block2.xmin, block2.xmax = 25, 10000 myfont = pygame.font.SysFont('monospace', 40) count = 0 while True: pygame.time.delay(1) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() elif event.type == pygame.KEYDOWN: if event.key == ord('q'): pygame.quit() screen.fill(BLACK) text = myfont.render("Count:" + str(count), 1, (0, 0, 255)) screen.blit(text, (300, 200)) for i in range(timesteps): if block1.collide(block2): v1 = block1.bounce(block2) v2 = block2.bounce(block1) block1.v_x = v1 block2.v_x = v2 count += 1 if block1.wall_collide(): count += 1 block1.reverse() block1.move() block2.move() block1.show(screen, (255, 0, 0)) block2.show(screen, (0, 255, 0)) pygame.display.update()
def test_get_block_h(form,exp_w,exp_h): b = Block("test",form) assert b.w == exp_w assert b.h == exp_h
tst_blk=[] tst_blk.append(Block("test1",[[1,1]])) tst_blk.append(Block("test2",[[1,1]])) tst_blk.append(Block("test3",[[1],[1]])) results,eval = my_brain.search_blocks_of_a_permutation_setting(my_board,tst_blk,[0,1,2]) for i in range(len(results)): assert not results[i].playable assert results[i].block.name == tst_blk[i].name @pytest.mark.parametrize( "tst_b", [ ([ Block("t1",[[1]]), Block("t2",[[1,1,1],[1,1,1],[1,1,1]]), Block("t3",[[1,1],[1,1]]) ]) ] ) def test_search_blocks_of_a_permutation_setting_secondblock_cantplay(my_brain,my_board,tst_b): my_board.now = [[1 if x!=y else 0 for x in range(board_size)] for y in range(board_size)] result,eval = my_brain.search_blocks_of_a_permutation_setting(my_board,tst_b,[0,1,2]) r_1 = result.popleft() assert r_1.playable r_2 = result.popleft() assert not r_2.playable def test_search_all_permutation_blocks(my_brain,my_board,tst_block):
def test_board_can_not_set_here_bacic(my_board,cantset_points,block): for i,j in cantset_points: my_board.now[i][j] = 1 b = Block("test",block) assert not my_board.can_set(b,0,0)
def tst_block(): bl = [Block(("test",[[1]]))] yield bl
def test_board_can_set_here_in_empty2(my_board,x,y,b,canset): for i,j in canset: my_board.now = set_tst_board_block(my_board.now, i, j, [[1]]) block = Block("test",b) assert my_board.can_set(block,x,y)
def test_board_can_set_here_in_empty(my_board): blocks = Blocks() for n,f in blocks.pattern.items(): b = Block(n,f) assert my_board.can_set(b,0,0)
def test_board_can_set_here_exception_position_2(my_board,x,y,b): test_b = Block("test", b) assert not my_board.can_set(test_b,x,y)
def test_board_can_set_here_exception_position(my_board,x,y): b = Block("test", [[1]]) assert not my_board.can_set(b,x,y)
def my_player(): results = [] b1 = Block("test", [[1]]) result_1 = Result_Calc(x=0, y=0, block=b1, playable=False) results.append(result_1) yield (make_player([b1], results, 5))
def my_player2(): results = [] b2 = Block("test", [[1, 1, 1, 1, 1]]) result_1 = Result_Calc(x=0, y=0, block=b2, playable=True) results.append(result_1) yield (make_player([b2], results, 5))