Program where the computer plays against a human opponent

Assignment Help Programming Languages
Reference no: EM13192439

Part 1

You will implement a program where the computer plays against a human opponent. The game will start with a random number of marbles between 10 and 100. A random number between 0 and 1 will be used to decide who takes the first turn (0 computer, 1 human opponent) and the human opponent has to decide whether he or she wants the computer to play in a novice or expert mode. In the novice mode the computer will always take a random number between 1 and half of the marbles that there are in the pile at the moment whenever it is its turn. In the expert mode the computer always takes enough marbles to make the number of marbles left on the pile a power of 2 minus 1 (e.g, 3, 7, 31) except if the size of the pile is already one less than the power of 2. In that case the computer will take a random number of
marbles. Note that the computer will always win in expert mode if it has the first turn but also a human opponent will always win if he or she knows the winning strategy.

Part 2

Once the game has been implemented you will use it for a Nim on-line Man-Machine competition. Every time that the game is played the result of the game will be updated in a text file. The file consists of two lines: the first line stores the times that the computer has won and lost and the second line the times that any human user has won and lost. At end of the competition the winner will be displayed. To make it more realistic you can ask some of your friends to play the game without telling them the winning strategy.

Question 1

Write a function playNovice(marbles) that given the number of marbles returns a random number between 1 and half of the number of marbles.

>>> playNovice(90)

35

Test your function several times with numbers between 10 and 100

Question 2

Write a function userPlay(marbles) that asks the user for the number of marbles that he or she would like to take and returns that number. The function should keep asking the user for the number of marbles if he or she takes more than half of the marbles.

Question 3

Write a program that creates one pile of marbles with a random number of marbles and decides who starts the game. The program will call userPlay when the user plays and playNovice when it is the computer turns.

Question 4

Modify the code of question 3 so the program keeps playing until there is only one marble left and inform at the end who has won the game.

Test that the program works by displaying the output of running the program.

Question 5

Write a function playExpert(marbles) that given a number of marbles returns the number of marbles that the computer will take following the expert mode rules. The number of marbles left on the pile will be a power of 2 minus 1 (e.g, 3, 7, 31) except if the size of the pile is already one less than the power of 2. In that case the computer will return a random number of marbles.

>>> playExpert(90)

27 (if computer takes 27 there will 90-27= 63 (26-1) marbles left )

>>playExpert(31) number of marbles a power of 2 minus 1

Question 6

Amend the program in question 4 so the user decides whether the computer plays in novice or expert mode.

Test that the program works by showing the results of playing using the two different modes.

Question 7

Update question 6 (or question 4 if you did not manage to implement playExpert) and a create a function called gameNim() that plays the game and returns 0 if the computer lost and 1 otherwise. 

Question 8

Now you have to create a list called scores that stores 4 numbers: the first two numbers are the number of win and lost games for the computer and the other two for the human opponent. Create a text file called scores.txt with notepad with two lines: the first two lines are the computer scores and the second line human scores.

776_Computer programming.png

Write a function called initScores to open the file and returns the list scores with the numbers in the text file.

>>initScores(scores)
[0,0,0,0]

Test it by adding numbers manually in the text file

Question 9

Now write a function updateScores(scores) that saves the information on the list scores to the text file scores.txt

Question 10

Write a program that initializes the list scores with the values in the text file scores.txt and calls the gameNim() function, once the game has finished the list scores are updated with the results. The program will play as many games as the users wants. Once the user stops playing the scores.txt file is updated. That means that if you play the games again later, the scores list will start with the values stored in the file and not with all the scores to 0

Question 11

Add a function to display who has won the competition - the machine or the human opponents

Question 12: Analysis and results

Write a report about your final implementation and the results of testing the game. You should talk about:

- Whether or not you have managed to implement the playExpert mode of the game. If you do not manage to implement it you should carry on with the rest of the questions in novice mode.

- Any errors in your program.

- The testing of the final game by displaying the results of running the final game once.

- Results of the competition you should answer questions like: how many times did you play the game? How many people try the game? What can you extract from the final results? Is it the computer better than the human opponent?

Reference no: EM13192439

Questions Cloud

What are the probs and cons of a greek exit from euro area : Explain why a country with a large primary goverment budget deficit and high government debt can end up in a vicious cricle where government debt tends to increase faster and faster. Use this analyse the current situation in Greece.
Explain the differences between liberalism and conservatism : Explain the differences between Liberalism and Conservatism. Does ideology really make a difference in the United States today? How does this affect the two party system?
Depict the complete lewis dot structure : Draw the complete Lewis Dot Structure, and identify for each molecule the shape, the hybridization on the central atom
Write the null hypothesis and the alternative hypothesis : For the following statement, write the null hypothesis and the alternative hypothesis. Then, label the one that is the claim being made.
Program where the computer plays against a human opponent : Write a function playExpert(marbles) that given a number of marbles returns the number of marbles that the computer will take following the expert mode rules.
American history few would select 1919 : If asked to name one of the most troubling years in American history few would select 1919. Many think that after World War I, the “war to end all wars,” the United States was a peaceful and prosperous place because the hostilities ended.
Find the volume of the resulting solid : A ball of radius 11 has a round hole of radius 7 drilled through its center. Find the volume of the resulting solid.
What is differences between absolute ppp and relative ppp : Explain what it meant by liquidity trap.Use the interest parity condition to show how the exchange rate is determined in such a situation. What can monetary policy so in such a situation
How long will they each have the same amount of money : currently, tyrone has $60 and his sister has $135, both ger an allowance os $5 each week. tyrone decide to save his entire allowance, but his sister spends all hers each week plus an additional $10 each week. after how long will they each have the..

Reviews

Write a Review

Programming Languages Questions & Answers

  Program accept word from user and pass it to function

Your program should accept the word from user and pass it to function. Function requires to check if word is "freezing". If it is, program must alert user.

  Write program to prints question do you want to continue

Write a program which prints question"do you want to continue?"and reads user input. if user input is"y", "yes", "ok", "sure", or "why not?" , print out "ok".

  Program to calculate area of two-dimensional shape

Implement the Shape hierarchy shown in following figure. Each TwoDimensionalShape should contain method getArea to calculate the area of the two-dimensional shape.

  Design logic for application for company to store breakdown

Design the logic for an application for a company that wants a report containing a breakdown of payroll by department. Input includes each employee's last name.

  Generate an object-oriented design for a system

Generate an object-oriented design for a system that keeps tracks of your CD and DVD collection. Identify each of the classes, associated data, and operations for the classes.

  Write loop header to hold odd number between a range

Suppose the int variables i and result have been declared but not initialized. Write down for loop header, that is something of form.

  Design program which uses array of shape

Design a program which uses the array of Shape references to objects of each concrete class in hierarchy. The program must print text description of object to which each array element refers.

  Program to compute net pay of employee

A program to compute net pay of employee. It must permit one to read deposit number, account name, amount deposited, and yesr.check.

  Create logic for program to count readers by genders

Create the logic for a program that would produce a count of readers by gender within age group - that is, under 20 females, under 20 males, etc.

  Write program to choose female student-completed more course

Suppose that input consists of a student file. You are told to write a program to select female students who have completed more than two courses.

  Technique of multiplexed switching of analogue signals

Explain briefly from what you notice about the digital output values in (a) why a digital output value of an A/D converter is rarely the exact same digital value as the analogue input value.

  Write program to read data for employee and print name

Write a program which reads data for employee and prints name and salary of employee. Data read is: Name. Number of hours worked.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd