In Depth Post 6: The Last Code

Welcome back to my last blog post before In Depth night! Looking back at this year, it’s been a pretty hectic year. It’s been a rough year for everyone around the world, so give yourself a quick pat on the back for making it this far. Now in terms of In Depth, I’ve been having a blast learning how to code from my mentor. I’ve come a really long way from not knowing anything about coding what so ever to knowing enough to even create short programs and games through code.

This week, my mentor and I worked on a quick game inspired by the prisoner’s dilemma. I would assume most people would know what this is by now, but for those of you who don’t, let me give you a quick run down on what this is. The prisoner’s dilemma is a pretty famous logic game that shows how two rational participants may not always cooperate with one another. The scenario goes something like this:

Two members of a criminal gang are arrested and imprisoned. Each prisoner is in solitary confinement with no means of communicating with the other. The prosecutors lack sufficient evidence to convict the pair on the principal charge, but they have enough to convict both on a lesser charge. Simultaneously, the prosecutors offer each prisoner a bargain. Each prisoner is given the opportunity either to betray the other by testifying that the other committed the crime, or to cooperate with the other by remaining silent. The possible outcomes are:

  • If A and B each betray the other, each of them serves two years in prison
  • If A betrays B but B remains silent, A will be set free and B will serve three years in prison
  • If A remains silent but B betrays A, A will serve three years in prison and B will be set free
  • If A and B both remain silent, both of them will serve only one year in prison (on the lesser charge).

So basically what we did this week was create a program that will play this “game” against you. You are able to select how many rounds to have, and you person with the least about of “years” (penalty points) wins. Now, let’s do what we usually do and go over the code!

This portion is the introduction that basically says “hello” and asks how many rounds you want to participate in. The cout function has been explained in most of my other posts, so I’ll be focusing more on any of the new information from this month. I’ve also added a quick if statement that makes sure that you can’t enter any number that’s less than or equal to 0 since that’s not very realistic.

Next in the code are some of the variables that we’ll be using. We have a counter that will help us keep track of which number round we’re on, pscore which is just the player’s score, cscore which is the computer’s score, pmove which is the player move, and previous move. I’ll be explaining the purpose of previous move later on, so just keep the last two lines in mind for now.

We now have the beginning of the meat of our code. We start off with a while statement that will basically repeat the code inside until a specified condition is met. In this case, we set it so that as long as the counter (Which is counting which round we are on) is less than or equal to the number of rounds that the player has set, the code will repeat. Then, inside the while statement, we have the round number, and the question that asks if you will cooperate, or defect.

We now have the important portion of the while code. As I explained earlier, there are a total of four different outcomes that could occur in this game, either you both cooperate, person A cooperates and person B does not, person B cooperates and person A does not, or you both do not cooperate. So, we created four different if statements so that the program will know what to do in each scenario. For example, take a look at the first one. If both you and the computer cooperate, you will both gain 1 penalty point each, and the program will state what happened.

Then at the very end of the while loop, we have a small piece of code that basically determines the algorithm that the computer follows in order to make its move. Remember the previous move variable that I asked you to remember near the start? Well, that is how the program determines what move to make next. It uses a strategy called the tit for tat strategy where you start by cooperating, and simply copy your opponent’s last move for every move afterwards. This is a strategy that has been proven to work well through many studies. This is why near the beginning of the code, the previousmove variable had been set to c, which basically means cooperate. So after each round, the previousmove variable will be set to what ever the player move was that round. Then, the counter will go up by one to signal that a round has gone by.

Finally, we have the end where the scores of each side are announced, and a quick if statement determines whether you won, lost, or tied.

All this code accumulates into this quick program:

Looking at the path of my learning, it started off very linear as I didn’t have the knowledge or skill to have many options. My mentor focused more on giving me the proper theory so that I could get some sort of start. After some time, my mentor began to provide me with choices and alternatives. More paths started to open up as I got a choice in which projects or assignments I wanted to do. However, my choices are usually limited to two choices since my mentor felt most comfortable teaching projects that she herself had done in the past. So, having a much older, and experienced mentor may have provided me with much more alternatives for learning, and many more choices for my projects. However, I’m still very content with my current mentor who had worked very hard to teach me so much regarding coding in c++. She’s easy to talk to and helps make sure I thoroughly understand all the topics at hand. If it wasn’t for her, I probably wouldn’t have been able to even explain my code like I have been in all of my blog posts.

Speaking of choices and alternatives, for my final In Depth project, my mentor gave me two different projects to choose from. One is much more ambitious than the other, but has a lot more potential and fun included. I don’t want to spoil the surprise, so I’m afraid I’m going to have to keep it a secret. The other option is more of a backup option for if the previous choice doesn’t work out. But I’m definitely going to be working hard to produce my envisioned results for the end. So, I’ll see you next time at the In Depth night. I look forward to producing my biggest project yet!

– MJ


Leave a Reply

Your email address will not be published. Required fields are marked *