jkhsjdhjs

functions.h

Dec 17th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.24 KB | None | 0 0
  1. #include <conio.h> //für _getch()
  2. #include <iostream> //für in- output
  3. #include <clocale> //für setlocale
  4. #include <windows.h> //u.a. für SetConsoleTitleA()
  5. #include <vector> //für std::vektoren
  6. #include <array> //für std::arrays
  7. using namespace std;
  8.  
  9. #define PLAYER true
  10. #define ENEMY false
  11.  
  12. bool CheatMode = false, SaveOptions = false;
  13. int counter, columns = 17, lines = 8;
  14.  
  15. enum Colors { //Farbcodes definieren
  16.     BLACK,
  17.     DARKBLUE,
  18.     DARKGREEN,
  19.     DARKTURQUOIS,
  20.     DARKRED,
  21.     PURPLE,
  22.     GOLD,
  23.     GRAY,
  24.     DARKGRAY,
  25.     BLUE,
  26.     GREEN,
  27.     TURQUOIS,
  28.     RED,
  29.     PINK,
  30.     YELLOW,
  31.     WHITE
  32. };
  33.  
  34. void CursorVisibility(bool x) { //Cursor sichtbar oder unsichtbar machen
  35.     CONSOLE_CURSOR_INFO info;
  36.     HANDLE  out;
  37.     info.bVisible = x;
  38.     info.dwSize = 1;
  39.     out = GetStdHandle(STD_OUTPUT_HANDLE);
  40.     SetConsoleCursorInfo(out, &info);
  41. }
  42.  
  43. int getKey() { //Tastenabfrage, welche Taste gedrückt wurde
  44.     int result = _getch();
  45.     if (result == 0) {
  46.         result = 256 + _getch();
  47.     }
  48.     else if (result == 224) {
  49.         result = 512 + _getch();
  50.     }
  51.     return result;
  52.     /*Pfeil oben = 584
  53.     Pfeil rechts = 589
  54.     Pfeil unten = 592
  55.     Pfeil links = 587*/
  56. }
  57.  
  58. void gotoxy(int x, int y) { //Konsolencursor an eine bestimmte Position setzen
  59.     HANDLE hConsole;
  60.     COORD cursorLoc;
  61.     cout.flush();
  62.     cursorLoc.X = x;
  63.     cursorLoc.Y = y;
  64.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  65.     SetConsoleCursorPosition(hConsole, cursorLoc);
  66. }
  67.  
  68. void Color(WORD color) { //Text farbig machen
  69.     SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
  70. }
  71.  
  72. vector<int> CoordsConverting() {
  73.     vector<int> retval;
  74.     retval.push_back(1);
  75.     retval.push_back(2);
  76.     return retval;
  77. }
  78.  
  79. struct Pair { //damit ihn retval ohne arrays zwei werte gespeichert sind und an andere Funktionen übergeben werden können
  80.     int One, Two;
  81. };
  82.  
  83. Pair CoordsConverting(int x, int y) { //Koordinaten werden in integers konvertiert, um sie mit PlaceSth platzieren zu können
  84.     Pair retval;
  85.     if (tolower(x) >= 'a' && tolower(x) <= 'q') {
  86.         retval.One = 20 + (tolower(x) - 'a') * 10;
  87.     }
  88.     else {
  89.         retval.One = 0;
  90.     }
  91.     if (y >= '1' && y <= '8') {
  92.         retval.Two = 10 + (y - '1') * 6;
  93.     }
  94.     else {
  95.         retval.Two = 0;
  96.     }
  97.     return retval;
  98. }
  99.  
  100. void PlaceSth(int x, int y, WORD color) { //etwas im Feld platzieren
  101.     Color(color);
  102.     for (int i = -2; i <= 2; i++) {
  103.         gotoxy(x - 4, y + i);
  104.         cout << "XXXXXXXXX";
  105.     }
  106. }
  107.  
  108. void Legend(int x) { //Legende zum Feld hinzufügen
  109.     gotoxy(10, 64);
  110.     Color(WHITE);
  111.     cout << "Legende:";
  112.     if (x == 1) {
  113.         gotoxy(10, 65);
  114.         Color(YELLOW);
  115.         cout << "X";
  116.         Color(WHITE);
  117.         cout << " = Deine Schiffe";
  118.     }
  119.     if (x == 2) {
  120.         gotoxy(10, 65);
  121.         Color(BLUE);
  122.         cout << "X";
  123.         Color(WHITE);
  124.         cout << " = Wasser-Treffer";
  125.         gotoxy(10, 66);
  126.         Color(DARKRED);
  127.         cout << "X";
  128.         Color(WHITE);
  129.         cout << " = Schiff-Treffer";
  130.     }
  131.     if (x == 3) {
  132.         gotoxy(10, 65);
  133.         Color(YELLOW);
  134.         cout << "X";
  135.         Color(WHITE);
  136.         cout << " = Deine Schiffe";
  137.         gotoxy(10, 66);
  138.         Color(BLUE);
  139.         cout << "X";
  140.         Color(WHITE);
  141.         cout << " = feindliche Wasser-Treffer";
  142.         gotoxy(10, 67);
  143.         Color(DARKRED);
  144.         cout << "X";
  145.         Color(WHITE);
  146.         cout << " = feindliche Schiff-Treffer";
  147.     }
  148.     if (x == 4) {
  149.         gotoxy(10, 67);
  150.         Color(TURQUOIS);
  151.         cout << "X";
  152.         Color(WHITE);
  153.         cout << " = feindliche Schiffe";
  154.     }
  155. }
  156.  
  157. void Field(bool x) { //Spielfeld mit Legende erstellen
  158.     int i, k = 1;
  159.     char character = 'A';
  160.     if (x == PLAYER) {
  161.         gotoxy(94, 0);
  162.         Color(RED);
  163.         cout << "DEIN FELD";
  164.     }
  165.     else {
  166.         gotoxy(90, 0);
  167.         Color(RED);
  168.         cout << "GEGNERISCHES FELD";
  169.     }
  170.     Color(TURQUOIS);
  171.     for (i = 20; i <= columns * 10 + 10; i += 10) {
  172.         gotoxy(i, 4);
  173.         cout << character;
  174.         character++;
  175.     }
  176.     for (i = 10; i <= lines * 6 + 4; i += 6) {
  177.         gotoxy(10, i);
  178.         cout << k;
  179.         k++;
  180.     }
  181.     Color(WHITE);
  182.     for (i = 15; i <= columns * 10 + 15; i += 10) {
  183.         for (k = 4; k <= lines * 6 + 7; k++) {
  184.             gotoxy(i, k);
  185.             cout << "#";
  186.         }
  187.     }
  188.     for (i = 7; i <= lines * 6 + 7; i += 6) {
  189.         for (k = 10; k <= columns * 10 + 15; k++) {
  190.             gotoxy(k, i);
  191.             cout << "#";
  192.         }
  193.     }
  194.     if (x == PLAYER && counter == 0) {
  195.         Legend(1);
  196.     }
  197.     else if (x == ENEMY) {
  198.         Legend(2);
  199.     }
  200.     else {
  201.         Legend(3);
  202.     }
  203.     if (CheatMode == true && counter != 0 && x == ENEMY) {
  204.         Legend(4);
  205.     }
  206.     if (x == PLAYER) {
  207.         for (i = 0; i <= 3; i++) {
  208.             if (Schiffe[i][0] != 0) {
  209.                 PlaceSth(Schiffe[i][0], Schiffe[i][1], YELLOW);
  210.             }
  211.         }
  212.         for (i = 0; i <= columns * lines - 1; i++) {
  213.             if (SchüsseKI[i][0] != 0) {
  214.                 PlaceSth(SchüsseKI[i][0], SchüsseKI[i][1], BLUE);
  215.             }
  216.         }
  217.         for (i = 0; i <= 3; i++) {
  218.             if (TrefferKI[i][0] != 0) {
  219.                 PlaceSth(TrefferKI[i][0], TrefferKI[i][1], DARKRED);
  220.             }
  221.         }
  222.     }
  223.     else if (x == ENEMY) {
  224.         if (CheatMode == true) {
  225.             for (i = 0; i <= 3; i++) {
  226.                 PlaceSth(SchiffeKI[i][0], SchiffeKI[i][1], TURQUOIS);
  227.             }
  228.         }
  229.         for (i = 0; i <= columns * lines - 1; i++) {
  230.             if (Schüsse[i][0] != 0) {
  231.                 PlaceSth(Schüsse[i][0], Schüsse[i][1], BLUE);
  232.             }
  233.         }
  234.         for (i = 0; i <= 3; i++) {
  235.             if (Treffer[i][0] != 0) {
  236.                 PlaceSth(Treffer[i][0], Treffer[i][1], DARKRED);
  237.             }
  238.         }
  239.     }
  240.     counter++;
  241. }
  242.  
  243. void PlayerPlaceFlattop() { //Flugzeugträger mit Pfeiltasten platzieren
  244.     int x = 20, y = 10, Key, i, A = 0, B = 0;
  245.     CursorVisibility(false);
  246.     i = y;
  247.     do {
  248.         do {
  249.             i = y;
  250.             for (i; i <= 28 + y; i += 6) {
  251.                 PlaceSth(x, i, YELLOW);
  252.             }
  253.             Key = getKey();
  254.             i = y;
  255.             for (i; i <= 28 + y; i += 6) {
  256.                 PlaceSth(x, i, BLACK);
  257.             }
  258.             if (Key == 589) {
  259.                 if (x < 180) {
  260.                     x += 10;
  261.                 }
  262.             }
  263.             else if (Key == 587) {
  264.                 if (x > 20) {
  265.                     x -= 10;
  266.                 }
  267.             }
  268.             else if (Key == 584) {
  269.                 if (y > 10) {
  270.                     y -= 6;
  271.                 }
  272.             }
  273.             else if (Key == 592) {
  274.                 if (y < 26) {
  275.                     y += 6;
  276.                 }
  277.             }
  278.         } while (Key != 32);
  279.         y += 12;
  280.         x -= 20;
  281.         do {
  282.             i = x;
  283.             for (i; i <= 40 + x; i += 10) {
  284.                 PlaceSth(i, y, YELLOW);
  285.             }
  286.             Key = getKey();
  287.             i = x;
  288.             for (i; i <= 40 + x; i += 10) {
  289.                 PlaceSth(i, y, BLACK);
  290.             }
  291.             if (Key == 589) {
  292.                 if (x < 140) {
  293.                  x += 10;
  294.                 }
  295.             }
  296.             else if (Key == 587) {
  297.                 if (x > 20) {
  298.                     x -= 10;
  299.                 }
  300.             }
  301.             else if (Key == 584) {
  302.                 if (y > 10) {
  303.                     y -= 6;
  304.                 }
  305.             }
  306.             else if (Key == 592) {
  307.                 if (y < 52) {
  308.                     y += 6;
  309.                 }
  310.             }
  311.         } while (Key != 32);
  312.         y -= 12;
  313.         x += 20;
  314.     } while (true);
  315.  
  316. }
Advertisement
Add Comment
Please, Sign In to add comment