QuestionQuestion

Transcribed TextTranscribed Text

Memory Game You may have played the pair memory game as a kid. You are given a grid and are allowed to uncover 2 cells at a time showing you what is underneath. Each cell has a matching value somewhere else on the board, so the player will want to remember where they uncovered a value at for later. Classes You will have to implement a couple of classes in this creating the classes with methods as given will help quite a bit when it comes to finishing up this program. Consult the solution for the methods and definitions. You can add extra methods or attributes as needed. Sample Program >>> RESTART >>> How difficulty should the game be? (1, 2, 3) ==> badinput You must enter an integer from 1 to 3 How difficulty should the game be? (1, 2, 3) ==> You can only enter 1 2 or 3 for the difficulty How difficulty should the game be? (1, 2, 3) ==> 4 You can only enter 1 2 or 3 for the difficulty How difficulty should the game be? (1, 2, 3) ==> 1 Memory Board 1 2 3 4 1 * * * 2 * * * 3 Enter the first row, col to uncover. Separated by commas ==> no comma Enter the first row, col to uncover. Separated by commas ==> no, comma 3 * * * Enter the first row, col to uncover. Separated by commas ==> no comma Enter the first row, col to uncover. Separated by commas no, comma The row must be an integer. Please try again Enter the first row, col to uncover. Separated by commas ==> 1, comma The col must be an integer. Please try again Enter the first row, col to uncover. Separated by commas 1, 5 Error : Col must be less than or equal to 4 Enter the first row, col to uncover. Separated by commas 1, 1 Memory Board 1234 1 E * * 2 * * * * 3 * * Enter the second row, col to uncover. Separated by comma ==> 1, 1 Error : 1, 1 is already being shown Enter the second row, col to uncover. Separated by comma ==> 1, 2 Memory Board 1234 1 LEC** 2 * * * 3 * * Memory Board 1234 1 * 2 * 3 * * * * Memory Board 1 2 3 4 1 * * * 2 3 Enter the first row, col to uncover. Separated by commas ==> 1,3 Memory Board 1231 4 1 |**D* 2 3 Enter the second row, col to uncover. Separated by comma ==> 1, 4 Memory Board 1234 1 **DB 2 * * 3 Memory Board 1234 1 * * 2 3 Enter the first row, col to uncover. Separated by commas ==> 2, 1 Memory Board 1234 1 2 * * * 3 * * Enter the second row, col to uncover. Separated by comma ==> 2, 2 Memory Board 1234 1 * * 2 B * * 3 * Memory Board 1234 1 2 * 3 Enter the first row, col to uncover. Separated by commas ==> 1, 4 Memory Board 1234 1 * * * B 2 * * * 3 * * Enter the second row, col to uncover. Separated by comma ==> 2,2 Memory Board 1 2 3 4 1 ***** 2 B * * 3 * * Memory Board 1234 1| **** 2 * * * * * 3 * * Enter the first row, col to uncover. Separated by commas ==> 2,3 Memory Board 1234 1 **** 2 * B A * 3 * * Enter the second row, col to uncover. Separated by comma ==> 2, 4 Memory Board 1234 1 ***B 2 * B A C 3 * * * Memory Board 1234 1 **** 2 * * * * * 3 * * Enter the first row, col to uncover. Separated by commas ==> 2, 2 Error : 2, 2 is already being shown Enter the first row, col to uncover. Separated by commas ==> 1,2 Memory Board 1234 1**** 2 * B * * Enter the first row, col to uncover. Separated by commas ==> 2, 2 Error : 2, 2 is already being shown Enter the first row, col to uncover. Separated by commas ==> 1, 2 Memory Board 1234 1 * C * B 2 * B * * 3 * * * Enter the second row, col to uncover. Separated by comma ==> 2, 4 Memory Board 1234 1| * C * B 2 * B * C 3 * * * * Memory Board 1234 1 * C * B 2 * B * C 3 * * Enter the first row, col to uncover. Separated by commas ==> 3, 1 Memory Board 1234 1 * C * B 2 * B * C 3 F * * * Enter the second row, col to uncover. Separated by comma ==> 3,2 Memory Board 1234 1**** 2 * B * C Enter the second row, col to uncover. Separated by comma ==> 3,2 Memory Board 1234 1 I C C B B 2 B * C 3 D * * Memory Board 1234 1 I * C * B 2 B * C 3 * Enter the first row, col to uncover. Separated by commas ==> 1,1 Memory Board 1234 1 JEC*B 2 * * * * * 3 * * Enter the second row, col to uncover. Separated by comma ==> 3, 3 Memory Board 1234 1EC*B 2 * * * * * C 3***** Memory Board 1234 1EC*B 2 * * * * C 3| * E * Enter the first row, col to uncover. Separated by commas ==> 3,4 Memory Board 1234 1EC*B 2 * * * * C 3***A Enter the second row, col to uncover. Separated by comma ==> 3,1 Memory Board 1234 1EC*B 2 * * * * * 3 F * E A Memory Board 1234 1EC*B 21*B*C 3***** Enter the first row, col to uncover. Separated by commas ==> 1, 3 Memory Board 1234 1 ECDB 2 * * * * * C 3 E * Enter the second row, col to uncover. Separated by comma ==> 3,2 Memory Board 1234 1ECDB 2 3 * DE * Memory Board 1234 1 E C D B 2| * * * * C 3| * Enter the first row, col to uncover. Separated by commas ==> 2, 1 Memory Board 1234 1ECDB 2FB*C 3 * D E * Enter the second row, col to uncover. Separated by comma ==> 3,1 Memory Board 1234 1 LECDB 2 FB* C 3 * Memory Board 1234 1ECDB 2 * C 1 2 3 4 1ECDB 2 F B * C 3 I F D E * Memory Board 1234 1 JECDB 2 F B * C 3 I F D E * Enter the first row, col to uncover. Separated by commas ==> 2, 3 Memory Board 1234 1ECDB 2 F F A A C 3 F D E E * Enter the second row, col to uncover. Separated by comma ==> 3, 4 Memory Board 1234 1ECDB 2 IFBAC 3 F D E A Do you want to play again? Y/YES/N/NOy How difficulty should the game be? (1, 2, 3) ==> 3 Memory Board 123456 1.1 Do you want to play again? Y/YES/N/NOy How difficulty should the game be? (1, 2, 3) 3 Memory Board 1 2 3 4 5 6 1 * * * 2 * * * * 3 * * * * 4 * * Enter the first row, col to uncover. Separated by commas ==> Specification Point Breakdown - May be modified as needed Points Requirement 5 Header 20 Readability, variable naming, comments, working program 5 Proper functions 5 Cell init setup and working properly 5 Cell method working properly 5 Cell str method working properly 5 Cell flip method working properly 5 Board init setup and working properly 5 Board str_ method working properly 5 Board validate_choice method working properly 5 Cell eq_ method working properly 5 Cell str method working properly 5 Cell flip method working properly 5 Board init setup and working properly 5 Board str__ method working properly 5 Board validate_choice method working properly 5 Board play_choice1 method working properly 5 Board play_choice2 method working properly 5 Board end_turn method working properly 5 Board game_over method working properly 5 Difficulty level is validated to ensure that it is an integer and is 1, 2 or 3 5 Row column input is validated that is separated by a comma and each can be turned into an integer. 5 Asks the user if they want to play again and validates appropriately only values of Y/YES/N or NO. We are going to use python to help us calculate the area under a curve. ( Do not use to cheat in Calculus ). For instance the following function is for f(x) = x2 shown in Figure 1. 1 0:5 05 a b Figure 1 We can estimate the area under this curve from a to b, by calculating the area of a rectangle. The area of a rectangle is simply area = width * height. We can use the height of the function at the midpoint of a and b The midpoint of a and b above is 0.75. the value of 0.752 2 is 0.5625. The width is 0.5, so the area from a to b is 0.28125 as shown in figure 2 0.5 05 a b Figure 2. That's not a very good estimate, so we can split it into 2 rectangle. one going from 0.5 to 0.75 and another from 0.75 to 1. Rectangle 1 Center point is at 0.625 and f(0.625) is 0.390625, so the area is 0.25 * 0.390625 which means rectangle 1 has an area of 0.09765625. Rectangle 2 center point is at 0.875, and the f(x) at 0.875 is 0.765625. So the area of Rectangle 2 is 0.19140625. The total of both rectangles is 0.2890625 which is a closer estimate of area under x2 Figure 3 shows the 2 rectangles. 1 0:5 05 a b Still not a great estimate, but if we do 1000 rectangles our estimate gets closers. With 1000 rectangles we estimate the area under the curve as 0.29166665625 and the actual area is 0.291666666666666. How do we know when to stop. If we double the number of rectangles each time, we can get the difference between the last 2 calculations. Once they are under a certain tolerance value we can consider it close enough. f(x) = 2 # of Rectangles Estimate of area absolute value of difference from previous 1 0.28125 2 0.2890625 0.0078125 4 0.291015625 0.001953125 8 0.29150390625 0.00048828125 Calculate the midpoint midpoint = (a+b)/2 Eval We need to get the function from the user, but how to execute it. Python has an eval function that can be used to run the evaluation from a string. user function = input ("Enter a function ") Enter a function x**2 x = 1 &gt;&gt;&gt; eval (user_function) 1 X = 0.5 &gt;&gt;&gt; eval (user_function) 0.25 Ln: 240 Col: 0 It takes some practice, but you can see, you set a value for X, and run the eval of the string and it will evaluate the expression and return a result. If the user enters something erroneous an error will be generated. You can use try except to catch these elegantly. user function = input ("Enter a function ") Enter a function + X = 5 eval (user_function) Traceback (most recent call last) : File "<pyshell#69>" line 1, in <module> eval (user_function) File "<string>", line 1 SyntaxError: unexpected EOF while parsing Ln: 255 Col: 4 </string></module></pyshell#69>Sample Program ( Notice the error handling for input and bad formulas.) RESTART &gt;&gt;&gt; This program will estimate the area under a curve. You can enter a formula in terms of X. 2*x or x**3 etc Enter the formula + Enter the value for a ==&gt; 1 Enter the value for b ==&gt; 2 Enter the value the tolerance ==&gt; 0.001 There was a syntax error with the formula you entered unexpected EOF while parsing (<string>, line 1) Do you want to calculate another? Y/N y This program will estimate the area under a curve. You can enter a formula in terms of X. 2*x or x**3 etc Enter the formula ==&gt; x**: 2 Enter the value for a a You must enter a floating point number Enter the value for a ==&gt; 0.5 Enter the value for b ==&gt; hello You must enter a floating point number Enter the value for b 0 The value for b must be greater than 0.5 </string>Enter the value for b hello You must enter a floating point number Enter the value for b ==&gt; 0 The value for b must be greater than 0.5 Enter the value for b ==&gt; 0.5 The value for b must be greater than 0.5 Enter the value for b ==&gt; 1 Enter the value the tolerance ==&gt; tolerance You must enter a floating point number Enter the value the tolerance ==&gt; 0.00001 Using 64 rectangles we estimate the area under the curve is 0.29166412353515625 Do you want to calculate another? Y/N y This program will estimate the area under a curve. You can enter a formula in terms of X. 2*x or x**3 etc Enter the formula ==&gt; 2**x + 5 Enter the value for a ==&gt; 1 Enter the value for b ==&gt; 10 Enter the value the tolerance ==&gt; 0.01 Using 1024 rectangles we estimate the area under the curve is 1519.432051710278 Do you want to calculate another? Y/N y This program will estimate the area under a curve. You can enter a formula in terms of X. 2*x or x**3 etc Enter the formula ==&gt; 2*x**2 + X + 5 Enter the value for a ==&gt; 1 Enter the formula ==&gt; 2*X**2 + X + 5 Enter the value for a ==&gt; 1 Enter the value for b ==&gt; 2 Enter the value the tolerance ==&gt; 0.000001 Using 1024 rectangles we estimate the area under the curve is 11.166666507720947 Do you want to calculate another? Y/N e Enter either Y or N Do you want to calculate another? Y/N n Thanks for using The Estimator &gt;&gt;&gt;

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import string
import random

class Cell(object):

    def __init__(self, value):
       """
       :param value: The value that is currently in the cell.
       Initializes the cell with the value and sets it to not visible.
       :return:
       """
       self.value = value
       self.visible = False
   
    def __eq__(self, compare):
       """
       :param compare: Returns True if both instances have the same value. False if not
       :return:
       """
       return self.value == compare.value

    def __str__(self):
       """
       :return: Returns the String representation of the cell.
                * if it is not visible.
                The value if it is visible.
       """
       if self.visible == True:
            return self.value
       else:
            return '*'

    def flip(self):
       """
       Flips over the cell. Shows it, it if it was hidden. Hides it, if it was shown.
       :return: None...

By purchasing this solution you'll be able to access the following files:
Solution1.py and Solution2.py.

$80.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats