猜單詞遊戲

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define YES 1
#define NO 0
void menu();
int  sjs();
void js();
void tc();
void startgame();
bool quit=1;
int i=1;
char str[20][10]={"getby","honey","about","canoe","ski","hawk","haul","firewood","of","overdue","on","indoor","over","pursue","oversee","organ","stack","wicked","blast","between"};
void main(){
	while(quit){
		menu();
		cout<<"請選擇一種操作: ";
		int a;
		cin>>a;
		switch(a){
			case 1:{
				js();
				break;
			}
			case 2:{
				cout<<"你是第"<<i<<"個玩家"<<endl;
				startgame();
				i++;
				cout<<"想再玩一次嗎?:(y/n)";
				char op;
				cin>>op;
				if(op=='n')
					quit=0;
				break;


			}
			case 3:
				quit=0;
		}
	}
		cout<<"遊戲結束,想玩更多遊戲,請上http://www.sanri.com"<<endl;
		getchar();
}
void menu(){
	cout<<endl;
	cout<<"************************~~歡迎進入sanri猜單詞遊戲~~**************************"<<endl;
	cout<<"*****************************菜單如下,請選擇:*****************************"<<endl;
	cout<<endl;
	cout<<"*****************************·*1:遊戲說明*·*********************************"<<endl;
	cout<<"*****************************·*2:開始遊戲*·*********************************"<<endl;
	cout<<"*****************************·*3:退出*·*********************************"<<endl;
}
int sjs(){
	int n;
	srand(time(NULL));
	n=rand()%20;
	return n;
}
void js(){
	cout<<endl;
	cout<<"遊戲規則如下:"<<endl;
	cout<<"系統有20個已設置好了的單詞,遊戲開始後,系統將隨機從中抽取,玩家每次從鍵盤輸入一個字母進行猜測,如果這個字母不在單詞中系統提示玩家不對,玩家繼續猜測,猜詞次數由玩家自行設置,直到玩家在規定的次數裏猜出來了單詞中的所有字母或者超出遊戲次數,則遊戲結束."<<endl;
	cout<<endl;
}
void startgame(){
	char word[10];
	int have;       //have用來標識字母是否猜對
	int flag[10];   //標記每個字母
	int win=NO,all_win=YES,lenth=0,b,d;
	char ch;
	int right=0,wrong=0;
	b=sjs();
	for(int i=0;str[b-1][i]!='\0';i++){
		word[i]=str[b-1][i];
		lenth++;
	}
	for(int j=0;j<lenth;j++)
		flag[j]=0;
	cout<<"*******************************開始遊戲*******************************"<<endl;
	cout<<"你抽中的是第"<<b<<"個單詞"<<endl;
	cout<<"抽中的單詞長度爲:"<<lenth<<endl;
	cout<<"你準備猜多少次: ";
	cin>>d;
	while(d<=10){
		cout<<"爲確保遊戲的正常運行,請重新輸入一個稍大的猜詞次數"<<endl;
		cout<<"你準備猜多少次: ";
		cin>>d;
	}
	//system("pause");
	while(win==NO&&d){
		cout<<"你還剩"<<d<<"次"<<endl;
		cout<<"你要猜的單詞大概是這個樣子"<<endl;
		for(int i=0;i<lenth;i++){
			if(flag[i]==1)
				putchar(word[i]);
			else{
				putchar('_');
				putchar(' ');
			}
		}
		cout<<endl;
		cout<<"請輸入一個字母用來猜測:";
		cin>>ch;
		getchar();
		for(int k=0;k<lenth;k++){
			if(flag[k]==0&&ch==word[k]){
				flag[k]=1;
				have=YES;                  //猜對了
				right++;
				d--;
			}
		}
			if(have==NO){
				cout<<"對不起,你猜錯了,下次加油哦!";
				wrong++;
				d--;
			}
			else{
				cout<<"你猜對了,不錯哦"<<endl;
				cout<<"你猜的字母在這個單詞裏面"<<endl;
			}
			have=NO;
			for(int m=0;m<lenth;m++){
				if(flag[m]!=1){
					all_win=NO;
					break;
				}
			}
			if(all_win)
				win=YES;
			all_win=YES;
	}
	if(win==YES){
		cout<<"你贏了,恭喜你"<<endl;
		cout<<"這個單詞是:";
		for(int n=0;n<lenth;n++)
			putchar(word[n]);
		cout<<endl;


	}
	else{
		cout<<"不好意思,你輸了"<<endl;
		cout<<"這個單詞是:";
		for(int n=0;n<lenth;n++)
			putchar(word[n]);
		cout<<"你猜錯了"<<wrong<<"次";
		cout<<"你猜對了"<<right<<"次";
	}
}
	


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章