a number guessing game. Your task is to crack a code which
has been randomly created by the computer. To crack the code, you make guesses and the
computer responds with clues.
• At the beginning, player will be asked to input a number n (an integer value), which
corresponds to the size of an array.
• Then, an array of size n will be created and each array element will be initialized to a
number in range 1 to n.
• Next, the array will be shuffled with the following function:
• Making Guesses
To make a guess of the number sequence, simply fill in a row with digits between 1 – n
separated by a space.
• Using the clues
A “O” symbol means “Right value” and “Right position”.
A “X” symbol means either “Wrong value” or “Wrong position”.
// "randNoArr" is a constant pointer to a dynamically created array
// "size" is the size of the dynamic array pointed by randNoArr
void shuffle( int* const randNoArr, int size )
{
for( int i=0; i<(size-1); i++ )
{
int r = i + (rand() % (size-i));
int temp = randNoArr[i];
randNoArr[i] = randNoArr[r];
randNoArr[r] = temp;
}
}
CCCU – Division of Computer Studies (DCO), City University of Hong Kong
9
• Example output should look like this:
Enter total number: 6
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 1
Enter Guess: 1 2 3 4 5 6
X X X X X X
-----------------------------------------------------------
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 2
Enter Guess: 1 3 2 5 6 4
X X X X O X
-----------------------------------------------------------
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 3
Enter Guess: 3 2 1 4 6 5
O X X X O X
-----------------------------------------------------------
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 4
Enter Guess: 3 4 5 2 6 1
O O O X O X
-----------------------------------------------------------
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 5
Enter Guess: 3 4 5 1 6 2
O O O O O O
-----------------------------------------------------------
Congratulations! You win in 5 steps
These are the work so far, but I don't know how to initialize number in an array.
1 2 3
|
for(int i= 1 ; i < size ; i++){
int num1 = i;
}
|
How to initialize 1, 2, 3, 4, 5, 6 in the array if the user input 6 or 1, 2, 3, 4, 5, 6, 7, 8 in the array if the user input 8.
Thanks!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>
using namespace std;
// "randNoArr" is a constant pointer to a dynamically created array
// "size" is the size of the dynamic array pointed by randNoArr
void shuffle( int* const randNoArr, int size )
{
for( int i=0; i<(size-1); i++ )
{
int r = i + (rand() % (size-i));
int temp = randNoArr[i];
randNoArr[i] = randNoArr[r];
randNoArr[r] = temp;
}
}
int main()
{
int roundn = 1;
int size = 0; // intialize to zero so if cin fails, it's within invalid bounds.
cout << "Enter total number: ";
cin >> size;
int* arr = new int[size];
for(int i= 1 ; i < size ; i++){
int num1 = i;
}
cout << "Number Guessing"<<endl;
cout << "Enter 6 digits (1-6) separated by a space "<<endl;
cout << "----------------------------------------------------------- "<<endl;
cout << "Round "<<roundn++<<endl;
cout << "Enter Guess: ";
for(int i= 0 ; i < size ; i++){
cin >> arr[i];}
// Hold the command window
system("pause");
return 0;
}
|