#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
int* newGame(int gameDifficulty)
{
srand ( time(NULL) );
int* gameBoard = new int[gameDifficulty];
for (int i = 0; i < gameDifficulty; i++)
{
gameBoard[i] = (rand() % 10);
}
return gameBoard;
}
void printArray(int* arrayToPrint, int gameDifficulty)
{
cout << "The solution is : ";
for ( int i = 0; i < gameDifficulty; i++ )
{
cout << *(arrayToPrint+i) << " ";
}
cout << endl;
}
int main(int argc, char* argv[])
{
int quit = 0;
while(quit != 1)
{
char playAgain = NULL;
char answer = NULL;
int correctPlaces = 0;
int wrongPlaces = 0;
int won = 0;
int gameDifficulty = 0;
cout << "What difficulty would you like to play at? (0-5)" << endl;
cin >> gameDifficulty;
if (gameDifficulty > 5)
{
cout << "Difficulty selected was too high! 5 selected!" << endl;
gameDifficulty = 5;
}
gameDifficulty += 4;
cout << endl;
int* gameNumbers = newGame(gameDifficulty);
int* enteredNumbers = new int[gameDifficulty];
while(answer != 'y' && answer != 'n')
{
cout << "Would you like to see the solution? (y/n)" << endl;
cin >> answer;
switch (answer)
{
case 'y' : printArray(gameNumbers,gameDifficulty); break;
case 'n' : break;
default : cout << "Please enter y or n!" << endl; break;
}
}
while (won != 1)
{
cout << "Guess " << gameDifficulty << " numbers pressing enter after each entry" << endl;
for ( int k = 0; k < gameDifficulty; k++)
{
cin >> enteredNumbers[k];
}
for (int i = 0; i < gameDifficulty; i++)
{
if (enteredNumbers[i] == gameNumbers[i])
{
correctPlaces += 1;
}
for (int j = 0; j < gameDifficulty; j++)
{
if (enteredNumbers[j] == gameNumbers[i] && enteredNumbers[i] != gameNumbers[i] && enteredNumbers[j] != gameNumbers[j])
{
wrongPlaces += 1;
break;
}
}
}
cout << "C = " << correctPlaces << " W = " << wrongPlaces << endl;
if (correctPlaces == gameDifficulty)
{
won = 1;
}
correctPlaces = 0;
wrongPlaces = 0;
}
cout << "Congratulations you have won!" << endl;
cout << endl << endl << endl;
delete [] gameNumbers;
delete [] enteredNumbers;
while(playAgain != 'n' && playAgain != 'y')
{
cout << "Would you like to play again? (y/n)" << endl;
cin >> playAgain;
cout << endl;
switch (playAgain)
{
case 'n' : quit = 1; break;
case 'y' : break;
default : cout << "Please enter y or n!" << endl << endl;
}
}
}
return 0;
}