CPSC120A
Fundamentals of Computer Science I

Lab 13

Graphics

Use the command line to create a new directory called lab13 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Point In Circle

Earlier this semester we estimated pi using the Leibniz approximation. Another way of estimating the value of pi is to use Monte Carlo Estimation. The technique is named after the casino because it depends on random chance and probability. Before you can use the technique to estimate pi, you need a test for whether a randomly generated point is inside of a circle.

Details

In a file called point_in_circle.py, write a function called is_point_in_circle(point_x, point_y, circle_x, circle_y, circle_radius) that returns True or False depending on whether the specified point is inside the specified circle. Consider exactly on the edge of the circle as outside the circle. The parameters point_x and point_y are the x and y coordinates of the point. The parameters circle_x, circle_y, and circle_radius are the x and y coordinates of the center of the circle and the radius of the circle.

Make sure to test your function by calling the function multiple times with different parameters. Make sure your code follows the course's code conventions.

Test Cases

Function Parameters Expected Output
1, 1, 0, 0, 2 True
0, 2, 0, 0, 2 False
2, 2, 0, 0, 2 False
-7, -7, -10, -10, 5 True
-5, -10, -10, -10, 5 False
-6, -6, -10, -10, 5 False

Hint

A point is inside of a circle if the distance between the point and the center of the circle is less than the radius of the circle.

The distance between two points can be computed using the Euclidean Distance formula:

$$d = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$$

Where \(d\) is the distance between the points \((x_1, y_1)\) and \((x_2, y_2)\).

Challenge

Use turtle to draw a circle and hundreds of randomly generated points. If a point is inside the circle the point should be drawn as a green dot, if it is outside the circle it should be drawn as a red dot.

Rock, Paper, Scissors

Many a momentous school yard decision, such as who is it, has been decided with the game Rock, Paper, Scissors. In case you don't know, Rock, Paper, Scissors is played by two players picking one of three choices: rock, paper, or scissors. The winner is determined by the rules:

  1. Rock crushes scissors.
  2. Scissors cuts paper.
  3. Paper smothers rock.

If the two players both pick the same thing, the game is a tie.

Details

In a file called rock_paper_scissors.py, write a function called play_rock_paper_scissors() that plays a single round of the game Rock, Paper, Scissors. The function should prompt the user to enter their play, generate a random play for the computer, and print who won (and why). The user should enter their input as either 'r', 'p', or 's'. If the user enters invalid input the program should inform the user.

Exmaple

$ python3 rock_paper_scissors.py
Enter your play(r, p, or s): r
Computer play is s.
Rock crushes scissors, you win!
$ python3 rock_paper_scissors.py
Enter your play(r, p, or s): p
Computer play is s.
Scissors cut paper, sorry, you lose.
$ python3 rock_paper_scissors.py
Enter your play(r, p, or s): s
Computer play is s.
A tie, nobody wins or loses.
$ python3 rock_paper_scissors.py
Enter your play(r, p, or s): t
Sorry, I don't understand the move 't'.

Hint

Getting the user's input is as simple as using the input function. Don't forget to include the prompt for the user.

Generating the computer's move is a little more tricky because you need to use the randint function to generate a string. Do this by generating a random number between 0 and 2 and assigning the computer's move to a variable based on the random number. For example, if the random number is 0, make the computer's move 'r'. If the random number is 1, make the computer's move 'p', etc. Make sure you don't use Magic Numbers to accomplish this.

Testing who won requires having conditional statements for the different cases the game can end in. There are 3 ways the player can win, 3 ways the player can lose, and 3 ways for a tie to occur. These can be checked with nested if statements, but I think you will find it easier if you create one conditional for each of these situations using Boolean operators. For example, to check if the user won with rock the conditional needs to check the the user's move is 'r' and the computer's move is scissors.

Challenge

The game Rock, Paper, Scissors can be generalized to any odd number of possible moves. The more possible moves there are the lower the chance of a tie. One popular 5-move version is Rock, Paper, Scissors, Lizard, and Spock. The regular Rock, Paper, Scissor rules apply with the additional rules:

  1. Rock crushes lizard
  2. Paper disproves Spock
  3. Scissors decapitate lizard
  4. Lizard poisons Spock
  5. Lizard eats paper
  6. Spock smashes scissors
  7. Spock vaporizes rock

Modify your Rock, Paper, Scissors function to play Rock, Paper, Scissors, Lizard, Spock.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.