很急的高分悬赏~~!!C语程序设计实践!!!!!很重要,希望高手能认真的帮帮忙!! 100
猜数字游戏用户从键盘输入4位不重复的数,来匹配计算机给出的4位随机数,若数字和位置均等同。表示用户赢了。每猜一次,计算机均给出提示信息(x,y),x表示数字、位置都匹配的...
猜数字游戏
用户从键盘输入4位不重复的数,来匹配计算机给出的4位随机数,若数字和位置均等同。表示用户赢了。每猜一次,计算机均给出提示信息(x,y),x表示数字、位置都匹配的个数,y表示数字匹配但位置不匹配的个数。
要求:1. 交互界面友好;
2. 给定H键作为帮助键,帮助用户尽快达到目的,无论何时,按下H键,均能得到详细信息,如:第一位数字正确,第2,3位数字位置不对等
3. 给定K键作为管理键,按下后可以查看计算机所给的4位数,但查看前需要输入密码,密码自定。
4. 设置游戏分值,将每次猜的结果以分值的形式给出,如初始分值1000分,每猜一次扣100分,若猜对一个数,奖励20分(计分算法自定)。
5. 将得分最高的前3位的分值在每次开始新游戏前显示出来,同时,在每次游戏结束后将前3位的分值存盘,文件名自定。
要C++ source file 要是好的话追加财富 展开
用户从键盘输入4位不重复的数,来匹配计算机给出的4位随机数,若数字和位置均等同。表示用户赢了。每猜一次,计算机均给出提示信息(x,y),x表示数字、位置都匹配的个数,y表示数字匹配但位置不匹配的个数。
要求:1. 交互界面友好;
2. 给定H键作为帮助键,帮助用户尽快达到目的,无论何时,按下H键,均能得到详细信息,如:第一位数字正确,第2,3位数字位置不对等
3. 给定K键作为管理键,按下后可以查看计算机所给的4位数,但查看前需要输入密码,密码自定。
4. 设置游戏分值,将每次猜的结果以分值的形式给出,如初始分值1000分,每猜一次扣100分,若猜对一个数,奖励20分(计分算法自定)。
5. 将得分最高的前3位的分值在每次开始新游戏前显示出来,同时,在每次游戏结束后将前3位的分值存盘,文件名自定。
要C++ source file 要是好的话追加财富 展开
3个回答
展开全部
加分吧。
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string>
using namespace std;
class Game
{
public:
Game()
{
srand((unsigned int)time(NULL));
inputs[4] = 0;
keys[4] = 0;
for (int i = 0; i < 3; i++)
highScores[i] = -30000;
try {
ifstream ifs("score.txt");
for (int i = 0; i < 3; i++)
ifs >> highScores[i];
} catch(...)
{
}
cout << "Guide: Q - quit; R - restart; H - info; K - cheat" << endl;
run();
}
~Game()
{
ofstream of("score.txt");
for (int i = 0; i < 3 && highScores[i] > -30000; i++)
of << highScores[i] << endl;
}
void run()
{
char k;
start();
while (1) {
if (nInput == 0) {
cout << "\nInput 4 figures: ";
}
k = getch();
switch(k)
{
case 'H':
case 'h':
if (nInput == 0)
showInfo();
break;
case 'K':
case 'k':
if (nInput == 0)
showKeys();
break;
case 'Q':
case 'q':
if (nInput == 0)
if (quit())
return;
break;
case 'R':
case 'r':
if (nInput == 0)
if (restart())
start();
break;
default:
guess(k);
}
}
}
void guess(char k)
{
if (k <'0' || k >'9')
return;
cout << k;
inputs[nInput++] = k;
if (nInput < 4)
return;
nInput = 0;
if (isFinished()) {
start();
return;
}
}
void printHighScores()
{
cout << "\nTop 3 high Scores:" << endl;
for (int i = 0; i < 3; i++) {
if (highScores[i] > -30000)
cout << highScores[i] << endl;
else
cout << "None" << endl;
}
}
bool restart()
{
cout << "\nDo you really want to restart? (Y/N)";
do {
char k = getche();
if (k == 'Y' || k == 'y')
return true;
if (k == 'N' || k == 'n')
return false;
cout << "\nPlease input Y or N:";
} while(1);
}
void start()
{
score = 0;
genKeys();
memset(inputs, 0, 4);
score = 1000;
cout << "\n*** New game ***" << endl;
printHighScores();
nInput = 0;
}
bool isFinished()
{
int x = 0, y = 0;
for (int i = 0; i < 4; i++) {
if (inputs[i] == keys[i])
x++;
else if (flags[inputs[i]-'0'])
y++;
}
score += x * 20;
if (x == 4) {
if (score > highScores[0]) {
highScores[2] = highScores[1];
highScores[1] = highScores[0];
highScores[0] = score;
} else if (score > highScores[1]) {
highScores[2] = highScores[1];
highScores[1] = score;
} else if (score > highScores[2]) {
highScores[2] = score;
}
cout << "\nCorrect! Your score:" << score << endl;
return true;
}
else {
printf("\n(%d, %d)\n", x, y);
score -= 100;
printf("Score = %d\n", score);
}
return false;
}
bool quit()
{
cout << "\nDo you really want to quit? (Y/N)";
do {
char k = getche();
if (k == 'Y' || k == 'y')
return true;
if (k == 'N' || k == 'n')
return false;
cout << "\nPlease input Y or N:";
} while(1);
return true;
}
void genKeys()
{
for (int i = 0; i < 10; i++)
flags[i] = false;
for (int i = 0; i < 4; i++) {
do {
keys[i] = rand() % 10 + '0';
for (int j = 0; j < i; j++)
if (keys[i] == keys[j])
i--;
} while(0);
flags[keys[i] - '0'] = true;
}
}
void showInfo()
{
cout << "\nYour inputs: " << inputs << endl;
for (int i = 0; i < 4; i++) {
cout << i;
if (inputs[i] == keys[i])
cout << " match!" << endl;
else if (flags[inputs[i]-'0'])
cout << " position incorrect." << endl;
else
cout << " no match." << endl;
}
}
void showKeys()
{
cout << "\nInput password(=abcd):" << endl;
std::string s;
cin >> s;
if (s == "abcd")
cout << "Keys = " << keys << endl;
else
cout << "Password is incorrect." << endl;
}
private:
int highScores[3];
char inputs[5];
int nInput;
char keys[5];
bool flags[10];
int score;
};
int main()
{
Game g;
return 0;
}
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string>
using namespace std;
class Game
{
public:
Game()
{
srand((unsigned int)time(NULL));
inputs[4] = 0;
keys[4] = 0;
for (int i = 0; i < 3; i++)
highScores[i] = -30000;
try {
ifstream ifs("score.txt");
for (int i = 0; i < 3; i++)
ifs >> highScores[i];
} catch(...)
{
}
cout << "Guide: Q - quit; R - restart; H - info; K - cheat" << endl;
run();
}
~Game()
{
ofstream of("score.txt");
for (int i = 0; i < 3 && highScores[i] > -30000; i++)
of << highScores[i] << endl;
}
void run()
{
char k;
start();
while (1) {
if (nInput == 0) {
cout << "\nInput 4 figures: ";
}
k = getch();
switch(k)
{
case 'H':
case 'h':
if (nInput == 0)
showInfo();
break;
case 'K':
case 'k':
if (nInput == 0)
showKeys();
break;
case 'Q':
case 'q':
if (nInput == 0)
if (quit())
return;
break;
case 'R':
case 'r':
if (nInput == 0)
if (restart())
start();
break;
default:
guess(k);
}
}
}
void guess(char k)
{
if (k <'0' || k >'9')
return;
cout << k;
inputs[nInput++] = k;
if (nInput < 4)
return;
nInput = 0;
if (isFinished()) {
start();
return;
}
}
void printHighScores()
{
cout << "\nTop 3 high Scores:" << endl;
for (int i = 0; i < 3; i++) {
if (highScores[i] > -30000)
cout << highScores[i] << endl;
else
cout << "None" << endl;
}
}
bool restart()
{
cout << "\nDo you really want to restart? (Y/N)";
do {
char k = getche();
if (k == 'Y' || k == 'y')
return true;
if (k == 'N' || k == 'n')
return false;
cout << "\nPlease input Y or N:";
} while(1);
}
void start()
{
score = 0;
genKeys();
memset(inputs, 0, 4);
score = 1000;
cout << "\n*** New game ***" << endl;
printHighScores();
nInput = 0;
}
bool isFinished()
{
int x = 0, y = 0;
for (int i = 0; i < 4; i++) {
if (inputs[i] == keys[i])
x++;
else if (flags[inputs[i]-'0'])
y++;
}
score += x * 20;
if (x == 4) {
if (score > highScores[0]) {
highScores[2] = highScores[1];
highScores[1] = highScores[0];
highScores[0] = score;
} else if (score > highScores[1]) {
highScores[2] = highScores[1];
highScores[1] = score;
} else if (score > highScores[2]) {
highScores[2] = score;
}
cout << "\nCorrect! Your score:" << score << endl;
return true;
}
else {
printf("\n(%d, %d)\n", x, y);
score -= 100;
printf("Score = %d\n", score);
}
return false;
}
bool quit()
{
cout << "\nDo you really want to quit? (Y/N)";
do {
char k = getche();
if (k == 'Y' || k == 'y')
return true;
if (k == 'N' || k == 'n')
return false;
cout << "\nPlease input Y or N:";
} while(1);
return true;
}
void genKeys()
{
for (int i = 0; i < 10; i++)
flags[i] = false;
for (int i = 0; i < 4; i++) {
do {
keys[i] = rand() % 10 + '0';
for (int j = 0; j < i; j++)
if (keys[i] == keys[j])
i--;
} while(0);
flags[keys[i] - '0'] = true;
}
}
void showInfo()
{
cout << "\nYour inputs: " << inputs << endl;
for (int i = 0; i < 4; i++) {
cout << i;
if (inputs[i] == keys[i])
cout << " match!" << endl;
else if (flags[inputs[i]-'0'])
cout << " position incorrect." << endl;
else
cout << " no match." << endl;
}
}
void showKeys()
{
cout << "\nInput password(=abcd):" << endl;
std::string s;
cin >> s;
if (s == "abcd")
cout << "Keys = " << keys << endl;
else
cout << "Password is incorrect." << endl;
}
private:
int highScores[3];
char inputs[5];
int nInput;
char keys[5];
bool flags[10];
int score;
};
int main()
{
Game g;
return 0;
}
更多追问追答
追问
这个好像跟我们的那个不太一样#include
cout 的样子
可以加你Q吗?
追答
你直接复制到VC里编译运行就可以了。iostream是C++标准库里的,只能用在C++里。stdio.h C++和C语言都能用。
展开全部
猜数游戏源码
............\CompNum.dat
............\Debug
............\.....\guestnum.exe
............\.....\guestnum.ilk
............\.....\guestnum.obj
............\.....\guestnum.pch
............\.....\guestnum.pdb
............\.....\guestnum.res
............\.....\guestnumDlg.obj
............\.....\Mark.dat
............\.....\rank.obj
............\.....\StdAfx.obj
............\.....\vc60.idb
............\.....\vc60.pdb
............\guestnum.aps
............\guestnum.clw
............\guestnum.cpp
............\guestnum.dsp
............\guestnum.dsw
............\guestnum.exe
............\guestnum.h
............\guestnum.ncb
............\guestnum.opt
............\guestnum.plg
............\guestnum.rc
............\guestnumDlg.cpp
............\guestnumDlg.h
............\iCom.dat
............\iHis.dat
............\Mark.dat
............\rank.cpp
............\rank.h
............\ReadMe.txt
............\res
............\...\guestnum.rc2
............\resource.h
............\resource.hm
............\StdAfx.cpp
............\StdAfx.h
............\CompNum.dat
............\Debug
............\.....\guestnum.exe
............\.....\guestnum.ilk
............\.....\guestnum.obj
............\.....\guestnum.pch
............\.....\guestnum.pdb
............\.....\guestnum.res
............\.....\guestnumDlg.obj
............\.....\Mark.dat
............\.....\rank.obj
............\.....\StdAfx.obj
............\.....\vc60.idb
............\.....\vc60.pdb
............\guestnum.aps
............\guestnum.clw
............\guestnum.cpp
............\guestnum.dsp
............\guestnum.dsw
............\guestnum.exe
............\guestnum.h
............\guestnum.ncb
............\guestnum.opt
............\guestnum.plg
............\guestnum.rc
............\guestnumDlg.cpp
............\guestnumDlg.h
............\iCom.dat
............\iHis.dat
............\Mark.dat
............\rank.cpp
............\rank.h
............\ReadMe.txt
............\res
............\...\guestnum.rc2
............\resource.h
............\resource.hm
............\StdAfx.cpp
............\StdAfx.h
追问
要VC里C++ source file ,这个是啥的????
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
很急吗?我现在没时间,这个不是太难!
追问
挺急的~!!!我们明天就要开始做,我都不会。。。可以加你Q吗?742972247
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询