当前位置: 首页>>代码示例>>Python>>正文


Python board.Board类代码示例

本文整理汇总了Python中term2048.board.Board的典型用法代码示例。如果您正苦于以下问题:Python Board类的具体用法?Python Board怎么用?Python Board使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Board类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Game

class Game(object):
	def __init__(self, next_move, hidemode=False, **kws):
		self.board = Board(**kws)
		self.score = 0
		self.next_move = next_move
		self.hidemode = hidemode

	def play(self):
		while True:
			if not self.hidemode:
				system('cls')
				print self.__str__()
			if self.board.won or not self.board.can_move():
				break
			self.score += self.board.move(self.next_move())

		return self.board.won, self.score

	def __str__(self):
		b = self.board
		rg = range(SIZE)
		s = '\n'.join(' '.join(str(cell) for cell in row) for row in self.board.cells)

		top = '\n' * 4
		bottom = '\n'*4
		scores = ' \tScore: %5d\n' % self.score
		return top + s.replace('\n', scores, 1) + bottom
开发者ID:harrisse,项目名称:term2048,代码行数:27,代码来源:game.py

示例2: test_canMove_no_empty_cell_can_collapse

 def test_canMove_no_empty_cell_can_collapse(self):
     b = Board(size=2)
     b.cells = [
         [2, 2],
         [4, 8]
     ]
     self.assertTrue(b.canMove())
开发者ID:cychoi,项目名称:term2048,代码行数:7,代码来源:test_board.py

示例3: test_move_collapse_and_win

 def test_move_collapse_and_win(self):
     b = Board(size=2, goal=4)
     b.cells = [
         [2, 2],
         [0, 0]
     ]
     b.move(Board.LEFT, add_tile=False)
     self.assertTrue(b.won())
开发者ID:cychoi,项目名称:term2048,代码行数:8,代码来源:test_board.py

示例4: test_getLine

 def test_getLine(self):
     b = Board(size=4)
     l = [42, 17, 12, 3]
     b.cells = [
         [0]*4,
         l,
         [0]*4,
         [0]*4
     ]
     self.assertSequenceEqual(b.getLine(1), l)
开发者ID:cychoi,项目名称:term2048,代码行数:10,代码来源:test_board.py

示例5: test_move_collapse_chain_four_same_tiles

 def test_move_collapse_chain_four_same_tiles(self):
     b = Board()
     b.cells = [
         [2, 2, 2, 2],
         [0]*4,
         [0]*4,
         [0]*4
     ]
     self.assertEqual(b.move(Board.LEFT, add_tile=False), 8)
     self.assertSequenceEqual(b.getLine(0), [4, 4, 0, 0])
开发者ID:JosephRedfern,项目名称:term2048,代码行数:10,代码来源:test_board.py

示例6: test_move_collapse_chain_line_left

 def test_move_collapse_chain_line_left(self):
     b = Board()
     b.cells = [
         [0, 2, 2, 4],
         [0]*4,
         [0]*4,
         [0]*4
     ]
     self.assertEqual(b.move(Board.LEFT, add_tile=False), 4)
     self.assertSequenceEqual(b.getLine(0), [4, 4, 0, 0])
开发者ID:JosephRedfern,项目名称:term2048,代码行数:10,代码来源:test_board.py

示例7: test_move_collapse_chain_line_right2

 def test_move_collapse_chain_line_right2(self):
     b = Board()
     b.cells = [
         [0, 4, 2, 2],
         [0]*4,
         [0]*4,
         [0]*4
     ]
     self.assertEqual(b.move(Board.RIGHT, add_tile=False), 4)
     self.assertSequenceEqual(b.getLine(0), [0, 0, 4, 4])
开发者ID:JosephRedfern,项目名称:term2048,代码行数:10,代码来源:test_board.py

示例8: test_move_collapse_chain_line

 def test_move_collapse_chain_line(self):
     # from https://news.ycombinator.com/item?id=7398249
     b = Board()
     b.cells = [
         [0, 2, 2, 4],
         [0]*4,
         [0]*4,
         [0]*4
     ]
     self.assertEqual(b.move(Board.RIGHT, add_tile=False), 4)
     self.assertSequenceEqual(b.getLine(0), [0, 0, 4, 4])
开发者ID:cychoi,项目名称:term2048,代码行数:11,代码来源:test_board.py

示例9: test_move_collapse

    def test_move_collapse(self):
        b = Board(size=2)
        b.cells = [
            [2, 2],
            [0, 0]
        ]

        b.move(Board.LEFT, add_tile=False)
        self.assertSequenceEqual(b.cells, [
            [4, 0],
            [0, 0]
        ])
开发者ID:cychoi,项目名称:term2048,代码行数:12,代码来源:test_board.py

示例10: test_move_dont_add_tile_if_nothing_move2

 def test_move_dont_add_tile_if_nothing_move2(self):
     b = Board()
     b.cells = [
         [8, 4, 4, 2],
         [0, 2, 2, 0],
         [0]*4,
         [0]*4
     ]
     self.assertEqual(b.move(Board.UP), 0)
     self.assertEqual(len([e for l in b.cells for e in l if e != 0]), 6)
     self.assertEqual(b.getLine(0), [8, 4, 4, 2])
     self.assertEqual(b.getLine(1), [0, 2, 2, 0])
开发者ID:cychoi,项目名称:term2048,代码行数:12,代码来源:test_board.py

示例11: __init__

    def __init__(self, scores_file=SCORES_FILE, colors=None,
                 store_file=STORE_FILE, clear_screen=True,
                 mode=None, azmode=False, **kws):
        """
        Create a new game.
            scores_file: file to use for the best score (default
                         is ~/.term2048.scores)
            colors: dictionnary with colors to use for each tile
            store_file: file that stores game session's snapshot
            mode: color mode. This adjust a few colors and can be 'dark' or
                  'light'. See the adjustColors functions for more info.
            other options are passed to the underlying Board object.
        """
        self.board = Board(**kws)
        self.score = 0
        self.scores_file = scores_file
        self.store_file = store_file
        self.clear_screen = clear_screen

        self.best_score = 0

        self.__colors = colors or self.COLORS
        self.__azmode = azmode

        self.loadBestScore()
        self.adjustColors(mode)
开发者ID:bfontaine,项目名称:term2048,代码行数:26,代码来源:game.py

示例12: reset

 def reset(self):
     try: 
         print self.score, self.best_score, self.count, self.get_frame().max()
         del self.board
     except: pass
     #self.board = Board(**kws)
     self.board = Board(goal=512)
     self.score = 0
     self.count = 0
     self.moved = False
     self.pts = 0
开发者ID:choupi,项目名称:NDHUDLWorkshop,代码行数:11,代码来源:my2048.py

示例13: __init__

 def __init__(self, scores_file=SCORES_FILE, colors=COLORS,
         mode=None, **kws):
     """
     Create a new game.
         scores_file: file to use for the best score (default
                      is ~/.term2048.scores)
         colors: dictionnary with colors to use for each tile
         mode: color mode. This adjust a few colors and can be 'dark' or
               'light'. See the adjustColors functions for more info.
         other options are passed to the underlying Board object.
     """
     self.board = Board(**kws)
     self.score = 0
     self.scores_file = scores_file
     self.__colors = colors
     self.loadBestScore()
     self.adjustColors(mode)
开发者ID:emidln,项目名称:term2048,代码行数:17,代码来源:game.py

示例14: __init__

 def __init__(self, **kws):
     """
     Create a new game.
     """
     self.board = Board(**kws)
     self.score = 0
     self.__colors = {
         2:    Fore.GREEN,
         4:    Fore.BLUE + Style.BRIGHT,
         8:    Fore.CYAN,
         16:   Fore.RED,
         32:   Fore.MAGENTA,
         64:   Fore.CYAN,
         128:  Fore.BLUE + Style.BRIGHT,
         256:  Fore.MAGENTA,
         512:  Fore.GREEN,
         1024: Fore.RED,
         2048: Fore.YELLOW,
         # just in case people set an higher goal they still have colors
         4096: Fore.RED,
         8192: Fore.CYAN,
     }
开发者ID:jkohvakk,项目名称:python-tdd-training-exercises,代码行数:22,代码来源:game.py

示例15: mGame

class mGame(Game):
    def __init__(self, vis=False):
        if vis: Game.__init__(self, scores_file=None, store_file=None)
        self.best_score=0
        self.vis=vis
        self.reset()

    def reset(self):
        try: 
            print self.score, self.best_score, self.count, self.get_frame().max()
            del self.board
        except: pass
        #self.board = Board(**kws)
        self.board = Board(goal=512)
        self.score = 0
        self.count = 0
        self.moved = False
        self.pts = 0
        #self.clear_screen = clear_screen
        #self.__colors = colors
        #self.__azmode = azmode

    def play(self, action):
        self.moved = False
        pts=self.board.move(action+1)
        self.pts=pts
        self.incScore(pts)
        if pts>0: 
            self.count+=1
            self.moved=True
        if self.vis:
            margins = {'left': 4, 'top': 4, 'bottom': 4}
            self.clearScreen()
            print(self.__str__(margins=margins))
            time.sleep(0.1)
        
    def get_state(self):
        return self.board.cells

    def get_state_fake(self, action):
        b = Board()
        b.cells=self.board.cells
        b.move(action+1)
        return b.cells

    def get_score(self):
        if self.board.won(): return 1
        elif self.pts>0: return 1- 1.0/self.pts
        return 0
        #return self.score/2048.0
        #elif not self.board.canMove(): s=-1
        #elif not self.moved: s=-5
        #return self.count/80+math.log(float(self.get_frame().max()))/5.0+s
        #return self.count/100.0
        #return self.count/100.0+math.log(float(self.get_frame().max()))

    def is_over(self):
        return self.board.won() or not self.board.canMove()
    def is_won(self):
        return self.board.won()

    @property
    def name(self):
        return "2048"
    @property
    def nb_actions(self):
        return 4

    def get_frame(self):
        ll=numpy.vectorize(lambda x:math.log(x+1))
        #s=[self.get_state_fake(1),self.get_state_fake(2),self.get_state_fake(3),self.get_state_fake(4),self.get_state()]
        s=self.get_state()
        return ll(numpy.array(s).astype('float32'))

    def draw(self):
        return self.get_state()
开发者ID:choupi,项目名称:NDHUDLWorkshop,代码行数:76,代码来源:my2048.py


注:本文中的term2048.board.Board类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。