C/C++ Number Game

下一版本

介绍

这是一个双人数字游戏,这个程序刚开始会让你输入一个数,表示序列的长度,然后摁下Enter,接着第一个人在序列中输入1 ~ 2个1,第二个人在序列中输入1 ~ 2个0,直到序列无空间为止,最后一个输入的人算输。

代码

最终版本:

#include<bits/stdc++.h>
//#include<windows.h>
#include <conio.h>
#include <sstream>
using namespace std;

char PrN , o ;
int PrNN[2] ;
string s , n2 ;
int n , vn ;
char c;
void output1()
{
	cout << "Roules:" << endl 
	<< "1. You can choose the first or second to enter;" << endl
	<< "2. Player 1 enters 1 and player 2 enters 0;" << endl 
	<< "3. You can choose to enter one or two at a time;" << endl 
	<< "4. The last person to enter will lose the game." << endl ;
	cout << "Please tell me how long you want the string to be for typing" << ":" ;
//	if(n != 0 && n == vn)
//	{
//		cout << "There are " << n <<" characters left." << endl ;
//	}
}
void output2()
{
	cout << vn <<endl ;
	/*
	if(n != 1)
{
	cout << "There are " << n <<" characters left." << endl ;
	}
	else 
{
	cout <<"There are 1 character left." <<endl ;
	}
	*/
}
int main()
{
//	while (1)
//	{
	cout << "Before begin the game , it is recommended that the length of the string be limited to one line." << endl ;
	cout << "Plese enter 'o' to next: " ;
	nback:;
	o = getchar() ;
	if(o == 'o')
	{
		system("cls") ;
	}
	else 
	{
		goto nback ;
	}
	brk:;
	pd:;
	s = "" ;
	n = 0 ;
	PrNN[1] = 0 ;
	PrNN[2] = 0 ;
	output1() ;
//	n2 = getchar() ;
//	if (n2 <= '9' && n2 >= '0')
//	{
//		
//	}
//	cin >> n ;
	
//	bool isdigit(char c) ;// 检查 c 是否数字
//	bool isascii(char c) ;// 检查 c 是否ascii字符
//	bool islower(char c) ;// 检查 c 是否小写字母
//	bool isupper(char c) ;// 检查 c 是否大写字母
	
//	scanf("%d" , &n) ;
	cin >> n2 ;
	c = n2[0] ;
	if( isdigit(c) )
	{
		stringstream geek(n2) ;
		geek >> n ;
//		cout << endl ;
	}
	else 
	{
		system("cls") ;
		goto pd ;
	}
	if (n == 0)
	{
		n = 0 ;
		system("cls") ;
		goto pd ;
	}
	vn = n ;
	while (n--)
	{ 
		code: ;
//		if (s.size() != 0)
//		{
//		}
		PrN = getch() ;
		if (PrN != '1' and PrN != '0')
		{
			system("cls") ;
			output1() ; output2() ;
//			cout << '\r' ;
//			int m = s.size() + 1 ;
//			while (m--)
//			{
//				cout << ' ' ;
//			}
//			cout << '\r' ;
			cout << s ;
			goto code;
		}
		if (PrN == '1')
		{
			PrNN[0] = 0 ;
			PrNN[1] ++ ;
			if (PrNN[1] > 2)
			{
				system("cls") ;
				output1() ; output2() ;
//				cout << '\r' ;
//				int m = s.size() + 1 ;
//				while (m--)
//				{
//					cout << ' ' ;
//				}
//				cout << '\r' ;
				cout << s ;
				goto code;
			}
			else{
				s += PrN ;
///				system("cls") ;
///				output1() ; output2() ;
///				cout << '\r' ;
///				cout << s ;
				cout << PrN ;
			}
		}
		else 
		{
			PrNN[1] = 0 ;
			PrNN[0] ++ ;
			if (PrNN[0] > 2)
			{
				system("cls") ;
				output1() ; output2() ;
//				cout << '\r' ;
//				int m = s.size() + 1 ;
//				while (m--)
//				{
//					cout << ' ' ;
//				}
//				cout << '\r' ;
				cout << s ;
				goto code;
			}
			else{
				s += PrN ;
///				system("cls") ;
///				output1() ; output2() ;
//				cout << '\r' ;
///				cout << s ;
				cout << PrN ;
			}
		}
	}
//		cin >> c ;
//		if (s[s.size()] == '1')
	if (PrN == '1')
	{
		cout << endl ;
		cout << "Player 2 win!" << endl ;
	}
	else 
	{
		cout << endl ;
		cout << "Player 1 win!" << endl ;
	}
//		Sleep (1000) ;
	cout << "Plese press the o to next , or press the q to quit:" ;
	back:;
	o = getchar() ;
	if (o == 'o')
	{
		cout << 'o' << endl ;
		system("cls");
		goto brk ;
	}
	else if (o == 'q') 
	{
		goto en ;
	}
	else 
	{
		goto back ;
	}
//	}
	en:;
	return 0;
}
友情链接:https://jasonxing.eu.org/

posted on 2024-02-01 13:30  清隐  阅读(14)  评论(0)    收藏  举报

导航