Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Battleship v2.3.9
- //
- // Author: Elena Polyakova
- // Date: 15-Nov-2019
- #include <cstdlib>
- #include <iostream>
- #include <windows.h>
- #include <iomanip>
- #include <cctype>
- #include <cstring>
- #define SZ 64
- using namespace std;
- void printFields(char[][SZ],char[][SZ],char[][SZ], HANDLE);
- void greeting(HANDLE);
- void fillingFields(char[][SZ],char[][SZ],char[][SZ]);
- void printStagePlacementShips(char[][SZ],char[][SZ],char[][SZ], HANDLE, int[]);
- int main() {
- HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(h, 0x0007);
- char str[16] = {};
- char yf[SZ][SZ] = {}, ofv[SZ][SZ] = {}, ofi[SZ][SZ] = {}; // yf - your field
- // ofv - opponent's field visible
- // ofi - opponent's field invisible
- int crs[4] = {4,3,2,1}; // crs - count of remaining ships
- bool errorFlag = 0;
- greeting(h);
- fillingFields(yf, ofv, ofi);
- do {
- printStagePlacementShips(yf, ofv, ofi, h, crs);
- if (errorFlag == 1) {
- SetConsoleTextAttribute(h, 0x000C);
- cout << " ERROR! WRONG DIRECTION OF SHIP!\n";
- SetConsoleTextAttribute(h, 0x0007);
- errorFlag = 0;
- }
- cout << " Enter: ";
- cin.getline(str,8);
- int hmd = 0, ficrd = 0, secrd = 0; // hmd - how many decks
- // ficrd - first coordinate
- // secrd - second coordinate
- char drct; // drct - direction
- hmd = str[0]-48;
- secrd = tolower(str[2])-96;
- if (hmd != 1) {
- if (str[4] == ' ') {
- ficrd = str[3]-48;
- drct = tolower(str[5]);
- }
- else {
- int fn, sn;
- fn = str[3]-48;
- sn = str[4]-48;
- ficrd = fn*10+sn;
- drct = tolower(str[6]);
- }
- }
- else {
- if (str[4] != ' ' && str[4] != '\0') {
- int fn, sn;
- fn = str[3]-48;
- sn = str[4]-48;
- ficrd = fn*10+sn;
- }
- else
- ficrd = str[3]-48;
- }
- // Проверка выходит ли за границы корабль
- if (ficrd < 1 || ficrd > 10 || secrd < 1 || secrd > 10 || yf[ficrd][secrd] == '@')
- errorFlag = 1;
- if (errorFlag == 0) {
- if (hmd != 1) {
- if (drct == 'r') {
- for (int j = 1; j < hmd; ++j)
- if (secrd+j < 1 || secrd+j > 10 || yf[ficrd][secrd+j] == '@')
- errorFlag = 1;
- if (errorFlag == 0)
- for (int j = 1; j < hmd; ++j)
- yf[ficrd][secrd+j] = '@';
- }
- if (drct == 'l') {
- for (int j = 1; j < hmd; ++j)
- if (secrd-j < 1 || secrd-j > 10 || yf[ficrd][secrd-j] == '@')
- errorFlag = 1;
- if (errorFlag == 0)
- for (int j = 1; j < hmd; ++j)
- yf[ficrd][secrd-j] = '@';
- }
- if (drct == 'u') {
- for (int j = 1; j < hmd; ++j)
- if (ficrd-j < 1 || ficrd-j > 10 || yf[ficrd-j][secrd] == '@')
- errorFlag = 1;
- if (errorFlag == 0)
- for (int j = 1; j < hmd; ++j)
- yf[ficrd-j][secrd] = '@';
- }
- if (drct == 'd') {
- for (int j = 1; j < hmd; ++j)
- if (ficrd+j < 1 || ficrd+j > 10 || yf[ficrd+j][secrd] == '@')
- errorFlag = 1;
- if (errorFlag == 0)
- for (int j = 1; j < hmd; ++j)
- yf[ficrd+j][secrd] = '@';
- }
- }
- if (errorFlag == 0) {
- yf[ficrd][secrd] = '@';
- --crs[hmd-1];
- }
- }
- } while (crs[0] || crs[1] || crs[2] || crs[3]);
- system("pause");
- return 0;
- }
- void greeting(HANDLE h) {
- cout << endl;
- cout << "Hello! This is the game ";
- SetConsoleTextAttribute(h, 0x0009);
- cout << "Battleship v2.3.7" << endl;
- SetConsoleTextAttribute(h, 0x0007);
- cout << "The rules are simple:" << endl;
- cout << "1) The game has 2 players: ";
- SetConsoleTextAttribute(h, 0x000A);
- cout << "You";
- SetConsoleTextAttribute(h, 0x0007);
- cout << " and ";
- SetConsoleTextAttribute(h, 0x000C);
- cout << "Your computer";
- SetConsoleTextAttribute(h, 0x0007);
- cout << "." << endl;
- cout << "2) Players take turns calling coordinates on an unknown opponent's field." << endl;
- cout << "3) If have rival on these coordinates there is ship (coordinates are busy), then" << endl;
- cout << "ship or his part of \"drowns\", and engulfed receives the right do another move." << endl;
- cout << "4) The aim of the player is the first to sink all the enemy ships." << endl;
- system("pause");
- }
- void fillingFields(char yf[][SZ], char ofv[][SZ], char ofi[][SZ]) {
- for (int i = 0; i < 12; ++i) {
- for (int j = 0; j < 12; ++j) {
- if (j == 0 || i == 0 || j == 12-1 || i == 12-1)
- yf[i][j] = '#';
- else
- yf[i][j] = '~';
- }
- for (int j = 0; j < 12; ++j) {
- if (j == 0 || i == 0 || j == 12-1 || i == 12-1) {
- ofv[i][j] = '#';
- ofi[i][j] = '#';
- }
- else {
- ofv[i][j] = '~';
- ofi[i][j] = '~';
- }
- }
- }
- }
- void printFields(char yf[][SZ], char ofv[][SZ], char ofi[][SZ], HANDLE h) {
- cout << endl;
- cout << " ";
- SetConsoleTextAttribute(h, 0x000A);
- cout << "Your field";
- SetConsoleTextAttribute(h, 0x0007);
- cout << " ";
- SetConsoleTextAttribute(h, 0x000C);
- cout << "Opponent's field" << endl;
- SetConsoleTextAttribute(h, 0x0007);
- cout << " ";
- SetConsoleTextAttribute(h, 0x000E);
- cout << "A B C D E F G H I J A B C D E F G H I J" << endl;
- SetConsoleTextAttribute(h, 0x0007);
- for (int i = 0; i < 12; ++i) {
- for (int j = 0; j < 3; ++j)
- cout << ' ';
- if (i != 0 && i != 12-1) {
- SetConsoleTextAttribute(h, 0x000E);
- cout << setw(2) << i;
- SetConsoleTextAttribute(h, 0x0007);
- }
- for (int j = 0; j < 12; ++j) {
- if (j == 0 || i == 0 || j == 12-1 || i == 12-1) {
- if ((i == 0 && j == 0) || (i == 12-1 && j == 0))
- cout << " ";
- SetConsoleTextAttribute(h, 0x0008);
- cout << setw(2) << yf[i][j];
- SetConsoleTextAttribute(h, 0x0007);
- }
- else {
- if (yf[i][j] == '~') {
- SetConsoleTextAttribute(h, 0x0009);
- cout << setw(2) << yf[i][j];
- SetConsoleTextAttribute(h, 0x0007);
- }
- if (yf[i][j] == '@') {
- SetConsoleTextAttribute(h, 0x000A);
- cout << setw(2) << yf[i][j];
- SetConsoleTextAttribute(h, 0x0007);
- }
- }
- }
- cout << " ";
- if (i != 0 && i != 12-1) {
- SetConsoleTextAttribute(h, 0x000E);
- cout << setw(2) << i;
- SetConsoleTextAttribute(h, 0x0007);
- }
- for (int j = 0; j < 12; ++j) {
- if (j == 0 || i == 0 || j == 12-1 || i == 12-1) {
- if ((i == 0 && j == 0) || (i == 12-1 && j == 0))
- cout << " ";
- SetConsoleTextAttribute(h, 0x0008);
- cout << setw(2) << ofv[i][j];
- SetConsoleTextAttribute(h, 0x0007);
- }
- else {
- SetConsoleTextAttribute(h, 0x0009);
- cout << setw(2) << ofv[i][j];
- SetConsoleTextAttribute(h, 0x0007);
- }
- }
- cout << endl;
- }
- }
- void printStagePlacementShips(char yf[][SZ], char ofv[][SZ], char ofi[][SZ], HANDLE h, int crs[]) {
- system("cls");
- printFields(yf, ofv, ofi, h);
- cout << endl;
- cout << " Place the ships in ";
- SetConsoleTextAttribute(h, 0x000A);
- cout << "your field";
- SetConsoleTextAttribute(h, 0x0007);
- cout << "." << endl;
- cout << " To set up a single deck ship just select \"1 [1st coordinate][2nd coordinate]\"." << endl;
- cout << " Example: 1 A1" << endl;
- cout << " For others select \"[which deck] [1st coordinate][2nd coordinate] [direction]\"." << endl;
- cout << " Right - R, Left - L, Up - U, Down - D" << endl;
- cout << " Example: 3 A1 R" << endl << endl;
- SetConsoleTextAttribute(h, 0x000A);
- cout << " @@@@";
- SetConsoleTextAttribute(h, 0x0007);
- cout << " x ";
- if (crs[3] == 0) {
- SetConsoleTextAttribute(h, 0x000C);
- cout << crs[3];
- SetConsoleTextAttribute(h, 0x0007);
- }
- else {
- SetConsoleTextAttribute(h, 0x0009);
- cout << crs[3];
- SetConsoleTextAttribute(h, 0x0007);
- }
- cout << " left" << endl;
- SetConsoleTextAttribute(h, 0x000A);
- cout << " @@@";
- SetConsoleTextAttribute(h, 0x0007);
- cout << " x ";
- if (crs[2] == 0) {
- SetConsoleTextAttribute(h, 0x000C);
- cout << crs[2];
- SetConsoleTextAttribute(h, 0x0007);
- }
- else {
- SetConsoleTextAttribute(h, 0x0009);
- cout << crs[2];
- SetConsoleTextAttribute(h, 0x0007);
- }
- cout << " left" << endl;
- SetConsoleTextAttribute(h, 0x000A);
- cout << " @@";
- SetConsoleTextAttribute(h, 0x0007);
- cout << " x ";
- if (crs[1] == 0) {
- SetConsoleTextAttribute(h, 0x000C);
- cout << crs[1];
- SetConsoleTextAttribute(h, 0x0007);
- }
- else {
- SetConsoleTextAttribute(h, 0x0009);
- cout << crs[1];
- SetConsoleTextAttribute(h, 0x0007);
- }
- cout << " left" << endl;
- SetConsoleTextAttribute(h, 0x000A);
- cout << " @";
- SetConsoleTextAttribute(h, 0x0007);
- cout << " x ";
- if (crs[0] == 0) {
- SetConsoleTextAttribute(h, 0x000C);
- cout << crs[0];
- SetConsoleTextAttribute(h, 0x0007);
- }
- else {
- SetConsoleTextAttribute(h, 0x0009);
- cout << crs[0];
- SetConsoleTextAttribute(h, 0x0007);
- }
- cout << " left" << endl;
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement