CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 20 - Problem Solving


As usual, create two directories for today's class. Create a directory called lecture20 under activities, and a directory called lab20 under labs.


Randomizer!

Time to spin the wheel and find out what you will be submitting for your final portion of this weeks quiz grade.


In-Class Activity 1

Guessing Game

Create a file called guessing_game.py in your lecture20 directory. The program in this file should generate a random number, and prompt the user to enter a guess of the number. The user should be allowed to keep guessing until they get the guess correct.

Your program should indicate if the users guess is higher or lower than the randomly selected value.

Challenge After the user guesses twice, your program should indicate whether the user is getting warmer or colder compared to their previous guess.

Lazy Programming

I've been forcing you to do a lot of things this semester, and most of you are not sure exactly why. Let's spend some time exploring how to approach solving problems.


In-class Activity 2

Point in Triangle

Read the following mini-assignment prompt. I will go through the "lazy" solution in class.

Create a program that draws a triangle and a random point and tests if the point is inside the triangle. The program should draw a triangle that takes up the majority of the window. You can pick the location of each of the corners, or vertices, of the triangle. The program should also generate and draw a random point in the window. The color of the circle should be determined by whether the point is inside or outside the triangle.

A point is inside a triangle if it is on the interior side of all three edges of the triangle. For example, the point P in the illustration below is on the interior side of the edge from B to C and the edge from C to A, but it is not on the interior side of the edge from A to B.

It is possible to determine which side of an edge a point is on using the equation:

s = (V1x − V2x) × (V1y − Py) − (V1y − V2y) × (V1x − Px)

Where V1 and V2 are vertices that define an edge of the triangle, and P is a point. If the vertex V1 is clockwise from V2, then s will be positive if P is on the interior side of the line segment between V1 and V2.


Lab Assignment 20

Prisoners Dilemma

The Prisoners Dilemma is a classical psycological experiment that has deep ties to game theory as well. It was originally proposed in the 1950's, but research in the field continues to this day.

In the Prisoners Dilemma, two prisoners (A and B) are pitted against each other. Both prisoners are given the same options: confess to the crime, or deny the crime. They are even given the following information:

Write a program prisoners_dilemma.py where you can play the prisoners dilemma against a random computer. Your program should keep track of how many years you (and the computer) serve in each round. After some number of pre-defined rounds, print the average number of years served by you, and the average number of years served by the computer prisoner.

Make sure you test your program well. How many test cases do you need? Include your test cases in your program file. Comment them appropriately.

Challenge As you have no doubt seen, the random strategy for the computer prisoner throws a kink into any strategy you attempt to perform. A random prisoner does not really represent a rational actor in this simulation. The good news is there are known stretegies for the iterated prisoners' dilemma. One strategy is known as tit-for-tat, where the prisoner always acts in the same way their opponent did in the previous round. Implement this strategy for prisoner 1, and have this strategy play the random prisoner 2. Who wins in this case?

Submission

When you have finished, create a tar file of your lab20 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czvf lab20.tgz lab20/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 20. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your files.

Do not forget to email your partner today's files!


Last modified: Fri Oct 11 13:31:51 EDT 2013