Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Исправление ошибок #9

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# DZcards
Были добавлены логины
Изначальное кол-во денег игрока = 1000
В конце игры логин игрока и его деньги записываются в файл(file.txt)
8 changes: 7 additions & 1 deletion client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ def game(self):

def main(self):
if input('Подключиться к серверу (yes(y)/no(n))?: ').lower() == 'y':
self.id = self.recv()
print(self.recv())#Логиин
self.send(input())
print(self.recv())#Добро пожаловать
money = self.recv()
self.send("All is norm")
print('Ваши деньги:' + money + "\n")
print("Регистрация на сервере успешно выполнена.")
self.id = self.recv()
self.wait()
self.game()
else:
Expand Down
119 changes: 118 additions & 1 deletion comparator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,122 @@
from random import choice
from cards import *

class Flush:
def __str__(self):
return "Flush"

def check(cards):
suits = {c.suit for c in cards}
return len(suits) == 1

class Pair:
def __str__(self):
return "Pair"

def check(cards):
nom = {}
for v in map(lambda x: x.value.value, cards):
nom[v] = nom.get(v, 0) + 1
return len(nom) == 4

class TwoPairs:
def __str__(self):
return "TwoPairs"

def check(cards):
nom = {}
for v in map(lambda x: x.value.value, cards):
nom[v] = nom.get(v, 0) + 1
return len(nom) == 3 and max(nom.values()) == 2 and min(nom.values()) == 1

class Set:
def __str__(self):
return "Set"

def check(cards):
nom = {}
for v in map(lambda x: x.value.value, cards):
nom[v] = nom.get(v, 0) + 1
return len(nom) == 3 and max(nom.values()) == 3 and min(nom.values()) == 1

class FullHouse:
def __str__(self):
return "FullHouse"

def check(cards):
nom = {}
for v in map(lambda x: x.value.value, cards):
nom[v] = nom.get(v, 0) + 1
return len(nom) == 2 and max(nom.values()) == 3

class Quads:
def __str__(self):
return "Quads"

def check(cards):
nom = {}
for v in map(lambda x: x.value.value, cards):
nom[v] = nom.get(v, 0) + 1
return len(nom) == 2 and max(nom.values()) == 4

class Straight:
def __str__(self):
return "Straight"

def check(cards):
s = list(map(lambda x: x.value.value, cards))
return not Pair.check(cards) and max(s) - min(s) == 4

class High:
def check(cards):
return True

class StraightFlush():
def __str__(self):
return "StraightFlush"

def check(cards):
return Straight.check(cards) and Flush.check(cards)

combinations = [StraightFlush, Quads, FullHouse, Flush, Straight, Set, TwoPairs, Pair, High]

class Comparator:
def combine(self, cards):
r = []
for i in range(6):
for j in range(i + 1, 7):
r.append(cards[:i] + cards[i + 1: j] + cards[j + 1:])
return r

def compare(self, clients, table):
return [choice(list(clients.keys()))]
res = {}
for client_id, cards in clients:
res[max(map(max_combine, combine(cards + table)))].append(client_id)
ans = []
for key in sorted(res):
ans.append(res[key])
return ans

def max_combine(cards):
for c in combinations:
if c.check(cards):
return c()

Suits = [Diamonds, Clubs, Hearts, Spades]

cards = [
Card(2, Diamonds),
Card(5, Diamonds),
Card(9, Diamonds),
Card(11, Diamonds),
Card(0, Diamonds),
Card(2, Hearts),
Card(5, Hearts),
Card(5, Clubs),
Card(5, Clubs),
Card(2, Clubs)
]

#cards = [Card(i, Diamonds) for i in range(2, 7)]

print(*cards)
69 changes: 55 additions & 14 deletions diler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from cards import createCards
from random import randint
from comparator import Comparator
from time import sleep

class Diler:
class Client:
Expand All @@ -18,13 +19,16 @@ def ready(self):
class Check(Ready):
pass

class AllIn(Ready):
class Bet(Ready):
pass

class Called(Ready):
class AllIn(Bet):
pass

class Rised(Ready):
class Called(Bet):
pass

class Rised(Bet):
pass

class Pass(Ready):
Expand All @@ -36,22 +40,29 @@ def __init__(self, id, conn):
self.id = id
self.cards = []
self.status = Diler.Client.NotReady()
self.bet = 0
self.money = 1000
self.pas = False

def __eq__(self, other):
return self.id == other.id

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

def ready(self):
return self.status.ready()
def ready(self, max_bet):
return self.money == 0 or self.bet >= max_bet or self.pas

def __init__(self, server):
self.server = server
self.deck = createCards()
self.table = []
self.clients = []
self.comparator = Comparator()
self.rise_client = None
self.bet = 0
self.bank = 0
self.big_blind = 100

def getClient(self, client):
missing = Diler.Client(client[1][1], client[0])
Expand All @@ -72,33 +83,47 @@ def getState(self):
return self.clients

def roundRun(self):
if len(list(filter(lambda x: type(x.status) != Diler.Client.Pass, self.clients))) < 2:
return

for client in filter(lambda x: type(x.status) != Diler.Client.Pass, self.clients):
client.status = Diler.Client.NotReady()

k = 0 if rise_client is None else rise_client
k = 0 if self.rise_client is None else self.rise_client

while not all([client.ready() for client in self.clients]):
while not all([client.ready() for client in self.clients]) or len(list(filter(lambda x: type(x.status) != Diler.Client.Pass, self.clients))) < 2:
if not self.clients[k].ready():
self.server.send(self.clients[k].conn, 'ask')
sleep(0.01)
if self.bet > 0:
self.server.send(self.clients[k].conn, 'ask1 ' + str(bet - self.clients[k].bet))
else:
self.server.send(self.clients[k].conn, 'ask0')
ans = self.server.recv(self.clients[k].conn)
self.server.broadcast('info: игрок ' + str(self.clients[k].id) + ' ответил ' + ans)
if ans == 'pass':
self.broadcast('info: игрок ' + str(self.clients[k].id) + ' ответил ' + ans)
if ans == 'check':
self.clients[k].status = Diler.Client.Check()
elif ans == 'pass':
self.clients[k].status = Diler.Client.Pass()
elif ans == 'call':
self.clients[k].status = Diler.Client.Called()
elif ans == 'rise':
elif ans[:4] == 'rise' or ans[:3] == 'bet':
for c in filter(lambda x: type(x.status) != Diler.Client.Pass, self.clients):
c.status = Diler.Client.NotReady()
self.clients[k].status = Diler.Client.Rised()
self.rise_client = k
bet = int(ans[4:])
self.bank += bet
self.clients[k].bet = bet
else:
self.clients[k].status = Diler.Client.Pass()
print('error: непонятный ответ от клиента')
k = (k + 1) % len(self.clients)

def blind(self):
self.bank += self.big_blind // 2
self.bank += self.big_blind
self.clients[0].bed(self.big_blind // 2)
self.clients[1].bed(self.big_blind)
self.bet = True
return "Игрок " + str(self.clients[0].id) + " поставил " + str(self.big_blind // 2) + ", игрок " + str(self.clients[1].id) + " поставил " + str(self.big_blind)

def flop(self):
self.roundRun()
for i in range(3):
Expand Down Expand Up @@ -129,3 +154,19 @@ def opening(self):
else:
res = 'победители: ' + ', '.join(map(str, win))
return res

def game(self):
self.broadcast(self.blind())
self.broadcast(self.flop())
self.broadcast(self.turn())
self.broadcast(self.river())
self.broadcast(self.opening())
self.next_turn()
self.broadcast("Спасибо за игру!")

def broadcast(self, msg):
for client in self.clients:
self.server.send(client.conn, msg)

def next_turn(self):
self.clients.append(self.clients.pop(0))
Empty file added file.txt
Empty file.
3 changes: 2 additions & 1 deletion network.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import socket

class Connection:
"Класс для констант подключения"
timeout = 5
adress = '127.0.0.1'
port = 1234
players = 2
Expand All @@ -27,6 +27,7 @@ class Server:
Занимается созданием сокета, установлением связи с клиентами, а также и получением и отправкой им сообщений."""
def __init__(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(Connection.timeout)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((Connection.adress, Connection.port))
self.socket.listen(Connection.players)
Expand Down
Loading