Quantcast
Channel: DaniWeb Solved Topics
Viewing all articles
Browse latest Browse all 582

converting python code to java?

$
0
0

Hi, I am new to Java but familiar with python, I need to convert my code from python to Java can anyone help me? Much appreciated!

import random


suits = ['C', 'S', 'H', 'D']
val = list(range(1,14))


class Card:
    def __init__(self, suits, val):
        self.suit = suits
        self.value = val

    def show(self):
        print("{} of {}". format(self.value, self.suit))


class Deck:
    def __init__(self):
        self.cards = []
        self.build()

    def build(self):
        for s in ['C', 'S', 'H', 'D']:
            for v in list(range(1,14)):
                self.cards.append(Card(s, v))

    def show(self):
        for c in self.cards:
            c.show()

    def shuffle(self):
        for i in range(len(self.cards) - 1, 0, -1):
            r = random.randint(0, i)
            self.cards[i], self.cards[r] = self.cards[r], self.cards[i]

    def drawCard(self):
        if len(self.cards) > 0:
            return self.cards.pop(0)
        else:
            return None

    def returnCard(self, card):
        self.cards.append(card)


# %%

def game():
    deck = Deck()
    deck.shuffle()
    matches = [False for i in range(13)]
    table = [[] for i in range(13)]

    win = 0

    while len(deck.cards) > 0 and win == 0:
        for i in range(1,14):
            if all(matches) == True:
                win = 1
                break
            else:
                if len(deck.cards) > 0:
                    if matches[i-1] == False:
                        card = deck.drawCard()
                        if card.value == i:
                            matches[i-1] = True
                            for j in table[i-1]:
                                deck.returnCard(j)
                            table[i-1].clear()
                        table[i-1].append(card)
                    else:
                        continue
    return(win)

#%%
wins = 0 
n = 100000
for i in range(n):
    wins += game()

winrate = wins/n
print(winrate)

Viewing all articles
Browse latest Browse all 582

Latest Images

Trending Articles



Latest Images