problem with knights

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 16
Reputation: maxmaxwell is an unknown quantity at this point 
Solved Threads: 1
maxmaxwell maxmaxwell is offline Offline
Newbie Poster

problem with knights

 
0
  #1
Dec 10th, 2007
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?



  
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. using namespace std;
  5. #include <ctime>
  6. #include <cstdlib>
  7.  
  8. int ranNum();
  9. int setRow(int rannu, int currow);
  10. int setCol(int rannu,int curcol);
  11. int modRow(int rannum);
  12. int modCol(int rannum);
  13. bool legal(int theBoard[8][8], int currow, int curcol, int modrow, int modcol);
  14.  
  15. int main()
  16. {
  17. srand(time(0));
  18. //create board
  19. int theBoard[8][8];
  20. for (int i = 0; i < 8; i++ )
  21. for (int j = 0; j < 8; j++ )
  22. theBoard[i][j]=0;
  23.  
  24. //process
  25. int rannu=ranNum();
  26. int crrow=4;
  27. int currow=setRow(rannu, crrow);
  28. cout<<"current row"<<currow<<endl;
  29. int crcol=4;
  30. int curcol=setCol(rannu, crcol);
  31. cout<<"current col"<<curcol<<endl;
  32.  
  33. //set modrow, modcol
  34. int modrow= modRow( rannu);
  35. int modcol= modCol( rannu);
  36. cout<<"mod row "<<modrow<<endl<<"modcol "<<modcol<<endl;
  37.  
  38. bool check=legal(theBoard, currow, curcol, modrow, modcol);
  39. if (check==true)
  40. {
  41. theBoard[currow][curcol]=1;
  42.  
  43. }
  44. else
  45. cout<<"nay"<<endl;
  46.  
  47.  
  48.  
  49.  
  50. //print board
  51. for (int trow=0; trow<8; trow++)
  52. {
  53. for (int tcol=0; tcol<8; tcol++)
  54. {
  55. cout<<theBoard[tcol][trow]<<"\t";
  56. }
  57. cout<<endl<<endl<<endl;
  58. }
  59.  
  60.  
  61. return 0;
  62. }
  63.  
  64. int ranNum()
  65. {
  66. int rannum=rand()%8;
  67. cout<<"original rannum"<<rannum<<endl;
  68. return rannum;
  69. }
  70.  
  71. int modRow(int rannum)
  72. {
  73. int modrow=0;
  74. if (rannum==1 || rannum==0)
  75. modrow=modrow+2;
  76.  
  77. if (rannum==2 || rannum==3)
  78. modrow=modrow-2;
  79.  
  80. if (rannum==4 || rannum==5)
  81. modrow=modrow+1;
  82.  
  83. if (rannum==6 || rannum==7)
  84. modrow=modrow-1;
  85. return modrow;
  86. }
  87. int modCol(int rannum)
  88. {
  89. int modcol=0;
  90. if (rannum==0)
  91. modcol=modcol+1;
  92.  
  93. if (rannum==1)
  94. modcol=modcol-1;
  95.  
  96. if (rannum==2)
  97. modcol=modcol+1;
  98.  
  99. if (rannum==3)
  100. modcol=modcol-1;
  101.  
  102. if (rannum==4)
  103. modcol=modcol+2;
  104.  
  105. if (rannum==5)
  106. modcol=modcol-2;
  107.  
  108. if (rannum==6)
  109. modcol=modcol+2;
  110.  
  111. if (rannum==7)
  112. modcol=modcol-2;
  113. return modcol;
  114. }
  115.  
  116.  
  117. int setRow(int rannu, int currow)
  118. {
  119. if (rannu==1 || rannu==0)
  120. currow=currow+2;
  121.  
  122. if (rannu==2 || rannu==3)
  123. currow=currow-2;
  124.  
  125. if (rannu==4 || rannu==5)
  126. currow=currow+1;
  127.  
  128. if (rannu==6 || rannu==7)
  129. currow=currow-1;
  130. return currow;
  131. }
  132.  
  133. int setCol(int rannu,int curcol)
  134. {
  135. if (rannu==0)
  136. curcol=curcol+1;
  137.  
  138. if (rannu==1)
  139. curcol=curcol-1;
  140.  
  141. if (rannu==2)
  142. curcol=curcol+1;
  143.  
  144. if (rannu==3)
  145. curcol=curcol-1;
  146.  
  147. if (rannu==4)
  148. curcol=curcol+2;
  149.  
  150. if (rannu==5)
  151. curcol=curcol-2;
  152.  
  153. if (rannu==6)
  154. curcol=curcol+2;
  155.  
  156. if (rannu==7)
  157. curcol=curcol-2;
  158. return curcol;
  159. }
  160.  
  161. bool legal(int theBoard[8][8], int currow, int curcol, int modrow, int modcol)
  162. {
  163. if ( currow+modrow >=0 && curcol+modcol >=0 && currow+modrow <=7 && curcol+modcol <=7 && theBoard[currow+modrow ][curcol+modcol ]==0)
  164. return true;
  165. else
  166. return false;
  167. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: danzona is an unknown quantity at this point 
Solved Threads: 3
danzona danzona is offline Offline
Newbie Poster

Re: problem with knights

 
0
  #2
Dec 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: maxmaxwell is an unknown quantity at this point 
Solved Threads: 1
maxmaxwell maxmaxwell is offline Offline
Newbie Poster

Re: problem with knights

 
0
  #3
Dec 10th, 2007
Originally Posted by danzona View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: maxmaxwell is an unknown quantity at this point 
Solved Threads: 1
maxmaxwell maxmaxwell is offline Offline
Newbie Poster

Re: problem with knights

 
0
  #4
Dec 10th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: problem with knights

 
0
  #5
Dec 11th, 2007
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!!
"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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: maxmaxwell is an unknown quantity at this point 
Solved Threads: 1
maxmaxwell maxmaxwell is offline Offline
Newbie Poster

Re: problem with knights

 
0
  #6
Dec 13th, 2007
Thanks I got it all figured out and running.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC