Homework Question : Model a \"knights tour\"where the initial knight\'s position
ID: 3551661 • Letter: H
Question
Homework Question : Model a "knights tour"where the initial knight's position is chosen at random, and each successive move is chosen at random. One problem you need to solve in this task is enabling your program to determine when the Knight has hit a dead end; that is no valid moves remain, thus ending the tour. Run your program several times to see if a full or closed tour is ever found at random. Print the board contents after a successful run of finding a full or closed tour. Compute the average length of a random tour over 100 experiments. Here is where i am right now: Correct me if I'm wrong but i think i made makeRandomKnightMove() return a bool properly. The program is not running correctly and says the stack around board is corrupted (Run-Time Check Failure #2 - Stack around the variable 'board' was corrupted.), which I'm not completely sure what it means, or gives the error: Unhandled exception at 0x00DE6BD0 in ConsoleApplication2.exe: 0xC0000005: Access violation writing location 0xFFFFFFF5 so I'm at a loss of what to do. I'm guessing it has something to do with either how I'm passing board or I'm asking for an address that doesn't exist in it but I am not sure. Sorry for the bad coding or mistakes, I'm very new to c++ and programming in general. // ConsoleApplication2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include using namespace std; const int SIZE = 8; void displayBoard(int board[SIZE][SIZE]); bool isValidMove(int rowLoc, int colLoc, int cboard[SIZE][SIZE], int testMove); bool makeRandomKnightMove(int curRow, int curCol, int &chooseRow, int &chooseCol, int cboard[SIZE][SIZE]); int oneRun(int totalMoves); int rowChange[] = { 2, 1, -1, -2, -2, -1, 1, 2 }; int colChange[] = { -1, -2, -2, -1, 1, 2, 2, 1 }; int wins = 0; int game = 0; int oneRun(int totalMoves){ game++; int moveCounter = 1; int board[SIZE][SIZE] = { 0 }; //array for values on board int KnightcurRow = rand() % 9; int KnightcurCol = rand() % 9; board[KnightcurRow][KnightcurCol] = 1; //initial location of Knight int chooseRow = 0, chooseCol = 0; // used to store upcoming move while (!makeRandomKnightMove(KnightcurRow, KnightcurCol, chooseRow, chooseCol, board)){ //Knight's Move makeRandomKnightMove(KnightcurRow, KnightcurCol, chooseRow, chooseCol, board); board[KnightcurRow][KnightcurCol] = moveCounter; moveCounter++; board[chooseRow][chooseCol] = moveCounter; KnightcurRow = chooseRow; KnightcurCol = chooseCol; if (!makeRandomKnightMove(KnightcurRow, KnightcurCol, chooseRow, chooseCol, board)){ if (moveCounter>64){ cout << "The knight finished its tour on game: " << game << endl; cout << "The knight moved: " << moveCounter << " time(s)." << endl; cout << "The board looks like: " << endl; displayBoard(board); totalMoves = totalMoves + (moveCounter - 1); wins++; } else{ totalMoves = totalMoves + (moveCounter - 1); } } } return totalMoves; } bool makeRandomKnightMove(int curRow, int curCol, int &chooseRow, int &chooseCol, int cboard[SIZE][SIZE]){ int testMove = rand() % 8; int x = 0; int testRow = curRow + rowChange[testMove]; int testCol = curCol + colChange[testMove]; while (!isValidMove(testRow, testCol, cboard, testMove)){ // choose again testMove = rand() % 9; testRow = curRow + rowChange[testMove]; testCol = curCol + colChange[testMove]; x++; if (x>100){ return false; } } chooseRow = testRow; chooseCol = testCol; return true; } bool isValidMove(int rowLoc, int colLoc, int cboard[SIZE][SIZE], int testMove){ if ((rowLoc >= 0) && (rowLoc = 0) && (colLoc > x; return 0; }//END main functionExplanation / Answer
#include "stdafx.h" #include #include #include using namespace std; const int SIZE = 8; void displayBoard(int board[SIZE][SIZE]); bool isValidMove(int rowLoc, int colLoc, int cboard[SIZE][SIZE], int testMove); bool makeRandomKnightMove(int curRow, int curCol, int &chooseRow, int &chooseCol, int cboard[SIZE][SIZE]); int oneRun(int totalMoves); int rowChange[] = { 2, 1, -1, -2, -2, -1, 1, 2 }; int colChange[] = { -1, -2, -2, -1, 1, 2, 2, 1 }; int wins = 0; int game = 0; int oneRun(int totalMoves){ game++; int moveCounter = 1; int board[SIZE][SIZE] = { 0 }; //array for values on board int KnightcurRow = rand() % 9; int KnightcurCol = rand() % 9; board[KnightcurRow][KnightcurCol] = 1; //initial location of Knight int chooseRow = 0, chooseCol = 0; // used to store upcoming move while (!makeRandomKnightMove(KnightcurRow, KnightcurCol, chooseRow, chooseCol, board)){ //Knight's Move makeRandomKnightMove(KnightcurRow, KnightcurCol, chooseRow, chooseCol, board); board[KnightcurRow][KnightcurCol] = moveCounter; moveCounter++; board[chooseRow][chooseCol] = moveCounter; KnightcurRow = chooseRow; KnightcurCol = chooseCol; if (!makeRandomKnightMove(KnightcurRow, KnightcurCol, chooseRow, chooseCol, board)){ if (moveCounter>64){ coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.