Home > OS >  Error with a player turn ending loop in the Zombie Dice game
Error with a player turn ending loop in the Zombie Dice game

Time:06-07

I'm trying to develop a code for the zombie dice game.

My problem is when the first player replies that he doesn't want to continue playing.

The game ends and I can't get the second player to play. Can someone help me?

import random

print("========== ZOMBIE DICE (PROTÓTIPO SEMANA 4) ==========")
print("========= WELCOME TO THE ZOMBIE DICE GAME! ========")
numeroJogadores = 0
while numeroJogadores < 2:
  numeroJogadores = int(input("Enter the number of players: "))
  print(numeroJogadores)

  if numeroJogadores < 2:
    print("NOTICE: You must have at least 2 players to continue!")

  listaJogadores = []
  
  for i in range(numeroJogadores):

    nome = str(input("\nEnter player name: "   str(i 1)   ": "))
    listaJogadores.append(nome)

  print(listaJogadores)

  dadoVerde = ("brain", "steps", "brain", "shot", "steps", "brain")
  dadoAmarelo = ("shot", "steps", "brain", "shot", "steps", "brain")
  dadoVermelho = ("shot", "steps", "shot", "brain", "steps", "shot")

  listaDados = [dadoVerde, dadoVerde, dadoVerde, dadoVerde, dadoVerde, dadoVerde, 
                dadoAmarelo, dadoAmarelo, dadoAmarelo, dadoAmarelo, 
                dadoVerde, dadoVermelho, dadoVermelho]

  print("\nSTARTING THE GAME...")

  jogadorAtual = 0
  dadosSorteados = []
  tiros = 0
  cerebros = 0
  passos = 0

  while True:

    print("PLAYER TURN: ", listaJogadores[jogadorAtual])

    
    for i in range (3):
      numeroSorteado = random.randint(0, 12)
      dadoSorteado = listaDados[numeroSorteado]

      if (dadoSorteado == dadoVerde):
        corDado = "Green"
      elif (dadoSorteado == dadoAmarelo):
        corDado = "Yellow"
      else:
        corDado = "Red"

      print("Dice Drawn: ", corDado)

      dadosSorteados.append(dadoSorteado)

    print("\nThe faces drawn were: ")

    for dadoSorteado in dadosSorteados:
      numeroFaceDado = random.randint(0,5)

      if dadoSorteado[numeroFaceDado] == "brain":
        print("- brain (you ate a brain)")
        cerebros = cerebros   1
      elif dadoSorteado[numeroFaceDado] == "tiro":
        print("- shot (you got shot)")
        tiros = tiros   1
      else:
        print("- steps (a victim escaped)")
        passos = passos   1

    print("\nCURRENT SCORE: ")
    print("brins: ", cerebros)
    print("shots: ", tiros)

    if cerebros >= 13:
      print("Congratulations, you've won the game!")
      break
    elif tiros >= 3:
      print("You lost the game!")
      break
    else:
      continuarTurno = str(input("\nNOTICE: Do you want to continue playing dice? (y=yes / n=no)")).lower()
      if continuarTurno == "n":
        jogadorAtual = jogadorAtual   1
        dadossorteados = 0
        tiros = 0
        cerebros = 0
        passos = 0

        if jogadorAtual > numeroJogadores:
          print("Finalizing the game prototype")
      else:
        print("Starting another round of the current turn")
        dadossorteados = []

print("======================================================")
print("===================== END OF THE GAME ====================")
print("======================================================")

Does anyone know what I'm doing wrong?

I'm new as a programmer. If anyone knows how to help me with this problem, I would be grateful.

CodePudding user response:

EDIT: Well now the code just works. I can switch players just fine, but if you go through all the characters it crashes again since jogadorAtual gets too big for listaJogadores. I don't quite understand the game, but assuming you want to start back with the first player, an elegant way you can accomplish that is by doing modulus with the % operator, which divides two numbers but returns the remainder. If you divide the number of players by the size of listaJogadores, you'll always get a number inside listaJogadores's range.

# Change this...
print("PLAYER TURN: ", listaJogadores[jogadorAtual])

# ...to this
print("PLAYER TURN: ", listaJogadores[jogadorAtual % len(listaJogadores)])

If that's not what you need for your game, let me know.


Original answer: Is the game ending or is it crashing? When I run the game, it always crashes with:

  File "C:\Users\me\Desktop\Untitled-1.py", line 56, in <module>
    diceDrawns.append(diceDrawn)
AttributeError: 'int' object has no attribute 'append'

This is because after looping, you try to do diceDrawns.append(), but you've already replaced the diceDrawns list with an integer on line 87. I'm not sure if you meant to replace diceDrawn instead, but that's definitely the source of the problem.

An unrelated note: You can do = as a shorthand way to increment a variable by a certain amount, so you can replace a lot of instances of things like currentPlayer = currentPlayer 1 with currentPlayer = 1, and the same can be done with any operator, so you could also do things like -= or *=

  • Related