As part of this project, I needed to implement one popular game evolution доверия which perfectly shows game theory.
The game is presented to work in the terminal
To run, you will need a script that includes game methods and python at least version 3.7
I have each role represented as a separate class that inherits from the parent class Player
class Player(object): ...
Class attributes contain two variables:
action_one = "Cooperator"
action_two = "Cheater"
They reflect the actions that are available to the characters - to steal or cooperate.
The magic method __init__
, which is called when initializing the class, takes on the role of a character (in the form of a string) and its [next action](#attributes of the class). Also initializes the empty counters of the last role.
choice_action(other_object)
The choice of an action is represented by thechoice_action()
method, which takes as an argument the class of another character and changes its own and the number of points of another class
role_proper(other_object)
Method that is used in derived classes, to change your role (depending on the number of rounds, or moves, which were made, and so on)
add_registry(num:int)
Remembers the current role and writes it to theself.last_role
field adds the current counter to num
get_registry()
Returns a counter (can be changed outside the class)
get_lost_role()
Returns the last role
get_action()
Returns the action that is in theself.action
variable
get_role()
Returns the current character role
reset()
Resets the counter, changes the last role
All roles completely inherit the [Player class](#player class)
- Cheater
Initializes the steal action immediately. And all further actions will only be stealing.
class Cheater(Player): ...
- Cooperator
Initializes the cooperate action immediately. All further actions will always cooperate.
class Cooperator(Player): ...
- Copycat
Starts with cooperation, repeats the action of the character made in the last turn.
class Cooperator(Player): ...
- Grudger
He starts with cooperation, but if the player has stolen at least once, he forever changes his actions to theft
class Grudger(Player): ...
- Detective
He starts with cooperation, alternates cooperation and theft up to turn 5, if someone has stolen at least once during these moves, he begins to imitate, repeat the last actions. If no one has stolen anything during these 4 moves, they switch to stealing.
clas Detective(Player): ...
- Rat
Added by me as an unfair role, he knows the maximum number of rounds (it is accepted at
class Rat(Player): ...
__init__
), if this is the last move, the final action will always be to steal.
Actions:
- those who cooperate receive their own and a +1 bonus
- those who steal get points from those who cooperate and their point
- those who steal get nothing
rol1 / rol2 | Cheater | Cooperator |
---|---|---|
Cheater | 0/0 | +3/-1 |
Cooperator | -1/+3 | +2/+2 |
class Game(object):...
It is needed to launch and configure the game.
When initializing def __init__(self, matches=10)
can accepts the number of rounds that 2 players will play. By request - 10. It contains the counters self.registry = Counter()
of all the points of the characters.
game_mechanic(player1, player2)
Necessarily accepts two roles, any at your discretion. He starts a game between two characters and adds their number of points to the total score, which is stored in theself.registry
get_registry()
Returns the counter as the Counter data type
play_all_combinations()
Starts the game between all combinations of character roles.
play_with_player(player)
Running a character game against all character roles
play(player1=None, player2=None)
To start the game by default - either with everyone, all against each other, or two characters against each other
top3()
Displays the top 3 roles during the game.
reset_registry()
To clear the counter
user@pc src % python3.11 main.py
main.py
from morality import *
def main():
game = Game()
game.play(player1=Cheater(), player2=Cooperator())
game.top3()
if __name__ == "__main__":
main()