Ps:

有人可能好奇我为什么大费周章的去写主函数,而不直接把所有操作放在类中,Why?类就好比骨干(存放核心的数据和功能),最好提供接口, 避免了类中的输入输出,当然,这也不是绝对的。

C++:

#include<iostream>
#include<string>
#include<ctime>
using namespace std;
class GuessNumber {
public:
void Get();
int Guess(char);
string get_g_word() {
return g_word;
}
string get_word() {
return word;
}
int get_miss() {
return miss;
}
private:
int miss = 0;
string words[7] = { "write","that","important","transform","judge","index","run" };
string word;
string g_word;
};
void GuessNumber::Get() {
srand((unsigned int)time(0)); //随机数
word = words[rand() % 7 ]; //伪随机数?——跟运行时间有关
g_word=string(word.length(), '*');
}
int GuessNumber::Guess(char ch) {
if (word.find(ch) == string::npos) { //字母没有出现在单词中
miss++;
return 2;
}
if (g_word.find(ch) != string::npos) { //已猜过(出现在g_word中)
miss++;
return 1;
}
for (int i = 0; i < word.length(); i++) //猜中,修改g_word
if (word[i] == ch)
g_word[i] = ch;
return 0;
}
int main() {
char ch;
string str;
GuessNumber My;
int key;
while (1) {
My.Get();
while (My.get_g_word() != My.get_word()) {
cout << "(Guess) Enter a letter in word " << My.get_g_word() << " > ";
cin >> ch;
key = My.Guess(ch);
if (key == 2)
cout << " " << ch << " is not in the word" << endl;
else if (key == 1)
cout << " " << ch << " is already in the word" << endl;
}
cout << "The word is " << My.get_word() << ". You missed " << My.get_miss() << " time " << endl;
cout << "Do you want to guess another word? Enter y or n >";
cin >> str;
if (str == "n")
break;
system("cls"); //清屏
} return 0;
}

Java:

请自己写。

Java 7.35 游戏:猜字游戏(C++&Java)的更多相关文章

  1. 猜字游戏java

    一.实践目的 1.掌握基本输入输出. 2.掌握方法定义与调用,理解参数传递方式. 3.掌握数组的声明.定义与初始化,数组的处理. 4.掌握数组作为方法参数和返回值. 二.实践要求 利用方法.数组.基本 ...

  2. 044 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 06 使用do-while循环实现猜字游戏

    044 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 06 使用do-while循环实现猜字游戏 本文知识点:do-while循环深入运用 案例练习 案例 ...

  3. Leetcode 299.猜字游戏

    猜字游戏 你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为"Bulls&q ...

  4. 《Java语言程序设计》编程练习8.9(游戏:#字游戏)

    8.9 (游戏:#字游戏)在并字游戏中,两个玩家使用各自的标志(一方用X则另一方就用O),轮流填写3x3的网格中的某个空格.当一个玩家在网格的水平方向.垂直方向或者对角线方向上出 现了三个相同的X或三 ...

  5. C - 一个C语言猜字游戏

    下面是一个简陋的猜字游戏,玩了一会儿,发现自己打不过自己写的游戏,除非赢了就跑,最高分没有过1000. 说明:srand(time(NULL))和rand(),srand,time和rand都是函数, ...

  6. CASIO 5800P计算器游戏--猜数字游戏

    CASIO 5800P 计算器游戏--猜数字游戏原代码 我编的计算器小游戏--猜数字游戏 LbI I↙ "xxGUESS NUMBERxx xPROGRAMMER:JCHx -------- ...

  7. 51 Nod 不一样的猜字游戏

    1536 不一样的猜数游戏  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 瓦斯亚和皮台亚在玩一个简单的游戏.瓦 ...

  8. Java实现 LeetCode 299 猜数字游戏

    299. 猜数字游戏 你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为"B ...

  9. 初识Java,猜字游戏

    import java.util.*; public class caizi{ public static void main(String[] args){ Scanner in=new Scann ...

随机推荐

  1. Html - Table 表头固定和 tbody 设置 height 在IE不起作用的解决

    原文地址,转载请注明出处:http://www.cnblogs.com/jying/p/6294063.html 做项目的时候发现给 tbody设置 height 和 overflow-y 在IE下不 ...

  2. JavaScriptConverter

    public class DatePartsConverter : JavaScriptConverter { public override IEnumerable<Type> Supp ...

  3. Java中for循环中的的try-catch

    异常处理 当for循环遇上try-catch @Test public void forThrow(){ final int size = 6; for (int i=0; i<size; i+ ...

  4. Linux 安装nginx 及配置

    安装openssl库   # yum install -y openssl openssl-devel 安装gcc     # yum install gcc-c++ 安装 PCRE    # yum ...

  5. 移动端web页面滚动不流畅,卡顿闪烁解决方案

    移动端web页面滚动不流畅,卡顿闪烁解决方案   1.ios端的-webkit-overflow-scrolling属性可控制页面滚动效果,设置如下实现惯性滚动和弹性效果: -webkit-overf ...

  6. mysql 定时备份任务

    备份方案: 本地备份并同步至远程服务器,保留30天数据 1. 本地数据库备份,备份数据库gold_ecooy,naiang#!/bin/bash#xliang#Created Time: 2018-1 ...

  7. TOJ 4383 n % ( pow( p , 2) ) ===0

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4383 描述 There is a ...

  8. View 常用方法

    id layout_width layout_height layout_margin.layout_marginTop minWidth minHeight background layout_gr ...

  9. 大数据入门推荐 - 数据之巅 大数据革命,历史、现实与未来等五本PDF

    扫码时备注或说明中留下邮箱付款后如未回复请至https://shop135452397.taobao.com/联系店主

  10. 关于vs2010开发的ASP项目部署到XPSP2系统上出现找不到Reportviewer.XX.文件的解决方案

    尝试方法如下: 1.将webform.dll.winform.dll.common.dll三个引用直接复制到服务器的Bin目录,未解决问题,提示无法正确加载,程序及已关闭等. 2.SQLSysClrT ...