2017-07-20 8 views
1

CPUとユーザーがカードを繰り返し使用しています。私はと同様にshuffle関数を使用しました。ユーザーとCPUがカードを繰り返さないようにする方法はありますか?繰り返し乱数の取得を停止するにはどうすればよいですか?

ここ実行の例である:ここ

はあなたのカードは、次のとおり
1)クラブ
の10 2)ダイヤモンド
3の4)ダイヤモンド
4の6) 7つのクラブ
5)10のクラブ

カードをプレイするには、一度に1つずつ番号を入力するだけです。行われ、 入力空白

あなたは演奏:ダイヤモンドダイヤモンドのクラブザ・6の10の4果たしたクラブの7

CPU:スペードザ・ジャックをJack of Spades

ご覧のとおり、ユーザーには10個のクラブが2回与えられました。

マイコード:

import random 
import math 
from collections import Counter 
print("Gui-less poker sucks, but it sure is addicting probably") 
if 1: 
    deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52] 
    random.shuffle(deck) 
    hcardss = [""] 
    hcardsc = [""] 
    hcardsh = [""] 
    ccardss = [""] 
    ccardsc = [""] 
    ccardsh = [""] 
    ingame = "true" 
    while (ingame == "true"): 
     undone = 5 
     while (undone > 0): 
      card = deck.pop() 
      # print(card) 
      temp = card/13 
      temp2 = card/4 
      temp = math.floor(temp) 
      temp2 = math.floor(temp2) 
      temp = temp + 1 
      # temp2 = temp2 + 1 
      #print(temp) 
      #print(temp2) 
      # undone -= 1 
      hcardss.append(temp) 
      hcardsc.append(temp2) 
      if (temp == 1): 
       temp3 = " of Spades" 
      elif (temp == 2): 
       temp3 = " of Diamonds" 
      elif (temp == 3): 
       temp3 = " of Clubs" 
      else: 
       temp3 = " of Hearts" 
      if (temp2 == 10): 
       temp4 = "Jack" 
      elif (temp2 == 11): 
       temp4 = "Queen" 
      elif (temp2 == 12): 
       temp4 = "King" 
      elif (temp2 == 13): 
       temp4 = "Ace" 
      else: 
       temp4 = str(temp2 + 1) 
      # print("Your card was the " + temp4 + temp3) 
      hcardsh.append("The " + temp4 + temp3) 
      undone -= 1 
     undone = 5 
     random.shuffle(deck) 
     while (undone > 0): 
      # THIS ONE IS THE COMPUTER 
      card = deck.pop() 
      # print(card) 
      temp = card/13 
      temp2 = card/4 
      temp = math.floor(temp) 
      temp2 = math.floor(temp2) 
      temp = temp + 1 
      # temp2 = temp2 + 1 
      #print(temp) 
      #print(temp2) 
      # undone -= 1 
      ccardss.append(temp) 
      ccardsc.append(temp2) 
      if (temp == 1): 
       temp3 = " of Spades" 
      elif (temp == 2): 
       temp3 = " of Diamonds" 
      elif (temp == 3): 
       temp3 = " of Clubs" 
      else: 
       temp3 = " of Hearts" 
      if (temp2 == 10): 
       temp4 = "Jack" 
      elif (temp2 == 11): 
       temp4 = "Queen" 
      elif (temp2 == 12): 
       temp4 = "King" 
      elif (temp2 == 13): 
       temp4 = "Ace" 
       temp4 = str(temp2 + 1) 
      # print("Your card was the " + temp4 + temp3) 
      ccardsh.append("The " + temp4 + temp3) 
      undone -= 1 
     print() 
     print() 
     print() 
     print("Here are your cards:") 
     print("1) " + hcardsh[1]) 
     print("2) " + hcardsh[2]) 
     print("3) " + hcardsh[3]) 
     print("4) " + hcardsh[4]) 
     print("5) " + hcardsh[5]) 
     print("To play cards, simply type their number one at a time. When done, input blank") 
     instant = "true" 
     doneinput = "false" 
     hplay = [""] 
     while (doneinput == "false"): 
      latestinput = input("> ") 
      if (latestinput == ""): 
       if (instant == "true"): 
        print("Okay, you fold") 
        ingame = "false" 
        exit() 
       doneinput = "true" 
      else: 
       if (int(latestinput) in hplay): 
        print("You already picked that one!") 
       else: 
        hplay.append(int(latestinput)) 
     # print("The cards you played are " + str(hplay)) 
     doneinput = "false" 
     cplay = [""] 
     while (doneinput == "false"): 
      latestinput = random.randint(1,5) 
      if (latestinput == ""): 
       doneinput = "true" 
      else: 
       if (int(latestinput) in cplay): 
        doneinput = "true" 
       else: 
        cplay.append(int(latestinput)) 
     #print("So you played " + str(hplay)) 
     #print("And the cpu played " + str(cplay)) 
     #print("So you played the " + hcardsh[hplay[1]] + hcardsh[hplay[2]] 
     times = len(hplay) 
     # times = times - 1 
     hplayh = [""] 
     cplayh = [""] 
     sub = 1 
     print() 
     print() 
     print("You played:") 
     while (sub < times): 
      hplayh.append(hcardsh[hplay[sub]]) 
      print(hcardsh[hplay[sub]]) 
      sub += 1 
     sub = 1 
     times = len(cplay) 
     print() 
     print() 
     print("CPU played:") 
     while (sub < times): 
      cplayh.append(ccardsh[cplay[sub]]) 
      print(ccardsh[cplay[sub]]) 
      sub += 1 
     #print(str(hplay) 
     #print(str(cplayh)) 
     hscore = 0 
     cscore = 0 
     #checker = 1 
     #highnumber = 0 
     #quantity = [""] 
     #quancheck = 0 
     htrans = [""] 
     temp5 = 1 
     while (len(hplay) > temp5): 
      htrans.append(int(hcardsc[hplay])) 
      temp5 += 1 
     ctrans = [""] 
     temp5 = 1 
     while (len(cplay) > temp5): 
      ctrans.append(int(ccardsc[cplay])) 
      temp5 += 1 

     hoccur = Counter(htrans).most_common() 
     coccur = Counter(ctrans).most_common() 
     print(hoccur) 
     #while (len(hplay) > checker): 
     # if (hcardsc[hplay[checker]] > highnumber): 
      #  quancheck += 1 
      # quantity.append(quancheck) 
      #checker += 1 


     ingame = "false" 
+3

です。それで、あなたは同じカードを次に得ることはできません。 – byxor

+0

私は 'deck'の宣言を外側whileループの内側に移動するべきだと思います。 –

+0

また、「14」と「15」のような2つの数字が「4」と「13」によって分割されて同じ値を返すようになると、これが起こる可能性はかなり高いです。 –

答えて

1

リストをシャッフルし、それが正しいから値をポップし1と52の間にカード番号を生成する方法。

問題は、番号からカードのスーツと値を生成する方法にあります。例えば

  • 床(4分の26)= 7、床(4分の27)= 7
  • 床(13分の26)= 2、床(13分の27)= 2

したがって、26と27は同じカードと解釈されます。あなたのオリジナルのアイデアを維持

、あなたはむしろ13で除算の商と余りを使用することができます。

q = card_num // 13 # integer division 
r = card_num % 13 # modulo operator 

または、両方を一度に、divmodを使用して:

q, r = divmod(26, 13) 
print(q,r) 
# (2, 0) 
q, r = divmod(27, 13) 
print(q,r) 
# (2, 1) 

あなたは0になります< = q < = 3および0 < = r < = 13

注:
コード内のいくつかの点を改善することができます。簡単な変更:

deck = list(range(1, 53)) 

は短くなります。;)

+0

ありがとうございました!これは助けになった! – Killermarsh

0

これは私が常に

は、まずカードのクラス

class Card(object): 
    def __init__(self,value,suite): 
     self.value = value 
     self.suite = suite 
    def __str__(self): 
     # override behaviour of printing a card object 
     card_names = ["Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"] 
     return card_names[self.value] 

    def __repr__(self): 
     #override the repr of a card to print the card name and suite 
     suites = "Clubs Spades Diamonds Hearts" 
     return "%s of %s"%(self,suites[self.suite]) 

    def __cmp__(self,other_card): 
     return cmp(self.value,other_card.value) 

を定義するカードゲームは、クラスのための優秀テスト根拠を作ると思います...クラスで動作するように学ぶ絶好の機会のように思えます私たちのすべてのカードが含まれているデッキクラス

class Deck(object): 
    def __init__(self): 
     self.cards = [] 
     for suite in range(4): 
      for value in range(13): 
       self.cards.append(Card(value,suite)) 
    def shuffle(self): 
     self.cards.shuffle() 
    def deal(self): 
     if self.cards: 
      return self.cards.pop() 
     raise StopIteration("Out Of Cards...") 
    def __len__(self): 
     return len(self.cards) 

は今、あなたはplayメソッドを作成することができます

def play_game(): 
    deck = Deck() 
    player_1 = [deck.deal() for _ in range(5) ] # deal 5 cards to player1 
    player_2 = [deck.deal() for _ in range(5) ] # deal 5 cards to player2(cpu) 
    game_over = False 
    while not game_over: 
    for i,card in enumerate(player_1): 
     #play a turn 
     print("%d. %r"%(i,card)) 
     player_1.append(deck.deal() 
     player_2.append(deck.deal() 
    print("Winner is Player 1") 

その後、メインプログラムループは、単にあなたが(あなたが本当にしたい場合や `list`)、カードの` set`を作成し、ランダムな要素を選択し、それを削除することができ

while still_playing: 
    play_game() 
    still_playing = check_with_player() 
+0

小さなニックピット:あなたは 'スーツ'の代わりに 'スイート'と書いてあり、テキストにいくつかの他のタイプミスがあります。 – byxor

関連する問題