📜  简单兴趣的Python程序(1)

📅  最后修改于: 2023-12-03 15:41:08.893000             🧑  作者: Mango

简单兴趣的Python程序

Python是一种受欢迎的编程语言,使用Python编写简单有趣的程序十分容易。在本文中,我们将介绍一些有趣的Python程序示例。

程序示例
猜数字游戏

这是一个简单的Python程序,让用户猜测一个数字,如果猜对了,程序就会结束,否则会给出提示信息并继续询问,直到猜对为止。

import random

num = random.randint(1, 100)

while True:
    guess = int(input("请输入一个数字:"))
    if guess == num:
        print("恭喜你,猜对了!")
        break
    elif guess < num:
        print("你猜的数字太小了,再试试吧。")
    else:
        print("你猜的数字太大了,再试试吧。")
翻转字符串

这个程序将输入的字符串翻转并输出。

def reverse_string(s):
    return s[::-1]

s = input("请输入一个字符串:")
result = reverse_string(s)
print(result)
黑白棋游戏

这个程序实现了一个简单的黑白棋游戏,让用户和计算机随机下棋,直到棋盘填满或者没有合法的下棋位置为止。

import random

# 行列数
board_size = 8

# 初始棋盘
board = [[0 for i in range(board_size)] for j in range(board_size)]
board[3][3] = board[4][4] = 1
board[3][4] = board[4][3] = 2

# 打印棋盘
def print_board():
    print("  ", end="")
    for i in range(board_size):
        print(i+1, end=" ")
    print()
    for i in range(board_size):
        print(chr(ord('a')+i), end=" ")
        for j in range(board_size):
            if board[i][j] == 0:
                print(".", end=" ")
            elif board[i][j] == 1:
                print("O", end=" ")
            else:
                print("X", end=" ")
        print()

# 检查是否有合法的下棋位置
def has_valid_moves(player):
    for i in range(board_size):
        for j in range(board_size):
            if board[i][j] == 0:
                if is_valid_move(player, i, j):
                    return True
    return False

# 判断是否为合法的下棋位置
def is_valid_move(player, row, col):
    if row < 0 or row >= board_size or col < 0 or col >= board_size:
        return False
    if board[row][col] != 0:
        return False
    for dr in [-1, 0, 1]:
        for dc in [-1, 0, 1]:
            if dr == 0 and dc == 0:
                continue
            r, c = row+dr, col+dc
            while r >= 0 and r < board_size and c >= 0 and c < board_size and board[r][c] != 0 and board[r][c] != player:
                r += dr
                c += dc
            if r >= 0 and r < board_size and c >= 0 and c < board_size and board[r][c] == player:
                return True
    return False

# 下棋
def make_move(player, row, col):
    board[row][col] = player
    for dr in [-1, 0, 1]:
        for dc in [-1, 0, 1]:
            if dr == 0 and dc == 0:
                continue
            r, c = row+dr, col+dc
            changed = []
            while r >= 0 and r < board_size and c >= 0 and c < board_size and board[r][c] != 0 and board[r][c] != player:
                changed.append((r, c))
                r += dr
                c += dc
            if r >= 0 and r < board_size and c >= 0 and c < board_size and board[r][c] == player:
                for (r, c) in changed:
                    board[r][c] = player

# 计算棋子数量
def count_pieces():
    black_count = 0
    white_count = 0
    for i in range(board_size):
        for j in range(board_size):
            if board[i][j] == 1:
                black_count += 1
            elif board[i][j] == 2:
                white_count += 1
    return (black_count, white_count)

# 游戏主循环
player = random.randint(1, 2)
while True:
    print_board()
    print("黑棋:", count_pieces()[0])
    print("白棋:", count_pieces()[1])
    if not has_valid_moves(player):
        print("玩家", player, "没得下了")
        player = 3 - player
        if not has_valid_moves(player):
            print("游戏结束")
            break
        continue
    elif player == 1:
        print("黑棋走")
    else:
        print("白棋走")
    while True:
        row = input("请输入行号(a-h):")
        col = input("请输入列号(1-8):")
        row = ord(row) - ord('a')
        col = int(col) - 1
        if is_valid_move(player, row, col):
            make_move(player, row, col)
            player = 3 - player
            break
        else:
            print("非法位置,请重新输入")
结语

通过编写这些Python程序,您可以更深入地理解Python语言,并增强自己的编程能力。这些示例只是Python编程的冰山一角,继续探索,并发挥您的创意,您将会发现Python世界是何等美妙。