#pragma once
class newGame
{
public:
newGame(void);
virtual void gameStart(void) = 0;
virtual void setGame(int nums) = 0;
virtual void inGame(void) = 0;
virtual ~newGame(void){}
};
class guessNumberGame : public newGame
{
public:
guessNumberGame(void);
void gameStart(void);
void setGame(int nums);
void inGame(void);
void examNum(void);
virtual ~guessNumberGame(void);
private:
int _nums;
int _A,_B;
char* _secretNum;
char* _guessNum;
};
//newGame.cpp//
#include
#include
#include "newGame.h"
newGame::newGame(void)
{
}
guessNumberGame::guessNumberGame(void)
{
gameStart();
}
guessNumberGame::~guessNumberGame(void)
{
}
void guessNumberGame::gameStart(void)
{
int nums;
printf("Enter a number to start the game:(4~6 to play, 0 to exit.)\n");
while (scanf("%d",&nums))
{
if (nums==0) break;
if (nums<4 || nums>6)
{
printf("Wrong input!\n");
printf("Enter a number to start the game:(4~6 to play, 0 to exit.)\n");
continue;
}
_nums=nums;
setGame(_nums);
inGame();
printf("Enter a number to start the game:(4~6 to play, 0 to exit.)\n");
}
printf("Exit the game. Thank you for playing.\n");
system("pause");
}
void guessNumberGame::setGame(int _nums)
{
_secretNum = new char[_nums];
_guessNum = new char[_nums];
int arr[10];
int j=0,tmp;
srand(time(NULL));
for (int i=0;i<10;i++)
{
arr[i]=i;
}
while(j<_nums)
{
tmp = rand() % 10;
if (arr[tmp]>-1)
{
_secretNum[j]=arr[tmp];
arr[tmp]=-1;
j++;
}
}
printf("secretNum = ");
for (int i=0;i<_nums;i++)
printf("%d",_secretNum[i]);
printf("\n");
}
void guessNumberGame::inGame(void)
{
int chance = 10;
for (int i=0;i
printf("Enter your guess(%d):",i+1);
flushall();
for (int j=0;j<_nums;j++)
{
scanf("%c",(_guessNum+j));
}
examNum();
printf("%dA%dB\n",_A,_B);
if (_A == _nums)
{
printf("You win!\n");
break;
}
//for (int j=0;j<_nums;j++)
//{
// printf("%c",_guessNum[j]);
//}
}
}
void guessNumberGame::examNum(void)
{
_A = _B = 0;
for (int i=0;i<_nums;i++)
{
for (int j=0;j<_nums;j++)
{
if (_secretNum[i]+48==_guessNum[j])
{
_B++;
if (i==j) _A++;
continue;
}
}
}
}
int main()
{
guessNumberGame game;
return 0;
}