Exercise 1: Prime number speed game In this exercise you are required to impleme
ID: 3683493 • Letter: E
Question
Exercise 1: Prime number speed game In this exercise you are required to implement a simple one player multi-turn game. In each turn the player tries to enter as many prime numbers as he/she can within a pre-set time span (say 20 seconds). After each number the user inputs, the program displays the current score which is the count of correct prime numbers. In the following two cases, the program displays an error message to the player (see the red box in the sample run): - If the player enters a non-prime number, “NOT A PRIME NUMBER!!!” message should be displayed, followed by the unchanged score. - If the player enters a prime number he/she already entered during the current turn, “REPEATED – YOU LOSE A POINT!!!” message is displayed. The score is decreased by one and displayed. When a turn ends, the program should display the score. If the player achieved a new high score, a message should be displayed indicating that. After each turn, the program should ask the user if he/she wants to play another turn.
Tips and notes: 1. To control a timed game, you can use a while loop with MATLAB built in functions tic and toc. As you can guess from their cool names, they have to do with managing a stop watch. tic starts/restarts a stop watch timer toc returns number of seconds elapsed since the stop watch was started/restarted. Experiment with these two functions on MATLAB Command Window by entering the following commands
tic
timeElapsed = toc
timeElapsed = toc
timeElapsed = toc
tic
timeElapsed = toc
timeElapsed = toc
Notice how the second call to tic restarts the timer. The following is a skeleton for a while loop that you can use to control one turn in the game, where the length of the turn is 20 seconds. tic while toc < 20 code block that manages one turn of the game end
Explanation / Answer
#include <iostream>
#include <time.h>
#include <vector>
#include <math.h>
using namespace std;
int isPrime(int n){
int i;
if (n==2)
return 1;
if (n%2==0)
return 0;
for (i=3;i<=sqrt(n);i+=2)
if (n%i==0)
return 0;
return 1;
}
int main(){
int maxScore=0;
while(true){
int timeSpan =500,sum=0,count=0;
vector<int> check;
clock_t t;
t=clock();
cout<<"Enter as many numbers as you can in this timespan: "<<endl;
while(clock()-t < timeSpan){
int num;
cin>>num;
if(isPrime(num)==1){
int i=0;
for(i=0;i<check.size();++i){
if(check[i]==num){
cout<<"REPEATED – YOU LOSE A POINT!!!"<<endl;
count--;
break;
}
}
if(i==check.size()){
check.push_back(num);
sum=sum+num;
count++;
}
}
else{
cout<<"NOT A PRIME NUMBER!!!"<<endl;
}
cout<<"Current score is: "<<count<<endl;
}
cout<<"Your score after this turn is: "<<sum<<endl;
if(sum>maxScore){
cout<<"You achieved a HIGH Score"<<endl;
maxScore=sum;
}
cout<<"Press -1 to quit"<<endl;
int enter;
cin>>enter;
if(enter==-1)
break;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.