JoKeRooo7 / the_evolution_of_trust.game.py

as part of the study of python, a game from game theory was implemented - the evolution of trust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The evolution of trust

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

Start

To run, you will need a script that includes game methods and python at least version 3.7

Characters

I have each role represented as a separate class that inherits from the parent class Player

Player class

    class Player(object): ...

Attributes of the Player class

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.

Initializing the Player class

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.

Methods of the Player class

  • choice_action(other_object) The choice of an action is represented by the choice_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 the self.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 the self.action variable
  • get_role() Returns the current character role
  • reset() Resets the counter, changes the last role

Role

All roles completely inherit the [Player class](#player class)

  • Cheater
    class Cheater(Player): ...
    Initializes the steal action immediately. And all further actions will only be stealing.
  • Cooperator
    class Cooperator(Player): ...
    Initializes the cooperate action immediately. All further actions will always cooperate.
  • Copycat
    class Cooperator(Player): ...
    Starts with cooperation, repeats the action of the character made in the last turn.
  • Grudger
    class Grudger(Player): ...
    He starts with cooperation, but if the player has stolen at least once, he forever changes his actions to theft
  • Detective
    clas Detective(Player): ...
    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.
  • Rat
    class Rat(Player): ...
    Added by me as an unfair role, he knows the maximum number of rounds (it is accepted at __init__), if this is the last move, the final action will always be to steal.

Points in the game

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

Game Class

class Game(object):...

It is needed to launch and configure the game.

Initializing the Game class

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.

Methods of the Game class

  • 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 the self.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

Launch example

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()

About

as part of the study of python, a game from game theory was implemented - the evolution of trust


Languages

Language:Python 100.0%