PokerGame决定卡

时间:2015-10-12 04:27:38

标签: python python-3.x

我正在做一个程序,其中生成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"

准确打印列的行列。

2 个答案:

答案 0 :(得分: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())
    }
}

答案 1 :(得分: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