这个c语言(c++)的程序要怎么写?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了这个c语言(c++)的程序要怎么写?相关的知识,希望对你有一定的参考价值。
内容:设计一个猜数字游戏,它能根据游戏规则猜数。
功能:产生原始数字:产生4个随机数,并按任意顺序排列。
游戏提示:用1A2B形式提示玩家当前猜数字的匹配情况。例如1A表示玩家猜测的数字有1个与系统产生的原始数字符合,并且位置也符合。2B表示玩家猜测的数字有2个与系统产生的原始数字符合,但位置不符合。
原始数字显示:当用户10次仍然不能猜出正确的原始数字和顺序时,系统提示玩家失败,并给玩家显示原始数字和顺序。
提示:可能涉及语法点:数组、选择与循环、函数
完全不明白要怎么出随机排序还要对号,如果有高人能编写出完整的可运行的程序,我会再追加分数!
当用户10次仍然不能猜出正确的原始数字和顺序时,系统提示玩家失败,并给玩家显示原始数字和顺序。
这点好像没有达到啊!不过强人,做到这里已经非常感谢了
我需要的程序代码是能直接复制在Microsoft visual c++6.0中即可运行出结果的
#include <stdlib.h>
#include <time.h>
void RandomShuffle(int* a, int n)
int tmp, i, pick;
for(i = 0; i < n; ++i)
pick = rand() % n;
tmp = a[i];
a[i] = a[pick];
a[pick] = tmp;
void PrintSeq(int* beg, int* end)
while(beg != end)
printf("%d ", *beg++);
putchar('\n');
void ReadSeq(int* beg, int* end)
while(beg != end)
scanf("%d", beg++);
int Analyse(int* resBeg, int* resEnd, int* beg, int* end)
int fully_match = 0;
int numbers_match = 0;
int* p1, * p2;
for(p1 = beg; p1 != end; ++p1)
for(p2 = resBeg; p2 != resEnd; ++p2)
if(*p2 == *p1 && p2 - resBeg == p1 - beg)
++fully_match;
else if(*p2 == *p1)
++numbers_match;
if(fully_match != end - beg)
return printf("Clue: %dA%dB\n", fully_match, numbers_match), 0;
else
return 1;
void Guess(int* resBeg, int* resEnd, int allowedTimes)
int* myNumBeg = (int*)malloc(sizeof(int) * (resEnd - resBeg));
int* myNumEnd = myNumBeg + (resEnd - resBeg);
printf("NOTE: No Duplicates!\n");
do
printf("\nEnter %d numbers(You have %d more chance):\n", myNumEnd - myNumBeg, allowedTimes);
ReadSeq(myNumBeg, myNumEnd);
while(!Analyse(resBeg, resEnd, myNumBeg, myNumEnd) && --allowedTimes > 0);
if(allowedTimes != 0)
printf("\nYou got it!\n");
else
printf("\nGame Over!!\n");
printf("Origin seq is: ");
PrintSeq(resBeg, resEnd);
free(myNumBeg);
int main()
srand((unsigned)time(0));
int a[] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;
RandomShuffle(a, 10); // 随机打乱
Guess(a, a + 4, 10); // 选4个数字作为答案,允许用户猜10次
return 0;
有失败提示和给出答案结果的过程啊,以下是(3次机会)我在vc6.0的测试:
NOTE: No Duplicates!
Enter 4 numbers(You have 3 more chance): // 第一次
1 2 3 4
Clue: 1A0B
Enter 4 numbers(You have 2 more chance): // 第二次
2 3 1 4
Clue: 0A1B
Enter 4 numbers(You have 1 more chance): // 第三次
1 2 4 3
Clue: 1A0B
Game Over!! // 没猜着,猜着的话是You got it!
Origin seq is: 0 2 9 7 // 答案(原始数组)
Press any key to continue 参考技术A 以下是主要代码
这个程序是个Win32窗口程序,如果要完整工程源代码请给我你的电子邮箱地址
我的:runtime_class@163.com
// GuessNum.cpp
//
//GuessNum App
//SigmaTera
//2007-08-14
#include "stdafx.h"
#include "resource.h"
#include <ctime>
#include <cstdlib>
void GenerateNum(LPTSTR lpDights,int nLength);
void CheckInputNum(LPCTSTR lpDigits,LPCTSTR lpGenDigits,LPTSTR lpResult,int nLenDigits,int nLenResult);
bool CheckInput(LPCTSTR lpDigits);
BOOL WINAPI DlgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
// TODO: Place code here.
DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_DIALOG_MAIN),NULL,DlgProc,0);
return 0;
BOOL WINAPI DlgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
static TCHAR szDigit[5];
static TCHAR szResult[5];
static TCHAR szInputDigits[5];
static TCHAR szTips[16];
static int nTimes = 10;
if (uMsg == WM_INITDIALOG)
EnableWindow(GetDlgItem(hWnd,IDOK),FALSE);
SetWindowText(GetDlgItem(hWnd,IDC_TIPS),"祝您成功!");
else if (uMsg == WM_CLOSE)
EndDialog(hWnd,0);
else if (uMsg == WM_COMMAND)
switch(LOWORD(wParam))
case IDGEN:
GenerateNum(szDigit,4);
EnableWindow(GetDlgItem(hWnd,IDOK),TRUE);
nTimes = 10;
break;
case IDOK:
GetWindowText(GetDlgItem(hWnd,IDC_EDIT_INP),szInputDigits,5);
if (lstrlen(szInputDigits) < 4 || CheckInput(szInputDigits) == false)
MessageBox(0,"输入非法","提示",MB_OK);
SetWindowText(GetDlgItem(hWnd,IDC_EDIT_INP),"");
break;
CheckInputNum(szDigit,szInputDigits,szResult,4,4);
SetWindowText(GetDlgItem(hWnd,IDC_SHOW_RESULT),szResult);
if (lstrcmp(szInputDigits,szDigit) == 0)
MessageBox(0,"恭喜您猜对了!","提示",0);
EnableWindow(GetDlgItem(hWnd,IDOK),FALSE);
nTimes--;
wsprintf(szTips,"您还有%d次机会",nTimes);
if (nTimes == 0)
SetWindowText(GetDlgItem(hWnd,IDC_TIPS),"失败了:(");
MessageBox(0,"抱歉,您在10次内没有答对","提示",0);
EnableWindow(GetDlgItem(hWnd,IDOK),FALSE);
break;
SetWindowText(GetDlgItem(hWnd,IDC_TIPS),szTips);
break;
case IDCANCEL:
EndDialog(hWnd,0);
break;
return FALSE;
void GenerateNum(LPTSTR lpDights,int nLength)
srand(time(0));
int i = 0;
int nGen = 0;
bool bGenerated = false;
while(i < nLength)
bGenerated = false;
nGen = rand() % 10 + 48;
for (int j = i;j >= 0;j--)
if (lpDights[j] == nGen)
bGenerated = true;
break;
if (bGenerated)
continue;
else
lpDights[i] = nGen;
i++;
void CheckInputNum(LPCTSTR lpDigits,LPCTSTR lpGenDigits,LPTSTR lpResult,int nLenDigits,int nLenResult)
int nPosID = 0;
int nSameLetterID = nLenDigits;
TCHAR szTemp[256];
for (int i = 0;i < nLenDigits;i++)
if (lpGenDigits[i] == lpDigits[i])
nPosID++;
for (int j = 0;j < nLenDigits;j++)
if (lpGenDigits[i] == lpDigits[j])
nSameLetterID--;
wsprintf(szTemp,"%dA%dB",nPosID,nSameLetterID);
lstrcpyn(lpResult,szTemp,nLenResult + 1);
bool CheckInput(LPCTSTR lpDigits)
bool bGenerated = false;
int i = 0;
while(lpDigits[i] != '\0')
bGenerated = false;
if (i == 0)
i++;
continue;
for (int j = i - 1;j >= 0;j--)
if (lpDigits[j] == lpDigits[i])
return false;
break;
i++;
return true;
参考技术B #include <iostream.h>
#include <time.h>
#include <stdlib.h>
void Checkout(char [],char [],int &,int &);
void Swap(char &a,char &b);
void main()
bool right=false;
char goon;
int times=0,perfect,nearly,init;
char guess[5],in[255],orig[10]='0','1','2','3','4','5','6','7','8','9';
srand((unsigned)time(NULL));
while (1)
for(int i=0;i<4;i++)
init=rand()%10;
Swap(orig[i],orig[init]);
for(i=0;i<4;i++)
guess[i]=orig[i];
guess[5]='\0';
right=false;
for(times=0;times<10 && !right;times++)
perfect=0;nearly=0;
cin>>in;
Checkout(guess,in,perfect,nearly);
cout<<perfect<<"A"<<nearly<<"B"<<endl;
if(perfect==4)
right=true;
if (right)
cout<<"You Get It!\n";
else
cout<<"Try Harder Next Time!\nThe number is ";
for (i=0;i<4;i++)
cout<<guess[i];
cout<<endl;
cout<<"Continue? y for yes,n for no ";
cin>>goon;
if(goon!='y' &&goon!='Y')
break;
void Checkout(char gn[4],char ck[4],int &p,int &n)
for (int i=0;i<4;i++)
if(ck[i]==gn[i])
p++;
else
for (int j=0;j<4;j++)
if(ck[i]==gn[j])
n++;
void Swap(char &a,char &b)
char temp;
temp=a;
a=b;
b=temp;
本回答被提问者采纳 参考技术C 简单,看完就知道怎么做了
提示下:
产生随机数有个专用函树的,而且你这个程序产生的随机数没有排序的必要吧
将随机数放到数组里,将用户输入的数放到另一个数组里
利用循环加判断就可以搞定了
就提示这么多了,剩下的都是些语法问题了 参考技术D 很简单嘛
以上是关于这个c语言(c++)的程序要怎么写?的主要内容,如果未能解决你的问题,请参考以下文章