跳到正文

休闲·娱乐

海战

James_L约 9 分钟
原文
文章信息与版本

文章信息

发布
2024年6月8日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 9 分钟
获赞
1

版本

  1. v1当前

游戏规则: 游戏分两个阶段 1.放置 按下鼠标放置战舰于白色光标处,按空格键水平/垂直旋转 2.射击 点击鼠标射击,你和AI轮流来 击沉对方所有战舰即可胜利 代码:

C++
#include<game.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0)
using namespace std;
int Player[20][20],Enemy[20][20];
int GridX=18,GridY=21;
void PrintScreen()
{
	// Player Output
	for(int i=1;i<=10;i++)
	{
		gotoxy(0,i-1);
		for(int j=1;j<=10;j++)
		{
			if(Player[i][j]==0)BColor(BLUE);
			if(Player[i][j]==3)BColor(BLACK);
			if(Player[i][j]==4)BColor(GREY);
			if(Player[i][j]==5)BColor(LIGHTGREY);
			if(Player[i][j]==-1)BColor(RED);
			if(Player[i][j]==-2)BColor(DARKBLUE);
			printf("  ");
		}
	}
	
	// Enemy Output
	for(int i=1;i<=10;i++)
	{
		gotoxy(26,i-1);
		for(int j=1;j<=10;j++)
		{
			if(Enemy[i][j]>=0)BColor(BLUE);
			if(Enemy[i][j]==-1)BColor(GREY);
			if(Enemy[i][j]==-2)BColor(DARKBLUE);
			printf("  ");
		}
	}
}
void PlaceShip()
{
	// Player
	int x,y,ship=3;
	bool dir=0;
	while(ship<=5)
	{
		x=GetSubX()/GridX,y=GetSubY()/GridY;
		for(int i=1;i<=10;i++)
		{
			gotoxy(0,i-1);
			for(int j=1;j<=10;j++)
			{
				if(Player[i][j]==0)BColor(BLUE);
				if(Player[i][j]==3)BColor(BLACK);
				if(Player[i][j]==4)BColor(GREY);
				if(Player[i][j]==5)BColor(LIGHTGREY);
				if(i==y&&j==x)BColor(WHITE);
				printf("  ");
			}
		}
		BColor(BLACK);
		gotoxy(0,10);
		if(dir)cout<<"Vertical (Down)    ";
		else cout<<"Horizonal (Right)   ";
		if(GetKeyState(' ')<0)
		{
			dir=1-dir;
			while(GetKeyState(' ')<0);
		}
		if(KEY_DOWN(VK_LBUTTON))
		{
			bool ok=1;
			if(dir==0)
			{
				for(int j=x;j<=x+ship-1;j++)
				{
					if(Player[y][j]!=0)ok=0;
				}
			}
			else
			{
				for(int i=y;i<=y+ship-1;i++)
				{
					if(Player[i][x]!=0)ok=0;
				}
			}
			if(ok==1)
			{
				if(dir==0)
				{
					for(int j=x;j<=x+ship-1;j++)
					{
						if(j>10)ok=0;
						Player[y][j]=ship;
					}
				}
				else
				{
					for(int i=y;i<=y+ship-1;i++)
					{
						if(i>10)ok=0;
						Player[i][x]=ship;
					}
				}
				ship++;
			}
			while(KEY_DOWN(VK_LBUTTON));
		}
	}
	//Enemy
	ship=3;
	while(ship<=5)
	{
		x=rand()%10+1,y=rand()%10+1;
		dir=rand()%2;
		bool ok=1;
		if(dir==0)
		{
			for(int j=x;j<=x+ship-1;j++)
			{
				if(j>10)ok=0;
				if(Enemy[y][j]!=0)ok=0;
			}
		}
		else
		{
			for(int i=y;i<=y+ship-1;i++)
			{
				if(i>10)ok=0;
				if(Enemy[i][x]!=0)ok=0;
			}
		}
		if(ok==1)
		{
			if(dir==0)
			{
				for(int j=x;j<=x+ship-1;j++)
				{
					Enemy[y][j]=ship;
				}
			}
			else
			{
				for(int i=y;i<=y+ship-1;i++)
				{
					Enemy[i][x]=ship;
				}
			}
			ship++;
		}
	}
}
void PlayerShoot()
{
	int x,y;
	while(1)
	{
		x=GetSubX()/GridX-13,y=GetSubY()/GridY;
		for(int i=1;i<=10;i++)
		{
			gotoxy(26,i-1);
			for(int j=1;j<=10;j++)
			{
				if(Enemy[i][j]>=0)BColor(BLUE);
				if(Enemy[i][j]==-1)BColor(GREY);
				if(Enemy[i][j]==-2)BColor(DARKBLUE);
				if(i==y&&j==x)BColor(WHITE);
				printf("  ");
			}
		}
		if(KEY_DOWN(VK_LBUTTON))
		{
			if(Enemy[y][x]>=0)
			{
				if(Enemy[y][x]==0)Enemy[y][x]=-2;
				else Enemy[y][x]=-1;
				while(KEY_DOWN(VK_LBUTTON));
				return;
			}
		}
	}
}
int TargetX=-1,TargetY=-1,dir=1;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void EnemyShoot()
{
	int x,y;
	int cnt=0;
	while(1)
	{
		cnt++;
		if(TargetX==-1||TargetY==-1||cnt>20)x=rand()%10+1,y=rand()%10+1;// Random Search
		else //Tracked
		{
			int dir=rand()%4;// Spread Search
			x=TargetX+dy[dir],y=TargetY+dx[dir];
		}
		if(Player[y][x]>=0)
		{
			if(Player[y][x]==0)Player[y][x]=-2;
			else
			{
				Player[y][x]=-1;
				TargetX=x,TargetY=y;// Target Discovered
			}
			return;
		}
	}
}
void WinDetect()
{
	bool ok=0;
	for(int i=1;i<=10;i++)
	{
		for(int j=1;j<=10;j++)
		{
			if(Player[i][j]>0)ok=1;
		}
	}
	if(ok==0)
	{
		PrintScreen();
		cout<<"YOU LOSE!";
		while(1);
	}
	
	ok=0;
	for(int i=1;i<=10;i++)
	{
		for(int j=1;j<=10;j++)
		{
			if(Enemy[i][j]>0)ok=1;
		}
	}
	if(ok==0)
	{
		PrintScreen();
		cout<<"YOU WIN!";
		while(1);
	}
	
}
void StartGame()
{
	PlaceShip();
	while(1)
	{
		PrintScreen();
		PlayerShoot();
		PrintScreen();
		EnemyShoot();
		WinDetect();
	}
}
int main()
{
	HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(hStdin, &mode);
    mode&=~ENABLE_QUICK_EDIT_MODE;
    mode&=~ENABLE_INSERT_MODE;
    mode&=~ENABLE_MOUSE_INPUT;
    SetConsoleMode(hStdin, mode);
	srand(time(0));
	system("title Sea Battle     V0.01");
	system("mode con cols=83 lines=30");
	cls();
	cout<<"Sea Battle"<<endl<<endl;
	cout<<"Press any key to start..."<<endl;
	getch();
	cls();
	StartGame();
}
原文
存档评论2 条抓取于