During the course of our lives, it is inevitable that we will meet people. Most will end up as acquaintances, some will stay as friends. Out of these friends, some may be difficult to handle, others may be easy. The easy ones are not a problem, they are understanding and you can talk to them about any issues, i.e. if they do things that annoy you, you can bring it up and they would be understanding enough to stop annoying you.
However, almost always you will encounter difficult friends, friends that you want to keep but are difficult to handle. Difficult in the sense that you can’t talk sense into them, and although you try to be nice and understanding to them, the favour is not returned and they annoy you for no reason (or worse, for a reason!) and simply would not stop being annoying.
In this situation, most people do not know what to do. One, you could just stop befriending them. Or two, you could try and tolerate their behaviour. Whatever it is, the above choices may not be the optimal choice; you may not want to lose that friend, and at the same time you may not want to tolerate that sort of (mis)behaviour forever.
If you face such a problem, you may want to consider using “Tit for tat”.

What is “Tit for tat”, you may ask. “Tit for tat” is a highly effective strategy in iterated prisoner’s dilemma. Before you go “Wha-?”, let me explain without getting too much into the technical details.
Prisoner’s dilemma is a problem in game theory. Now, game theory is a branch of applied mathematics which mainly tries to mathematically capture behaviour in strategic situations. According to Wikipedia:
While initially developed to analyze competitions in which one individual does better at another’s expense (zero sum games), it has been expanded to treat a wide class of interactions, which are classified according to several criteria. Today, “game theory is a sort of umbrella or ‘unified field’ theory for the rational side of social science, where ‘social’ is interpreted broadly, to include human as well as non-human players (computers, animals, plants)” (Aumann 1987).
To put it simply, game theory can be used to analyze complex human behaviour, especially interaction between individuals in different scenarios. Prisoner’s dilemma is one such scenario, which simulates interaction between two mutually exclusive individuals. Mutually exclusive here means that the two individuals make decisions independently of each other, which also mean that they don’t talk to each other. You can see how this relate to your relationship with your “difficult friend”, you both make decisions that affect each other without consulting each other. In other words, you can’t talk or communicate to the “difficult friend”.
Now, iterated prisoner’s dilemma simply means that the interaction between the two individuals are a prolonged process, and not a one-time event, which is similar to your interaction with your “difficult friend”, as your friendship is over a period of time, which may include many iteration of a prisoner’s dilemma event.
Back to “Tit for tat”, it is a highly effective strategy in iterated prisoner’s dilemma. When used over a large number of competitions, it has the highest odds of scoring the highest marks compared to other strategies. It is a simple strategy devised by Anatol Rapoport, containing only 4 lines of BASIC. The strategy is simply to cooperate with the opponent (in our case, the “difficult friend”) in the first round, and after that, do what the opponent does the previous round. If the opponent cooperates (be nice to you), you cooperate in the next round, if the opponent defects (treats u badly), you defect the next round.
Translated into our interaction with the “difficult friend”, according to the strategy, you should always be nice FIRST. After that, if your “difficult friend” are nice, you can be nice back. But if your “difficult friend” is mean, you be mean back. Simple as that.
The strategy may sound a little too simplistic at first, but you shouldn’t take it too literally. Being mean here does not necessarily mean you have to literally be mean to your “difficult friend” and box him in the face. What I interpret here is, we should always be nice to a friend in the beginning to show goodwill, and after that, if they are nice, we can be nice in return. However, if they start to be mean and do not return the goodwill, then you should cease to be nice. You should not be a doormat, and you need to send the message across that you can’t be bullied. As to how to do that, I’ll leave it to you to decide, but I’m sure you get the point.
Note: I do not recommend using this strategy in dealing with your spouse or loved ones. This is simply because it can easily turn into revenge attacks and snowballing resentment, which may cause irreversible damage and implode a relationship so much that it will finally break. This strategy should only be used in scenarios that the two individuals are mutually exclusive, i.e. between you and a difficult friend. For spouses or loved ones, they cannot be considered as mutually exclusive. If you consider your spouse or loved one as mutually exclusive from you, you two may have a bigger problem than our topic at hand, and you may want to consider counselling instead..
Good luck!