這是一個非常無聊的遊戲.
不過想寫這遊戲的人很多.
直接po一篇上來僅供參考.
如果這個遊戲自己還寫不出來的話..
請再多加油吧
可以的話最好是別看別人的 code 自己寫
// ====================================
// FileName: Guess.cpp
// Author : Edison.Shih.
// Complier: VC 6.0
////////////////////////////////////////////////////////////////
// header
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
////////////////////////////////////////////////////////////////
// sub function declare
unsigned GetDig(); // 取得要玩的位數
void poker(char *ans, unsigned size); // poker 演算法
void Get_Check(char *input, char *ans, unsigned size); // 進行 GetInput 及 CheckAns 動作
void GetInput(char *input, char *ans, unsigned size);// 取得輸入之數字
void GetCheck(char *ans, unsigned size);
unsigned CheckAns(char *input, char* ans, unsigned size);// 比較輸入與輸出之值
bool GetAgain(); // 再玩一次
inline void ShowAns(char *ans, unsigned size);
////////////////////////////////////////////////////////////////
// main function
int main(int argc, char *argv[])
{
unsigned dig=0;
char *ans;
bool again;
do{
dig = GetDig();
ans = (char*)malloc(sizeof(char)*dig);
poker(ans, dig);
GetCheck(ans, dig);
again = GetAgain();
free(ans);
}while(again);
return 0;
}
////////////////////////////////////////////////////////////////
// Get Digit
unsigned GetDig()
{
unsigned dig=0;
do{
printf("How many digits do you want to guest(1~9)?");
scanf("%u",&dig);
getchar(); // clear the input buffer;
if(dig<=0 || dig>10) printf("Input error!\n");
}while(dig<=0 || dig>10);
return dig;
}
////////////////////////////////////////////////////////////////
// Generate Ans.
void swap(char *ch1,char *ch2)
{
char temp;
temp = *ch1;
*ch1 = *ch2;
*ch2 = temp;
}
void poker(char *ans, unsigned dig)
{
unsigned i=0, j=0;
srand(time(NULL));
// poker initialize
char POKER[]="0123456789";
// poker random
for(i=0; i<10; i++) j=rand()%10, swap(&POKER[i],&POKER[j]);
// take first dig.
for(i=0; i<dig; i++) ans[i]=POKER[i];
}
////////////////////////////////////////////////////////////////
// GetCheck
void GetCheck(char *ans, unsigned size)
{
char *input = (char*)malloc(sizeof(char)*size);
char buffer[200];
unsigned i=0, j=0;
unsigned AB=0;
unsigned times=0;
clock_t t1, t2;
FILE *fp1=fopen("Recording.txt","wb");
t1 = clock();
do{
GetInput(input, ans, size);
AB=CheckAns(input, ans, size);
printf("\t\t\t\t");
printf("(%3u) ",++times);
ShowAns(input,size);
printf(" -> %uA%uB\n",(unsigned)(AB/10), AB%10);
fprintf(fp1,"(%3u) ", times);
for(i=0; i<size; i++) fprintf(fp1,"%c",input[i]);
fprintf(fp1," -> %uA%uB\r\n", (unsigned)(AB/10), AB%10);
}while((unsigned)(AB/10)!=size);
t2 = clock();
fclose(fp1);
system("cls");
fp1=fopen("Recording.txt","rb");
while(fgets(buffer, 200, fp1)!=NULL) printf(buffer);
fclose(fp1);
printf("\n You spend %.3f secs \n", (double)(t2-t1)/(double)(CLOCKS_PER_SEC));
free(input);
remove("Recording.txt");
}
////////////////////////////////////////////////////////////////
// GetInput
void GetInput(char *input, char *ans, unsigned size)
{
unsigned i=0, j=0;
unsigned len=0;
char buffer[20];
bool error=false;
do
{
error = false;
printf("INPUT:");
scanf("%s",buffer);
getchar(); // clear buffer;
if(strlen(buffer)!=size) error=true; // 數字長度不對
for(i=0; i<size; i++) if(!isdigit(buffer[i])) error = true; // 不是數字
for(i=0; i<size-1; i++) // 數字重覆
{
for(j=i+1; j<size-1; j++)
{
if(buffer[i]==buffer[j]) error=true;
}
}
if(error) printf("\t\t\t\tinput Error!\n");
}while(error);
for(i=0; i<size; i++) input[i] = buffer[i];
}
////////////////////////////////////////////////////////////////
//
unsigned CheckAns(char *input, char *ans, unsigned size)
{
unsigned i=0, j=0;
unsigned A=0, B=0;
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
if(ans[i]==input[j])
{
(i==j)?(A++):(B++);
break;
}
}
}
return (A*10+B);
}
////////////////////////////////////////////////////////////////
// again or not.
bool GetAgain()
{
char again;
printf("Try again(Y/N)?");
again = getchar();
getchar(); // clear buffer
system("cls");
if(again=='N' || again=='n')
{
printf("thx for play\n");
return false;
}
else return true;
}
inline void ShowAns(char *ans, unsigned size){ for(unsigned i=0; i<size; i++)printf("%c",ans[i]);}