前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python小程序

Python小程序

作者头像
青灯古酒
发布2023-10-16 08:38:47
1670
发布2023-10-16 08:38:47
举报
文章被收录于专栏:青灯古酒

Python游戏代码

代码语言:javascript
复制
class Board:
    def __init__(self, size=15):
        self.size = size
        self.board = [[' ' for _ in range(size)] for _ in range(size)]
        self.current_player = 'X'

    def display(self):
        print('    ' + '   '.join(str(i) for i in range(self.size)))
        print('   +' + '---+' * self.size)
        for i in range(self.size):
            row = f' {i} |'
            for j in range(self.size):
                row += f' {self.board[i][j]} |'
            print(row)
            print('   +' + '---+' * self.size)

    def make_move(self, x, y):
        if not self.is_valid_move(x, y):
            return False
        self.board[x][y] = self.current_player
        self.current_player = 'O' if self.current_player == 'X' else 'X'
        return True

    def is_valid_move(self, x, y):
        if x < 0 or x >= self.size or y < 0 or y >= self.size:
            return False
        if self.board[x][y] != ' ':
            return False
        return True

    def check_winner(self):
        for i in range(self.size):
            for j in range(self.size):
                if self.board[i][j] == ' ':
                    continue
                if self.check_right(i, j) or self.check_down(i, j) or self.check_diagonal_down_right(i, j) or self.check_diagonal_down_left(i, j):
                    return self.board[i][j]
        return None

    def check_right(self, x, y):
        count = 1
        while y + count < self.size and self.board[x][y + count] == self.board[x][y]:
            count += 1
        return count >= 5

    def check_down(self, x, y):
        count = 1
        while x + count < self.size and self.board[x + count][y] == self.board[x][y]:
            count += 1
        return count >= 5

    def check_diagonal_down_right(self, x, y):
        count = 1
        while x + count < self.size and y + count < self.size and self.board[x + count][y + count] == self.board[x][y]:
            count += 1
        return count >= 5

    def check_diagonal_down_left(self, x, y):
        count = 1
        while x + count < self.size and y - count >= 0 and self.board[x + count][y - count] == self.board[x][y]:
            count += 1
        return count >= 5

if __name__ == '__main__':
    board = Board()
    while True:
        board.display()
        move = input(f"{board.current_player}, make a move (row column): ")
        try:
            x, y = move.split()
            x, y = int(x), int(y)
        except:
            print('Invalid input.')
            continue
        if not board.make_move(x, y):
            print('Invalid move.')
            continue
        winner = board.check_winner()
        if winner is not None:
            board.display()
            print(f"Game over. {winner} wins!")
            break

这个代码实现了一个简单的五子棋游戏,玩家需要在棋盘上下五子棋,先连成五个的玩家即为胜者。你可以运行这个代码,体验一下这个游戏的玩法。

运行结果:

Python小程序_Python
Python小程序_Python
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-05-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档