鱼C论坛

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

python中让一个玩家猜对字母之后一直猜,猜错后才换另一个人猜

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

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

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

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 21:21:46 | 显示全部楼层
你可以使用一个循环来实现让一个玩家赢了之后一直猜,等他猜错之后就换另一个玩家猜的功能。首先,在主函数中新增一个变量来记录当前猜字母的玩家,初始值为player1。然后在while循环中,打印出当前轮到哪个玩家猜,并接收玩家的猜测。如果猜测正确,则继续让当前玩家猜;如果猜测错误,则切换到另一个玩家猜。具体代码如下所示:
import randomdef 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: ")    currentPlayer = player1 # 当前猜字母的玩家    instructions(player1,player2)    blanks = initializeBlanks(secretWord)    while "#" in blanks and wrongCount < 5:        print(currentPlayer, "It's your turn to guess.")        letter = getGuess(wrongList, currentPlayer)        if letter in secretWord:            blanks = fillInBlanks(secretWord, blanks, letter, currentPlayer)        else:            wrongList.append(letter)            wrongCount = wrongCount + 1            print("Wrong guess!")        statusReport(wrongList, wrongCount, blanks)        display(wrongCount)                if not "#" in blanks:            print(currentPlayer, "Congratulations! You are the winner!")            # 当前玩家赢了之后,继续让他猜            continue        else:            # 猜错了,切换到另一个玩家            if currentPlayer == player1:                currentPlayer = player2            else:                currentPlayer = player1    print("You are both losers.")# 其他函数的代码保持不变
在这个修改后的代码中,我们新增了一个currentPlayer变量来记录当前猜字母的玩家。然后在while循环中,先打印出当前轮到哪个玩家猜,再接收该玩家的猜测。如果猜测正确,则继续让当前玩家猜;如果猜测错误,则切换到另一个玩家猜。当一个玩家赢了之后,通过`continue`语句继续下一轮循环,即继续让他猜。直到有一个玩家猜错或者猜对所有字母,循环才会结束。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-15 07:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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