Advertisement
Mike_be

Programing Languages 6th work dif 3 (Bednyakov Michail, 6107

Jun 6th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.28 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <iomanip>
  6. #include <Windows.h>
  7.  
  8. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //Console address to change text color
  9.  
  10. /*
  11. Printing color messages in console
  12. Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
  13. Colors: https://i.stack.imgur.com/ZG625.png
  14. */
  15. inline bool print_str(std::string str, int k, int n)
  16. {
  17.     SetConsoleTextAttribute(hConsole, k);
  18.     for (int l = 0; l < int(str.size()); l++)
  19.     {
  20.         std::cout << str[l];
  21.         Sleep(n);
  22.     }
  23.     SetConsoleTextAttribute(hConsole, 14);
  24.     return true;
  25. }
  26.  
  27. //Some funny error check
  28. void read_error(bool& only_digits, bool& negative, int& dot_count, std::string& input)
  29. {
  30.     const std::string null_err = "You wrote nothing, stop it";
  31.     const std::string mess_err = "S-stop testing me, you baka! >/////<";
  32.     const std::string neg_str_err = "How do you think letters would be negative? Are you so silly? Please";
  33.     const std::string neg_dot_err = "You are not supposed to write negative fractional number here";
  34.     const std::string dot_str_err = "Developer doesn't know what to write here, but this is a error, so please";
  35.     const std::string string_err = "This is not a number, do you think i can read it? Please";
  36.     const std::string dot_err = "This number can't be fractional, you silly. Please";
  37.     const std::string neg_err = "You number is way too negative";
  38.     const std::string repeat = ", type again: ";
  39.     if (!only_digits && dot_count != 0 && negative) { print_str(mess_err, 13, 5); print_str(repeat, 13, 5); }
  40.     else if (!only_digits && negative) { print_str(neg_str_err, 4, 5); print_str(repeat, 4, 5); }
  41.     else if (!only_digits && dot_count != 0) { print_str(dot_str_err, 4, 5); print_str(repeat, 4, 5); }
  42.     else if (dot_count != 0 && negative) { print_str(neg_dot_err, 4, 5); print_str(repeat, 4, 5); }
  43.     else if (!only_digits) { print_str(string_err, 4, 5); print_str(repeat, 4, 5); }
  44.     else if (dot_count != 0) { print_str(dot_err, 4, 5); print_str(repeat, 4, 5); }
  45.     else if (negative) { print_str(neg_err, 4, 5); print_str(repeat, 4, 5); }
  46.     else if (empty(input)) { print_str(null_err, 4, 5); print_str(repeat, 4, 5); }
  47. }
  48.  
  49. //Checks for digit, can process cyrillic
  50. bool is_digit(char s)
  51. {
  52.     if (s >= '0' && s <= '9') return true;
  53.     else return false;
  54. }
  55.  
  56. int read_uint()
  57. {
  58.     //Checks variable for correct input (number)
  59.     std::string input;
  60.     getline(std::cin, input);
  61.     int dot_count = 0, k = 0;
  62.     bool only_digits = true, negative = false;
  63.     while (!only_digits || dot_count == 0 || input.length() == 0)
  64.     {
  65.         if (input[0] == '-')
  66.         {
  67.             k = 1;
  68.             negative = true;
  69.         }
  70.         for (int i = input.length() - 1; i >= k; i--)
  71.         {
  72.             if (!is_digit(input[i]))
  73.             {
  74.                 if (input[i] == '.' || input[i] == ',') dot_count += 1;
  75.                 else only_digits = false;
  76.             }
  77.         }
  78.         if (only_digits && dot_count == 0 && !negative && input.length() != 0)
  79.         {
  80.             return strtol(input.c_str(), NULL, 0); //Returns number if there is no errors
  81.         }
  82.         else
  83.         {
  84.             read_error(only_digits, negative, dot_count, input); //Sends error data to show some funny text :-DD
  85.             getline(std::cin, input);
  86.             only_digits = true;
  87.             dot_count = 0;
  88.             negative = false;
  89.             k = 0;
  90.         }
  91.     }
  92.     return 0;
  93. }
  94.  
  95. //Looping turns for chess knight
  96. int turns[8][8] =
  97. {
  98.     50, 11, 24, 63, 14, 37, 26, 35,
  99.     23, 62, 51, 12, 25, 34, 15, 38,
  100.     10, 49, 64, 21, 40, 13, 36, 27,
  101.     61, 22, 9,  52, 33, 28, 39, 16,
  102.     48, 7,  60, 1,  20, 41, 54, 29,
  103.     59, 4,  45, 8,  53, 32, 17, 42,
  104.     6,  47, 2,  57, 44, 19, 30, 55,
  105.     3,  58, 5,  46, 31, 56, 43, 18
  106. };
  107.  
  108. //Changes matrix above to make it from given coords as first turn
  109. void turns_change(int x, int y)
  110. {
  111.     int change = turns[y][x] - 1;
  112.     for (int i = 0; i < 8; i++)
  113.     {
  114.         for (int k = 0; k < 8; k++)
  115.         {
  116.             turns[i][k] -= change;
  117.             if (turns[i][k] < 1)
  118.                 turns[i][k] += 64;
  119.         }
  120.     }
  121. }
  122.  
  123. //Simple function to go coords in console and write numbers
  124. void gotoXY(int column, int line)
  125. {
  126.     COORD coord;
  127.     coord.X = column;
  128.     coord.Y = line;
  129.     SetConsoleCursorPosition(
  130.         GetStdHandle(STD_OUTPUT_HANDLE),
  131.         coord
  132.     );
  133. }
  134.  
  135. //Clears screen for correct turns animation if there were several more lines with errors, with which i had previously
  136. void ClearScreen()
  137. {
  138.     HANDLE                     hStdOut;
  139.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  140.     DWORD                      count;
  141.     DWORD                      cellCount;
  142.     COORD                      homeCoords = { 0, 0 };
  143.  
  144.     hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  145.     if (hStdOut == INVALID_HANDLE_VALUE) return;
  146.  
  147.     /* Get the number of cells in the current buffer */
  148.     if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
  149.     cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  150.  
  151.     /* Fill the entire buffer with spaces */
  152.     if (!FillConsoleOutputCharacter(
  153.         hStdOut,
  154.         (TCHAR) ' ',
  155.         cellCount,
  156.         homeCoords,
  157.         &count
  158.     )) return;
  159.  
  160.     /* Fill the entire buffer with the current colors and attributes */
  161.     if (!FillConsoleOutputAttribute(
  162.         hStdOut,
  163.         csbi.wAttributes,
  164.         cellCount,
  165.         homeCoords,
  166.         &count
  167.     )) return;
  168.  
  169.     /* Move the cursor home */
  170.     SetConsoleCursorPosition(hStdOut, homeCoords);
  171. }
  172.  
  173. void horse_animation(int x, int y)
  174. {
  175.     for (int i = 1; i <= 64; ++i)
  176.     {
  177.         for (int l = 0; l <= 7; l++)
  178.         {
  179.             for (int k = 0; k <= 7; k++)
  180.             {
  181.                 if (turns[k][l] == i)
  182.                 {
  183.                     x = k; y = l;
  184.                 }
  185.             }
  186.         }
  187.         gotoXY(1 + x * 3, 1 + y * 2);
  188.         std::cout << i;
  189.         Sleep(100);
  190.     }
  191. }
  192.  
  193. int main()
  194. {
  195.     std::string dummy;
  196.     int x = 0, y = 0;
  197.     print_str("Hoi FOlKS! it's me, chess Temi. Write CoRDs of ur Knight!\n", 13, 5);
  198.     print_str("Write your horizontal coords (x): ", 10, 5);
  199.     do
  200.     {
  201.         x = read_uint() - 1;
  202.     } while ((x < 0 || x > 7) && print_str("Write number from 1 to 8: ", 12, 3));
  203.     print_str("Write your horizontal coords (y): ", 10, 5);
  204.     do
  205.     {
  206.         y = read_uint() - 1;
  207.     } while ((y < 0 || y > 7) && print_str("Write number from 1 to 8: ", 12, 3));
  208.     ClearScreen();
  209.     for (int i = 0; i < 8; i++)
  210.     {
  211.         for (int k = 0; k < 8; k++)
  212.         {
  213.             print_str("---", 15, 0);
  214.         }
  215.         print_str("-", 15, 0);
  216.         std::cout << std::endl;
  217.         for (int k = 0; k < 9; k++)
  218.         {
  219.             print_str("|  ", 15, 0);
  220.         }
  221.         std::cout << std::endl;
  222.  
  223.     }
  224.     for (int k = 0; k < 8; k++)
  225.     {
  226.         print_str("---", 15, 0);
  227.     }
  228.     print_str("-", 15, 1);
  229.     turns_change(x, y);
  230.     horse_animation(x, y);
  231.     gotoXY(0, 17);
  232.     system("pause");
  233.     return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement