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


Python Board.is_full方法代码示例

本文整理汇总了Python中board.Board.is_full方法的典型用法代码示例。如果您正苦于以下问题:Python Board.is_full方法的具体用法?Python Board.is_full怎么用?Python Board.is_full使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在board.Board的用法示例。


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

示例1: main

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import is_full [as 别名]
def main(prepop_board=None):
    if prepop_board:
        board = Board(prepop_board)
    else:
        board = Board()
    p1 = Human('O')
    p2 = Human('X')
    players = [p1, p2]
    current_player = 0
    while not (board.is_full() or board.get_winner()):
        #os.system('clear')
        print board
        print
        player = players[current_player]
        while True:
            move = player.get_next_move(board)
            try:
                board.move(move, player.symbol)
                break
            except:
                pass
        current_player = (current_player + 1) % 2

    # display final state
    os.system('clear')
    print board
    print
    winner = board.get_winner()
    if winner:
        print winner + ' wins!'
    else:
        print 'A draw!'
开发者ID:tomviner,项目名称:connect4,代码行数:34,代码来源:main.py

示例2: test_is_full

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import is_full [as 别名]
 def test_is_full(self):
     """ tests is_full """
     b      = Board(Game(),3)
     for move in b._board:
         b.put_mark("X",move)
     
     actual   = b.is_full()
     expected = True
     self.assertEqual(expected, actual)
开发者ID:Lh4cKg,项目名称:Tic-Tac-Toe-1,代码行数:11,代码来源:test_board.py

示例3: Game

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import is_full [as 别名]
class Game():
    def __init__(self):
        self.ai = Ai()
        self.board = Board(9)
        self.player = Player()
        self.on_turn = self.player
   
    def start(self):
        in_progres = True
        while in_progres:
            if self.on_turn == self.player:
                player_choice = self.player.ask_for_move(self.board)
                try:
                    self.board.make_move(player_choice, self.player.symbol)
                except ValueError:
                    return self.player.ask_for_move(self.board)
                self.on_turn = self.ai
                print(self.board.draw())
            else:
                print('Ai move:')
                ai_move = self.ai.get_ai_move(self.board)
                self.board.make_move(ai_move, self.ai.symbol)
                self.on_turn = self.player
                
                print(self.board.draw())

            if self.board.check_winning(self.player.symbol):
                print('Congrats, You win!!!')
                break
            elif self.board.check_winning(self.ai.symbol):
                print('You are LOOOOOOOOOOSER :P')
                break
            elif self.board.is_full():
                print('TIE')
                
            in_progres = not self.board.is_full()
开发者ID:Shosh,项目名称:hb_tic_tac_toe,代码行数:38,代码来源:game.py

示例4: __init__

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import is_full [as 别名]
class Game:
    def __init__(self, interface, colors, player_types):
        self.interface = interface
        self.playing = True
        self.winner = None
        self.colors = colors
        self.board = Board(self.interface)
        self.players = []
        for player in player_types:
            self.players.append(eval(player)(self._get_color(), self.colors, self.interface))

    def _get_color(self):
        if not self.players:
            return choice(self.colors)
        else:
            colors = list(self.colors)
            colors.remove(self.players[0].color)
            return colors[0]

    def _move(self, player, board):
        try:
            column = player.get_move(board)
            move = self.board.make_move(column, player.color)
        except error.InvalidMoveError:
            return None
        return move

    def _order_players(self, player):
        if self.players.index(player) != 0:
            self.players.reverse()

    def play(self):
        self._order_players(self.interface.new_game(self.players, self.board))
        while self.playing:
            for player in self.players:
                move = None
                while move is None:
                    move = self._move(player, self.board)
                self.board.print_board()
                if self.board.player_won(player.color):
                    self.winner = player
                    self.playing = False
                    break
                if self.board.is_full():
                    self.winner = None
                    self.playing = False
                    break
        self.interface.end_game(self.winner, self.board)
开发者ID:andschwa,项目名称:uidaho-cs470-connectfour,代码行数:50,代码来源:game.py

示例5: __init__

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import is_full [as 别名]
class Game:
    def __init__(self,board_size=3):
        self.board_size = board_size
        self.reset()

    def reset(self):
        self.board = Board(self,self.board_size)
        
    def intro(self):
        print('Welcome to Jose\'s Unbeatable Tic Tac Toe!')
        print """

        Use your keypad as if it were the board
        
        7 | 8 | 9
        ---------
        4 | 5 | 6
        ---------
        1 | 2 | 3
        
        Have fun, and good luck.
        """

    def letter_choice(self):
        letter = None
        while not (letter == 'X' or letter == 'O'):
            print('Do you want to be X or O?')
            letter = raw_input().upper()

        return letter

    def create_players(self):
        print( "X goes first.")
        self.human_player = HumanPlayer(game = self)
        self.ai_player    = AIPlayer(game = self)
        self.human_player.set_playing_letter(self.letter_choice())
        self.ai_player.set_playing_letter({"X":"O","O":"X"}.get(self.human_player.playing_letter))
        print "[DEBUG] ai_player plays as",self.ai_player.playing_letter
        print "[DEBUG] human player plays as",self.human_player.playing_letter
        # We need an ordered list for the mainloop
        return self.human_first() and [self.human_player,self.ai_player] or [self.ai_player,self.human_player]

    def human_first(self):
        return self.human_player.playing_letter == "X"
        
    def play_again(self):
        print('Do you want to play again? (yes or no)')
        return (raw_input().lower().startswith('y'))

    def mainloop(self):
        players         = self.create_players()
        game_is_playing = True
        while game_is_playing:
            for player in players :
                player.play()
                #self.board.show()
                if player.wins():
                    print "[DEBUG] show board after win"
                    self.board.show()                    
                    player.display_winning_message()
                    # gets out of the while loop
                    game_is_playing = False
                    # gets out of the foor loop
                    break
                elif self.board.is_full():
                    self.board.show()
                    print('The game is a tie.')
                    # gets out of the while loop
                    game_is_playing = False
                    # gets out of the foor loop
                    break

        return self.play_again()
开发者ID:Lh4cKg,项目名称:Tic-Tac-Toe-1,代码行数:75,代码来源:game.py

示例6: BoardTest

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import is_full [as 别名]
class BoardTest(unittest.TestCase):
    def setUp(self):
        self.b = Board(9)
        
    def test_all_like(self):
        a = [1, 2, 3, 4]
        b = [1, 1, 1, 1]
        self.assertTrue(self.b.all_like(b, 1))
        self.assertFalse(self.b.all_like(a, 3))

    def test_is_free(self):
        self.b.moves = [' ', 'X', 'O']
        self.assertTrue(self.b.is_free(0))
        self.assertFalse(self.b.is_free(1))
        self.assertFalse(self.b.is_free(2))

    def test_prepare_for_print(self):
        self.b.moves = [' ', 'X', 'O', ' ', 'X', 'O']
        self.assertEqual(self.b.prepare_for_print(),
                         [[' ', 'X', 'O'], [' ', 'X', 'O']])

    def test_is_winner_top_row_should_be_true(self):
        self.b.moves = ['X', 'X', 'X',
                        ' ', ' ', ' ',
                        ' ', ' ', ' ']
        self.assertTrue(self.b.check_winning('X'))

    def test_is_winner_top_row_should_be_false(self):
        self.b.moves = ['X', 'O', 'X',
                        ' ', ' ', ' ',
                        ' ', ' ', ' ']
        self.assertFalse(self.b.check_winning('X'))

    def test_is_winner_middle_row_should_be_true(self):
        self.b.moves = [' ', ' ', ' ',
                        'X', 'X', 'X',
                        ' ', ' ', ' ']
        self.assertTrue(self.b.check_winning('X'))

    def test_is_winning_middle_row_should_be_false(self):
        self.b.moves = [' ', ' ', ' ',
                        'O', 'X', 'X',
                        ' ', ' ', ' ']
        self.assertFalse(self.b.check_winning('X'))

    def test_is_winning_bottom_row_should_be_true(self):
        self.b.moves = [' ', ' ', ' ',
                        ' ', ' ', ' ',
                        'X', 'X', 'X']
        self.assertTrue(self.b.check_winning('X'))

    def test_is_winning_bottom_row_should_be_false(self):
        self.b.moves = [' ', ' ', ' ',
                        ' ', ' ', ' ',
                        'X', 'X', 'O']
        self.assertFalse(self.b.check_winning('X'))

    def test_is_winning_diagonals_should_be_true(self):
        self.b.moves = ['X', ' ', ' ',
                        ' ', 'X', ' ',
                        ' ', ' ', 'X']
        self.assertTrue(self.b.check_winning('X'))

        self.b.moves = [' ', ' ', 'X',
                        ' ', 'X', ' ',
                        'X', ' ', ' ']
        self.assertTrue(self.b.check_winning('X'))

    def test_is_winning_diagonals_should_be_false(self):
        self.b.moves = ['X', ' ', ' ',
                        ' ', 'O', ' ',
                        ' ', ' ', 'X']
        self.assertFalse(self.b.check_winning('X'))

        self.b.moves = [' ', ' ', 'X',
                        ' ', 'X', ' ',
                        'O', ' ', ' ']
        self.assertFalse(self.b.check_winning('X'))

    def test_is_valid_move_should_be_true(self):
        self.b.moves = [' ', 'X', 'O']
        self.assertTrue(self.b.is_valid_move(0))

    def test_is_valid_move_should_be_false(self):
        self.b.moves = [' ', 'X', 'O']
        self.assertFalse(self.b.is_valid_move(1))
        self.assertFalse(self.b.is_valid_move(2))

    def test_is_full_should_be_false(self):
        self.b.moves = [' ', 'X', 'O']
        self.assertFalse(self.b.is_full())

    def test_is_full_should_be_true(self):
        self.b.moves = ['X', 'X', 'O']
        self.assertTrue(self.b.is_full())

    def test_make_move_should_be_true(self):
        self.b.moves = [' ', 'X', 'O']
        self.assertTrue(self.b.make_move(0, 'X'))
        self.assertEqual(self.b.moves[0], 'X')
#.........这里部分代码省略.........
开发者ID:Shosh,项目名称:hb_tic_tac_toe,代码行数:103,代码来源:board_test.py

示例7: TestBoard

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import is_full [as 别名]
class TestBoard(TestCase):
    def setUp(self):
        self.board = Board()

    def test_clear_board_on_start(self):
        self.assertTrue(self.board.is_clear())

    def test_clear_board(self):
        self.assertTrue(self.board.is_clear())
        self.board.put(0, 0, HUMAN)
        self.assertFalse(self.board.is_clear())
        self.board.clear()
        self.assertTrue(self.board.is_clear())

    def test_full_board(self):
        self.assertFalse(self.board.is_full())
        self.board.put(1, 1, HUMAN)
        self.assertFalse(self.board.is_full())
        self.board.board = [[COMPUTER, COMPUTER, COMPUTER],
                            [COMPUTER, HUMAN, COMPUTER],
                            [COMPUTER, COMPUTER, COMPUTER]]
        self.assertTrue(self.board.is_full())

    def test_can_put_on_board(self):
        self.assertTrue(self.board.can_put(0, 0))
        self.assertTrue(self.board.put(0, 0, HUMAN))

        self.assertFalse(self.board.can_put(0, 0))
        self.assertTrue(self.board.can_put(0, 1))
        self.assertTrue(self.board.can_put(1, 1))
        self.assertFalse(self.board.put(0, 0, HUMAN))

        self.assertEqual(HUMAN, self.board.owner_of(0, 0))

    def test_next_player(self):
        self.board.player_starts(COMPUTER)
        self.assertEqual(HUMAN, self.board.next_player())
        self.assertEqual(COMPUTER, self.board.next_player())
        self.assertEqual(HUMAN, self.board.next_player())

    def test_walk(self):
        self.board.player_starts(COMPUTER)
        self.board.player_starts(HUMAN)
        self.assertEqual(HUMAN, self.board.player_turn())
        self.assertTrue(self.board.put(0, 0, HUMAN))
        self.assertEqual(COMPUTER, self.board.player_turn())

    def test_game_winner(self):
        self.board.board = [[HUMAN, EMPTY, EMPTY],
                            [EMPTY, HUMAN, EMPTY],
                            [EMPTY, EMPTY, HUMAN]]
        self.assertEqual(HUMAN, self.board.get_winner())

        self.board.board = [[HUMAN, EMPTY, COMPUTER],
                            [HUMAN, HUMAN, HUMAN],
                            [COMPUTER, COMPUTER, EMPTY]]
        self.assertEqual(HUMAN, self.board.get_winner())

        self.board.board = [[COMPUTER, HUMAN, COMPUTER],
                            [COMPUTER, HUMAN, COMPUTER],
                            [HUMAN, COMPUTER, HUMAN]]
        self.assertEqual(EMPTY, self.board.get_winner())

        self.board.board = [[EMPTY, HUMAN, COMPUTER],
                            [COMPUTER, HUMAN, COMPUTER],
                            [EMPTY, COMPUTER, HUMAN]]
        self.assertEqual(None, self.board.get_winner())

        self.board.board = [[EMPTY, HUMAN, COMPUTER],
                            [HUMAN, COMPUTER, HUMAN],
                            [COMPUTER, COMPUTER, HUMAN]]
        self.assertEqual(COMPUTER, self.board.get_winner())

        self.board.board = [[EMPTY, HUMAN, HUMAN],
                            [HUMAN, COMPUTER, HUMAN],
                            [COMPUTER, COMPUTER, COMPUTER]]
        self.assertEqual(COMPUTER, self.board.get_winner())

        self.board.board = [[EMPTY, HUMAN, HUMAN],
                            [HUMAN, COMPUTER, HUMAN],
                            [COMPUTER, COMPUTER, COMPUTER]]
        self.assertEqual(COMPUTER, self.board.get_winner())

        self.board.board = [[HUMAN, COMPUTER, HUMAN],
                            [HUMAN, COMPUTER, HUMAN],
                            [HUMAN, HUMAN, COMPUTER]]
        self.assertEqual(HUMAN, self.board.get_winner())

        self.board.board = [[HUMAN, COMPUTER, HUMAN],
                            [COMPUTER, COMPUTER, HUMAN],
                            [HUMAN, EMPTY, HUMAN]]
        self.assertEqual(HUMAN, self.board.get_winner())

    def test_evaluate_move(self):
        board = [[EMPTY, EMPTY, EMPTY],
                 [COMPUTER, HUMAN, EMPTY],
                 [COMPUTER, EMPTY, HUMAN]]
        self.board.board = deepcopy(board)
        self.assertEqual(1, self.board.evaluate_move(0, 0, HUMAN))
        self.assertEqual(1, self.board.evaluate_move(0, 0, COMPUTER))
#.........这里部分代码省略.........
开发者ID:ReekenX,项目名称:python-tic-tac-toe,代码行数:103,代码来源:tests.py


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