Advertisement
Misha_

Battleship v2.3.9

Nov 18th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.92 KB | None | 0 0
  1. // Battleship v2.3.9
  2. //
  3. // Author: Elena Polyakova
  4. // Date: 15-Nov-2019
  5.  
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <windows.h>
  9. #include <iomanip>
  10. #include <cctype>
  11. #include <cstring>
  12. #define SZ 64
  13.  
  14.  
  15. using namespace std;
  16.  
  17.  
  18. void printFields(char[][SZ],char[][SZ],char[][SZ], HANDLE);
  19. void greeting(HANDLE);
  20. void fillingFields(char[][SZ],char[][SZ],char[][SZ]);
  21. void printStagePlacementShips(char[][SZ],char[][SZ],char[][SZ], HANDLE, int[]);
  22.  
  23.  
  24.  
  25.  
  26.  
  27. int main() {
  28.     HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
  29.     SetConsoleTextAttribute(h, 0x0007);
  30.  
  31.     char str[16] = {};
  32.     char yf[SZ][SZ] = {}, ofv[SZ][SZ] = {}, ofi[SZ][SZ] = {}; // yf - your field
  33.                                                               // ofv - opponent's field visible
  34.                                                               // ofi - opponent's field invisible
  35.     int crs[4] = {4,3,2,1};                                   // crs - count of remaining ships
  36.     bool errorFlag = 0;
  37.  
  38.  
  39.     greeting(h);
  40.  
  41.     fillingFields(yf, ofv, ofi);
  42.  
  43.  
  44.     do {
  45.         printStagePlacementShips(yf, ofv, ofi, h, crs);
  46.         if (errorFlag == 1) {
  47.             SetConsoleTextAttribute(h, 0x000C);
  48.             cout << "   ERROR! WRONG DIRECTION OF SHIP!\n";
  49.             SetConsoleTextAttribute(h, 0x0007);
  50.             errorFlag = 0;
  51.         }
  52.         cout << "   Enter: ";
  53.         cin.getline(str,8);
  54.         int hmd = 0, ficrd = 0, secrd = 0;          // hmd - how many decks
  55.                                                     // ficrd - first coordinate
  56.                                                     // secrd - second coordinate
  57.         char drct;                                  // drct - direction
  58.         hmd = str[0]-48;
  59.         secrd = tolower(str[2])-96;
  60.         if (hmd != 1) {
  61.             if (str[4] == ' ') {
  62.                 ficrd = str[3]-48;
  63.                 drct = tolower(str[5]);
  64.             }
  65.             else {
  66.                 int fn, sn;
  67.                 fn = str[3]-48;
  68.                 sn = str[4]-48;
  69.                 ficrd = fn*10+sn;
  70.                 drct = tolower(str[6]);
  71.             }
  72.         }
  73.         else {
  74.             if (str[4] != ' ' && str[4] != '\0') {
  75.                 int fn, sn;
  76.                 fn = str[3]-48;
  77.                 sn = str[4]-48;
  78.                 ficrd = fn*10+sn;
  79.             }
  80.             else
  81.                 ficrd = str[3]-48;
  82.         }
  83.  
  84.         // Проверка выходит ли за границы корабль
  85.         if (ficrd < 1 || ficrd > 10 || secrd < 1 || secrd > 10 || yf[ficrd][secrd] == '@')
  86.             errorFlag = 1;
  87.  
  88.  
  89.         if (errorFlag == 0) {
  90.             if (hmd != 1) {
  91.                 if (drct == 'r') {
  92.                     for (int j = 1; j < hmd; ++j)
  93.                         if (secrd+j < 1 || secrd+j > 10 || yf[ficrd][secrd+j] == '@')
  94.                             errorFlag = 1;
  95.                     if (errorFlag == 0)
  96.                         for (int j = 1; j < hmd; ++j)
  97.                             yf[ficrd][secrd+j] = '@';
  98.                 }
  99.                 if (drct == 'l') {
  100.                     for (int j = 1; j < hmd; ++j)
  101.                         if (secrd-j < 1 || secrd-j > 10 || yf[ficrd][secrd-j] == '@')
  102.                             errorFlag = 1;
  103.                     if (errorFlag == 0)
  104.                         for (int j = 1; j < hmd; ++j)
  105.                             yf[ficrd][secrd-j] = '@';
  106.                 }
  107.                 if (drct == 'u') {
  108.                     for (int j = 1; j < hmd; ++j)
  109.                         if (ficrd-j < 1 || ficrd-j > 10 || yf[ficrd-j][secrd] == '@')
  110.                             errorFlag = 1;
  111.                     if (errorFlag == 0)
  112.                         for (int j = 1; j < hmd; ++j)
  113.                             yf[ficrd-j][secrd] = '@';
  114.                 }
  115.                 if (drct == 'd') {
  116.                     for (int j = 1; j < hmd; ++j)
  117.                         if (ficrd+j < 1 || ficrd+j > 10 || yf[ficrd+j][secrd] == '@')
  118.                             errorFlag = 1;
  119.                     if (errorFlag == 0)
  120.                         for (int j = 1; j < hmd; ++j)
  121.                             yf[ficrd+j][secrd] = '@';
  122.                 }
  123.             }
  124.             if (errorFlag == 0) {
  125.                 yf[ficrd][secrd] = '@';
  126.                 --crs[hmd-1];
  127.             }
  128.         }
  129.     } while (crs[0] || crs[1] || crs[2] || crs[3]);
  130.     system("pause");
  131.  
  132.     return 0;
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. void greeting(HANDLE h) {
  149.     cout << endl;
  150.     cout << "Hello! This is the game ";
  151.     SetConsoleTextAttribute(h, 0x0009);
  152.     cout << "Battleship v2.3.7" << endl;
  153.     SetConsoleTextAttribute(h, 0x0007);
  154.     cout << "The rules are simple:" << endl;
  155.     cout << "1) The game has 2 players: ";
  156.     SetConsoleTextAttribute(h, 0x000A);
  157.     cout << "You";
  158.     SetConsoleTextAttribute(h, 0x0007);
  159.     cout << " and ";
  160.     SetConsoleTextAttribute(h, 0x000C);
  161.     cout << "Your computer";
  162.     SetConsoleTextAttribute(h, 0x0007);
  163.     cout << "." << endl;
  164.     cout << "2) Players take turns calling coordinates on an unknown opponent's field." << endl;
  165.     cout << "3) If have rival on these coordinates there is ship (coordinates are busy), then" << endl;
  166.     cout << "ship or his part of \"drowns\", and engulfed receives the right do another move." << endl;
  167.     cout << "4) The aim of the player is the first to sink all the enemy ships." << endl;
  168.     system("pause");
  169. }
  170.  
  171.  
  172. void fillingFields(char yf[][SZ], char ofv[][SZ], char ofi[][SZ]) {
  173.     for (int i = 0; i < 12; ++i) {
  174.         for (int j = 0; j < 12; ++j) {
  175.             if (j == 0 || i == 0 || j == 12-1 || i == 12-1)
  176.                 yf[i][j] = '#';
  177.             else
  178.                 yf[i][j] = '~';
  179.         }
  180.         for (int j = 0; j < 12; ++j) {
  181.             if (j == 0 || i == 0 || j == 12-1 || i == 12-1) {
  182.                 ofv[i][j] = '#';
  183.                 ofi[i][j] = '#';
  184.             }
  185.             else {
  186.                 ofv[i][j] = '~';
  187.                 ofi[i][j] = '~';
  188.             }
  189.         }
  190.     }
  191. }
  192.  
  193.  
  194.  
  195. void printFields(char yf[][SZ], char ofv[][SZ], char ofi[][SZ], HANDLE h) {
  196.     cout << endl;
  197.     cout << "           ";
  198.     SetConsoleTextAttribute(h, 0x000A);
  199.     cout << "Your field";
  200.     SetConsoleTextAttribute(h, 0x0007);
  201.     cout << "                       ";
  202.     SetConsoleTextAttribute(h, 0x000C);
  203.     cout << "Opponent's field" << endl;
  204.     SetConsoleTextAttribute(h, 0x0007);
  205.     cout << "        ";
  206.     SetConsoleTextAttribute(h, 0x000E);
  207.     cout << "A B C D E F G H I J                A B C D E F G H I J" << endl;
  208.     SetConsoleTextAttribute(h, 0x0007);
  209.     for (int i = 0; i < 12; ++i) {
  210.         for (int j = 0; j < 3; ++j)
  211.             cout << ' ';
  212.         if (i != 0 && i != 12-1) {
  213.             SetConsoleTextAttribute(h, 0x000E);
  214.             cout << setw(2) << i;
  215.             SetConsoleTextAttribute(h, 0x0007);
  216.         }
  217.         for (int j = 0; j < 12; ++j) {
  218.             if (j == 0 || i == 0 || j == 12-1 || i == 12-1) {
  219.                 if ((i == 0 && j == 0) || (i == 12-1 && j == 0))
  220.                     cout << "  ";
  221.                 SetConsoleTextAttribute(h, 0x0008);
  222.                 cout << setw(2) << yf[i][j];
  223.                 SetConsoleTextAttribute(h, 0x0007);
  224.             }
  225.             else {
  226.                 if (yf[i][j] == '~') {
  227.                     SetConsoleTextAttribute(h, 0x0009);
  228.                     cout << setw(2) << yf[i][j];
  229.                     SetConsoleTextAttribute(h, 0x0007);
  230.                 }
  231.                 if (yf[i][j] == '@') {
  232.                     SetConsoleTextAttribute(h, 0x000A);
  233.                     cout << setw(2) << yf[i][j];
  234.                     SetConsoleTextAttribute(h, 0x0007);
  235.                 }
  236.             }
  237.         }
  238.         cout << "         ";
  239.         if (i != 0 && i != 12-1) {
  240.             SetConsoleTextAttribute(h, 0x000E);
  241.             cout << setw(2) << i;
  242.             SetConsoleTextAttribute(h, 0x0007);
  243.         }
  244.         for (int j = 0; j < 12; ++j) {
  245.             if (j == 0 || i == 0 || j == 12-1 || i == 12-1) {
  246.                 if ((i == 0 && j == 0) || (i == 12-1 && j == 0))
  247.                     cout << "  ";
  248.                 SetConsoleTextAttribute(h, 0x0008);
  249.                 cout << setw(2) << ofv[i][j];
  250.                 SetConsoleTextAttribute(h, 0x0007);
  251.             }
  252.             else {
  253.                 SetConsoleTextAttribute(h, 0x0009);
  254.                 cout << setw(2) << ofv[i][j];
  255.                 SetConsoleTextAttribute(h, 0x0007);
  256.             }
  257.         }
  258.         cout << endl;
  259.     }
  260. }
  261.  
  262.  
  263. void printStagePlacementShips(char yf[][SZ], char ofv[][SZ], char ofi[][SZ], HANDLE h, int crs[]) {
  264.     system("cls");
  265.     printFields(yf, ofv, ofi, h);
  266.     cout << endl;
  267.     cout << "   Place the ships in ";
  268.     SetConsoleTextAttribute(h, 0x000A);
  269.     cout << "your field";
  270.     SetConsoleTextAttribute(h, 0x0007);
  271.     cout << "." << endl;
  272.     cout << "   To set up a single deck ship just select \"1 [1st coordinate][2nd coordinate]\"." << endl;
  273.     cout << "   Example: 1 A1" << endl;
  274.     cout << "   For others select \"[which deck] [1st coordinate][2nd coordinate] [direction]\"." << endl;
  275.     cout << "   Right - R, Left - L, Up - U, Down - D" << endl;
  276.     cout << "   Example: 3 A1 R" << endl << endl;
  277.     SetConsoleTextAttribute(h, 0x000A);
  278.     cout << "   @@@@";
  279.     SetConsoleTextAttribute(h, 0x0007);
  280.     cout << " x ";
  281.     if (crs[3] == 0) {
  282.         SetConsoleTextAttribute(h, 0x000C);
  283.         cout << crs[3];
  284.         SetConsoleTextAttribute(h, 0x0007);
  285.     }
  286.     else {
  287.         SetConsoleTextAttribute(h, 0x0009);
  288.         cout << crs[3];
  289.         SetConsoleTextAttribute(h, 0x0007);
  290.     }
  291.     cout << " left" << endl;
  292.  
  293.     SetConsoleTextAttribute(h, 0x000A);
  294.     cout << "   @@@";
  295.     SetConsoleTextAttribute(h, 0x0007);
  296.     cout << "  x ";
  297.     if (crs[2] == 0) {
  298.         SetConsoleTextAttribute(h, 0x000C);
  299.         cout << crs[2];
  300.         SetConsoleTextAttribute(h, 0x0007);
  301.     }
  302.     else {
  303.         SetConsoleTextAttribute(h, 0x0009);
  304.         cout << crs[2];
  305.         SetConsoleTextAttribute(h, 0x0007);
  306.     }
  307.     cout << " left" << endl;
  308.  
  309.     SetConsoleTextAttribute(h, 0x000A);
  310.     cout << "   @@";
  311.     SetConsoleTextAttribute(h, 0x0007);
  312.     cout << "   x ";
  313.     if (crs[1] == 0) {
  314.         SetConsoleTextAttribute(h, 0x000C);
  315.         cout << crs[1];
  316.         SetConsoleTextAttribute(h, 0x0007);
  317.     }
  318.     else {
  319.         SetConsoleTextAttribute(h, 0x0009);
  320.         cout << crs[1];
  321.         SetConsoleTextAttribute(h, 0x0007);
  322.     }
  323.     cout << " left" << endl;
  324.  
  325.     SetConsoleTextAttribute(h, 0x000A);
  326.     cout << "   @";
  327.     SetConsoleTextAttribute(h, 0x0007);
  328.     cout << "    x ";
  329.     if (crs[0] == 0) {
  330.         SetConsoleTextAttribute(h, 0x000C);
  331.         cout << crs[0];
  332.         SetConsoleTextAttribute(h, 0x0007);
  333.     }
  334.     else {
  335.         SetConsoleTextAttribute(h, 0x0009);
  336.         cout << crs[0];
  337.         SetConsoleTextAttribute(h, 0x0007);
  338.     }
  339.     cout << " left" << endl;
  340.     cout << endl;
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement