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;
}