battle ship game c++

 欢迎点击链接加入群聊【程序代写-接单群】共同致富:https://jq.qq.com/?_wv=1027&k=5WxihsL 

可直接联系客服QQ交代需求:953586085

群号:733065427

欢迎光临程序代写小店https://item.taobao.com/item.htm?spm=a230r.1.14.59.255028c3ALNkZ0&id=586797758241&ns=1&abbucket=15#detail

#include <iostream>

#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>   
using namespace std;
void displaygamerwindow();
void shiplocation();
void systemlocation();
void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken);
void systemwindow();
void locationselect(int capacity, int left, int up, int word_input);
char shiplabel[10], systemlabel[10];
int x, locatevertical, locatehorizontal, locateupwards, locateleftwards, computevertical, computehorizontal, computeupwards, computeleftwards;
char gamerboard[10][10] =
{
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
};
char hiddensystem[10][10] =
{
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
};
char displayedsystem[10][10] =
{
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
	{ 00,00,00,00,00,00,00,00,00,00 },
};

int main(void)
{
	HANDLE result;
	result = GetStdHandle(STD_OUTPUT_HANDLE);
	cout << "Loading Please Wait : " << endl;
	Sleep(1000);
	system("CLS");
	cout << "Loading Please Wait :" << endl;
	Sleep(1000);
	system("CLS");
	cout << "Loading Please Wait :" << endl;
	Sleep(1000);
	system("CLS");
	cout << "Loading Please Wait :" << endl;
	Sleep(1000);
	system("CLS");

	SetConsoleTextAttribute(result, FOREGROUND_BLUE | BACKGROUND_CYAN);
	cout << "Lets Play BattleShip Game :" << endl;
	SetConsoleTextAttribute(result, FOREGROUND_CYAN | FOREGROUND_GREEN | FOREGROUND_BLUE);

	systemlocation();
	systemwindow();
	return 0;
}

void systemwindow()
{

	computehorizontal = 0;
	computevertical = 0;
	cout << "" << endl;
	cout << " System Board " << endl;
	cout << "........................................................." << endl;
	cout << " |0|1|2|3|4|5|6|7|8|9|";
	cout << '\n';
	cout << computehorizontal << "|";
	do
	{
		if (computehorizontal == 9 && computevertical == 10)
		{
			break;
		}
		if (computevertical > 9 && computehorizontal < 9)
		{
			cout << "|" << endl;
			computehorizontal = computehorizontal + 1;
			cout << (computehorizontal) << "|";
			computevertical = 0;
		}
		if (computehorizontal < 10)
		{
			if (computevertical < 9)
			{
				cout << hiddensystem[computehorizontal][computevertical] << " ";
			}
			if (computevertical > 8)
			{
				cout << hiddensystem[computehorizontal][computevertical];
			}
		}
		computevertical = computevertical + 1;
	} while (computehorizontal < 10);
	computehorizontal = 0;
	computevertical = 0;
	cout << "|" << endl;
	cout << "................................................................" << endl;
	cout << "" << endl;
}

void systemlocation()
{
	srand(time(NULL));
	for (x = 5;x >= 2;x--)
	{
		switch (x)
		{
		case 5:
			strcpy(systemlabel, "Carrier");
			break;
		case 4:
			strcpy(systemlabel, "Battleship");
			break;
		case 3:
			strcpy(systemlabel, "Kruiser");
			break;
		case 2:
			strcpy(systemlabel, "Destroyer");
			break;
		}
		do
		{
			computeleftwards = rand() % (10);
			computeupwards = rand() % (10);
		} while ((hiddensystem[computeleftwards][computeupwards] != 00));
		hiddensystem[computeleftwards][computeupwards] = int(systemlabel[0]);
		systemoption(x, computeleftwards, computeupwards, (int(systemlabel[0])));
	}
	do
	{
		computeleftwards = rand() % (10);
		computeupwards = rand() % (10);
	} while ((hiddensystem[computeleftwards][computeupwards] != 00));
	hiddensystem[computeleftwards][computeupwards] = 00;
	systemoption(3, computeleftwards, computeupwards, 00);
}

void systemoption(int capacity1, int leftsidedirection, int upwards, int word_taken)
{
	srand(time(NULL));
	int select, circle;
	select = 1 + rand() % (4);
	switch (select)
	{
	case 1:
		for (circle = 1;circle<capacity1;circle++)
		{
			if (hiddensystem[leftsidedirection - circle][upwards] != 00 || int(leftsidedirection - (capacity1 - 1))<(0))
			{
				hiddensystem[leftsidedirection][upwards] = 00;
				x = x + 1;
				break;
			}
			if (int(leftsidedirection - (capacity1 - 1)) >= (0) && x == capacity1)
			{
				for (circle = 1;circle<capacity1;circle++)
				{
					hiddensystem[leftsidedirection - circle][upwards] = word_taken;
				}
				systemwindow();
			}
		}
		break;
	case 2:
		for (circle = 1;circle<capacity1;circle++)
		{
			if (hiddensystem[leftsidedirection + circle][upwards] != 00 || int(leftsidedirection - (capacity1 - 1))>(9))
			{
				hiddensystem[leftsidedirection][upwards] = 00;
				x = x + 1;
				break;
			}
			if (int(leftsidedirection - (capacity1 - 1)) <= (9) && x == capacity1)
			{
				for (circle = 1;circle<capacity1;circle++)
				{
					hiddensystem[leftsidedirection + circle][upwards] = word_taken;
				}
				systemwindow();
			}
		}
		break;
	case 3:
		for (circle = 1;circle<capacity1;circle++)
		{
			if (hiddensystem[leftsidedirection - circle][upwards] != 00 || int(leftsidedirection - (capacity1 - 1))>(9))
			{
				hiddensystem[leftsidedirection][upwards] = 00;
				x = x + 1;
				break;
			}
			if (int(upwards + (capacity1 - 1)) <= (9) && x == capacity1)
			{
				for (circle = 1;circle<capacity1;circle++)
				{
					hiddensystem[leftsidedirection][upwards + circle] = word_taken;
				}
				systemwindow();
			}
		}
		break;
	case 4:
		for (circle = 1;circle<capacity1;circle++)
		{
			if (hiddensystem[leftsidedirection - circle][upwards] != 00 || int(upwards - (capacity1 - 1))<(0))
			{
				hiddensystem[leftsidedirection][upwards] = 00;
				x = x + 1;
				break;
			}
			if (int(upwards - (capacity1 - 1)) >= (0) && x == capacity1)
			{
				for (circle = 1;circle<capacity1;circle++)
				{
					hiddensystem[leftsidedirection][upwards - circle] = word_taken;
				}
				systemwindow();
			}
		}
		break;
	}
}

void displaygamerwindow()
{
	locatehorizontal = 0;
	locatevertical = 0;
	cout << "" << endl;
	cout << " Gamer 's Board" << endl;
	cout << "................................................................." << endl;
	cout << " |0|1|2|3|4|5|6|7|8|9|";
	cout << '\n';
	cout << locatehorizontal << "|";
	do
	{
		if (locatehorizontal == 9 && locatevertical == 10)
		{
			break;
		}
		if (locatevertical > 9 && locatehorizontal < 9)
		{
			cout << "|" << endl;
			locatehorizontal = locatehorizontal + 1;
			cout << (locatehorizontal) << "|";
			locatevertical = 0;
		}
		if (locatehorizontal < 10)
		{
			if (locatevertical < 9)
			{
				cout << gamerboard[locatehorizontal][locatevertical] << " ";
			}
			if (locatevertical > 8)
			{
				cout << gamerboard[locatehorizontal][locatevertical] << flush;
			}
		}
		locatevertical = locatevertical + 1;
	} while (locatehorizontal < 10);
	locatehorizontal = 0;
	locatevertical = 0;
	cout << "|" << endl;
	cout << "................................................................................" << endl;
	cout << "" << endl;
}

void locationselect(int capacity, int left, int up, int word_input)
{
	int select, circle;
	cout << "Do you need any ship to face: " << '\n' << "1. UP 2.DOWN 3.RIGHT 4.LEFT" << '\n';
	cin >> select;
	switch (select)
	{
	case 1:
		for (circle = 1;circle<capacity;circle++)
		{
			if (gamerboard[left - circle][up] != 00 || int(left - (capacity - 1))<(0))
			{
				gamerboard[left][up] = 00;
				cout << "Don't place ships illegally..." << endl;
				x = x + 1;
				break;
			}
			if (int(left - (capacity - 1)) >= (0) && x == capacity)
			{
				for (circle = 1;circle<capacity;circle++)
				{
					gamerboard[left - circle][up] = word_input;
				}
				displaygamerwindow();
			}
		}
		break;
	case 2:
		for (circle = 1;circle<capacity;circle++)
		{
			if (gamerboard[left + circle][up] != 00 || int(left - (capacity - 1))>(9))
			{
				gamerboard[left][up] = 00;
				cout << "Please check ships place correctly." << endl;
				x = x + 1;
				break;
			}
			if (int(left - (capacity - 1)) <= (9) && x == capacity)
			{
				for (circle = 1;circle<capacity;circle++)
				{
					gamerboard[left + circle][up] = word_input;
				}
				displaygamerwindow();
			}
		}
		break;
	case 3:
		for (circle = 1;circle<capacity;circle++)
		{
			if (gamerboard[left - circle][up] != 00 || int(left - (capacity - 1))>(9))
			{
				gamerboard[left][up] = 00;
				cout << "Please check ships place correctly" << endl;
				x = x + 1;
				break;
			}
			if (int(up + (capacity - 1)) <= (9) && x == capacity)
			{
				for (circle = 1;circle<capacity;circle++)
				{
					gamerboard[left][up + circle] = word_input;
				}
				displaygamerwindow();
			}
		}
		break;
	case 4:
		for (circle = 1;circle<capacity;circle++)
		{
			if (gamerboard[left - circle][up] != 00 || int(up - (capacity - 1))<(0))
			{
				gamerboard[left][up] = 00;
				cout << "Please check ships place correctly" << endl;
				x = x + 1;
				break;
			}
			if (int(up - (capacity - 1)) >= (0) && x == capacity)
			{
				for (circle = 1;circle<capacity;circle++)
				{
					gamerboard[left][up - circle] = word_input;
				}
				displaygamerwindow();
			}
		}
		break;
	}
}

void shiplocation()
{
	for (x = 5;x >= 2;x--)
	{
		switch (x)
		{
		case 5:
			strcpy(shiplabel, "Carrier");
			break;
		case 4:
			strcpy(shiplabel, "Battleship");
			break;
		case 3:
			strcpy(shiplabel, "Kruiser");
			break;
		case 2:
			strcpy(shiplabel, "Destroyer");
			break;
		}
		do
		{
			cout << "Locating one end of the " << shiplabel << "." << '\n';
			cout << "Place left side coordinate followed by top :" << '\n';
			cin >> locateleftwards;
			cin >> locateupwards;
		} while (locateleftwards<0 || locateleftwards>9 || locateupwards<0 || locateupwards>9 || (gamerboard[locateleftwards][locateupwards] != 00));
		gamerboard[locateleftwards][locateupwards] = int(shiplabel[0]);
		displaygamerwindow();
		locationselect(x, locateleftwards, locateupwards, (int(shiplabel[0])));
	}
	do
	{
		cout << "Locate one end of the Submarine." << '\n';
		cout << "Place left side coordinate followed by top " << '\n';
		cin >> locateleftwards;
		cin >> locateupwards;
	} while (locateleftwards<0 || locateleftwards>9 || locateupwards<0 || locateupwards>9 || (gamerboard[locateleftwards][locateupwards] != 00));
	gamerboard[locateleftwards][locateupwards] = 00;
	displaygamerwindow();
	locationselect(3, locateleftwards, locateupwards, 00);
}

欢迎光临程序代写小店https://item.taobao.com/item.htm?spm=a230r.1.14.59.255028c3ALNkZ0&id=586797758241&ns=1&abbucket=15#detail

猜你喜欢

转载自blog.csdn.net/qq_24624539/article/details/86818814