#include <iostream> //Using for input and output
#include <iomanip>  //To set each number a determined place in the print function

                                          This program will try to move a knight
                                           around a chess board to each space 
                                           exactly one time.
                                          How I will do that?
                                          I will first create the board by a determined size.
                                          I will fill the board with 0s.
                                          I will define whether a move is in bounds,
                                          and I will define whether a move is already taken
                                          I will define if there is a next move to be made 
                                            from a given space.
                          If a space is determined to be in bounds, free, and a next move
                          is possible. Then I want to write that space. 
                                          Knight Moves
                                          Row    Col
                                          +1      +2
                                          -1      +2
                                          +1      -2
                                          -1      -2
                                          +2      +1
                                          -2      +1
                                          +2      -1
                                          -2      -1

void createboard();     //Creates the Board and fills its contents with 0s
     int board[8][8];             //array 
          int rm[]= {-2,+2,+2,-2,-1,+1,-1,+1},        //how the knight movs
              cm[] ={+1,+1,-1,-1,+2,+2,-2,-2};

void write(int,int,int);   //IN: are the Row, Col, and Move
bool inbounds(int,int);    //IN: are the Row, Col OUT: Is it in Bounds?
bool free(int,int);        //IN:are the Row, Col OUT: Is the space free? 
bool move(int,int,int);    //IN: The Starting Coordinates, and Move to begin at
void printboard();         //Prints The Board
using namespace std;       //used to set the variable scope
int main()
    createboard();         //Creates the 8x8 Board
    move(0,0,1);           //Sends 5 5 as the Starting Spot, and 1 is the move
    printboard();          //Prints the Board
    system("pause");       //Pause the System 
    return 1;
}//end of Main
void createboard()         //This will create the board
{                                 //Size 8 x 8
     int r = 0;                 //  Filled with 0s
     int c = 0;
      while (r < 8)
       while (c < 8)
                board[r][c] = 0;
                c++;        //New Colomn
 r++;                       //New Space
 c = 0;                          //Back to the beginning of the column
 } } //End of While and end of CreateBoard

void write(int r, int c, int m)           //Will set the move number
                                         //to the given coordinates
     { board[r][c] = m; }
bool inbounds(int r,int c)               //Are the coordinates on the board?
     if (r >=0 && c >=0 && r<8 && c<8)  //if 0 or more for Rows and Cols
        {return true;}                  //and less than 8, then its fine
     else {return false;}                     //other wise, No!
}                                      // end of inbounds

bool free(int r, int c)                //Is this space free?
     if (board[r][c] == 0)             //If its a 0, then its free
     { return true; }
     else { return false;}             //Otherwise its taken
}                                      //end of free

                                                //end of nextmove

bool move(int r, int c, int m)              //move the horse from the 

for (int i=0; i<8;++i)                    //loop designed to try each of
    {                                           //the 8 possible moves
    write(r,c,m);                               //Writes the number on the table
    int currentRow = r + rm[i];
    int currentCol= c + cm[i];
    cout <<i<<endl;
    if (free(r,c) && inbounds(r,c) && move(currentRow,currentCol,m+1)  )

          return true;
return false;
}//end of move

void printboard()                  //Print the board on the screen
int r = 0; //set the first row to 0 ---r for row
int c = 0; //set the column to to 0 ---m for column
cout << endl;

      while (r < 8)
       while (c < 8)
                cout << setw(3) << board[r][c] <<" "; //write board number in the right place
                c++;            //next space
             }//end of c while
       r++;                     //next row after the columns have been filled
       cout <<endl;                    //new line
       c = 0;                   //back to the first line
       }//end of r while
 }//end of print board

I am just getting 0s as an output. I thought this would work. Someone can tell me what Im doing wrong?

Edited by ~s.o.s~: snipped out personal information as per user request

8 Years
Discussion Span
Last Post by gparadox
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.