猜词游戏

编程实现猜词游戏

例:对于单词“hello”,程序提示输出:五个?,等待用户输入。用户输入时,若单词包含该字母,如“l”,则程序显示输出“??ll?”;若单词不含该字母,如“a”,则程序提示用户猜错。继续等待用户输入,直到用户猜出全部字母,或输入错误次数超过最大允许出错次数,游戏结束。
注:
1) 单词由程序内定,由全小写字母组成
2) 提示输出问号数量等于单词长度
3) 最大允许出错次数等于单词长度



// WordGuess.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


//check if cheating
bool checkCheating(const string strInput, const string strResult)
{
int rightMax = 0;
if (strInput.size() > strResult.size())
{
for (int i = 0; i < strResult.size(); ++i)
{
for (int j = 0; j < strInput.size(); ++j)
{
if (strResult[i] == strInput[j])
{
rightMax++;
break;
}
}
}


if (rightMax == strResult.size())
{
return true;
}
else
{
return false;
}
}


return false;
}


bool checkWordValidity(const string strResult)
{
bool isValid = true;


for (int i =0; i < strResult.size(); ++i)
{
if (strResult[i] < 'a' || strResult[i] > 'z')
{
isValid = false;
cout << "invalid word!" << endl;
break;
}
else
{
isValid = true;
}
}


return isValid;
}


int _tmain(int argc, _TCHAR* argv[])
{
//input one word
fstream fIn;
string strResult;
string strDisplay;
string strInput;
bool isRight = false;
int errorMax = 0;
bool isValid = true;
bool isCheating = false;


//cout << "please input one word you will guess: ";
//getline(cin, strResult);//can input space


check the word's validity
//while (true)
//{
// isValid = checkWordValidity(strResult);
// if (!isValid)
// {
// cout << "please input one word you will guess: ";
// getline(cin, strResult);//can input space
// }
// else
// {
// break;
// }
//}


//read words.txt
fIn.open("d://words.txt", ios::in);


if (!fIn)
{
cerr << "open file failed!" << endl;
return 0;
}


while (!fIn.eof())
{
getline(fIn, strResult);//read one line from words.txt

//init the displayed word
strDisplay.clear();
for (int i = 0; i < strResult.size(); ++i)
{
strDisplay += '?';
}




//guess this word
cout << "the word is:    " << strDisplay << endl;
cout << "you guess: ";
getline(cin, strInput);


//check if cheating
isCheating = checkCheating(strInput, strResult);
if (isCheating)
{
cout << "oh, you are cheating!" << endl;
return 0;
}


//guess the word
while (strDisplay != strResult)
{
for (int i = 0; i < strInput.size(); ++i)
{
for (int j = 0; j < strResult.size(); ++j)
{
if (strInput[i] == strResult[j])
{
strDisplay[j] = strResult[j];
isRight = true;
}
}


}


cout << "the word is:    " << strDisplay << endl;


if (!isRight)
{
errorMax++;
cout << "wrong guess! you have only " << 
strResult.size() - errorMax << " times!" << endl;
}


if (strResult == strDisplay)
{
cout << "perfect!" << endl << endl;
break;
}


if (errorMax == strResult.size())
{
cout << "game over!" << endl;
return 0;
}
else
{
cout << "you guess: ";
getline(cin, strInput);


isCheating = checkCheating(strInput, strResult);
if (isCheating)
{
cout << "oh, you are cheating!" << endl;
return 0;
}


isRight = false;
}


}
}


return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值