Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <vector>
- #include <iomanip>
- #include <Windows.h>
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //Console address to change text color
- /*
- Printing color messages in console
- Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
- Colors: https://i.stack.imgur.com/ZG625.png
- */
- inline bool print_str(std::string str, int k, int n)
- {
- SetConsoleTextAttribute(hConsole, k);
- for (int l = 0; l < int(str.size()); l++)
- {
- std::cout << str[l];
- Sleep(n);
- }
- SetConsoleTextAttribute(hConsole, 14);
- return true;
- }
- //Some funny error check
- void read_error(bool& only_digits, bool& negative, int& dot_count, std::string& input)
- {
- const std::string null_err = "You wrote nothing, stop it";
- const std::string mess_err = "S-stop testing me, you baka! >/////<";
- const std::string neg_str_err = "How do you think letters would be negative? Are you so silly? Please";
- const std::string neg_dot_err = "You are not supposed to write negative fractional number here";
- const std::string dot_str_err = "Developer doesn't know what to write here, but this is a error, so please";
- const std::string string_err = "This is not a number, do you think i can read it? Please";
- const std::string dot_err = "This number can't be fractional, you silly. Please";
- const std::string neg_err = "You number is way too negative";
- const std::string repeat = ", type again: ";
- if (!only_digits && dot_count != 0 && negative) { print_str(mess_err, 13, 5); print_str(repeat, 13, 5); }
- else if (!only_digits && negative) { print_str(neg_str_err, 4, 5); print_str(repeat, 4, 5); }
- else if (!only_digits && dot_count != 0) { print_str(dot_str_err, 4, 5); print_str(repeat, 4, 5); }
- else if (dot_count != 0 && negative) { print_str(neg_dot_err, 4, 5); print_str(repeat, 4, 5); }
- else if (!only_digits) { print_str(string_err, 4, 5); print_str(repeat, 4, 5); }
- else if (dot_count != 0) { print_str(dot_err, 4, 5); print_str(repeat, 4, 5); }
- else if (negative) { print_str(neg_err, 4, 5); print_str(repeat, 4, 5); }
- else if (empty(input)) { print_str(null_err, 4, 5); print_str(repeat, 4, 5); }
- }
- //Checks for digit, can process cyrillic
- bool is_digit(char s)
- {
- if (s >= '0' && s <= '9') return true;
- else return false;
- }
- int read_uint()
- {
- //Checks variable for correct input (number)
- std::string input;
- getline(std::cin, input);
- int dot_count = 0, k = 0;
- bool only_digits = true, negative = false;
- while (!only_digits || dot_count == 0 || input.length() == 0)
- {
- if (input[0] == '-')
- {
- k = 1;
- negative = true;
- }
- for (int i = input.length() - 1; i >= k; i--)
- {
- if (!is_digit(input[i]))
- {
- if (input[i] == '.' || input[i] == ',') dot_count += 1;
- else only_digits = false;
- }
- }
- if (only_digits && dot_count == 0 && !negative && input.length() != 0)
- {
- return strtol(input.c_str(), NULL, 0); //Returns number if there is no errors
- }
- else
- {
- read_error(only_digits, negative, dot_count, input); //Sends error data to show some funny text :-DD
- getline(std::cin, input);
- only_digits = true;
- dot_count = 0;
- negative = false;
- k = 0;
- }
- }
- return 0;
- }
- //Looping turns for chess knight
- int turns[8][8] =
- {
- 50, 11, 24, 63, 14, 37, 26, 35,
- 23, 62, 51, 12, 25, 34, 15, 38,
- 10, 49, 64, 21, 40, 13, 36, 27,
- 61, 22, 9, 52, 33, 28, 39, 16,
- 48, 7, 60, 1, 20, 41, 54, 29,
- 59, 4, 45, 8, 53, 32, 17, 42,
- 6, 47, 2, 57, 44, 19, 30, 55,
- 3, 58, 5, 46, 31, 56, 43, 18
- };
- //Changes matrix above to make it from given coords as first turn
- void turns_change(int x, int y)
- {
- int change = turns[y][x] - 1;
- for (int i = 0; i < 8; i++)
- {
- for (int k = 0; k < 8; k++)
- {
- turns[i][k] -= change;
- if (turns[i][k] < 1)
- turns[i][k] += 64;
- }
- }
- }
- //Simple function to go coords in console and write numbers
- void gotoXY(int column, int line)
- {
- COORD coord;
- coord.X = column;
- coord.Y = line;
- SetConsoleCursorPosition(
- GetStdHandle(STD_OUTPUT_HANDLE),
- coord
- );
- }
- //Clears screen for correct turns animation if there were several more lines with errors, with which i had previously
- void ClearScreen()
- {
- HANDLE hStdOut;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- DWORD count;
- DWORD cellCount;
- COORD homeCoords = { 0, 0 };
- hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- if (hStdOut == INVALID_HANDLE_VALUE) return;
- /* Get the number of cells in the current buffer */
- if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
- cellCount = csbi.dwSize.X *csbi.dwSize.Y;
- /* Fill the entire buffer with spaces */
- if (!FillConsoleOutputCharacter(
- hStdOut,
- (TCHAR) ' ',
- cellCount,
- homeCoords,
- &count
- )) return;
- /* Fill the entire buffer with the current colors and attributes */
- if (!FillConsoleOutputAttribute(
- hStdOut,
- csbi.wAttributes,
- cellCount,
- homeCoords,
- &count
- )) return;
- /* Move the cursor home */
- SetConsoleCursorPosition(hStdOut, homeCoords);
- }
- void horse_animation(int x, int y)
- {
- for (int i = 1; i <= 64; ++i)
- {
- for (int l = 0; l <= 7; l++)
- {
- for (int k = 0; k <= 7; k++)
- {
- if (turns[k][l] == i)
- {
- x = k; y = l;
- }
- }
- }
- gotoXY(1 + x * 3, 1 + y * 2);
- std::cout << i;
- Sleep(100);
- }
- }
- int main()
- {
- std::string dummy;
- int x = 0, y = 0;
- print_str("Hoi FOlKS! it's me, chess Temi. Write CoRDs of ur Knight!\n", 13, 5);
- print_str("Write your horizontal coords (x): ", 10, 5);
- do
- {
- x = read_uint() - 1;
- } while ((x < 0 || x > 7) && print_str("Write number from 1 to 8: ", 12, 3));
- print_str("Write your horizontal coords (y): ", 10, 5);
- do
- {
- y = read_uint() - 1;
- } while ((y < 0 || y > 7) && print_str("Write number from 1 to 8: ", 12, 3));
- ClearScreen();
- for (int i = 0; i < 8; i++)
- {
- for (int k = 0; k < 8; k++)
- {
- print_str("---", 15, 0);
- }
- print_str("-", 15, 0);
- std::cout << std::endl;
- for (int k = 0; k < 9; k++)
- {
- print_str("| ", 15, 0);
- }
- std::cout << std::endl;
- }
- for (int k = 0; k < 8; k++)
- {
- print_str("---", 15, 0);
- }
- print_str("-", 15, 1);
- turns_change(x, y);
- horse_animation(x, y);
- gotoXY(0, 17);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement