0

I'm stuck. I know that I want my program to make a move, subtract one value from the possible moves of the heuristic and when a place reaches 0 or if it is touched then I don't want it to try for it. I'm trying to make the Knight's tour go for the lowest values first in the accessibility array and then try to go for the next higher number. If anyone has any insight on how I would go about doing this I'd greatly appreciate it.

#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <iomanip>

using namespace std;
int access[8][8] ={ {2,3,4,4,4,4,3,2},
                    {3,4,6,6,6,6,4,3},
                    {4,6,8,8,8,8,6,4},
                    {4,6,8,8,8,8,6,4},
                    {4,6,8,8,8,8,6,4},
                    {4,6,8,8,8,8,6,4},
                    {3,4,6,6,6,6,4,3},
                    {2,3,4,4,4,4,3,2}};

bool boardTest(int move, int row, int col);
bool newMove(int board[8][8], int move, int row, int col);
bool validTest(int board[8][8], int move, int row, int col);
void printHeuristic(int[8][8]);
void printArray( int[8][8]);
void knightMvt(int board[8][8], int move, int &currentRow, int &currentCol);

int runKnight(int board[8][8], bool movesKnightTried[8], int startRow, int startCol);

const int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2};
const int vertical[8]= {-1, -2, -2, -1, 1, 2, 2, 1};

int main()
{
    bool movesKnightTried[8] = { 0 };
    int board[8][8] = { 0 };
    printHeuristic(access);
    int currentRow = 0;
    int currentCol = 0;
    
    int startRow = 0;
    int startCol=0;

    int counter = runKnight(board, movesKnightTried, startRow, startCol);
    printArray(board);

}

int runKnight(int board[8][8], bool movesKnightTried[8], int startRow, int startCol)
{
    int pastCounter;
    bool noValidMove = false;
    int currentRow;
    int currentCol;
    int counter = 1;
    currentRow = startRow;
    currentCol = startCol;

    int bestMove = -1;
    int bestAccessibility = 99;
    board[currentRow][currentCol] = counter;
    while (counter < 65 && noValidMove == false)
    {

        for (int move = 0; move<8;move++)
        {
        
            if (validTest(board, move, currentRow, currentCol) && 
            access[currentRow + vertical[move]][currentCol + horizontal[move]] <= bestAccessibility)
            {
                bestMove = move;
                bestAccessibility = access[currentRow+vertical[move]][currentCol + horizontal[move]];
                
                knightMvt(board, move,  currentRow, currentCol);
            
                counter++;
            
                board[currentRow][currentCol] = counter;
            
                break;
            
            }
            if (move == 7)
            {
                noValidMove = true;
            }
        
        }

    }
    
    return 0;
};

void printArray( int board[8][8])
{
    for (int r = 0; r < 8; r++)
    {
        for (int c = 0; c < 8; c++)
            cout << setw(3) << board[r][c] << ' ';
            
        cout << endl;
    }
};

void printHeuristic( int heuristic[8][8])
{
    for (int r = 0; r < 8; r++)
    {
        for (int c = 0; c < 8; c++)
            cout << setw(3) << heuristic[r][c] << ' ';
            
        cout << endl;
    }
};

bool boardTest(int move, int row, int col)
{
    if (row + vertical[move] <0 || row+ vertical[move]>7)
    {
        return false;
    }
    else if (col + horizontal[move]<0 || col + horizontal[move]>7)
    {
        return false;
    }
    else
    {
        return true;
    }
};


bool validTest(int board[][8], int move, int row, int col)
{
    if(boardTest(move, row, col) && newMove(board, move, row, col))
    {
        return true;
    }
    else
    {
        return false;
    }
};


bool newMove(int board[8][8], int move, int row, int col)
{
    if (board[row + vertical[move]][col + horizontal[move]] == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
};

void knightMvt(int board[8][8], int move, int& curRow, int& curCol)
{
    curRow += vertical[move];
    curCol += horizontal[move];
};
Tank Main
  • 9
  • 1
  • 1
    Are you familiar with the concept of [backtracking](https://en.wikipedia.org/wiki/Backtracking)? – Bob__ Sep 14 '20 at 21:31
  • I don't think backtracking is available for this code. We are to use the set of values and try to do it that way. – Tank Main Sep 15 '20 at 00:56

0 Answers0