I want to deal 2 players 2 cards

1

It is a card program that deals 2 cards randomly. What I want to do is give it to 2 different players but I do not know how to do it.

palos = ["h","d","c","s"]
rangos = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]

Mazo = []

for palo in palos:

    for rango in rangos:

       Mazo.append(rango + palo)
Mazo=set(list(Mazo))

import random

while len(Mazo)>2:
  Mazo.remove(random.choice(list(Mazo)))
  #print(Mazo)    

#print(Mazo)
print("\n")

for Carta in Mazo:
    print (Carta)
    
asked by Godprogrammer 16.10.2017 в 23:05
source

2 answers

0

A simple form could be the following:

import random

palos = ["h","d","c","s"]
rangos = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]

# Generamos el mazo de las 52 cartas
mazo = list(zip(palos * len(rangos), rangos* len(palos)))

jugador1 = []
jugador2 = []
# Desordenamos el mazo
random.shuffle(mazo)
while mazo:
  jugador1.append(mazo.pop())
  jugador2.append(mazo.pop())

print("Cartas jugador 2: {0}".format(jugador1))
print("Cartas jugador 2: {0}".format(jugador2))

In this example we do the following:

  • With list(zip(palos * len(rangos), rangos* len(palos))) we generate a list of the whole deck combining clubs and ranks by zip() .
  • With random.shuffle(mazo) we mess up the deck randomly
  • Then we simply go through the list as long as it has a letter and with the pop() method we obtain one and remove it from the list at the same time
  • We keep two lists for each player and we add them as we ask for each card with pop()
  • This example completely deals the cards to the two players.

If we only want to distribute two cards, we should do something like this:

import random

palos = ["h","d","c","s"]
rangos = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]

# Generamos el mazo de las 52 cartas
mazo = list(zip(palos * len(rangos), rangos* len(palos)))

jugador1 = []
jugador2 = []
# Desordenamos el mazo
random.shuffle(mazo)

jugador1.extend([mazo.pop(), mazo.pop()])
jugador2.extend([mazo.pop(), mazo.pop()])

print("Cartas jugador 2: {0}".format(jugador1))
print("Cartas jugador 2: {0}".format(jugador2))

What we do is "extend" the list of cards of each player with a new list of two elements by removing two cards from the deck for each player. Your original idea to keep a list of cards of the full deck is necessary since it is the only way to make sure that each letter we ask for is not one that we have already received.

    
answered by 17.10.2017 / 02:21
source
0

This is a form, where you define the number of cards per player:

import random
palos = ["h","d","c","s"]
rangos = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
cartas_por_jugador = 2

def draw_cartas():
    carta = random.choice(palos) + ' ' +str(random.choice(rangos))
    return [carta]


def stay_cartas(): 
    i = 0
    jugador = 1
    while i < len(rangos) and i < cartas_por_jugador*2:
        print("carta jugador", jugador, ":" , draw_cartas())
        i = i + 1 
        if(i == cartas_por_jugador):
            jugador = jugador + 1        
stay_cartas()

In this case, he will deal 2 cards for each player, example of the exit:

'carta jugador', 1, ':', ['d 3']
'carta jugador', 1, ':', ['c K']
'carta jugador', 2, ':', ['s J']
'carta jugador', 2, ':', ['h 3']
    
answered by 17.10.2017 в 01:55