| | |
problem with knights
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 16
Reputation:
Solved Threads: 1
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?
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?
c++ Syntax (Toggle Plain Text) -
- #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;
- }
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Solved Threads: 3
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 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.
•
•
Join Date: Nov 2007
Posts: 16
Reputation:
Solved Threads: 1
•
•
•
•
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.
•
•
•
•
First is there a better way to do it than a series of bool functions?
•
•
•
•
in determining all legal moves, and then assigning a random number, I run into some errors.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
![]() |
Similar Threads
- trouble with knights tour (recursion) (C++)
- cannot open drive volumes by double clicking on them (IT Professionals' Lounge)
- I made a BOo-BOO, now I can't start the computer... (Windows NT / 2000 / XP)
- Recursivity (C++)
- New Window (HTML and CSS)
- I need some help with a program on chess and knights (C)
Other Threads in the C++ Forum
- Previous Thread: Clearing the screen?
- Next Thread: Char array, getting weird output
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





