Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ap-Tanoh mbah ange pascal
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
M'Bah Ange Pascal Tanoh
ap-Tanoh mbah ange pascal
Commits
f8802c2c
Commit
f8802c2c
authored
1 year ago
by
Tanoh Mbah-ange-pascal
Browse files
Options
Downloads
Patches
Plain Diff
maj Projet_recursive
parent
c409312f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
TP8/card.py
+184
-0
184 additions, 0 deletions
TP8/card.py
with
184 additions
and
0 deletions
TP8/card.py
0 → 100755
+
184
−
0
View file @
f8802c2c
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod:`card` module
:author: `FIL - Faculté des Sciences et Technologies -
Univ. Lille <http://portail.fil.univ-lille1.fr>`_
:date: 2017, september.
:last revision: 2024, march
"""
from
__future__
import
annotations
import
random
class
Card
(
object
):
"""
Cards are defined by a value and a color.
Possible values and colors are listed in ``Card.VALUES`` and ``Card.COLORS``.
$$$ c1 = Card(
"
Ace
"
,
"
heart
"
)
$$$ c1.color
'
heart
'
$$$ c1.value
'
Ace
'
$$$ c1
Card(
"
Ace
"
,
"
heart
"
)
$$$ c2 = Card(
"
King
"
,
"
spade
"
)
$$$ c2.value in Card.VALUES
True
$$$ c2.color in Card.COLORS
True
$$$ c1 == c1
True
$$$ c1 != c1
False
$$$ c1 < c1
False
$$$ c1 <= c1
True
"""
## tuple of possible values and colors in ascending order
VALUES
=
(
"
Ace
"
,
"
2
"
,
"
3
"
,
"
4
"
,
"
5
"
,
"
6
"
,
"
7
"
,
"
8
"
,
"
9
"
,
"
10
"
,
"
Jack
"
,
"
Knight
"
,
"
Queen
"
,
"
King
"
)
COLORS
=
(
"
spade
"
,
"
heart
"
,
"
diamond
"
,
"
club
"
)
def
__init__
(
self
,
value
:
str
,
color
:
str
):
"""
creates a card with value and color
précondition : value in VALUES and color in COLORS
"""
self
.
color
=
color
self
.
value
=
value
def
__hash__
(
self
)
->
int
:
"""
Renvoie un haché de self.
"""
return
hash
((
self
.
color
,
self
.
value
))
def
__repr__
(
self
)
->
str
:
"""
return a string representation of the card
$$$ repr(Card(
'
Ace
'
,
'
heart
'
))
'
Card(
"
Ace
"
,
"
heart
"
)
'
"""
return
f
"
Card(
{
repr
(
self
.
value
)
}
,
{
repr
(
self
.
color
)
}
)
"
def
__str__
(
self
)
->
str
:
"""
return a string representation of the card
$$$ str(Card(
'
Ace
'
,
'
heart
'
))
'
Ace of heart
'
"""
...
def
compare
(
self
,
card
:
Card
)
->
int
:
"""
compares cards.
Order on cards is defined by order on values
return:
* a positive number if self is greater than card
* a negative number if self is lower than card
* 0 if self is the same than card
précondition: none
exemples:
$$$ c1 = Card(
'
Ace
'
,
'
heart
'
)
$$$ c2 = Card(
'
King
'
,
'
heart
'
)
$$$ c3 = Card(
'
Ace
'
,
'
spade
'
)
$$$ c1bis = Card(
'
Ace
'
,
'
heart
'
)
$$$ c1.compare(c2) < 0
True
$$$ c2.compare(c1) > 0
True
$$$ c1.compare(c3) == 0
True
"""
if
self
.
value
==
card
.
value
:
return
0
self_index
=
Card
.
VALUES
.
index
(
self
.
value
)
card_index
=
Card
.
VALUES
.
index
(
card
.
value
)
return
1
if
self_index
>
card_index
else
-
1
@staticmethod
def
deck
(
n_card
:
int
)
->
list
[
Card
]:
"""
return a list of `n_card` randomly chosen cards
precondition: n_card > 0 and n_card <= 4*13
Exemples:
$$$ cartes = Card.deck( 10 )
$$$ len(cartes) == 10
True
$$$ all( isinstance(c, Card) for c in cartes)
True
$$$ len(set(cartes))
len(cartes)
"""
deck
=
[
Card
(
value
,
color
)
for
value
in
Card
.
VALUES
for
color
in
Card
.
COLORS
]
random
.
shuffle
(
deck
)
return
deck
[:
n_card
]
def
__eq__
(
self
,
card
:
Card
)
->
bool
:
"""
return True if self equals card
False otherwise
"""
return
self
.
value
==
card
.
value
and
self
.
color
==
card
.
color
def
__neq__
(
self
,
card
:
Card
)
->
bool
:
"""
return True if self don
'
t equal card
False otherwise
"""
return
not
self
==
card
def
__lt__
(
self
,
card
:
Card
)
->
bool
:
"""
return True if self is strictly inferior to card
False otherwise
"""
return
self
.
compare
(
card
)
==
-
1
def
__le__
(
self
,
card
:
Card
)
->
bool
:
"""
return True if self is inferior or equal to card
False otherwise
"""
return
self
.
compare
(
card
)
<=
0
def
__gt__
(
self
,
card
:
Card
)
->
bool
:
"""
return True if self is strictly superior to card
False otherwise
"""
return
self
.
compare
(
card
)
==
1
def
__ge__
(
self
,
card
:
Card
)
->
bool
:
"""
return True if self is superior or equal to card
False otherwise
"""
return
self
.
compare
(
card
)
>=
0
if
(
__name__
==
'
__main__
'
):
import
apl1test
apl1test
.
testmod
(
'
card.py
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment