小羊驼的家

我的第一个C++小游戏

也许算是一个游戏吧。

此程序写于2019年7月26日10:21:29,我当时还没有学到C++的函数用法,此程序可能显得有些冗长,不过可以正常运行,并且已经具有了些许的可玩性。希望有朝一日,我有能力继续完善这个程序,编程不仅仅是我的一个业余爱好,更是我现今的目标。

以下内容为此程序的源代码:

#include <iostream>
using namespace std;

int main()
{
	cout << "-------------------------------\n";
    cout << "欢迎进入--23根火柴游戏\n"; 
	cout << "此游戏来自--羊驼哥已被注册\n";
	cout << "-------------------------------\n";
	cout << "游戏规则说明:\n";
	cout << "桌子上共有23根火柴,有两个玩家轮流\n";
	cout << "取走一根,两根或三根火柴。\n";
	cout << "您是第一个取走火柴的玩家,电脑将\n";
	cout << "扮演第二个玩家,当其中一个玩家\n";
	cout << "取到最后一根火柴时,这个玩家就会\n";
	cout << "输掉这局游戏。\n";
	cout << "这就是全部规则,祝您玩的愉快。\n";
	cout << "-------------------------------\n";
	cout << endl;
	cout << endl;

    int   all_the_number = 23, the_number_get_out = 0, number = 1, computer_get_out = 0;

	while (all_the_number > 3)
	{
		cout << "-------------------------------\n";
		cout << "第" << number << "回合\n";
		cout << "请输入您要取走火柴的数量:";
		cin >> the_number_get_out;
		
		while (!(the_number_get_out == 1|| the_number_get_out == 2|| the_number_get_out == 3))//判断玩家是否取得合适数量的火柴
		{
			cout << endl;
			cout << "请您遵守游戏规则!\n";
			cout << "请输入您要取走火柴的数量:";
			cin >> the_number_get_out;
		}

	    number++;

		if (the_number_get_out == 1 || the_number_get_out == 2 || the_number_get_out == 3)//玩家可以取走一根,两根或三根火柴
		{
			computer_get_out = 4 - the_number_get_out;//计算机要保证每一回合都会一共取走四根火柴
			all_the_number = all_the_number - 4;

			cout << endl;

			cout << "计算机取走了"<< computer_get_out <<"根火柴!\n";
			cout << "桌子上还剩"<< all_the_number <<"根火柴!\n";
			cout << "-------------------------------\n";
			cout << endl;
		}
		if (all_the_number == 3)//当只剩下三根火柴时,决胜局开始
		{
			cout << "-------------------------------\n";
			cout << "第6回合\n";
			cout << "请输入您要取走火柴的数量:";
			cin >> the_number_get_out;

		    while (!(the_number_get_out == 1 || the_number_get_out == 2 || the_number_get_out == 3))//判断玩家是否取得合适数量的火柴
		    {
		    cout << endl;
		    cout << "请您遵守游戏规则!\n";
		    cout << "请输入您要取走火柴的数量:";
		    cin >> the_number_get_out;
		    }
			if (the_number_get_out == 1)
			{
				cout << "桌子上还剩2根火柴!\n";
				cout << "只见计算机毫不犹豫的就有了其中的一根!\n";
				cout << endl;
				cout << "*******************************\n";
				cout << "非常遗憾,你输了这场游戏!\n";
				cout << "*******************************\n";
				cout << "你在这场游戏中一共玩了6个回合\n";
				cout << "*******************************\n";
			}
			if (the_number_get_out == 2)
			{
				cout << "桌子上还剩1根火柴!\n";
				cout << endl;
				cout << "*******************************\n";
				cout << "你真厉害,打败了计算机!\n";
				cout << "*******************************\n";
				cout << "你在这场游戏中一共玩了7个回合\n";
				cout << "*******************************\n";
			}
			if (the_number_get_out == 3)
			{
				cout << "哎,桌子上怎么没有火柴了?\n";
				cout << "哦,原来是你把最后一根取走了!\n";
				cout << endl;
				cout << "*******************************\n";
				cout << "你输了这场游戏\n";
				cout << "*******************************\n";
				cout << "你在这场游戏中一共玩了6个回合\n";
				cout << "*******************************\n";
			}
		}
    }
	cout << endl;
	cout << "感谢您参与我设计的这个简单游戏\n";//退出时的问候语
	cout << "-------------------------------\n";
	cout << "请按任意键以退出\n";
	cin.get();
	cout << "再见!\n";

	return 0;
}

下图是此程序的运行效果:

#C++ #梦始之地