Home > OS >  How to make slots in the basic board game?
How to make slots in the basic board game?

Time:12-11

I have a circular board (a list) which contains 10 slots. At the begging the slots are empty, as player moves to a unowned slot, the player becomes the owner and has to come up with a string for the slot. If another player gets on that slot, the player has to guess the string. If guests correcly he becomes the new owner of the slot and gets coins.

I do not understand what the problem is in this code?

from dataclasses import dataclass

@dataclass
class Player: 
    
    firstname: str
    
    lastname: str
    
    coins: int
    
    slot: int
    
    def full_info(self) -> str:
        
        return f"{self.firstname} {self.lastname} {self.coins} {self.slot}"

    @classmethod
    def from_user_input(cls) -> 'Player':        
        return cls(        
            firstname=input("Please enter your first name:"),
            
            lastname=input("Please enter your second name: "),
            
            coins=200,
            
            slot= 0)
minplayer, maxplayer, n = 2, 5, -1
while not(minplayer <= n <= maxplayer):   #definding the maximum and minimum players 
    n = int(input(f" Choose a number of players between {minplayer} and {maxplayer}: "))    
playersingame = []   #storing it in the list
for i in range(n):    
    playersingame.append(Player.from_user_input())
print([player.full_info() for player in playersingame])

board = [[ ] for i in range(10)]
for player in playersingame:    
    board[ player.slot ].append(player)
print(board)
print()
import random
for player in playersingame:    
    input(f"{player.firstname} {player.lastname}, please press enter to roll your die...")    
    die = random.randint(1,6)    
    print(f"You take {die} step{'s'* (die>1)} forward")    
    board[player.slot].remove(player)    
    player.slot  = die    
    board[player.slot].append(player)
print(board)
def shift(seq, n=0):
    a = n % len(seq)
    return seq[-a:]   seq[:-a] 
round_counter=1
rounds=30 
# round counter
while (round_counter <= rounds):    
    for player in playersingame:        
        input(f"{player.firstname} {player.lastname}, please press enter to roll your die...")        
        die = random.randint(1,6)        
        print(f"You take {die} step{'s'*(die>1)} forward")        
        board[player.slot].remove(player)        
        player.slot = (player.slot   die) % len(board)
board[player.slot].append(player)

The problem is with line 35, where the board is made I tried making the slots:

board = [[ ] for i in range(10)]
board = 
slot[1] = {"MagicString": "pizza", "Owner": ""} 
slot[2] = {"MagicString": "pasta", "Owner": ""}
slot[3] = {"MagicString": "cupcake", "Owner": ""}
slot[4] = {"MagicString": "chocolate", "Owner": ""}
slot[5] = {"MagicString": "tea", "Owner": ""}
slot[6] = {"MagicString": "Wings", "Owner": ""}
slot[7] = {"MagicString": "jelly", "Owner": ""}
slot[8] = {"MagicString": "milk", "Owner": ""}
slot[9] = {"MagicString": "soup", "Owner": ""}
slot[10] = {"MagicString": "cake", "Owner": ""}

CodePudding user response:

board = [[ ] for i in range(10)]  // line 1
board =                           // line 2
slot[1] = {"MagicString": "pizza", "Owner": ""}  // line 3
...

This code makes no sense.

In line 1 you define board to be a list of empty lists. Nothing wrong with that.

But in line 2, you throw away that definition, and start to define board again. But you don't finish the line and provide a right-hand-side for the expression. What do you want to assign board to be?

In line 3, you try to assign something to slot[1], but you haven't previously defined slot to be an object that can be subscripted with the [] operator.

So you have at least three errors here, two of which will be rejected by the Python interpreter.

If you want to replace the entries in the board list with dictionaries, you can say something like board[0] = {...}.

Or if you want to add these dictionaries to the lists in the board list, you can say something like board[0].append({...}).

Even better might be to define another class to represent a board slot, and define functions to add players and/or strings to objects of that class. Then make the board a list of these slot objects.

CodePudding user response:

Something like that should work; of course, it would be good to put the board printing into the 'round' loop, preferably after the 'Round n' printing, and better format it, including info on each slot's owner:

minplayer, maxplayer, n = 2, 5, -1
while not(minplayer <= n <= maxplayer):   #definding the maximum and minimum players 
    n = int(input(f" Choose a number of players between {minplayer} and {maxplayer}: "))    
playersingame = []   #storing it in the list
for i in range(n):    
    playersingame.append(Player.from_user_input())
print('Players in game: ',[player.full_info() for player in playersingame],'\n')


board = [{'players':[ ], 'magicstring': None, 'owner': None} for i in range(10)]
for player in playersingame:    
    board[player.slot]['players'].append(player)
print([[p.full_info() for p in s['players']] for s in board], '\n')


import random
round_counter=1
rounds=30 
# round counter
while (round_counter <= rounds):
    print("\n\nRound ", round_counter,'\n\n')
    for player in playersingame:        
        input(f"{player.firstname} {player.lastname}, please press enter to roll your die...")        
        die = random.randint(1,6)        
        print(f"You take {die} step{'s'*(die>1)} forward")        
        board[player.slot]['players'].remove(player)        
        player.slot = (player.slot   die) % len(board)
        board[player.slot]['players'].append(player)
        if board[player.slot]['owner'] == None:
            board[player.slot]['owner'] = player
            board[player.slot]['magicstring'] = input('Choose your magic string: ')
        else:
            print('placeholder')
    round_counter  = 1
  • Related