• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python board.Board类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: get_state_fake

 def get_state_fake(self, action):
     b = Board()
     b.cells=self.board.cells
     b.move(action+1)
     return b.cells
开发者ID:choupi,项目名称:NDHUDLWorkshop,代码行数:5,代码来源:my2048.py


示例17: test_move_collapse_triplet2

 def test_move_collapse_triplet2(self):
     b = Board(size=3)
     b.setLine(0, [2, 2, 2])
     b.move(Board.RIGHT, add_tile=False)
     self.assertSequenceEqual(b.getLine(0), [0, 2, 4])
开发者ID:cychoi,项目名称:term2048,代码行数:5,代码来源:test_board.py


示例18: test_addTile

 def test_addTile(self):
     b = Board(size=1)
     b.cells = [[0]]
     b.addTile(value=42)
     self.assertEqual(b.cells[0][0], 42)
开发者ID:cychoi,项目名称:term2048,代码行数:5,代码来源:test_board.py


示例19: Game

class Game(object):
    """
    A 2048 game
    """

    __dirs = {
        keypress.UP:      Board.UP,
        keypress.DOWN:    Board.DOWN,
        keypress.LEFT:    Board.LEFT,
        keypress.RIGHT:   Board.RIGHT,
    }

    __clear = 'cls' if os.name == 'nt' else 'clear'

    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,
        }

    def incScore(self, pts):
        """
        update the current score by adding it the specified number of points
        """
        self.score += pts

    def end(self):
        """
        return True if the game is finished
        """
        return not (self.board.won() or self.board.canMove())

    def readMove(self):
        """
        read and return a move to pass to a board
        """
        k = keypress.getKey()
        return Game.__dirs.get(k)

    def loop(self):
        """
        main game loop. returns the final score.
        """
        try:
            while True:
                os.system(Game.__clear)
                print(self.__str__(margins={'left': 4, 'top': 4, 'bottom': 4}))
                if self.board.won() or not self.board.canMove():
                    break
                m = self.readMove()
                self.incScore(self.board.move(m))

        except KeyboardInterrupt:
            return

        print('You won!' if self.board.won() else 'Game Over')
        return self.score

    def getCellStr(self, x, y):  # TODO: refactor regarding issue #11
        """
        return a string representation of the cell located at x,y.
        """
        c = self.board.getCell(x, y)

        if c == 0:
            return '  .'
        elif c == 1024:
            s = ' 1k'
        elif c == 2048:
            s = ' 2k'
        else:
            s = '%3d' % c

        return self.__colors.get(c, Fore.RESET) + s + Style.RESET_ALL

    def boardToString(self, margins={}):
        """
        return a string representation of the current board.
        """
        b = self.board
        rg = range(b.size())
        left = ' '*margins.get('left', 0)
        s = '\n'.join(
#.........这里部分代码省略.........
开发者ID:jkohvakk,项目名称:python-tdd-training-exercises,代码行数:101,代码来源:game.py


示例20: test_move_add_tile_if_collapse

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



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python game.Game类代码示例发布时间:2022-05-27
下一篇:
Python term.Term类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap