C++自创棋类小游戏

今天给大家带来我花了1周时间自创的小游戏,博主还是一名小学生,希望大家提提意见。这是我写的最长的C++代码,希望大家喜欢,不要抄袭,代码共1328行,任何编译器都可以。

#include
#include
#include
#include 
using namespace std;
#define Forij(x) for(int i=1;i<=x;i++) for(int j=1;j<=x;j++)
#define N 25 
int v;
char* u;
int fx[4][2]={{1,1},{1,0},{0,1},{1,-1}};
int Q,GG;
string C[20]={"●","○","﹢","═","║","╔","╚","╗","╝","·"};
void color(int a){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);} 
void gotoxy(int x,int y){COORD pos;pos.X=2*x;pos.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}
struct Gomoku
{
    int m[50][50],nx,ny;
    void reset()
	{
        system("cls");
        memset(m,-1,sizeof(m));
        color(7);
        for (int i=1; i<=N; i++)
		{
            gotoxy(0,i);cout<N || yy>N) break;
                    if (m[xx][yy]==(lst^1)){break;}
                    if (m[xx][yy]==lst) tmp++;
                    xx+=fx[Fx][0],yy+=fx[Fx][1];
                }
                if (tmp==5){return lst;}
            }
        }
        return -1;
    }
    int arnd(int x,int y)
	{
        int cnt=0;
        for (int i=x-1; i<=x+1; i++) if (i>0 && i<=N)
            for (int j=y-1; j<=y+1; j++) if (j>0 && j<=N)
                if (m[i][j]>-1) cnt++;
    }
    void get_val(int x,int y,int &val)
	{
        val=0;
        Forij(N)
		{
            for (int Fx=0,tmp,tk,xx,yy; Fx<4; Fx++)
			{
                xx=i,yy=j,tmp=tk=0;
                for (int k=1; k<=5; k++)
				{
                    if (xx>N || yy>N){tmp=0; break;}
                    if (m[xx][yy]==(x^1)){tmp=0; break;}
                    if (m[xx][yy]==x) tmp++,tk+=(1<<(k-1));
                    xx+=fx[Fx][0],yy+=fx[Fx][1];
                }
                switch(tmp)
				{
                    case 5:val+=800000000; break; 
                    case 4:val+=1000+350*y; break;
                    case 3:val+=(tk==14)?(300+600*y):(300+200*y); break;
                    case 2:val+=3+2*y; break;
                    case 1:val+=1+y; break;
                }
            }
        }
    }
    void AI(int x)
	{
        int best,brnd,bi,bj,v1,v2,kkk;
        best=-2147483647;brnd=-2147483647;
        Forij(N) 
		if (m[i][j]==-1)
		{
            m[i][j]=x;
            get_val(x,10,v1);get_val(x^1,80,v2);
            if (v1-v2>best) bi=i,bj=j,best=v1-v2;
            if (v1-v2==best)
                if ((kkk=arnd(i,j))>brnd)
                    brnd=kkk,bi=i,bj=j;
            m[i][j]=-1;
        }
        _drop(x,bi,bj);
    }
    void HM(int x)
	{
        char ch=getch();bool t=224;
        for (;;ch=getch())
		{
            if (ch=='w'||ch=='W'||ch==72&&t) {if (ny>1) ny--;}
            else if (ch=='s'||ch=='S'||ch==80&&t) {if (ny1) nx--;}
            else if (ch=='d'||ch=='D'||ch==77&&t) {if (nx= 4 && endx <= 6) && (endy >= 1 && endy <= 3 || endy >= 8 && endy <= 10))
		{
			return true;
		}
		return false;
	}
	~General()
	{
		Chessboard::End = false;
	}
};
class BodyGuard :public Chess
{
public:
	BodyGuard(int i) :Chess((i == 0 ? -2 : 2)) {}
	bool Judgement(Chessboard& ch, int startx, int starty, int endx, int endy)
	{
		int TempX = startx - endx;
		int TempY = starty - endy;
		int S_Id = ch.Getid(startx, starty);
		int E_Id = ch.Getid(endx, endy);
		if ((S_Id*E_Id <= 0) && (TempX*TempX + TempY * TempY == 2) && (endx >= 4 && endx <= 6) && (endy >= 1 && endy <= 3 || endy >= 8 && endy <= 10))
		{
			return true;
		}
		return false;
	}
};
class Chancellor :public Chess
{
public:
	Chancellor(int i) :Chess((i == 0 ? -3 : 3)) {}
	bool Judgement(Chessboard& ch, int startx, int starty, int endx, int endy)
	{
		int TempX = startx - endx;
		int TempY = starty - endy;
		int S_Id = ch.Getid(startx, starty);
		int E_Id = ch.Getid(endx, endy);
		if ((S_Id*E_Id <= 0) && (TempX*TempX + TempY * TempY == 8) && (endx % 2 != 0 && endx >= 1 && endy <= 9) && ((starty - 1) / 5 == (endy - 1) / 5) && !ch.Get(startx + (TempX / 2), starty + (TempY / 2)))
		{
			return true;
		}
		return false;
	}
};
class Horse :public Chess
{
public:
	Horse(int i) :Chess((i == 0 ? -4 : 4)) {}
	bool Judgement(Chessboard& ch, int startx, int starty, int endx, int endy)
	{
		int TempX = startx - endx;
		int TempY = starty - endy;
		int S_Id = ch.Getid(startx, starty);
		int E_Id = ch.Getid(endx, endy);
		if ((S_Id*E_Id <= 0) && (TempX*TempX + TempY * TempY == 5) && !ch.Get(startx + (TempX / 2), starty + (TempY / 2)))
		{
			return true;
		}
		return false;
	}
};
class Chariot :public Chess
{
public:
	Chariot(int i) :Chess((i == 0 ? -5 : 5)) {}
	bool Judgement(Chessboard& ch, int startx, int starty, int endx, int endy)
	{
		int TempX = startx - endx;
		int TempY = starty - endy;
		int S_Id = ch.Getid(startx, starty);
		int E_Id = ch.Getid(endx, endy);
		if ((S_Id*E_Id <= 0) && (!(TempX&&TempY)) && (TempX + TempY))
		{
			if (TempX)
			{
				int Sign = (TempX > 0 ? -1 : 1);
				for (int i = 1; i < fabs(TempX); i++)
				{
					if (ch.Get(startx + Sign * i, starty))
					{
						return false;
					}
				}
			}
			else
			{
				int Sign = (TempY > 0 ? -1 : 1);
				for (int i = 1; i < fabs(TempY); i++)
				{
					if (ch.Get(startx, starty + Sign * i))
					{
						return false;
					}
				}
			}
			return true;
		}
		return false;
	}
};
class Cannon :public Chess
{
public:
	Cannon(int i) :Chess((i == 0 ? -6 : 6)) {}
	bool Judgement(Chessboard& ch, int startx, int starty, int endx, int endy)
	{
		int TempX = startx - endx;
		int TempY = starty - endy;
		int S_Id = ch.Getid(startx, starty);
		int E_Id = ch.Getid(endx, endy);
		if ((S_Id*E_Id <= 0) && (!(TempX&&TempY)) && (TempX + TempY))
		{
			int Tmp = 0;
			if (TempX)
			{
				int Sign = (TempX > 0 ? -1 : 1);
				for (int i = 1; i < fabs(TempX); i++)
				{
					if (ch.Get(startx + Sign * i, starty))
					{
						Tmp++;
					}
				}
			}
			else
			{
				int Sign = (TempY > 0 ? -1 : 1);
				for (int i = 1; i < fabs(TempY); i++)
				{
					if (ch.Get(startx, starty + Sign * i))
					{
						Tmp++;
					}
				}
			}
			if (E_Id)
			{
				if (Tmp == 1)
				{
					return true;
				}
			}
			else
			{
				if (!Tmp)
				{
					return true;
				}
			}
		}
		return false;
	}
};
class Soldier :public Chess
{
public:
	Soldier(int i) :Chess((i == 0 ? -7 : 7)) {}
	bool Judgement(Chessboard& ch, int startx, int starty, int endx, int endy)
	{
		int TempX = startx - endx;
		int TempY = starty - endy;
		int S_Id = ch.Getid(startx, starty);
		int E_Id = ch.Getid(endx, endy);
		if ((S_Id*E_Id <= 0) && (S_Id*TempY <= 0))
		{
			if (fabs(TempY) == 1 && TempX == 0)
			{
				return true;
			}
			if (fabs(TempX) == 1 && TempY == 0)
			{
				if (((starty - 1) / 5 == 0 && S_Id < 0) || ((starty - 1) / 5 == 1 && S_Id > 0))
				{
					return true;
				}
			}
		}
		return false;
	}
};
int Chessboard::Player = -1;
bool Chessboard::End = true;
inline Chessboard::Chessboard()
{
	memset(c, NULL, sizeof(c));
}
inline Chess * Chessboard::Get(int x, int y)
{
	return c[x][y];
}
int Chessboard::Getid(int x, int y)
{
	if (c[x][y] != NULL)
	{
		return c[x][y]->Get();
	}
	return NULL;
}
bool Chessboard::Move(int startx, int starty, int endx, int endy)
{
	if (startx >= 1 && startx <= 9 && starty >= 1 && starty <= 10 && endx >= 1 && endx <= 9 && endy >= 1 && endy <= 10 && Getid(startx, starty) && Getid(startx, starty)*Player > 0 && c[startx][starty]->Judgement(*this, startx, starty, endx, endy))
	{
		if (c[endx][endy] != NULL)
		{
			delete c[endx][endy];           
		}
		c[endx][endy] = c[startx][starty];
		c[startx][starty] = NULL;
		Player *= -1;                       
		return true;
	}
	else
	{
		cout << "走法错误,请重新输入:" << endl;
		return false;
	}
}
void Chessboard::Init()
{
	c[1][1] = new Chariot(1);
	c[9][1] = new Chariot(1);
	c[2][1] = new Horse(1);
	c[8][1] = new Horse(1);
	c[3][1] = new Chancellor(1);
	c[7][1] = new Chancellor(1);
	c[4][1] = new BodyGuard(1);
	c[6][1] = new BodyGuard(1);
	c[5][1] = new General(1);
	c[2][3] = new Cannon(1);
	c[8][3] = new Cannon(1);
	c[1][4] = new Soldier(1);
	c[3][4] = new Soldier(1);
	c[5][4] = new Soldier(1);
	c[7][4] = new Soldier(1);
	c[9][4] = new Soldier(1);
	c[1][10] = new Chariot(0);
	c[9][10] = new Chariot(0);
	c[2][10] = new Horse(0);
	c[8][10] = new Horse(0);
	c[3][10] = new Chancellor(0);
	c[7][10] = new Chancellor(0);
	c[4][10] = new BodyGuard(0);
	c[6][10] = new BodyGuard(0);
	c[5][10] = new General(0);
	c[2][8] = new Cannon(0);
	c[8][8] = new Cannon(0);
	c[1][7] = new Soldier(0);
	c[3][7] = new Soldier(0);
	c[5][7] = new Soldier(0);
	c[7][7] = new Soldier(0);
	c[9][7] = new Soldier(0);
}
void Chessboard::Show()
{
	cout << endl;
	HANDLE handle;
	handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle, 0xF0);
	cout << "     P2  一 二 三 四 五 六 七 八 九" << endl << endl;
	char num[11][4] = { "零","一","二","三","四","五","六","七","八","九" ,"十" };
	for (int i = 1; i < 11; i++)
	{
		if (i == 6)
		{
			SetConsoleTextAttribute(handle, 0xF0);
			cout << "          ————楚河 汉界————" << endl << endl;
		}
		SetConsoleTextAttribute(handle, 0xF0);
		cout << "     " << num[i] << "  ";
		for (int j = 1; j < 10; j++)
		{
			if (c[j][i] != NULL)
			{
				if (c[j][i]->Get() > 0)
				{
					SetConsoleTextAttribute(handle, 0xF2);
					cout << Chessword[c[j][i]->Get() + 7] << " ";
				}
				else
				{
					SetConsoleTextAttribute(handle, 0xFC);
					cout << Chessword[c[j][i]->Get() + 7] << " ";
				}
			}
			else if ((i == 2 && j == 5) || (i == 9 && j == 5))
			{
				SetConsoleTextAttribute(handle, 0xF0);
				cout << "米" << " ";
			}
			else
			{
				SetConsoleTextAttribute(handle, 0xF0);
				cout << "十" << " ";
			}
		}
		cout << endl << endl;
	}
	SetConsoleTextAttribute(handle, 0xF0);
	cout << "     P1  一 二 三 四 五 六 七 八 九" << endl << endl;
}
void Chessboard::Play()
{
	HANDLE handle;
	handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle, 0xFC);
	system("cls");
	this->Init();
	this->Show();
	do
	{
		int startx, starty, aimx, aimy, iflag;
		int sid, aid;
		iflag = 0;
		do
		{
			sid = aid = 0;
			if ((Chessboard::Player == 1 ? 2 : 1) == 1)
			{
				SetConsoleTextAttribute(handle, 0xFC);
			}
			else
			{
				SetConsoleTextAttribute(handle, 0xF2);
			}
			cout << "请P" << (Chessboard::Player == 1 ? 2 : 1) << "输入起始棋子位置与目标位置的坐标:" << endl;
			cin >> startx >> starty >> aimx >> aimy;
		} while (!this->Move(startx, starty, aimx, aimy));
		system("cls");
		this->Show();
		for (int i = 4; i < 7; i++)
		{
			for (int j = 1; j < 11; j++)
			{
				if (c[i][j] != NULL)
				{
					if ((int)fabs(c[i][j]->Get()) == 1)
					{
						iflag++;
					}
					else if (iflag != 0 && iflag != 2)
					{
						if ((int)fabs(c[i][j]->Get()) != 1)
						{
							iflag--;
						}
					}
				}
			}
		}
		if (iflag == 2)
		{
			Player *= -1;
			Chessboard::End = false;
		}
	} while (Chessboard::End);
	if ((Chessboard::Player == 1 ? 1 : 2) == 1)
	{
		SetConsoleTextAttribute(handle, 0xFC);
	}
	else
	{
		SetConsoleTextAttribute(handle, 0xF2);
	}
	cout << "结束,赢家是Player" << (Chessboard::Player == 1 ? 1 : 2) << endl;
	
}
Chessboard::~Chessboard()
{
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 11; j++)
		{
			if (c[i][j] != NULL)
			{
				delete c[i][j];
				c[i][j] = NULL;
			}
		}
	}
}
void slowout(char *p)
{
	while(1)
	{
		if(*p!=0)
		{
			printf("%c",*p++);
		}
		else
		{
			break;
		}
		Sleep(125);
	}
	cout<>minzi;
	int mima;
	if(minzi==1){
		cout<<"名字:小火龙"<>gy2;
    if(gy2==0){
    	cout<<"身份:游客"<>mima;
		if(mima==5963){
			cout<<"登入成功"<>mima;
		if(mima==520520){
			cout<<"登入成功"<0){
    	cout<<"是否使用加钱卡?"<>sy;
		if(sy==1){
			jqk-=1;
			cout<<"购买成功!"<>xx;
    slowout("…+……loading……+……loading……+……loading……+……");
    if(xx==0){
    	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x06);
    	return 0;
   	}
    if(xx==1){ 
    	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x01);
		if(slk>0){
			cout<<"是否使用胜利卡?"<>sy;
			if(sy==1){
				slk--;
				cout<<"使用成功"<>fanhui; 
				if(fanhui==1){
					system("cls");
					main();
				} 
				else{
					cout<<"继续"<>v;
		if(v>1000){
			cout<<"It's too long!!!"<>v;
			if(v>1000){
				cout<<"It's too long!!!"<-1) break;
		            A.AI(1); GG=A.check(); if (GG>-1) break;
		        }
		        gotoxy(5,N+3);
		        if (GG==0) {
					Sleep(2000);
					cout<<"\a";
					printf("你侥幸赢了一局!");
					money+=2;
					start+=0.5;
					cout<<"是否返回主页,1=是,2=否"<>fanhui; 
					if(fanhui==1){
						system("cls");
						main();
					} 
					else{
						cout<<"继续"<>fanhui; 
					if(fanhui==1){
						system("cls");
						main();
					} 
					else{
						cout<<"继续"<>t;
		if(t==1){
        	A.reset();
	        for (GG=-1;;){
	            gotoxy(A.nx,A.ny);
	            A.HM(0); GG=A.check(); if (GG>-1) break;
	            A.AI(1); GG=A.check(); if (GG>-1) break;
	        }
	        gotoxy(5,N+3);
	        if (GG==0) {
				Sleep(2000);
				system("cls");
				cout<<"\a";
				printf("你侥幸赢了一局!");
				money+=2;
				start+=0.5;
				cout<<"是否返回主页,1=是,2=否"<>fanhui; 
				if(fanhui==1){
					system("cls");
					main();
				} 
				else{
					cout<<"继续"<>fanhui; 
				if(fanhui==1){
					system("cls");
					main();
				} 
				else{
					cout<<"继续"<>qd;
		int mm;
		int mm2;
		int mm3;
		int vip;
		if(qd==1){
			mm=10;
		}
		if(qd==2){
			cout<<"请输入密码:";
			cin>>mm2;
			if(mm2==5963){
				MessageBox(NULL,"你好,我是喷火龙,密码正确,VIP1模式,生命+10!!!","提示",MB_OK);
				mm=20; 
			}
			if(mm2!=5963){
				MessageBox(NULL,"你好,我是喷火龙,密码错误!!!","提示",MB_OK);
				system("cls");
				mm=10; 
				system("pause");
				cout<<"游戏继续"<>mm3;
			if(mm3==520520){
				MessageBox(NULL,"你好,我是喷火龙,密码正确,VIP2模式,请自定义生命数(猜几次)!!!","提示",MB_OK);
				cin>>mm;
			}
			if(mm3!=520520){
				MessageBox(NULL,"你好,我是喷火龙,密码错误!!!","提示",MB_OK);
				system("cls");
				mm=10; 
				system("pause");
				cout<<"游戏继续"<0){
    		cout<<"是否使用机会卡?"<>sy;
			if(sy==1){
				jhk-=1;
				cout<<"使用成功!"<>x;
			if(x>100||x<1){
				cout<<"浪费机会!!!"; 
			}
			if(xkey)
			{
				cout<<"大了"<>fanhui; 
				if(fanhui==1){
					system("cls");
					main();
				} 
				else{
					cout<<"继续"<>fanhui; 
		if(fanhui==1){
			system("cls");
			main();
		} 
		else{
			cout<<"继续"<>n;
	    cout<<"==============================================================\n0~几???";
	    cin>>m;
	    for(;;)
	    {
	        cout<<"==============================================================\n开始!!!";
	        int x=rand()%(m+1),y,h=0,e=m,t=rand()%n;
	        bool hh=1,ee=1;
	        do
	        {
	            cout<<"\n==============================================================\n";
	            cout<n?n:0;
	            if(!c[t])
	              cout<>y;
	            if(y==2)
	              c[t]=1;
	            if(y==1||c[t])
	            {
	                y=(h+e)/2;
	                cout<<"帮"<x)
	                {
	                    cout<<"大了";
	                    e=y; 
	                    continue;
	                }
	                if(y=e)
	            {
	                cout<<"浪费机会!!!";
	                continue;
	                if(!h&&!y)
	                  if(hh)
	                    hh=0;
	                else
	                {
	                    cout<<"浪费机会!!!";
	                    continue;
	                }
	                if(e==m&&y==m)
	                  if(ee)
	                    ee=0;
	                else
	                {
	                    cout<<"浪费机会!!!";
	                    continue;
	                }
	            }
	            if(y>x)
	            {
	                cout<<"大了";
	                e=y; 
	            }
	            if(y>v;
		if(v>1000){
			cout<<"It's too long!!!"<>v;
			if(v>1000){
				cout<<"It's too long!!!"<-1) break;
		            A.AI(1); GG=A.check(); if (GG>-1) break;
		        }
		        gotoxy(5,N+3);
		        if (GG==0) {
					Sleep(2000);
					cout<<"\a";
					printf("你侥幸赢了一局!");
					money+=2;
					cout<<"是否返回主页,1=是,2=否"<>fanhui; 
					if(fanhui==1){
						system("cls");
						main();
					} 
					else{
						cout<<"继续"<>fanhui; 
					if(fanhui==1){
						system("cls");
						main();
					} 
					else{
						cout<<"继续"<>dj; 
		if(dj==0){
			cout<<"成功退出"<>fanhui; 
			if(fanhui==1){
				system("cls");
				main();
			} 
			else{
				cout<<"继续"<=3){
				money-3;
				cout<<"购买成功!"<=2){
				start-2;
				cout<<"购买成功!"<=3){
				money-3;
				cout<<"购买成功!"<3){
			cout<<"无效输出。"<>gy;
	    if(gy==0){
	    	cout<<"成功退出"<>fanhui; 
			if(fanhui==1){
				system("cls");
				main();
			} 
			else{
				return 0;
			} 
		}
		if(gy==1){
			cout<<"你的money:"<>hq;
			if(hq==0){
				main();
			} 
			if(hq==1){
				if(money>=10){
					money-10;
					cout<<"VIP1级已购买成功,密码:5963"<=50){
					money-50;
					cout<<"VIP2级已购买成功,密码:520520"<>yonghu;
			if(yonghu==1){
				cout<<"请输入密码"<>mima;
				if(mima==5963){
					cout<<"登入成功"<>mima;
				if(mima==520520){
					cout<<"登入成功"<=3){
			system("cls");
			cout<<"是否返回主页,1=是,2=否"<>fanhui; 
			if(fanhui==1){
				system("cls");
				main();
			} 
			else{
				cout<<"继续"<8){
		cout<<"超出范围"<

你可能感兴趣的:(C++小游戏,c++,stm32,开发语言)