card dealer

Dave Angel davea at davea.name
Fri Sep 27 08:08:33 EDT 2013


On 27/9/2013 07:26, Dave Angel wrote:

> On 27/9/2013 06:24, markotaht at gmail.com wrote:
>
I sent the previous message long before I had finished.
>
> If you had created your global list containing list(range(52)), then you
> could do nearly your entire program with one call to random.sample.
>
> http://docs.python.org/3.3/library/random.html#random.sample
>

What is your goal?  Are you trying to make the logic match your C++
code?  Are you just trying to solve the problem, or are you trying to
figure out where the bug is in your present code.

If it were my problem, I'd build a deck as a list, shuffle it with
random.shuffle(), then deal the cards out in order.  I might also use
list.pop() to take items off, so I wouldn't need any separate counter.

If i were trying to figure out why the existing code fails, I'd start
with an assert right before setting the flags.

    assert(not kaart_tõmmatud[i])
    kaart_tõmmatud[i] = True

Once you do that, and it fires, you know explicitly that you have a bug.

i believe the code may be fixed by changing the function 
vali_järgmine_vaba()  to begin as follows:

def vali_järgmine_vaba(n):
    i = 0
    while kaart_tõmmatud[i]:
        i = i + 1

    while(n  > 0):

But I would point out that this is a horribly unreadable way to do it. 
It's possibly appropriate for C, which is mostly unreadable anyway.  But
even there, there are much better approaches.  For example, i recall
writing a shuffle function in C decades ago, which took an array of (52)
unique items and put them in random order.  For an array of size 52, it
did it with exactly 51 swaps, without any searching like you're doing in
the above function.


-- 
daveA





More information about the Python-list mailing list