65,211
社区成员
发帖
与我相关
我的任务
分享#include <stdafx.h>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
printf("Hangman game :-)\n-----------------------\n");
printf("How to play:\n"
"the challenger will put a word in h.txt in the same\n"
"directory as this application. After that run the program!\n\n");
ifstream fp("h.txt");
char s[20];fp >> s;
int n=strlen(s);int i,chances=10;char c,dd=s[0];
char a[20];char d[26];d[0]=0;int j=0,flag=0;
for(i=0;i<n;i++)a[i]='_'; a[n]='\0';
while(chances > 0) {
flag=1;
for(i=0;i<n;i++)printf("%c ",a[i]);
for(i=0;i<n;i++) {
if(a[i] != s[i]) flag=0;
}
{
string playerLetters;
random = (rand()%5)+1;
cout <<"*******\n";
cout <<"* *\n";
cout <<"* \n";
cout <<"* \n";
cout <<"* \n";
cout <<"========\n";
answer = wordBank[random];
string answer1=answer;
string playerWord(answer.size(),'-');
unsigned int tries = 0;
string alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
string anotheralpha;
do
{
cout << "\n\nLetters used : " << playerLetters << "\n\n";
cout << "\n\nUnused letters : " << alphabet << "\n\n";
cout << "\n\nAnswer: " << playerWord << "\n\n";
cout << "\n\nPlease enter a guess: " << "\n\n";
cin >> guess;
if (guess.length() > 1)
{
cout << "\n\nYou entered too many letters.\n\n";
tries = 5;
break;
}
anotheralpha.insert(0, guess);
if(anotheralpha.find(guess, 0)== string::npos) {
alphabet.erase(alphabet.find(guess, 0), 1);
}
if (answer.find(guess, 0)!= string::npos)
{
cout <<"\n\nRIGHT!!!\n\n";
int i = answer1.find(guess, 0);
playerWord.erase(i,1);
playerWord.insert(i,guess);
answer.erase((answer.find(guess, 0)), 1);
playerLetters += guess;
}
else
{
tries++;
cout <<"\n\nWRONG!\n\n";
playerLetters += guess;
if (tries == 1)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 2)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* | \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 3)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -| \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 4)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 5)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* | \n";
cout <<"========\n";
}
if (tries == 6)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* | |\n";
cout <<"========\n";
}
}
sort(playerLetters.begin(), playerLetters.end());
} while (tries < 6 && (answer.length() != 0));
playerLetters.erase(0, playerLetters.length());
if (tries >= 5)
{
cout << "\n\nYou dead!!!\n";
cout << "\n\nThe answer is: " << wordBank[random] <<"\n\n";
}
else
{
cout <<"\n\nYou won!You are powerful!!!\n\n";
cout <<"\n\nThe answer is: " << wordBank[random] << "\n\n";
}
random = 0;
tries = 0;
cout << "\n\nPlay again(y/n)?";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\n\nThanks for playing!\n\n";
return 0;
}
#include <cstdio>
#include <cstring>
#include <fstream>
using namespace std;
int main() {
printf("Hangman game :-)\n-----------------------\n");
printf("How to play:\n"
"the challenger will put a word in h.txt in the same\n"
"directory as this application. After that run the program!\n\n");
ifstream fp("h.txt");
char s[20];fp >> s;
int n=strlen(s);int i,chances=10;char c,dd=s[0];
char a[20];char d[26];d[0]=0;int j=0,flag=0;
for(i=0;i<n;i++)a[i]='_'; a[n]='\0';
while(chances > 0) {
flag=1;
for(i=0;i<n;i++)printf("%c ",a[i]);
for(i=0;i<n;i++) {
if(a[i] != s[i]) flag=0;
}
if(flag) break;
flag=0;
printf("used %s chances %d enter>",d,chances);
gets(&c);
if(strchr(d,c)!= NULL) continue; // already entered in missed.
s[0]=dd;
for(i=0;i<n;i++) {
if(s[i]==c ){ a[i]=c;flag=1;}
}
d[j++]=c;d[j]='\0';if(flag==0)chances--;
}
if(flag) printf("you won!\n"); else printf("you lost! the word was %s\n",s);
}
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
using namespace std;
int main()
{
char again;
string guess;
string s;
ifstream fp("h.txt");
string wordBank[6];
int i = 1;
while(i <= 6) {
fp >> wordBank[i];
i++;
string answer;
int random = 0;
unsigned int i = 0;
srand((unsigned)time(0));
cout << "\n\nLet's play Hangman!\n\n";
{
string playerLetters;
random = (rand()%5)+1;
cout <<"*******\n";
cout <<"* *\n";
cout <<"* \n";
cout <<"* \n";
cout <<"* \n";
cout <<"========\n";
answer = wordBank[random];
string answer1=answer;
string playerWord(answer.size(),'-');
unsigned int tries = 0;
string alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
string anotheralpha;
do
{
cout << "\n\nLetters used : " << playerLetters << "\n\n";
cout << "\n\nUnused letters : " << alphabet << "\n\n";
cout << "\n\nAnswer: " << playerWord << "\n\n";
cout << "\n\nPlease enter a guess: " << "\n\n";
cin >> guess;
if (guess.length() > 1)
{
cout << "\n\nYou entered too many letters.\n\n";
tries = 5;
break;
}
anotheralpha.insert(0, guess);
if(anotheralpha.find(guess, 0)== string::npos) {
alphabet.erase(alphabet.find(guess, 0), 1);
}
if (answer.find(guess, 0)!= string::npos)
{
cout <<"\n\nRIGHT!!!\n\n";
int i = answer1.find(guess, 0);
playerWord.erase(i,1);
playerWord.insert(i,guess);
answer.erase((answer.find(guess, 0)), 1);
playerLetters += guess;
}
else
{
tries++;
cout <<"\n\nWRONG!\n\n";
playerLetters += guess;
if (tries == 1)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 2)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* | \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 3)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -| \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 4)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 5)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* | \n";
cout <<"========\n";
}
if (tries == 6)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* | |\n";
cout <<"========\n";
}
}
sort(playerLetters.begin(), playerLetters.end());
} while (tries < 6 && (answer.length() != 0));
playerLetters.erase(0, playerLetters.length());
if (tries >= 5)
{
cout << "\n\nYou dead!!!\n";
cout << "\n\nThe answer is: " << wordBank[random] <<"\n\n";
}
else
{
cout <<"\n\nYou won!You are powerful!!!\n\n";
cout <<"\n\nThe answer is: " << wordBank[random] << "\n\n";
}
random = 0;
tries = 0;
cout << "\n\nPlay again(y/n)?";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\n\nThanks for playing!\n\n";
return 0;
}
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string s;
ifstream dict("aaa.txt");
//读出文件中的单词
while (dict>>s)
{
cout << s <<"\n";
}
dict.close();
fstream out("aaa.txt",ofstream::app);
s = "xxxx";
out << s << endl;//往文件中写入xxxx
}
ifstream fp("h.txt");
string wordBank[6];
int i = 1;
while(i <= 6) {
fp >> wordBank[i];
i++;
}
ifstream fp("h.txt");//楼主这不是写了的嘛 用文件流绑定到txt文件 就OK
char s[20];
fp >> s;//在这里写
//同样输出流
ofstream b("h.txt");