Question

Concepts: multi-dimension array and the Knight's Tour Problem A chess board consists of an 8 x 8 "array" of squares: int board[ROW][COL]={0}; A knight may move perpendicular to the edges o...

Concepts: multi-dimension array and the Knight's Tour Problem

A chess board consists of an 8 x 8 "array" of squares:

int board[ROW][COL]={0};

A knight may move perpendicular to the edges of the board, two squares in any of the 4 directions, then one square at right angles to the two-square move. The following lists all 8 possible moves a knight could make from board [3][3]:

board[5][4] or

board[5][2] or

board[4][5] or

board[4][1] or

board[1][4] or

board[1][2] or

board[2][5] or

board[2][1] or

Write a function named LegalMove, in class knight, which accepts a ROW and COL parameter for the

knight's current position, and a move number parameter (see table above) for the proposed new position.

The method should return true if the proposed move is legal, and false if not legal. The method should also pass reference parameters set by the method to the ROW and COL coordinates of the destination square if the move is legal.

You will need at least the following variables:

A function should initialize row and col to passed values, and should initialize the offsets array to the array differences (+/- 1 or 2) for row and col for each of the 8 moves.

GetLegalMoves() should fill the moves array with the move number (1-8) and each legal move that is possible from row, col, and return the number of moves in the array (0-8) and store this value in countLegalMoves.

Write some driver code in main that will place a knight at a given position, and test some possible moves (including some off-the-board moves).

If you have time, code the ClearBoard and the ShowBoard methods as well. It might be even more interesting to start on a full brute-force type Knight's Tour program. I say start on, becuase there is not enough time in a class period for even a fast programmer to complete such a program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++/h>
using namespace std;

/*
Utility function to check if a position is on board or not
*/
bool ifOnboard( int r, int c){
   if ( r>=0 && r<8 && c>=0 && c<8 )   //if it is in array range then it is on board
       return 1;
   else return 0;
}

/*
It checks if particular move according to the given table
is legal or not w.r.t the given current position of knight in
input parameters.
*/
bool LegalMove(int r, int c, int moveNo){
   //cases for every move
   switch(move){
       case 1:
           r+=2;
           c+=1;
           break;
       case 2:
           r+=2;
           c-=1;
           break;
       case 3:
           r+=1;
           c+=2;
           break;
       case 4:
           r+=1;
           c-=2;
           break;
       case 5:
           r-=2;
           c+=1;
           break;
       case 6:
           r-=2;
           c-=1;
           break;
       case 7:
           r-=1;
           c+=2;
           break;
       case 8:
           r-=1;
           c-=2;
           break;  
   }
   if ( ifOnboard(r,c) )
       return 1;
   else return 0;
}
/*
It will change value of the array with respective move, according to table,
if it is a legal move and it also return the count of the legal moves
where knight can move from its current position.
(r,c) is the position of the knight
*/
int GetLegalMoves( int** arr, int r, int c ){
    int count;       //count of legal moves from position arr[r][c]
   //we are marking every legal position in array as move number according to the table.
   if ( ifOnboard(r+2,c+1) ) {
       arr[r+2][c+1]=1;
       count++;
   }
   if ( ifOnboard(r+2,c-1) ) {
       arr[r+2][c-1]=2;
       count++;
   }
   if ( ofOnboard(r+1,c+2) ) {
       arr[r+1][c+2]=3;
       count++;
   }
   if ( ofOnboard(r+1,c-2) ) {
       arr[r+1][c-2]=4;
       count++;
   }
   if ( ifOnboard(r-2,c+1) ) {
       arr[r-2][c+1]=5;
       count++;
   }
   if ( ofOnboard(r-2,c-1) ) {
       arr[r-2][c-1]=6;
       count++;
   }
   if ( ifOnboard(r-1,c+2) ) {
       arr[r-1][c+2]=7;
       count++;
   }
   if ( ifOnboard(r-1,c-2) ) {
       arr[r-1][c-2]=8;
       count++;
   }
   return count;
}

//Driver program for testing
int main(){

   int arr[8][8]={0};   //declaration of 2d array initialised with all 0


   cout<< LegalMove(7,1,1) ;
   cout<< LegalMove(5,4,5) ;
   cout<< LegalMove(7,6,8) ;
   cout<< GetLegalMoves(arr,2,5) ;
  

   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Concepts: multi-dimension array and the Knight's Tour Problem A chess board consists of an 8 x 8 "array" of squares: int board[ROW][COL]={0}; A knight may move perpendicular to the edges o...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o i...

    In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member called gameState that holds one of the following values: X_WON, O_WON, or UNFINISHED - use an enum type for this, not string (the...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • Please develop the following code using C programming and using the specific functions, instructi...

    Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...

  • IrmaMoves.h : #include "IrmaMoves.h" typedef struct Move { Irma irma;            // an instance of Irma L...

    IrmaMoves.h : #include "IrmaMoves.h" typedef struct Move { Irma irma;            // an instance of Irma L Location from_loc; // location where Irma is moving from Location current_loc; // location where Irma is passing over Location to_loc; // location where Irma is moving to } Move; typedef struct Location { char col; // the square's column ('a' through 'h') int row; // the square's row (0 through 7) } Location; typedef struct Irma { int ws; // wind speed (MPH) int...

  • INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first....

    INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first. But rolling certain numbers will change the pace of the game. INCLUDE IN YOUR ASSIGNMENT: Annotation is a major part of any program. At the top of each of your C++ programs, you should have at least four lines of documentation: // Program name: tictactoe.cpp // Author: Twilight Sparkle // Date last updated: 5/26/2016 // Purpose: Play the game of Tic-Tac-Toe ASSIGNMENT: Sorry Game...

  • The Problem A robot is asked to navigate a maze. It is placed at a certain...

    The Problem A robot is asked to navigate a maze. It is placed at a certain position (the starting position) in the maze and is asked to try to reach another position (the goal position). Positions in the maze will either be open or blocked with an obstacle. Positions are identified by (x,y) coordinates. At any given moment, the robot can only move 1 step in one of 4 directions. Valid moves are: ● Go North: (x,y) -> (x,y-1) ●...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT