anntzer / redeal

A reimplementation of Thomas Andrews' Deal in Python.
Other
63 stars 41 forks source link

Is it possible to generate board from sample `'QTxx.J8x.9x.Q9xx 9xxx.AKT.J8x.8x AK.Qxxx.ATxx.ATx'` #37

Closed larrycai closed 10 months ago

larrycai commented 10 months ago

Is it possible to use redeal python code to generate deal with

sample='QTxx.J8x.9x.Q9xx 9xxx.AKT.J8x.8x AK.Qxxx.ATxx.ATx'

deal = ???(sample)
Deal.set_str_style('long')
print("sample:", deal)
anntzer commented 10 months ago

I think you need to predeal the high cards and then add a condition on the shape (as described at https://github.com/anntzer/redeal#predealing-and-scripting). I guess adding support for "x" in predeal would be nice, though.

larrycai commented 10 months ago

good indication, I paste sample below (u can reopen the ticket if u want to implement)

# sample.py
from redeal import *

# sample='QTxx.J8x.9x.Q9xx 9xxx.AKT.J8xx.8x AK.Qxxx.ATxx.ATx' # J8x.9xx.KQx.KJxx
# need simple func to convert to predeal
predeal = {"N": "QT J8 9 Q9", "E": "9 AKT J8 8", "S": "AK Q AT AT", "W": "J8 9 KQ KJ"}

def accept(deal):
    west, north, east, south = deal.west, deal.north, deal.east, deal.south
    return (
        north.shape in Shape("4324") and 
        east.shape in Shape("4342") and 
        south.shape in Shape("2443") and
        west.shape in Shape("3334")
    )

it is not so efficiency, but it works

$ redeal --max 50000 -n 1 sample.py 
♠QT62♥J82♦95♣Q976 ♠9753♥AKT♦J872♣82 ♠AK♥Q543♦AT64♣AT4 ♠J84♥976♦KQ3♣KJ53

Tries: 29346
larrycai commented 9 months ago

Give updates, now I created one method handles this without apply(), can be approved, but works for me

import random
from redeal import *

maxium_small_cards = 8
all_cards = "23456789TJQKA"

def replace_small_cards(suit, available_cards):
    suit = list(suit)
    for i, card in enumerate(suit):
        if card == "x":
            random_card = random.choice(available_cards)
            suit[i] = random_card
            available_cards.remove(random_card)
    return "".join(suit)

def process_input(sample):
    all_small_cards = [[str(i) for i in range(2, maxium_small_cards)] for _ in range(4)]
    hands = sample.split()

    all_result = []
    cards = [""] * 4
    fourth_hand = [""] * 4
    for i in range(len(hands)):
        suits = hands[i].split(".")
        new_hands = []
        for index, suit in enumerate(suits):
            new_suit = replace_small_cards(suit, all_small_cards[index])
            cards[index] = cards[index] + new_suit
            new_hands.append(new_suit)
        all_result.append(" ".join(new_hands))
    print(cards)
    for i in range(4):
        for card in all_cards:
            if card not in cards[i]:
                fourth_hand[i] = fourth_hand[i] + card
    all_result.append(" ".join(fourth_hand))

    #return " ".join(result)
    return dict(zip("NESW", all_result))

# Call the function with the corrected sample input
result = process_input("QTxx.J8x.9x.Q9xx 9xxx.AKT.J8xx.8x AK.Qxxx.ATxx.ATx")

predeal = result
#predeal = {"N": "QT67 J82 95 Q972", "E": "9532 AKT J873 83", "S": "AK Q543 AT42 AT5", "W": "J84 976 KQ6 KJ64"}
print(predeal)
dealer = Deal.prepare(predeal)
deal = dealer()
Deal.set_str_style('long')
print(deal)

The result looks like

$ python sample.py
['QT479652AK', 'J83AKTQ264', '93J867AT25', 'Q93286AT5']
{'N': 'QT47 J83 93 Q932', 'E': '9652 AKT J867 86', 'S': 'AK Q264 AT25 AT5', 'W': '38J 579 4QK 47JK'}

       ♠QT74
       ♥J83
       ♦93
       ♣Q932

♠J83          ♠9652
♥975          ♥AKT
♦KQ4          ♦J876
♣KJ74         ♣86

       ♠AK
       ♥Q642
       ♦AT52
       ♣AT5