I have an assignment where I'm supposed to place a knight on a chess board, and using a random number generator, make it move.
It cannot visit a space more than once.

I've tried this several ways, and started over from the beginning more than once.
this is how I approached it:
create an 8X8 array called theBoard
place a piece at theBoard[4][4]

from there I can get it to move once with no problem.

A move means setting the cell in the array (previously 0) to one. I know if i've been there before from the value of the cell.
The problem is when I tell it to move twice or more, It moves once, setting a space to one, but then returns to the starting space before moving again.

to eliminate that problem I made the setRow, and setCol functions, but I guess I'm not using them right.
Can someone help me get this stubborn knight to move?

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <ctime>
#include <cstdlib>

int ranNum();
int setRow(int rannu, int currow);
int setCol(int rannu,int  curcol);
int modRow(int rannum);
int modCol(int rannum);
bool legal(int theBoard[8][8], int currow, int curcol, int  modrow, int modcol);

int main()
{
    srand(time(0)); 
     //create board
    int theBoard[8][8];
    for (int i = 0; i < 8; i++ )
        for (int j = 0; j < 8; j++ )
            theBoard[i][j]=0;

    //process
    int rannu=ranNum();
    int crrow=4;
    int currow=setRow(rannu, crrow);
    cout<<"current row"<<currow<<endl;
    int crcol=4;
    int curcol=setCol(rannu, crcol);
    cout<<"current col"<<curcol<<endl;

    //set modrow, modcol
    int modrow= modRow( rannu);
    int modcol= modCol( rannu);
    cout<<"mod row "<<modrow<<endl<<"modcol "<<modcol<<endl;

    bool check=legal(theBoard, currow, curcol, modrow, modcol);
    if (check==true)
    {
        theBoard[currow][curcol]=1;

    }
    else
        cout<<"nay"<<endl;




    //print board
    for (int trow=0; trow<8; trow++)
    {
        for (int tcol=0; tcol<8; tcol++)
        {
            cout<<theBoard[tcol][trow]<<"\t";
        }
        cout<<endl<<endl<<endl;
    }


    return 0;
}

int ranNum()
{
    int rannum=rand()%8;
    cout<<"original rannum"<<rannum<<endl;
    return rannum;
}

int modRow(int rannum)
{
    int modrow=0;
    if (rannum==1 || rannum==0)
        modrow=modrow+2;

    if (rannum==2 || rannum==3)
        modrow=modrow-2;

    if (rannum==4 || rannum==5)
        modrow=modrow+1;        

    if (rannum==6 || rannum==7)
        modrow=modrow-1;
    return modrow;
}
int modCol(int rannum)
{
    int modcol=0;
    if (rannum==0)
    modcol=modcol+1;

    if (rannum==1)
        modcol=modcol-1;

    if (rannum==2)
        modcol=modcol+1;

    if (rannum==3)
        modcol=modcol-1;

    if (rannum==4)  
        modcol=modcol+2;

    if (rannum==5)
        modcol=modcol-2;

    if (rannum==6)  
        modcol=modcol+2;

    if (rannum==7)
        modcol=modcol-2;
    return modcol;
}


int setRow(int rannu, int currow)
{
    if (rannu==1 || rannu==0)
        currow=currow+2;

    if (rannu==2 || rannu==3)
        currow=currow-2;

    if (rannu==4 || rannu==5)
        currow=currow+1;        

    if (rannu==6 || rannu==7)
        currow=currow-1;
    return currow;
}

int setCol(int rannu,int  curcol)
{   
    if (rannu==0)
        curcol=curcol+1;

    if (rannu==1)
        curcol=curcol-1;

    if (rannu==2)
        curcol=curcol+1;

    if (rannu==3)
        curcol=curcol-1;

    if (rannu==4)   
        curcol=curcol+2;

    if (rannu==5)
        curcol=curcol-2;

    if (rannu==6)   
        curcol=curcol+2;

    if (rannu==7)
        curcol=curcol-2;
    return curcol;
}

bool legal(int theBoard[8][8], int currow, int curcol, int  modrow, int modcol)
{   
    if ( currow+modrow >=0 && curcol+modcol >=0 && currow+modrow <=7 && curcol+modcol <=7 && theBoard[currow+modrow ][curcol+modcol ]==0)
        return true;
    else
        return false;
}

Recommended Answers

All 5 Replies

I built the code and got the random value 4. So the knight tried to move from [4,4] to [5,6] to [7,8]. Since the board is defined on [0-7,0-7], this move is off the board. Which is the bug with your code. Doing the same move (i.e. up 1 over 2 is a move) repeatedly with a knight will run out of board fast.

I think the bug with the algorithm is that you are taking a random move and applying it to the current position of the piece. I think that what you should do is the following:
1. Given the position of the piece and its history, determine all of the legal moves
2. Select one of the moves from step 1 randomly.
3. Stop when the number of legal moves in step 1 is 0.

When you move the knight I notice that you are using 0 to mean the knight hasn't been there and 1 when the knight has been there. It might be more interesting if you numbered the knights moves in order. So 0 means the knight hasn't been there, 1 is the knight's starting position, 2 is the first move, 3 is the next move, etc. Then when you print the board out at the end you can see the knight's path.

I built the code and got the random value 4. So the knight tried to move from [4,4] to [5,6] to [7,8]. Since the board is defined on [0-7,0-7], this move is off the board. Which is the bug with your code. Doing the same move (i.e. up 1 over 2 is a move) repeatedly with a knight will run out of board fast.

I think the bug with the algorithm is that you are taking a random move and applying it to the current position of the piece. I think that what you should do is the following:
1. Given the position of the piece and its history, determine all of the legal moves
2. Select one of the moves from step 1 randomly.
3. Stop when the number of legal moves in step 1 is 0.

When you move the knight I notice that you are using 0 to mean the knight hasn't been there and 1 when the knight has been there. It might be more interesting if you numbered the knights moves in order. So 0 means the knight hasn't been there, 1 is the knight's starting position, 2 is the first move, 3 is the next move, etc. Then when you print the board out at the end you can see the knight's path.

Thanks, I'll get working on that, It makes sense.
Also, I will number the moves when I get everything working, as for now I just trying to get it working. I'll post my code up when I get done.

ok, in determining all legal moves, and then assigning a random number, I run into some errors. First is there a better way to do it than a series of bool functions?

First is there a better way to do it than a series of bool functions?

No, I can't think of a better way. danzona has given an excellent peice of advice. Stick to it.

in determining all legal moves, and then assigning a random number, I run into some errors.

In doing these logically simple tasks, so you won't get any logical errors. You might get syntax errors, which if you post them here, we can help you out. Keep the good work going!!

Thanks I got it all figured out and running.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.