鱼C论坛

 找回密码
 立即注册
查看: 668|回复: 1

python

[复制链接]
发表于 2023-11-27 09:30:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是我写完成的代码,请问怎么做到让一个玩家赢了之后可以一直让他猜?等他猜错之后就换另一个人猜?

import random
def main():
    f=open("cattle.txt")
    text=f.readlines()
    secretWord=random.choice(text)
    secretWord=secretWord.strip('\n')

    count=1
    wrongCount=0
    wrongList=[]
    player1=input("Enter your name: ")
    player2=input("Enter your name: ")
    instructions(player1,player2)
    blanks=initializeBlanks(secretWord)
    while "#" in blanks and wrongCount < 5:
        if count%2==0:
            player=player1
        else:
            player=player2
        count+=1
        letter=getGuess(wrongList,player)
        if letter in secretWord:
            blanks=fillInBlanks(secretWord,blanks,letter,player)
        else:
            wrongList.append(letter)
            wrongCount=wrongCount+1
            print("Wrong guess!")
        statusReport(wrongList,wrongCount,blanks)
        display(wrongCount)
        
    if not "#" in blanks:
        print(player,"Congratulations! You are the winner!")
    else:
        print("You are both losers.")
        


def instructions(p1,p2):
    print(p1,p2,"I will select a secret word and give you the number of the word.")
    print("You need to guess the letters in the word.")
    print("Correctly guessed letters will be uesd to fill in the blank, incorrecly guessed letters will be used to build a snowman.")
    print("The game will end if you successfully guess all the secret letters or you have 5 wrong guesses.")
   

def getGuess(wrongList,player):


    letter = input(player+"Guess a letter: ")
    punct="~!@#$%^&*()_+?:<>"
    if letter in wrongList:
        print("You have guessed this letter.")
        return getGuess(wrongList,player)
    if  letter in punct:
        print("This guess is invaild,guess again!")
        return getGuess(wrongList,player)
    else:
        letter=letter.lower()
        return letter
   
   

def statusReport(wrongList,wrongCount,blanks):
    print("Current status: ")
    print("You have guessed",wrongCount,"times wrong.")
    print("The letters you have guessed wrong are",wrongList)
    print("The secret word now is",blanks)


def initializeBlanks(s):
    blanks=["#"]*len(s)
    return blanks

def fillInBlanks(secretWord,blanks,l,player):
    location=secretWord.index(l)
    blanks[location]=l
    print(blanks)
    if not "#" in blanks:
        print(player,"Congratulations! You are the winner!")
    return blanks


def display(wrongCount):
    grid=[[' ',' ',' '],
          [' ',' ',' '],
          [' ',' ',' '],
          ['~','~','~']]
    Dict={1:[0,1,'O'],
        2:[1,1,'O'],
        3:[2,1,'O'],
        4:[1,0,'/'],
        5:[1,2,'\\']}
   
    for key in (Dict.keys()):
        if key<=wrongCount:
            value=Dict[key]
            row=value[0]
            col=value[1]
            item=value[2]
            grid[row][col]=item
   
    for row in grid:
        for col in row:
            print(col,end=" ")
        print("\n")

main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-27 09:30:33 | 显示全部楼层
要实现让一个玩家赢了之后可以一直让他猜,等他猜错之后就换另一个人猜,可以在主函数内加入循环,让玩家可以多次游戏。具体做法是,在while循环的最后添加一个询问是否继续游戏的输入,如果玩家想继续游戏,则清空之前的状态,重新选择秘密单词,重置计数和错误列表;如果玩家不想继续游戏,则跳出循环结束游戏。

下面是修改后的代码:

import random

def main():
    f=open("cattle.txt")
    text=f.readlines()
    secretWord=random.choice(text)
    secretWord=secretWord.strip('\n')
  
    while True:
        count=1
        wrongCount=0
        wrongList=[]
        player1=input("Enter your name: ")
        player2=input("Enter your name: ")
        instructions(player1,player2)
        blanks=initializeBlanks(secretWord)
        while "#" in blanks and wrongCount < 5:
            if count%2==0:
                player=player1
            else:
                player=player2
            count+=1
            letter=getGuess(wrongList,player)
            if letter in secretWord:
                blanks=fillInBlanks(secretWord,blanks,letter,player)
            else:
                wrongList.append(letter)
                wrongCount=wrongCount+1
                print("Wrong guess!")
            statusReport(wrongList,wrongCount,blanks)
            display(wrongCount)

        if not "#" in blanks:
            print(player,"Congratulations! You are the winner!")
        else:
            print("You are both losers.")

        continue_game = input("Do you want to continue playing? (Y/N)")
        if continue_game.lower() == 'n':
            break

def instructions(p1,p2):
    print(p1,p2,"I will select a secret word and give you the number of the word.")
    print("You need to guess the letters in the word.")
    print("Correctly guessed letters will be uesd to fill in the blank, incorrecly guessed letters will be used to build a snowman.")
    print("The game will end if you successfully guess all the secret letters or you have 5 wrong guesses.")

def getGuess(wrongList,player):
    letter = input(player + " Guess a letter: ")
    punct = "~!@#$%^&*()_+?:"
    if letter in wrongList:
        print("You have guessed this letter.")
        return getGuess(wrongList, player)
    if letter in punct:
        print("This guess is invaild, guess again!")
        return getGuess(wrongList, player)
    else:
        letter = letter.lower()
        return letter

def statusReport(wrongList,wrongCount,blanks):
    print("Current status: ")
    print("You have guessed",wrongCount,"times wrong.")
    print("The letters you have guessed wrong are",wrongList)
    print("The secret word now is",blanks)

def initializeBlanks(s):
    blanks = "#" * len(s)
    return blanks

def fillInBlanks(secretWord,blanks,l,player):
    locations = [i for i, letter in enumerate(secretWord) if letter == l]
    for location in locations:
        blanks = blanks[:location] + l + blanks[location+1:]
    print(blanks)
    if not "#" in blanks:
        print(player,"Congratulations! You are the winner!")
    return blanks

def display(wrongCount):
    grid = [
        "_____",
        "|   |",
        "|  ( )",
        "|  /|\\",
        "|  / \\",
        "|______"
    ]
    Dict = {
        1: grid[0],
        2: "\n".join(grid[:2]),
        3: "\n".join(grid[:3]),
        4: "\n".join(grid[:4]),
        5: "\n".join(grid)
    }

    for key in (Dict.keys()):
        if key == wrongCount:
            print(Dict[key])

main()

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-15 06:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表