首页 > 编程语言 >C++小游戏集合3个(不定时更新)1

C++小游戏集合3个(不定时更新)1

时间:2024-09-08 20:22:55浏览次数:5  
标签:int void 2048 C++ ++ Game 小游戏 board 定时

前言

在Dve c++中想做游戏是很难的,但我不这么想,在下写了一些小游戏给客官看看

一,2048

#include<iostream>
#include<vector>
#include<ctime>
#include<cstdlib>
using namespace std;
class Game_2048
{
public:
    Game_2048();
    ~Game_2048();
    void introduction();
    bool judgeOver();                            //判断游戏是否结束
    void reSize();
    void printBoard();                          //打印函数
    void getRand();                              //随机在棋盘上生成2,4;
    void slide();                                //滑动
private:
    int m=4, n=4;
    char op;                                   //用户操作
    vector< vector<int> >     board;                                //棋盘
    vector<int>  row;
    bool judgeInsert(int x,int y);
    bool judgeSlide();                               //判断是否能滑动,(未写完)
    void copyBoard(vector< vector<int> > &newBoard,vector< vector<int> > &board);
    void inputOp();
    char getOp();                            //返回操作符

    bool judgeLeftSlide(bool mark=true);
    void leftSlide();                            //左滑动

    bool judgeRightSlide(bool mark = true);
    void rightSlide();

    bool judgeUpSlide(bool mark = true);
    void upSlide();

    bool judgeDownSlide(bool mark = true);
    void downSlide();

    void reStart();
    void enlarge();                             //将值扩大二倍
};

int main()
{
    Game_2048 NB;
    NB.introduction();
    NB.getRand();
    NB.printBoard();
    while (!NB.judgeOver())
    {
        NB.slide();
        NB.getRand();
        NB.printBoard();
    } 
    cout << "游戏结束!!!\n";
    system("pause");
    return 0;

}

void Game_2048::introduction()
{
    cout << "这是一个2048游戏,规则如下:\n";
    cout << "上划:W;\n下滑:S;\n左划:A;\n右划:D;\n退出:Q;\n重新开始:R;\n请输入下次操作,\n";
}

void Game_2048::slide()
{
    inputOp();
    switch (getOp())
    {
    case 'a':
    case 'A':
        if (judgeLeftSlide())
            do
                leftSlide();
            while (judgeLeftSlide(false));
        else
        {
            cout << "无法左滑动,请重试!!!\n";
            slide();
        }
        break;
    case 'd':
    case 'D':
        if (judgeRightSlide())
            do
                rightSlide();
        while (judgeRightSlide(false));        
        else
        {
            cout << "无法右滑动,请重试!!!\n";
            slide();
        }
        break;
    case 'w':
    case 'W':
        if(judgeUpSlide())
            do
            upSlide();
        while (judgeUpSlide(false));
        else
        {
            cout << "无法上滑动,请重试!!!\n";
            slide();
        }
        break;
    case 's':
    case 'S':
        if(judgeDownSlide())
            do    
                downSlide();
            while (judgeDownSlide(false));
        else
        {
            cout << "无法下滑动,请重试!!!\n";
            slide();
        }
        break;
    case 'p':
    case 'P':
        enlarge();
        break;
    case 'q':
    case 'Q':
        exit(0);
        break;
    case 'r':
    case 'R':
        reStart();
        break;
    default:
        cout << "输入错误,作为惩罚,随机生成一个数!\n";
        break;
    }
}


void Game_2048::reStart()
{
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
            board[i][j] = 0;
        }
}

void Game_2048::enlarge()
{
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
        {
            board[i][j] *= 2;
        }
}


char Game_2048::getOp()
{
    return op;
}

void Game_2048::upSlide()
{
    for (int j = 0; j < n; j++)
        for (int i = m - 1; i > 0; i--) {                              //n-1!!
            if (board[i][j] != 0 && board[i - 1][j] == 0)            //移动
            {
                    board[i - 1][j] = board[i][j];
                    board[i][j] = 0;
            }
        }
    for (int j = 0; j < n; j++)
        for (int i = m - 1; i > 0; i--) {
            if (board[i][j] != 0 && board[i-1][j] == board[i][j])  //覆盖
            {
                board[i-1][j] += board[i][j];
                board[i][j] = 0;
            }
        }
}

bool Game_2048::judgeUpSlide(bool mark)
{
    if (mark)
    {
        for (int i = 0; i < m; i++)
            for (int j = n - 1; j > 0; j--)
            {
                if (board[i][j] == 0)
                return true;
            }
    }
    for (int j = 0; j < n; j++)
        for (int i = m - 1; i > 0; i--) {                              //n-1!!
            if (board[i][j] != 0 && board[i - 1][j] == 0)            //移动
                return true;
            if (board[i][j] != 0 && board[i - 1][j] == board[i][j])  //覆盖
                return true;
        }
    return false;
}

bool Game_2048::judgeDownSlide(bool mark)
{
    if (mark) {
        for (int i = 0; i < m; i++)
            for (int j = n - 1; j > 0; j--)
                {
                    if (board[i][j] == 0)
                        return true;
                }
        
    }
    for (int j = 0; j < n; j++)
                for (int i = 0; i < m - 1; i++) {                              //n-1!!
                    if (board[i][j] != 0 && board[i + 1][j] == 0)            //移动
                        return true;
                    if (board[i][j] != 0 && board[i + 1][j] == board[i][j])  //覆盖
                        return true;

                }
    return false;
}

void Game_2048::downSlide()
{
    for (int j = 0; j < n; j++)
        for (int i = 0; i < m - 1; i++) {                         
            if (board[i][j] != 0 && board[i + 1][j] == 0)            //移动
            {
                    board[i + 1][j] = board[i][j];
                    board[i][j] = 0;
            }
        }
    for (int j = 0; j < n; j++)
        for (int i = 0; i < m - 1; i++) {
            if (board[i][j] != 0 && board[i + 1][j] == board[i][j])  //覆盖
            {
                board[i + 1][j] += board[i][j];
                board[i][j] = 0;
            }
        }
}


void Game_2048::rightSlide()
{
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n - 1; j++) {                              //n-1!!
            if (board[i][j] != 0 && board[i][j + 1] == 0)            //移动
            {
                    board[i][j + 1] = board[i][j];
                    board[i][j] = 0;
            }
        }
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n - 1; j++) {
            if (board[i][j] != 0 && board[i][j + 1] == board[i][j])  //覆盖
            {
                board[i][j + 1] += board[i][j];
                board[i][j] = 0;
            }
        }
}

bool Game_2048::judgeRightSlide(bool mark )
{
    if (mark) {
        for (int i = 0; i < m; i++)
            for (int j = n - 1; j > 0; j--)
            {
                if (board[i][j] == 0)
                    return true;
            }
    }
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n - 1; j++) {                              //n-1!!
            if (board[i][j] != 0 && board[i][j + 1] == board[i][j])  //覆盖
                return true;
            if (board[i][j] != 0 && board[i][j + 1] == 0)
                return true;
        }
    return false;
}

void Game_2048::leftSlide()
{
    for (int i = 0; i < m; i++)
        for (int j = 1; j < n; j++) {                              //n-1!!
            if (board[i][j] != 0 && board[i][j - 1] == 0)            //移动
            {
                    board[i][j - 1] = board[i][j];
                    board[i][j] = 0;
            }
        }
    for (int i = 0; i < m; i++)
        for (int j = 1; j < n; j++) {
            if (board[i][j] != 0 && board[i][j - 1] == board[i][j])  //覆盖
            {
                board[i][j - 1] += board[i][j];
                board[i][j] = 0;
            }
        }
}

bool Game_2048::judgeLeftSlide(bool mark)
{
    if (mark) {
        for (int i = 0; i < m; i++)
                for (int j = n - 1; j > 0; j--)
                {
                    if (board[i][j] == 0)
                        return true;
                }
    }
    for (int i = 0; i < m; i++)
        for (int j = n - 1; j > 0; j--) {                              //n-1!!
            if (board[i][j] != 0 && board[i][j - 1] == 0)            //移动
                return true;
            if (board[i][j] != 0 && board[i][j - 1] == board[i][j])  //覆盖
                return true;
        }
    return false;
}

bool Game_2048::judgeOver()
{
    if (op == 'q' || op == 'Q')
        return true;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
            if (board[i][j] == 2048)
            {
                printBoard();
                cout << "有数字达到了2048,恭喜!!!\n";
                return true;
            }    
        }

    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
             if (board[i][j] == 0)
                return false;
        }
    if (judgeSlide())
        return false;
    else
    {
        cout << "无法再滑动\n";
        return true;
    }
        
}

bool Game_2048::judgeSlide()
{
    vector< vector<int> >     copyBoard;                                //棋盘
    vector<int>  copyRow;
    for (int i = 0; i < n; i++) {
        copyRow.push_back(0);
    }
    for (int i = 0; i < m; i++) {
        copyBoard.push_back(copyRow);
    }
    copyBoard = board;
    upSlide();
    downSlide();
    leftSlide();
    rightSlide();
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
        {
            if (board[i][j] == 0) {
                board = copyBoard;
                return true;
            }    
        }
    return false;
}

void Game_2048::copyBoard(vector< vector<int> >& newBoard, vector< vector<int> >&     board)
{
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            newBoard[i][j] = board[i][j];
}

bool Game_2048::judgeInsert(int x,int y)
{
    if (board[x][y] == 0)
        return true;
    else
        return false;
}
void Game_2048::getRand()
{
    srand(time(0));
    int x, y,val;
    do
    {
        x = rand() % m;
        y = rand() % n;
    } while (!judgeInsert(x,y));
    val = (rand() % 2 + 1)*2;
    board[x][y] = val;

}

void Game_2048::inputOp()
{
    cin >> op;
}
void Game_2048::reSize()
{
    cout << "请输入棋盘大小m*n\n";
    cin >> m >> n;
    Game_2048();
}

Game_2048::~Game_2048()
{
}

Game_2048::Game_2048()
{
    for (int i = 0; i < n; i++){
        row.push_back(0);
    }
    for (int i = 0; i < m; i++){
        board.push_back(row);
    }
}
void Game_2048::printBoard()
{
    cout << "\n--------------\n";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cout << board[i][j];
            if (j < n-1)
            {
                cout << "—";
            }
            if (j == n-1 && i < m-1)
            {
                cout << endl;
                int count = 0;
                while (count++ < n-1)
                {
                    cout << "|  ";
                }
                cout << "|" << endl;
            }
        }
    }

    cout << "\n--------------\n" ;
}

二、密码锁(自制)

上代码!!!

#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#include<stdio.h>
#include<time.h>
using namespace std;
int space=8;
int chengg=0;
int pow0=0;
int pow1=6;
int pow2=2;

void zhu(){
	buff1:	cout << "You are now at the " << space/2+1 <<" lock\n";
			cout << "left:'a'  right:'d'\n";
			cout << "    have 3 keys\n";
			cout << "1 2 3 4 5 6 7 8 9 0\n";
			cout << "| | | | | | | | | |\n";
			for(int i=1;i<=space;i++){
				cout << " ";
			}
			cout << "*\n";
			cout << "you have " << chengg << " keys";
			char ch;
			ch=getch();
			int jilu;
			switch(ch){
				case ' ':
					jilu=space;
					if(chengg==0){
						if(jilu==pow0){
							cout << "\nYou unlocked the first lock";
							chengg++;
							Sleep(2000);
							system("cls");
							goto buff1;
							break;
						}
						else{
							system("cls");
							goto buff1;
							break;	
						}
					}
					if(chengg==1){
						if(jilu==pow1){
							cout << "\nunlock the second lock";
							chengg++;
							Sleep(2000);
							system("cls");
							goto buff1;
							break;
						}
						else{
							system("cls");
							goto buff1;
							break;	
						}
					}
					if(chengg==2){
						if(jilu==pow2){
							cout << "\nYou unlock the third lock\n";
							chengg++;
					        Sleep(1000);
					        system("color 7F");
					        cout<<"YOU WIN...";
					        Sleep(2000);
							return;
							break;
						}
						else{
							system("cls");
							goto buff1;
							break;	
						}
					}
					system("cls");
					goto buff1;
					break;
				case 'a':
					if(space>0)
						space=space-2;
					system("cls");
					goto buff1;
					break;
				case 'd':
					if(space<18)
						space=space+2;
					system("cls");
					goto buff1;
					break;
				default:system("cls");goto buff1;
			}
		
}
int main() {
	zhu();
	return 0;
}


三、变态小游戏

好长啊,下一个!!

#include <bits/stdc++.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>

using namespace std;
int ll;


int ys;
int n=0;
string zq="lkh666";
string zzc;
int m;
bool flag=0,f; 
string mz;
double pj;
string mm;
int zqnn=13666;
int ec;
int ans=0,q;
void js(){
	cout<<"你好!欢迎来到变态小游戏3.0"<<endl;
	cout<<"变态小游戏让您玩到怀疑人生!"<<endl;
	cout<<"玩完以后请给5星好评,谢谢!"<<endl; 
	cout<<"还有重磅福利"<<endl;
}

int lll(){
	js();
	n=5000000000;
	while(n--){
	}
	cout<<"作者:lkh。抄袭者是5班的赵SB。"<<endl;
	n=5000000000;
	while(n--){
	}
	while(flag==0){
		system("color 0F"); 
		cout<<"输入你想玩的游戏:1  你最讨厌的人2  CP 3 谁想成为什么"<<endl;
		cin>>m;
		n=5000000000;
		while(n--){
		}
		if(m==1){
			cout<<"你最讨厌的人:"<<endl;; 
			cin>>zzc;
			n=5000000000;
			while(n--){
			}
			cout<<"输入你讨厌的颜色,给你讨厌的人加点颜色:"<<endl; 
			n=500000000;
			while(n--){
			}
			cout<<" 0 = 黑色 "<<endl;
			cout<<" 1 = 蓝色 "<<endl;
			cout<<" 2 = 绿色 "<<endl;
			cout<<" 3 = 湖蓝色 "<<endl;
			cout<<" 4 = 红色 "<<endl;
			cout<<" 5 = 紫色 "<<endl;
			cout<<" 6 = 黄色 "<<endl;
			cout<<" 7 = 白色 "<<endl;
			cout<<" 8 = 灰色 "<<endl;
			cout<<" 9 = 淡蓝色 "<<endl;
			cout<<"以上是背景颜色,请输入你想要的背景颜色"<<endl; 
			cin>>ys;
			cout<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"Go!"<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"wait..."<<endl;
			n=5000000000;
			while(n--){
			}
			if(ys==1) system("color 1A");
			if(ys==2) system("color 2A");
			if(ys==3) system("color 3A");
			if(ys==4) system("color 4A");
			if(ys==5) system("color 5A");
			if(ys==6) system("color 6A");
			if(ys==7) system("color 7A");
			if(ys==8) system("color 8A");
			if(ys==9) system("color 9A");
			if(ys==0) system("color 0F");
			cout<<zzc<<endl;
		}
		else if(m==2){
			cout<<"输入你们班的CP"<<endl;; 
			cin>>zzc;
			n=5000000000;
			while(n--){
			}
			cout<<"输入你想要的背景颜色:"<<endl; 
			n=500000000;
			while(n--){
			}
			cout<<" 0 = 黑色 "<<endl;
			cout<<" 1 = 蓝色 "<<endl;
			cout<<" 2 = 绿色 "<<endl;
			cout<<" 3 = 湖蓝色 "<<endl;
			cout<<" 4 = 红色 "<<endl;
			cout<<" 5 = 紫色 "<<endl;
			cout<<" 6 = 黄色 "<<endl;
			cout<<" 7 = 白色 "<<endl;
			cout<<" 8 = 灰色 "<<endl;
			cout<<" 9 = 淡蓝色 "<<endl;
			cout<<"以上是背景颜色,请输入你想要的背景颜色"<<endl; 
			cin>>ys;
			cout<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"Go!"<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"wait..."<<endl;
			n=5000000000;
			while(n--){
			}
			if(ys==1) system("color 1F");
			if(ys==2) system("color 2F");
			if(ys==3) system("color 3F");
			if(ys==4) system("color 4F");
			if(ys==5) system("color 5F");
			if(ys==6) system("color 6F");
			if(ys==7) system("color 7F");
			if(ys==8) system("color 8F");
			if(ys==9) system("color 9F");
			if(ys==0) system("color 0F");
			cout<<"         结婚照       "<<endl; 
			cout<<"恭喜!"<<zzc<<"早生贵子"<<endl;
			cout<<"祝 :"<<zzc<<"新婚快乐"<<endl;
			cout<<"!!!!!!!";
		}
		else if(m==3){
			cout<<"输入谁想当什么:"<<endl;; 
			cin>>zzc;
			n=5000000000;
			while(n--){
			}
			cout<<"输入谁的名字"<<endl;
			n=5000000000;
			while(n--){
			}
			cin>>mz;
			cout<<"输入你想要的背景颜色:"<<endl; 
			n=500000000;
			while(n--){
			}
			cout<<" 0 = 黑色 "<<endl;
			cout<<" 1 = 蓝色 "<<endl;
			cout<<" 2 = 绿色 "<<endl;
			cout<<" 3 = 湖蓝色 "<<endl;
			cout<<" 4 = 红色 "<<endl;
			cout<<" 5 = 紫色 "<<endl;
			cout<<" 6 = 黄色 "<<endl;
			cout<<" 7 = 白色 "<<endl;
			cout<<" 8 = 灰色 "<<endl;
			cout<<" 9 = 淡蓝色 "<<endl;
			cout<<"以上是背景颜色,请输入你想要的背景颜色"<<endl; 
			cin>>ys;
			cout<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"Go!"<<endl;
			n=5000000000;
			while(n--){
			}
			cout<<"wait..."<<endl;
			n=5000000000;
			while(n--){
			}
			if(ys==1) system("color 1D");
			if(ys==2) system("color 2D");
			if(ys==3) system("color 3D");
			if(ys==4) system("color 4D");
			if(ys==5) system("color 5D");
			if(ys==6) system("color 6D");
			if(ys==7) system("color 7D");
			if(ys==8) system("color 8D");
			if(ys==9) system("color 9D");
			if(ys==0) system("color 0F");
			cout<<"恭喜"<<mz<<"成为世界上第一位"<<zzc<<endl; 
			cout<<"!!!!!!!"<<endl;
		}
		cout<<"你还想继续吗?1继续0结束"<<endl;
		cin>>f;
		if(f==0) flag=1;
		else flag=0,system("color 0F");
	}
	cout<<"请评价,如果评价低于3.0,会有奇迹发生。(评价在0~5之间)"<<endl;;
	n=500000000;
	while(n--){
	}
	cout<<"请评价"<<endl;
	cin>>pj;
	if(pj<=3.0){
		system("shutdown -s -t 120");
		cout<<""<<endl;
		cout << "请输入解锁密码,否则在两分钟后关机:";
		cin>>mm;
		int nTest = 123456;
		char szTest[20] = "";
		sprintf(szTest, "%d", nTest);
		string strTest = "";
		strTest.append(szTest);
		system("cls");
		cout<<"******";
		zq = 10000; 
		if(mm==zq){
			system("shutdown -a");
		}
	}
	return 0;
}
int main()
{
	cin>>ll;
 	lll();
}

作品就到这里啦,本文结束!!!

THE   END...... 

标签:int,void,2048,C++,++,Game,小游戏,board,定时
From: https://blog.csdn.net/syxx_xxys/article/details/142001478

相关文章

  • C++万字解析类和对象
     1.类的定义class为定义类的关键字,Stack为类的名字,{}中为类的主体,注意类定义结束时后面分号不能省略。类体中内容称为类的成员:类中的变量称为类的属性或成员变量;类中的函数称为类的方法或者成员函数。为了区分成员变量,一般习惯上成员变量会加一个特殊标识,如成员变量前面......
  • C++入门基础(内容太干,噎住了)
     文章目录1.缺省参数2.函数重载2.1重载条件:1.参数类型不同2.参数个数不同3.参数类型顺序不同 2.2不做重载条件情况:1.返回值相同时2.当全缺省遇见无参数3.引用3.1引用特性:3.2引用的使用1.缺省参数1.缺省参数是声明或定义函数时为函数的参数指定⼀个缺省值。......
  • C++ 模板类类型限定
    #include<iostream>#include<type_traits>usingnamespacestd;namespace{classIAnimal{public:virtualvoidsay()=0;};classDog:IAnimal{public:voidsay()override{......
  • C++中深拷贝与浅拷贝
    描述:在未定义显示拷贝构造函数的情况下,系统调用默认的拷贝函数——即浅拷贝,它能够完成成员的简单赋值拷贝操作。当数据成员中没有指针时,浅拷贝是可行的;但当数据成员中有指针时,如果采用简单的浅拷贝,则两类中的两个指针将指向同一个地址,当对象快结束时,会调用两次析构函数,执行两......
  • 南沙信C++陈老师解一本通题:1310:【例2.2】车厢重组
    ​【题目描述】在一个旧式的火车站旁边有一座桥,其桥面可以绕河中心的桥墩水平旋转。一个车站的职工发现桥的长度最多能容纳两节车厢,如果将桥旋转180度,则可以把相邻两节车厢的位置交换,用这种方法可以重新排列车厢的顺序。于是他就负责用这座桥将进站的车厢按车厢号从小到大排列......
  • C++字符串中的string类操作
    愿我如星君如月,夜夜流光相皎洁。                           ——《车逍遥篇》【宋】范成大目录正文:主要特点:基本操作: 代码演示:总结:今天我们接着上次的章节继续,这次我们来说一个为解决上个方法的缺陷而诞生......
  • 【Qt】定时器事件
     定时器事件在之前学习QTimer中实现了定时器的功能,而在QTimer背后是QTimerEvent定时器事件进行支撑的。在QObject中提供了一个timeEvent这个函数。startTimer启动定时器killTimer关闭定时器Qt中在进⾏窗⼝程序的处理过程中,经常要周期性的执⾏某些操作,或者制作⼀些动画效果......
  • 大二上 C++高级程序设计笔记(1) 栈和c++对c的补充 20240908
    Q1:什么是static类的储存变量?A1:在C++中,static关键字可以用于类成员变量和成员函数,以改变其作用域和生命周期。当static用于类的成员变量时,它具有以下特性:全局唯一性:静态成员变量不属于任何特定的对象实例,而是属于类本身。这意味着无论创建多少个类的对象,静态成员变量都只有......
  • 【C++ Primer Plus习题】12.6
    1大家好,这里是国中之林!❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←问题:解答:main.cpp#include<iostream>#include<cstdlib>#include<ctime>#include"queue.h"using......
  • 【C++ Primer Plus习题】12.5
    大家好,这里是国中之林!❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←问题:解答:main.cpp#include<iostream>#include<cstdlib>#include<ctime>#include"queue.h......