How can I optimise this, so I can write something beside a1,a8,h1,h8 (because of long time)?

Viewed 23

The code is about the knight on chess field, you write a position of a knight and program calculates possible shortest turn to complete full trip on field. As I said in title you can write a1,a8,h1,h8 easily, the process won't take too long, but if you try something else, you would get a 20-minute+ adventure.

#include <iostream>
#include <iomanip>
#include "windows.h"
using namespace std;

const int SIZE1 = 8;
int arrX[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int arrY[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);;
WORD attr = BACKGROUND_BLUE |
BACKGROUND_INTENSITY;
DWORD cWrittenChars;

void printField() {
    SetConsoleTextAttribute(hOut, 15);
    int c = 0, c1 = 0, c2 = 0, swap = 0, swap1 = 0;

    for (int i = 0; i < 25; i++)
    {
        c1++;
        if ((i != 0 && i != 24) && c1 == 1)
        {
            cout << (char)204;
        }
        else if (i == 0)
        {
            cout << (char)201;
        }
        else if (i == 24)
        {
            cout << (char)200;
        }
        else
        {
            cout << (char)186;
        }

        if (c1 == 3) c1 = 0;
        if (c1 == 1) swap1++;

        for (int j = 0; j < 24; j++)
        {
            c++;
            c2++;
            if (i == 0 || i == 24)
            {
                if (i == 0 && j == 23) cout << (char)187;
                else if (i == 24 && j == 23) cout << (char)188;
                else if (i == 0 && c2 % 3 == 0) cout << (char)203;
                else if (i == 24 && c2 % 3 == 0) cout << (char)202;
                else cout << (char)205;
                continue;
            }

            if ((i != 1 && i != 24) && (c1 == 1 && j == 23))
            {
                cout << (char)185;
            }
            /*Chess field patern*/
            else if ((c == 1 || c == 2) && (c1 != 1))
            {

                if (swap % 2 == 0)
                {
                    cout << (char)177;
                }
                else
                {
                    cout << (char)176;
                }

            }
            /*Borders*/
            else
            {
                if ((i != 0 && c1 == 1) && c2 % 3 == 0)
                {
                    cout << (char)206;
                }
                else if (c1 == 1)
                {
                    cout << (char)205;
                }
                else
                {
                    cout << (char)186;
                }
                c = 0;
                swap++;
            }

        }
        c = 0;
        c2 = 0;
        if (swap1 % 2 != 0)
        {
            swap = 0;
        }
        else
        {
            swap = 1;
        }
        printf("\n");
    }
}

void Print(int arr[][SIZE1])
{
    int queue = 1;
    printField();

    while (queue <= 64)
    {
        for (short i = 0; i < SIZE1; i++)
        {
            for (short j = 0; j < SIZE1; j++)
            {
                for (short k = 0; k < 64; k++)
                {
                    if (queue == arr[i][j])
                    {
                        Sleep(30);
                        FillConsoleOutputAttribute(hOut, attr, 2, { (SHORT)(j * 3 + 1), (SHORT)(i * 3 + 1) }, &cWrittenChars);
                        FillConsoleOutputAttribute(hOut, attr, 2, { (SHORT)(j * 3 + 1), (SHORT)(i * 3 + 2) }, &cWrittenChars);
                        queue++;
                        break;
                    }
                }
            }
        }
    }
    SetConsoleTextAttribute(hOut, 12);
    cout << "Knight moves:" << '\n';
    for (short i = 0; i < SIZE1; i++)
    {
        for (short j = 0; j < SIZE1; j++)
        {
            printf(" %02d ", arr[i][j]);

        }
        puts("\n");
    }
    SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
}

void Horse(int arr[][SIZE1], int num, int i0, int j0)
{
    static int count = 0;
    count++;

    arr[i0][j0] = num++;

    for (int i = 0; i < 8; i++)
    {
        int inew = i0 + arrY[i];
        int jnew = j0 + arrX[i];

        if (num > SIZE1 * SIZE1)
        {
            Print(arr);
            exit(0);
        }

        if (inew < 0 || inew>SIZE1 - 1 || jnew<0 || jnew>SIZE1 - 1 || arr[inew][jnew] != 0)
            continue;
        Horse(arr, num, inew, jnew);
        arr[inew][jnew] = 0;
    }
}

int main(void)
{
    SetConsoleTextAttribute(hOut,FOREGROUND_GREEN);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    bool isTrue = true;
    char charChose;
    int width, height;
    char chars[8] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
    int nums[8] = { 7, 6, 5, 4, 3, 2, 1, 0 };
    while (isTrue)
    {
        printf("Chose character: ");
        cin >> charChose;
        puts("\n");
        for (int i = 0; i < 8; i++)
        {
            if (charChose == chars[i])
            {
                width = i;
                isTrue = false;
                break;
            }
            if (i == 7 && charChose != chars[i])
            {
                printf("Wrong character\n");
            }
        }
    }
    system("cls");

    isTrue = true;
    while (isTrue)
    {
        printf("Chose number: %c", charChose);
        cin >> height;
        if (height > 8 || height < 1)
        {
            printf("Wrong number\n");
        }
        else
        {
            isTrue = false;
        }
    }
    height--;
    system("cls");
    for (int i = 0; i < 8; i++) chars[i] = '\000';

    int field[SIZE1][SIZE1] = {};
    Horse(field, 1, nums[height], width);
    //for (int i = 0; i < 8; i++) chars[i] = '\000'
}
0 Answers
Related