2015-10-12 98 views
0

我正在做一个程序,其中2个随机数被生成分配给两行。该行是正在处理的球员是从数量转化为实际卡值 Ex. row 1290 contains [1,2,4,5,3,7,2,12,3,1,0] that has [2 of hearts, 5 of clubs, 7 of diamonds, queen of spades, ace of diamonds]扑克游戏决定卡

手我怎样才能让我的代码

import random 
#opening the poker file and generating the random numbers 
poker_file = open("poker-hand-testing.data",'r') 
number_one = random.randint(0,1000000) 
number_two = random.randint(0,1000000) 
row_int = 0 
#loop to check if both random numbers generated are different 
while number_one == number_two: 
    number_two = random.randint(0,1000000) 
#Establishing the hand drawn by both players 
for line_str in poker_file: 
    row_int = row_int + 1 
    row_str = line_str.split(',') 
    if row_int == number_one: 
     hand1 = row_str 
    elif row_int == number_two: 
     hand2 = row_str 
for i in [0,2,6,8]: 
    suit_int = int(hand1[i]) 
    if suit_int == 1: 
     suit1 = "Hearts" 
    elif suit_int == 2: 
     suit2 = "Spades" 
    elif suit_int == 3: 
     suit3 ="Diamonds" 
    elif suit_int == 4: 
     suit4 = "Clubs" 
for p in [1,3,5,7]: 
    value_int = int(hand1[p]) 
    if value_int == 1: 
     value_one = "Ace" 
    elif 1 < value_int < 11: 
     value2 = str(value_int) 
    elif value_int == 11: 
     value3 ="Jack" 
    elif value_int == 12: 
     value4 = "Queen" 
    elif value_int == 13: 
     value5 = "King" 

打印的行究竟是如何上市。

+1

Ick你为什么这样做?你在问问题。只需制作一张含有所有信息并拥有卡片对象列表的'卡片' –

+0

因此'poker_file'是这些“手?”的文本文件?每只手都在自己的线上,是吗?这不是说'hand1 [0]'是'“[”'? –

+0

@AdamSmith该文本文件不包含'“[”'因为它只是一行数字,但当我调用该文件时,它使它成为一个列表so'hand1 [0]'是'1'而不是'[' – YGarcia

回答

2
from enum import Enum, IntEnum 

hand1 = [1,2,4,5,3,7,2,12,3,1,0] 
hand2 = [2,3,3,3,4,3,1,3,4,1,0] 

class Suits(Enum): 
    Hearts = 1 
    Spades = 2 
    Diamonds = 3 
    Clubs = 4 
class Ranks(IntEnum): 
    Ace = 1 # writing this was the first time I thought to myself 
    Two = 2 # "...gee, I wish I were writing in golang right now" 
    Three = 3 # iotas are wonderful. 
    Four = 4 
    Five = 5 
    Six = 6 
    Seven = 7 
    Eight = 8 
    Nine = 9 
    Ten = 10 
    Jack = 11 
    Queen = 12 
    King = 13 

class Card(object): 
    def __init__(self, suit, rank): 
     self.suit = suit 
     self.rank = rank 

    @classmethod 
    def from_tuple(cls, tup): 
     suit, rank = tup 
     return cls(Suits(suit), Ranks(rank)) 

    def __str__(self): 
     return "{0.rank.name} of {0.suit.name}".format(self) 

    # This is allllll framework stuff so that it's easier to handle these later! 

import itertools 

def grouper(iterator, n): 
    """From itertools recipes""" 
    args = [iter(iterator)] * n 
    return itertools.zip_longest(fillvalue=0, *args) 

hand = [Card.from_tuple(group) for group in grouper(hand1, 2) 
     if 0 not in group] 
other_hand = [Card.from_tuple(group) for group in grouper(hand2, 2) 
       if 0 not in group] 

与大多数编程一样,困难的部分是构建框架。最简单的部分是事后处理。建立一些有用的东西,以后收获好处!

而在Golang,只是因为我练

package main 

import "fmt" 

type Card struct { 
    suit int 
    rank int 
} 

func (c *Card) String() string { 
    suit_mapping := map[int]string{ 
     1: "Hearts", 
     2: "Spades", 
     3: "Diamonds", 
     4: "Clubs"} 
    rank_mapping := map[int]string{ 
     1: "Ace", // I lied! iota only works on consts! 
     2: "Two", 
     3: "Three", 
     4: "Four", 
     5: "Five", 
     6: "Six", 
     7: "Seven", 
     8: "Eight", 
     9: "Nine", 
     10: "Ten", 
     11: "Jack", 
     12: "Queen", 
     13: "King", 
    } 
    return fmt.Sprintf("%s of %s", rank_mapping[c.rank], suit_mapping[c.suit]) 
} 

func main() { 
    hand1_values := []int{1, 2, 4, 5, 3, 7, 2, 12, 3, 1, 0} 
    hand1 := make([]Card, 0) 

    for idx, val := range hand1_values { 
     var suit int 
     var rank int 
     if val == 0 { 
      break // last value! 
     } 
     if idx%2 == 0 { 
      // suit 
      suit = val 
      rank = hand1_values[idx+1] 
     } else { 
      continue 
     } 
     c := Card{suit: suit, rank: rank} 
     hand1 = append(hand1, c) 
    } 

    for _, c := range hand1 { 
     fmt.Println(c.String()) 
    } 
} 
2

以下思路可能的帮助。您可以直接使用枚举值,而不是为手建立数字列表。

首先创建一个基于SuitsNumbers的产品的一整套卡。洗牌这一点,并从背包取出两只手,可选两只手整理和显示并排的两只手侧,然后剩余的卡包:

from enum import IntEnum 
import random 
from itertools import product 

hand_size = 5 
Suits = IntEnum('Suit', 'Hearts Spades Diamonds Clubs') 
Numbers = IntEnum('Number', 'Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King') 

# Create a complete pack of cards and shuffle it 
pack = [(s, n) for s, n in product(Suits, Numbers)] 
random.shuffle(pack) 

hand_1 = sorted(pack[:hand_size]) 
del(pack[:hand_size]) 

hand_2 = sorted(pack[:hand_size]) 
del(pack[:hand_size]) 

print '{:45} {}'.format('Hand 1', 'Hand 2') 
for card_1, card_2 in zip(hand_1, hand_2): 
    print '{:45} {}'.format(card_1, card_2) 

print 
print 'Remaining cards in pack' 
for card in pack: 
    print card 

给你以下类型的输出:

Hand 1          Hand 2 
(<Suit.Hearts: 1>, <Number.Seven: 7>)   (<Suit.Hearts: 1>, <Number.Ace: 1>) 
(<Suit.Spades: 2>, <Number.Seven: 7>)   (<Suit.Spades: 2>, <Number.Four: 4>) 
(<Suit.Spades: 2>, <Number.King: 13>)   (<Suit.Diamonds: 3>, <Number.Ace: 1>) 
(<Suit.Clubs: 4>, <Number.Ace: 1>)   (<Suit.Diamonds: 3>, <Number.Seven: 7>) 
(<Suit.Clubs: 4>, <Number.Three: 3>)   (<Suit.Clubs: 4>, <Number.Two: 2>) 

Remaining cards in pack 
(<Suit.Diamonds: 3>, <Number.Three: 3>) 
(<Suit.Diamonds: 3>, <Number.Five: 5>) 
(<Suit.Diamonds: 3>, <Number.Queen: 12>) 
(<Suit.Clubs: 4>, <Number.Jack: 11>) 
. 
. 
. 
etc