-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
976 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import copy | ||
|
||
class Card: | ||
def __init__(self, suit, value): | ||
self.suit = suit | ||
self.value = value | ||
|
||
def __repr__(self): | ||
return f"{self.value} of {self.suit}" | ||
|
||
def __str__(self): | ||
return f"{self.value} of {self.suit}" | ||
|
||
|
||
def parse_cards(data): | ||
hand_cards = [] | ||
cards = data.split('; Table: ') | ||
#print(cards) | ||
hand = cards[0].split(';') | ||
#print(hand) | ||
for card in hand: | ||
val = int(card.split(' ')[0]) | ||
suit = card.split(' ')[2] | ||
hand_cards.append(Card(suit, val)) | ||
#print(f'{val} of {suit}') | ||
table = [card.strip() for card in cards[1].strip().split(';') if card.strip()] | ||
table_cards = [] | ||
#print(table) | ||
for card in table: | ||
val = int(card.split(' ')[0]) | ||
suit = card.split(' ')[2] | ||
table_cards.append(Card(suit, val)) | ||
return hand_cards, table_cards | ||
|
||
def count_points(hand): | ||
if (len(hand) != 9): | ||
raise ValueError("Wrong number of cards in hand!") | ||
points = 0 | ||
distinct_values = set() | ||
distinct_colors = set() | ||
color_counts = {} | ||
value_counts = {} | ||
|
||
for card in hand: | ||
distinct_values.add(card.value) | ||
distinct_colors.add(card.suit) | ||
color_counts[card.suit] = color_counts.get(card.suit, 0) + 1 | ||
value_counts[card.value] = value_counts.get(card.value, 0) + 1 | ||
|
||
for card in hand: | ||
if (color_counts[card.suit] == 5 and value_counts[card.value] == 5): | ||
return 0 | ||
|
||
for color in distinct_colors: | ||
if color_counts[color] > 4: | ||
distinct_values = set() | ||
value_counts = {} | ||
for card in hand: | ||
if (card.suit != color): | ||
distinct_values.add(card.value) | ||
value_counts[card.value] = value_counts.get(card.value, 0) + 1 | ||
|
||
for value in distinct_values: | ||
if value_counts[value] < 5: | ||
points += int(value) | ||
|
||
return points | ||
|
||
def exchange_card(player_hand, table_cards, hand_index, table_index): | ||
if hand_index < 0 or hand_index >= len(player_hand): | ||
print("Wrong card index.") | ||
return | ||
if table_index < 0 or table_index >= len(table_cards): | ||
print("Wrong table index.") | ||
return | ||
hand = player_hand.copy() | ||
hand[hand_index] = table_cards[table_index] | ||
return hand | ||
|
||
def play(): | ||
while(True): | ||
#print("Wczytuje") | ||
status = input() | ||
hand, table = parse_cards(status) | ||
#print("Wczytane") | ||
points = count_points(hand) | ||
response = "fold" | ||
#print("Rozpoczynam liczenie") | ||
for i in range (9): | ||
for j in range (5): | ||
fake_hand = exchange_card(hand, table, i, j) | ||
fake_points = count_points(fake_hand) | ||
if (fake_points <= points): | ||
response = str(i) + " " + str(j) | ||
points = fake_points | ||
|
||
#print("Koncze liczenie") | ||
print(response) | ||
play() |
169 changes: 169 additions & 0 deletions
169
FileGatherer/Storage/42b6623a-a7d6-4dba-a9fa-aedeab8b019b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import random | ||
|
||
class Card: | ||
def __init__(self, suit, value): | ||
self.suit = suit | ||
self.value = value | ||
|
||
def __repr__(self): | ||
return f"{self.value} of {self.suit}" | ||
|
||
def __str__(self): | ||
return f"{self.value} of {self.suit}" | ||
|
||
class Deck: | ||
def __init__(self): | ||
self.cards = [] | ||
self.build() | ||
|
||
def build(self): | ||
suits = ['Yellow', 'Green', 'Blue', 'Red', 'Pink', 'White', 'Brown'] | ||
values = ['1', '2', '3', '4', '5', '6', '7', '8'] | ||
for suit in suits: | ||
for value in values: | ||
self.cards.append(Card(suit, value)) | ||
|
||
def shuffle(self): | ||
random.shuffle(self.cards) | ||
|
||
def draw(self): | ||
if len(self.cards) > 0: | ||
return self.cards.pop() | ||
else: | ||
return None | ||
|
||
def __len__(self): | ||
return len(self.cards) | ||
|
||
def __repr__(self): | ||
return f"Deck with {len(self)} cards" | ||
|
||
def count_points(hand): | ||
if (len(hand) != 9): | ||
raise ValueError("Wrong number of cards in hand!") | ||
points = 0 | ||
distinct_values = set() | ||
distinct_colors = set() | ||
color_counts = {} | ||
value_counts = {} | ||
|
||
for card in hand: | ||
distinct_values.add(card.value) | ||
distinct_colors.add(card.suit) | ||
color_counts[card.suit] = color_counts.get(card.suit, 0) + 1 | ||
value_counts[card.value] = value_counts.get(card.value, 0) + 1 | ||
|
||
for card in hand: | ||
if (color_counts[card.suit] == 5 and value_counts[card.value] == 5): | ||
return 0 | ||
|
||
for color in distinct_colors: | ||
if color_counts[color] > 4: | ||
distinct_values = set() | ||
value_counts = {} | ||
for card in hand: | ||
if (card.suit != color): | ||
distinct_values.add(card.value) | ||
value_counts[card.value] = value_counts.get(card.value, 0) + 1 | ||
|
||
for value in distinct_values: | ||
if value_counts[value] < 5: | ||
points += int(value) | ||
|
||
return points | ||
|
||
def deal_cards(deck, num_players, num_cards): | ||
players_hands = [[] for _ in range(num_players)] | ||
for _ in range(num_cards): | ||
for i in range(num_players): | ||
card = deck.draw() | ||
if card: | ||
players_hands[i].append(card) | ||
return players_hands | ||
|
||
def exchange_card(player_hand, table_cards, hand_index, table_index): | ||
if hand_index < 0 or hand_index >= len(player_hand): | ||
print("Wrong card index.") | ||
return | ||
if table_index < 0 or table_index >= len(table_cards): | ||
print("Wrong table index.") | ||
return | ||
player_hand[hand_index], table_cards[table_index] = table_cards[table_index], player_hand[hand_index] | ||
|
||
def give_bot_info(player, last): | ||
global hands | ||
global table_cards | ||
response = "" | ||
hand = hands[player] | ||
for card in hand: | ||
response += str(card) | ||
response += ";" | ||
response += " Table: " | ||
for card in table_cards: | ||
response += str(card) | ||
response += ";" | ||
if(last): | ||
response += "last" | ||
print(response) | ||
|
||
def select_winner(hands): | ||
index = 0 | ||
mini = 50 | ||
for i in range(len(hands)): | ||
if(count_points(hands[i]) < mini): | ||
index = i | ||
mini = count_points(hands[i]) | ||
print(index) | ||
|
||
|
||
def set_game(players = 4, cards = 9): | ||
global deck | ||
deck = Deck() | ||
deck.shuffle() | ||
|
||
global num_players | ||
global num_cards_per_player | ||
num_players = players # valid 2-5 (for currend set of cards) | ||
num_cards_per_player = cards # shouldnt be changed, unless with other mechanics | ||
|
||
global hands | ||
global table_cards | ||
hands = deal_cards(deck, num_players, num_cards_per_player) | ||
table_cards = [deck.draw() for _ in range(5)] | ||
|
||
def game(players = 2, cards = 9): | ||
set_game(players, cards) | ||
folds = 0 | ||
zero = False | ||
last = False | ||
iteration = 0 | ||
while(folds < 2 and iteration < 10000): | ||
iteration += 1 | ||
for player in range(players): | ||
print(player) | ||
if (folds > 1): | ||
last = True | ||
give_bot_info(player, last) | ||
response = input() | ||
if(response.lower() == 'fold'): | ||
folds += 1 | ||
else: | ||
exchange_card(hands[player], table_cards, int(response[0]), int(response[2])) | ||
if(count_points(hands[player]) == 0): | ||
zero = True | ||
break | ||
if(zero): | ||
break | ||
print(-1) | ||
select_winner(hands) | ||
|
||
def check_winner(): | ||
global hands | ||
global num_players | ||
|
||
for hand in hands: | ||
print(count_points(hand)) | ||
|
||
game() | ||
|
||
# check_winner() # debug to print players points |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.