Here’s a c# template:
using System;
using System.Collections.Generic;
using System.Linq;
public class Game
{
public Game()
{
// implement initializer here
}
public void AddCard(string suit, string value)
{
// implement function here
}
public string CardString(int card)
{
// implement function here
return "";
}
public bool CardBeats(int cardA, int cardB)
{
// implement function here
return false;
}
}
class Solution
{
public static List SplitWords(string s)
{
return string.IsNullOrEmpty(s) ? new List() : s.Trim().Split(’ ').ToList();
}
public static void Main()
{
Game game = new Game();
string[] suitValue = Console.ReadLine().Split(' ');
game.AddCard(suitValue[0], suitValue[1]);
Console.WriteLine(game.CardString(0));
suitValue = Console.ReadLine().Split(' ');
game.AddCard(suitValue[0], suitValue[1]);
Console.WriteLine(game.CardString(1));
Console.WriteLine(game.CardBeats(0, 1).ToString().ToLower());
}
}
A more idiomatic Python implementation:
from typing import List, Union
from enum import Enum
from math import inf
class Color(Enum):
RED = “Red”
BLACK = “Black”
class Suit(Enum):
HEARTS = “Hearts”
SPADES = “Spades”
CLUBS = “Clubs”
DIAMONDS = “Diamonds”
values = {“A”: 1,
**{str(i): i for i in range(2, 11)},
“J”: 11,
“Q”: 12,
“K”: 13,
“Inf”: inf,}
Values = Enum(“Values”, values)
class BaseCard:
def gt(self, other):
return self.value.value > other.value.value
def __eq__(self, other):
return self.value.value == other.value.value
class Card(BaseCard):
def init(self, suit: str, value: str):
super().init()
self.suit = Suit(suit)
self.value = Values[value]
def __repr__(self):
return f"{self.value.name} of {self.suit.value}"
class Joker(BaseCard):
def init(self, i: int, color: str):
super().init()
self.i = i
self.color = Color(color)
self.value = Values[“Inf”]
def __repr__(self):
return f"{self.color.value} Joker"
class Hand:
def init(self, cards: List[Union[Card, Joker]]):
self.__cards = cards
def __repr__(self):
return ", ".join([str(card) for card in self.__cards])
def __iter__(self):
return reversed(sorted(self.__cards))
def __gt__(self, other):
for item1, item2 in zip(self, other):
if item1!=item2:
return True if item1>item2 else False
return False
class Game:
def init(self):
self.__cards = []
self.__hands = []
def add_card(self, suit: str, value: str) -> None:
self.__cards.append(Card(suit, value))
def card_string(self, card: int) -> str:
return self.__cards[card]
def card_beats(self, card_a: int, card_b: int) -> bool:
return self.__cards[card_a] > self.__cards[card_b]
def add_joker(self, color: str) -> None:
self.__cards.append(Joker(len(self.__cards), color))
def add_hand(self, card_indices: List[int]) -> None:
cards = [card for i, card in enumerate(self.__cards) if i in card_indices]
self.__hands.append(Hand(cards))
def hand_string(self, hand: int) -> str:
return self.__hands[hand]
def hand_beats(self, hand_a: int, hand_b: int) -> bool:
return self.__hands[hand_a] > self.__hands[hand_b]
Are you planning to add codes for object oriented design in C++? This is restricting me to buy Premium.
Why are we calling super().__init__()
in the Hand
class constructor? It itself is a base class.
The Javascript main function has the line, handBList.push(card);, which throws an error because card is not defined.
This should probably be fixed