~cpp
// Knight.cpp: implementation of the CKnight class.
//
//////////////////////////////////////////////////////////////////////
#include "Knight.h"
#include "iostream"
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CKnight::CKnight(int sr, int sc)
{
int tempHorizontal[] = {2, 1, -1, -2, -2, -1, 1, 2};
int tempVertical[] = {-1, -2, -2, -1, 1, 2, 2, 1};
for (int row = 0 ; row < 8 ; row++){
for (int col = 0 ; col < 8 ; col++) {
m_ChessBoard[row][col] = 0;
m_Footprint[row * 8 + col] = 0;
}
m_Horizontal[row] = tempHorizontal[row];
m_Vertical[row] = tempVertical[row];
}
m_Move = 0;
m_CurrentRow = sr, m_CurrentColumn = sc;
}
CKnight::~CKnight()
{
}
void CKnight::showNightTour()
{
for (int row = 0 ; row < 8 ; row++) {
printf("\n\n");
for (int col = 0 ; col < 8 ; col++)
printf("%d\t", m_ChessBoard[col][row]);
}
printf("\n\n경 로 :\n");
for (int counter = 0 ; counter < 64 ; counter++){
printf(" %d", m_Footprint[counter]);
if (counter % 32 == 31)
printf("\n");
}
printf("이동횟수 : %d\n", m_Move);
system("pause");
system("cls");
}
void CKnight::showPosition()
{
printf("(%d, %d)", m_CurrentRow, m_CurrentColumn);
}
void CKnight::tour()
{
for (int counter = 1 ; counter < 65 ; counter++) {
m_ChessBoard[m_CurrentRow][m_CurrentColumn] = counter;
int direction = -1;
while (++direction <= 8 && m_ChessBoard[m_CurrentRow][m_CurrentColumn] < 64) {
if (direction > 7) { // BackStep
direction = m_Footprint[--counter];
int rewind = (direction + 4) % 8;
m_ChessBoard[m_CurrentRow][m_CurrentColumn] = 0;
m_CurrentRow += m_Horizontal[rewind];
m_CurrentColumn += m_Vertical[rewind];
continue;
}
m_CurrentRow += m_Horizontal[direction];
m_CurrentColumn += m_Vertical[direction];
if (m_CurrentRow < 0 || m_CurrentRow > 7
|| m_CurrentColumn < 0 || m_CurrentColumn > 7
|| m_ChessBoard[m_CurrentRow][m_CurrentColumn] != 0){
m_CurrentRow -= m_Horizontal[direction];
m_CurrentColumn -= m_Vertical[direction];
continue;
}
m_Footprint[counter] = direction;
m_Move++;
break;
}
}
}