Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active April 29, 2025 17:43
Show Gist options
  • Save sunmeat/3545de1fe55177f34d15b943a8f2ff54 to your computer and use it in GitHub Desktop.
Save sunmeat/3545de1fe55177f34d15b943a8f2ff54 to your computer and use it in GitHub Desktop.
horse moving C++
#include <iostream>
#include <windows.h>
using namespace std;
const int k = 5; // chess board size
int ar[k][k];
const int shift_count = 8;
COORD shift[]
{
{ 1, -2 }, { 2, -1 }, { 2, 1 }, { 1, 2 },
{ -1, 2 }, { -2, 1 }, { -2, -1 }, { -1, -2 }
};
int rollbacks = 0;
void show_state()
{
COORD info{ 0, 1 };
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h, info);
SetConsoleTextAttribute(h, 10);
for (int y = 0; y < k; y++)
{
for (int x = 0; x < k; x++)
{
cout << "\t" << ar[y][x];
}
cout << "\n\n";
}
}
bool pony(int x, int y, int step)
{
//show_state();
ar[y][x] = step;
if (step >= k * k)
return true;
for (int i = 0; i < shift_count; i++)
{
if ((x + shift[i].X >= 0) && (x + shift[i].X < k) &&
(y + shift[i].Y >= 0) && (y + shift[i].Y < k) &&
(ar[y + shift[i].Y][x + shift[i].X] == 0))
{
if (pony(x + shift[i].X, y + shift[i].Y, step + 1))
return true;
}
}
ar[y][x] = 0;
//show_state();
return false;
}
int main()
{
system("title Horse Move");
int x, y;
cout << "Put position, two numbers from 0 to " << k - 1 << ":\n";
cin >> x >> y;
if (x < 0 || x > k - 1) x = 0;
if (y < 0 || y > k - 1) y = 0;
system("cls");
pony(x, y, 1);
// results
show_state();
}
@johnfischbeck
Copy link

Hi Sunmeat,

I hope this message finds you safe. I'm curios as to what was the purpose of writing this script? I'm working to write a horse action identifier with 9axis gyro affixed on the front left pastern. I came across this code and others you've written and they all look very interesting.

I'd like to share some ideas with you and get your take one day.

Thank you and hope to hear from you soon.

John

@sunmeat
Copy link
Author

sunmeat commented Nov 29, 2023

Hi John @johnfischbeck ,

Thanks for your message and interest in my scripts! I'm glad they are getting your attention. My goal in writing these scripts is to show my students examples of using recursion. Very interested to hear about your project with the horse action identifier and gyroscope.

Let me know more specifically what exactly you would like to discuss or what ideas you have.
I look forward to hearing from you!

Best wishes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment