C++小游戏代码

根据英语学习的特点,编制一款可帮助学生背英文单词的小软件,要求具有以下功能:从词库中随机抽取一个单词,显示在屏幕上时,该单词被随机隐藏了一个、或两个、或三个字母,用户正确填空后,可得分;当得分累积到某些分值时,系统给予鼓励。
单词词库可以是任何合理的形式,如数组、字符串、文件等。用户可以设置每次使用时练习的单词组。用户填空时,可以有提示信息。
我晕,我要的是简单的代码,不是楼下的P话,请不要在这说什么胡言乱语,没本事不要占楼位,谢谢你了!

http://www.pudn.com/downloads97/sourcecode/others/detail399517.html
#include<iostream> //Standard input/output
#include<string> //String manipulation
#include<cctype> //Character manipulation and testing
#include<fstream> //File stream
#include<cstdlib> //Used for random function
#include<time.h> //Used for better random number
#include"draw.h" //run the draw.h file

using namespace std;
//Function declerations
void instruction(int& choice); //Give instructions and gets choice
void usergame(int i); //Plays 2nd user game
void compgame(int i); //Plays against computer
//Tests current letter and raplaces starred word
void test(string word,char letter,int& numwrong,string& temp,int i);
//Checks current letter and adds it to letters chosen output if not entered already
void lchosen(char letter,string& letterchosen,int& check,int& chosencounter,int i);
void rnd(string& word,int i); //Gets random word from file
void drawman(int numguess,int numwrong,int i); //Draws hangman
inline istream& Flush(istream& stream); //Flushses cin stream

//Start of main

int main()
{
int i=0; //Counter variable for loops
int exit=0; //Main loop exit variable
int choice; //users input choice for type of game or to exit
//Main control loop
do{ //while exit!=1
system("cls"); //执行系统命令清屏
instruction(choice);//Give instructions
switch(choice)
{
case 1:
usergame(i);//Calls user game
break;
case 2:
compgame(i);//Calls computer game
break;
case 3:
cout<<"Goodbye"<<endl;
exit=1;
break;
default:
cerr<<"Invalid choice---try again"<<endl;
}
}while(exit!=1);
//End main loop
system("pause"); //执行系统命令:暂停
return 0;
}
//End main

void instruction(int& choice)
{
cout<<" --Hangman--"<<endl<<endl;
cout<<" Created by laser"<<endl<<endl;
cout<<"*************************************************"<<endl;
cout<<endl;
cout<<"Enter --1--to play against user"<<endl;
cout<<"Enter --2--to play against computer"<<endl;
cout<<"Enter --3--to quit"<<endl;
cout<<endl;
cout<<"*************************************************"<<endl<<endl;
cout<<"Choice: ";
cin>>choice;

while(!cin)
{
cerr<<"Invalid character"<<endl;
cerr<<"Enter again --choice: ";
Flush(cin);
cin>>choice;
}
system("cls");
}

void usergame(int i)
{
int numguess=0;
int numwrong=0;
int check;
int wordcheck;
int end=0;
int chosencounter=0;
//
char letter;
string word;
string temp;
string letterchosen=" ";
//
do{
cout<<"How many chances does the person have(4--10):";
cin>>numguess;
}while(numguess<4||numguess>10);
cout<<"Enter word 2nd user: ";
cin>>word;
do{
wordcheck=0;
for(int i=0;i<word.length();i++)
{
if(!isalpha(word.at(i)))
{
wordcheck=1;
}
}
if(wordcheck==1)
{
cout<<"Invalid--Enter word again: ";
cin>>word;
}
}while(wordcheck==1);
temp=word;

for(i=0;i<word.length();i++)
{
temp.replace(i,1,1,'*');
}

system("cls");

do{
drawman(numguess,numwrong,i);

if(word==temp)
{
cout<<endl<<endl;
cout<<"You guessed it["<<word<<"]"<<endl<<endl;
system("pause");
end=1;
}

if(numwrong==numguess)
{
cout<<endl<<endl;
cout<<"You failed"<<endl<<endl;
system("pause");
end=1;
}

if(end==0)
{
cout<<endl<<endl<<endl;
cout<<"Letters chosen: "<<letterchosen<<endl;
cout<<endl<<endl<<endl;
cout<<"guesses left: "<<numguess-numwrong<<endl<<endl;
cout<<" "<<temp<<endl<<endl;
cout<<"Letter: ";
cin>>letter;

while(!isalpha(letter))
{
Flush(cin);
cout<<"Not a letter--enter letter: ";
cin>>letter;
}

lchosen(letter,letterchosen,check,chosencounter,i);

if(check==0)
{
test(word,letter,numwrong,temp,i);
}
else
{
;
}

system("cls");
}

system("cls");
}while(end!=1 && end!=2);
if(end==2)
{
cout<<"Correct word was["<<word<<"]"<<endl<<endl;
system("pause");
}
if(end==1)
{
cout<<" ";
}
system("cls");
}

void compgame(int i)
{
int numguess=0;
int numwrong=0;
int check;
int end=0;
int chosencounter=0;
char letter;
string word;
string temp;
string letterchosen=" ";
do{
cout<<"How many chances do you want(4--10): ";
cin>>numguess;
}while(numguess<4||numguess>10);

rnd(word,i);
temp=word;
for(i=0;i<word.length();i++)
{
temp.replace(i,1,1,'*');
}
system("cls");
do{
drawman(numguess,numwrong,i);
if(word==temp)
{
cout<<endl<<endl;
cout<<"You guessed it["<<word<<"]"<<endl<<endl;
system("pause");
end=1;
}
if(numwrong==numguess)
{
cout<<endl<<endl;
cout<<"You failed"<<endl<<endl;
system("pause");
end=2;
}

if(end==0)
{
cout<<endl<<endl<<endl;
cout<<"Letters chosen: "<<letterchosen<<endl;
cout<<endl<<endl<<endl;
cout<<"Guesses left: "<<numguess-numwrong<<endl<<endl;
cout<<" "<<temp<<endl<<endl;
cout<<"Letter: ";
cin>>letter;
while(!isalpha(letter))
{
Flush(cin);
cout<<"Not a letter---enter letter: ";
cin>>letter;
}
lchosen(letter,letterchosen,check,chosencounter,i);
if(check=0)
{
test(word,letter,numwrong,temp,i);
}
else
{
;
}
system("cls");

}
system("cls");
}while(end!=1&& end!=2);
if(end==2)
cout<<"Correct word was["<<word<<"]"<<endl<<endl;
system("pause");
if(end==1)
cout<<endl;
system("cls");
}

void lchosen(char letter,string& letterchosen,int& check,int& chosencounter,int i)
{
check=0;
for(i=0;i<letterchosen.length();i++)
{
if(letter==letterchosen.at(i))
{
check=1;
}
}

if(check==1)
{
cout<<endl;
cout<<"Letter already chosen"<<endl;
system("pause");
}

else
{
letterchosen.replace(chosencounter,1,1,letter);
chosencounter++;
}
}

void test(string word,char letter,int& numwrong,string& temp,int i)
{
int check2=0;

for(i=0;i<word.length();i++)
{
if(letter==word.at(i))
{
temp.replace(i,1,1,letter);
check2=1;
}
}

if(check2==0)
{
cout<<endl;
cout<<"Wrong letter"<<endl;
system("pause");
numwrong++;
}
}

void rnd(string& word,int i)
{
int x;
ifstream ins;
srand(time(NULL));
x=rand()%100;
ins.open("words.txt");

if(ins.fail())
{
cerr<<"Words.txt is not in same folder as hangman.exe,"<<endl
<<"put in correct file and run again and make sure it's"<<endl
<<"caled words.txt"<<endl;
system("pause");
main();
}
else
{
for(i=0;i<(x+1);i++)
{
getline(ins,word);
}
}
ins.close();
}

void drawman(int numguess,int numwrong,int i)
{
draw d;

for(i=0;i<=numwrong;i++)
{
if(numguess==4)
{
switch(i)
{
case 1:d.rope();cout<<endl;d.head();
cout<<endl;d.neck();break;
case 2:cout<<endl;d.leftarm();
d.rightarm();break;
case 3:cout<<endl;d.waisttop();break;
case 4:cout<<endl;d.leftleg();d.rightleg();
cout<<endl<<endl<<"Dead"<<endl;break;
}
}
else if(numguess==5)
{
switch(i)
{
case 1:d.rope();break;
case 2:cout<<endl;d.head();cout<<endl;d.neck();break;
case 3:cout<<endl;d.leftarm();d.rightarm();break;
case 4:cout<<endl;d.waisttop();break;
case 5:cout<<endl;d.leftleg();d.rightleg();
cout<<endl<<endl<<"Dead"<<endl;break;
}
}

else if(numguess==6)
{
switch(i)
{
case 1:d.rope();break;
case 2:cout<<endl;d.head();cout<<endl;d.neck();break;
case 3:cout<<endl;d.leftarm();d.rightarm();break;
case 4:cout<<endl;d.waisttop();break;
case 5:cout<<endl;d.leftleg();d.rightleg();
case 6:d.rightleg();cout<<endl<<"Dead"<<endl;break;
}
}
else if(numguess==7)
{
switch(i)
{
case 1:d.rope();break;
case 2:cout<<endl;d.head();cout<<endl;d.neck();break;
case 3:cout<<endl;d.leftarm();break;
case 4: d.rightarm();break;
case 5:cout<<endl;d.waisttop();break;
case 6:cout<<endl;d.leftleg();break;
case 7:d.rightleg();cout<<endl<<"Dead"<<endl;break;
}
}

else if(numguess==8)
{
switch(i)
{
case 1:d.rope();break;
case 2:cout<<endl;d.head();break;
case 3:cout<<endl;d.neck();break;
case 4:cout<<endl;d.leftarm();break;
case 5: d.rightarm();break;
case 6:cout<<endl;d.waisttop();break;
case 7:cout<<endl;d.leftleg();break;
case 8:d.rightleg();cout<<endl<<"Dead"<<endl;break;
}
}
else if(numguess==9)
{
switch(i)
{
case 1:d.rope();break;
case 2:cout<<endl;d.head();break;
case 3:cout<<endl;d.neck();break;
case 4:cout<<endl;d.leftarm();break;
case 5: d.rightarm();break;
case 6:cout<<endl;d.waisttop();break;
case 7:break;
case 8:cout<<endl;d.leftleg();break;
case 9:d.rightleg();cout<<endl<<"Dead"<<endl;break;
}
}

else if(numguess==10)
{
switch(i)
{
case 1:d.rope();break;
case 2:cout<<endl;d.head();break;
case 3:cout<<endl;d.neck();break;
case 4:cout<<endl;d.leftarm();break;
case 5: d.rightarm();break;
case 6:cout<<endl;d.waisttop();break;
case 7:break;
case 8:cout<<endl;d.leftleg();break;
case 9:d.rightleg();
cout<<"One last chance.What do you want on your tombstore?";break;
case 10:cout<<endl<<"Dead"<<endl;break;
}
}
}
}

inline istream& Flush(istream& stream)
{
stream.clear();
int chars_to_skip=stream.rdbuf()->in_avail();
return stream.ignore(chars_to_skip);
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2008-12-22
如果可以用TC,我可以给你写个。
只要程序就更容易了。

寒楼上的,你用TC,我可以想象你程序的UI,还有他的词库为了防止本机改动可能要用到局域网的东西,或者将词库做成数据库文件,哪里有你说的那么容易啊,我估计你说的是直接弄成文本文件IO吧
回答者: fzbzchenxi - 同进士出身 六级 12

回复:
楼主要的只是C++小游戏代码.
又不是当作商业软件,有要你说的那么复杂的吗?还局域网啊。

说句简单的就是和文曲星上的英文测试练习一样
第3个回答  2021-02-01

使用语言:C++使用工具:vs2019

第4个回答  2008-12-20

小游戏的C++代码
火脸柴人小游戏的C++代码实现这是一个简单的火脸柴人游戏,游戏规则如下:玩家通过方向键控制蓝色小人移动,避开位于(10, 10)位置的红色老女人。一旦小人与老女人相遇,游戏结束。代码使用C++编写,需要在编译时添加 `-std=c++11` 参数以确保正确编译。以下是关键函数的代码片段:void gotoxy(int x, ...

小游戏的C++代码
\/* A Simple Stickman Game in C++ *\/ \/* Compile with the command: g++ -std=c++11 -o game game.cpp *\/ include include include using namespace std;const unsigned char CTRL_KEY = 0XE0;const unsigned char LEFT = 0X4B;const unsigned char RIGHT = 0X4D;const unsigned char DOWN...

如何用C++编写一个小游戏
一个用C++编程的小游戏,可以实现的功能如下:1、随机生成数字;2、数字消除合并;3、判定游戏结束;一、游戏主体:因为用C++写的,所以用了类,棋盘用了一个二维数组,m是棋盘规格,取了4。class game { public:int i, j;game() { count1 = 0;for (i = 0; i < m; i++)for (j = ...

用C++编写的小游戏源代码
五子棋的代码:include<iostream> include<stdio.h> include<stdlib.h> include using namespace std;const int N=15; \/\/15*15的棋盘 const char ChessBoardflag = ' '; \/\/棋盘标志 const char flag1='o'; \/\/玩家1或电脑的棋子标志 const char flag2='X'; \/\/玩家2的棋子标...

求一C++小游戏源代码 简单点的?!!谢谢
void Close(void); \/*关闭游戏函数*\/ void DrawK(void); \/*画图函数*\/ void GameOver(void);\/*输出失败函数*\/ void GamePlay(); \/*游戏控制函数 主要程序*\/ void PrScore(void); \/*分数输出函数*\/ DELAY(char ch)\/*调节游戏速度*\/ { if(ch=='3'){ delay(gamespeed); \/*delay是延迟...

用c++来编写一个小游戏的源代码,要100-200行就可以了,可以再vc环境下运...
\/\/作者:小斌 include<graphics.h> include<conio.h> include<stdlib.h> \/\/using namespace std;const char LEFT=0x4b;const char RIGHT=0x4d;const char DOWN=0x50;const char UP=0x48;const char ESC=0x1b;const char ENTER=0x0d;const int BX=200;const int BY=170;const int SQ=30...

小游戏的C++代码
\/*一个火柴人游戏,亲自验证,可运行*\/ \/*在编译时添加如下命令:-std=c++11,否则会编译错误*\/ include <cstdio> include <cstdlib> include <Windows.h> include <thread> include <conio.h> using namespace std;const unsigned char CTRL_KEY = 0XE0;const unsigned char LEFT = 0X4B;cons...

C++做一个小游戏,有源代码的最好,谢谢
C++做一个小游戏,有源代码的最好,谢谢#include <iostream>#include<fstream>#include <ctime>#include <cmath>#include <stdlib.h>#include<stdio.h> //时间 /&#

能发个C++编程的小游戏的源代码给我吗?比如俄罗斯方块贪食蛇之类的...
void GameOver(void);\/*结束游戏*\/ void GamePlay(void);\/*玩游戏具体过程*\/ void PrScore(void);\/*输出成绩*\/ \/*主函数*\/ void main(void){ Init();\/*图形驱动*\/ DrawK();\/*开始画面*\/ GamePlay();\/*玩游戏具体过程*\/ Close();\/*图形结束*\/ } \/*图形驱动*\/ void Init(void){ ...

用C++帮我制作一个小游戏,并注,能让我理解的。
cout<<"比我的数字小哦!"<<endl;} } } \/\/\/ 楼上写的代码挺好,忘记了初始化随机种子 srand((unsigned int)(time(NULL));随机数是伪随机数。使用rand()函数一定要初始化随机种子。include<iostream.h> include<stdlib.h> int main(){ bool flag=true;srand((unsigned int)(time(NULL))...

相似回答