An Introduction
Many of you will be familiar with the famous game/paradox Prisoner's Dilema which is almost invariably used as an example when begining to understand game theory but for those that are not the Prisoner's Dilema goes as such:
"Two suspects are arrested by the police. The police have insufficient evidence for a conviction, and, having separated both prisoners, visit each of them to offer the same deal. If one testifies (defects from the other) for the prosecution against the other and the other remains silent (cooperates with the other), the betrayer goes free and the silent accomplice receives the full 10-year sentence. If both remain silent, both prisoners are sentenced to only six months in jail for a minor charge. If each betrays the other, each receives a five-year sentence. Each prisoner must choose to betray the other or to remain silent. Each one is assured that the other would not know about the betrayal before the end of the investigation. How should the prisoners act?"
On first consideration the answer seems obvious because they would be both better off if they remained silent. However, as there is no garuntee between the prisoners and they can't communicate because they have been seperated each must act in their own interest. The 'answer', or rather the solution attained from the Nash Equilibrium, is that they should always defect.
This is because when choosing to remain silent the possibilities are that they serve 6 months (or s 0.5 years) or serve 10 years in jail which totals 10.5 years - an average of 5.25. On the other hand, if they choose to defect they may spend 5 years in jail but they may also walk free (0 years) so it totals only 5 years - an average of 2.5. So, assuming that both prisoners are logical, they will both end up spending 5 years in jail where they could have spent 6 months!
Of course, all of what has been written above is theoretical and in real life there would be many other factors which would contribute to the decision (personal intregity?) but we are going to test this theory that it always best to defect, but rather than using real-life cases, we're going to use our programming experience.
Testing our theory
We are going to create two players, both of them computer controlled, who will pick to defect or co-operate randomly but at probabilities which we specify - this way we can make one nice player and one harsh player and see who prospers. If this game was only played the once it would not reflect the true outcomes of different 'personalities' so we are going to have our players repeatedly play the game over 1000 times.
Posted below is the commented code which i created as an adequete demonstration
Wednesday, 22 April 2009
Tuesday, 14 April 2009
Reference Guide
Here is a short reference for the most common JustBasic commands and operations you will be using. It is not intended to replace the built-in help but it may be simpler and easier to understand as an example is included with each definition.
print
The print statement is used to display text to the screen of the main window. It is possible to print both a literal string ie. text that you specifiy yourself using quotations or speech marks:
print "i am a print statement"
It is also possible to print a variable that may contain text the user has entered or to print the result of a mathmatical operation:
print text$
print 17x34
With the correct use of semi-colons the following three examples may be combined in one print statement
print "Thank you ";name$;"the answer to your sum is ";usernumber * 4
input
The input command pauses the program, displays a message and waits for input from the user which is then stored in a variable:
input "Enter your age";age
input "What is your name?";name$
wait
The wait command pauses the program
wait
end
the end command ends the program but does not close any windows, this must be done manually by the use of the 'close' statement
end
if statement
An if statement
The print statement is used to display text to the screen of the main window. It is possible to print both a literal string ie. text that you specifiy yourself using quotations or speech marks:
print "i am a print statement"
It is also possible to print a variable that may contain text the user has entered or to print the result of a mathmatical operation:
print text$
print 17x34
With the correct use of semi-colons the following three examples may be combined in one print statement
print "Thank you ";name$;"the answer to your sum is ";usernumber * 4
input
The input command pauses the program, displays a message and waits for input from the user which is then stored in a variable:
input "Enter your age";age
input "What is your name?";name$
wait
The wait command pauses the program
wait
end
the end command ends the program but does not close any windows, this must be done manually by the use of the 'close' statement
end
if statement
An if statement
Saturday, 11 April 2009
Getting all the stuff you need
- Download the executable file from the JustBasic website (2.4mb) here
- Install JustBasic to your hard drive (it's easier and faster to access if youchoose C:\JustBasic as the install directory rather than hiding the files in Program Files!)
- Check that JustBasic has been installed correctly by running it if it hasn't started automatically - you should be presented with a welcome page as below:
So What Now?
There is a simple walkthrough here which looks at creating a program which calculates the chances of you calling a toin coss right n times in a row. Oh, and the program will also calculate the odds we need to set to make sure we take in the cash from our betting friends! Get started with Tails Never Fails
Those with a little more experience, who have tampered with programming before or prefer to stray away from gambling, we'll be looking at proving that evil always prevails in Prisoner's Dilema.
First Steps
If you haven't already then install JustBasic
Create Your First Program
Run the program and Select File --> New to open a new file or click the furthest left icon (a blank piece of paper)
You may notice that there is a very limited array of tools and menus and there is a dauntingly large white space but don't worry we'll have it filled with code in no time.
To prove that programming code doesn't have to be confusing and complicated have a go at understanding this snippet of code by translating it into plain English.
The code:
The first line prompts the user to enter a number and this brings up a dialog box as shown below. After entering a number and pressing OK the program stores this number in a variable called 'number'. The program then prints (displays in the main window) a string of text followed by the result of the inputted 'number' multiplied by 17.

Congratulations you've made your first program in JustBasic and maybe even your first ever program. On the down side, you didn't actually do anything apart from copy and paste and the program isn't very useful unless you want to practise your 17 times tables...
For practice i suggest that you modify the code so that two numbers are inputted by the user and the program multiplies them together and prints the answer. This will not require the knowledge of anything that hasn't already been taught (not much admittedly) and may only take a few minutes but is a valuable exercise.
See My Proposed Solution
This is slightly more useful; perhaps a remedy for calculators with the multiply button stuck inside the body of the calculator, but more probabbly not.
Next Steps
On to something more fun then: creating a program which might actually make you some money if you're friends, like most people, get a bit hazy when it comes to understanding probability.
I suggest you open this reference page and bookmark it as it will be very useful to you if you need a bit more information on understanding some of the commands in JustBasic. The included help file is also very useful unless you're running Vista where running the help file will helpfully inform you that Microsoft have decided that you're not allowed to be helped. If this happens to you then microsoft offer a solution or you can access the online help documentation for LibertyBasic (JustBasic's big sister) which is mostly identical.
Move on to your first useful program - Tails Never Fails
Create Your First Program
Run the program and Select File --> New to open a new file or click the furthest left icon (a blank piece of paper)
You may notice that there is a very limited array of tools and menus and there is a dauntingly large white space but don't worry we'll have it filled with code in no time.
To prove that programming code doesn't have to be confusing and complicated have a go at understanding this snippet of code by translating it into plain English.
The code:
prompt "Enter a number...";numberNow copy and paste the code into your JustBasic window and run the code by pressing the 'Run' button (a large 'play' button in the toolbar) or by hitting Shift-F5.
print "17 times the number you chose equals ";number*17
wait
The first line prompts the user to enter a number and this brings up a dialog box as shown below. After entering a number and pressing OK the program stores this number in a variable called 'number'. The program then prints (displays in the main window) a string of text followed by the result of the inputted 'number' multiplied by 17.

Congratulations you've made your first program in JustBasic and maybe even your first ever program. On the down side, you didn't actually do anything apart from copy and paste and the program isn't very useful unless you want to practise your 17 times tables...
For practice i suggest that you modify the code so that two numbers are inputted by the user and the program multiplies them together and prints the answer. This will not require the knowledge of anything that hasn't already been taught (not much admittedly) and may only take a few minutes but is a valuable exercise.
See My Proposed Solution
This is slightly more useful; perhaps a remedy for calculators with the multiply button stuck inside the body of the calculator, but more probabbly not.
Next Steps
On to something more fun then: creating a program which might actually make you some money if you're friends, like most people, get a bit hazy when it comes to understanding probability.
I suggest you open this reference page and bookmark it as it will be very useful to you if you need a bit more information on understanding some of the commands in JustBasic. The included help file is also very useful unless you're running Vista where running the help file will helpfully inform you that Microsoft have decided that you're not allowed to be helped. If this happens to you then microsoft offer a solution or you can access the online help documentation for LibertyBasic (JustBasic's big sister) which is mostly identical.
Move on to your first useful program - Tails Never Fails
Subscribe to:
Posts (Atom)