9 Replies - 1308 Views - Last Post: 08 December 2012 - 11:31 PM Rate Topic: -----

#1 edypol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 12

I need to know how to move chess pieces in c

Posted 08 December 2012 - 05:58 PM

I have that function where I suppose to control informations a user is entering into the program. I need to know how to use those informations in order to move the pieces. Thank you in advance.
for exemple the user can enter Pe2 e4.

void controlGame()
{
char move[6];
    printf("enter move : ");
    getchar();
    scanf("%c", &move[6]);

    if(move[1] >='a' && move[1] <='h')

        if(move[2] >= 1 && move[2] <= 8)

            if(move[4] >='a' && move[4] <='h')

                if(move[5] >=1 && move[5] <=8)

matrixGame[4][5]=matrixGame[1][2];
showGame();
}




Is This A Good Question/Topic? 0
  • +

Replies To: I need to know how to move chess pieces in c

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: I need to know how to move chess pieces in c

Posted 08 December 2012 - 06:21 PM

The way I set it up is that every move was a struct of two members - a fromSqr, and a toSqr (both chars, but not strings). The piece is known, because of the piece value of the square on the fromSqr, so the P is not needed. Just e2-e4 is all it needs, and the - is not part of the move, but automatically printed up for the player, after he enters the fromSqr.

You can't fit a move into a single char, so I'm not sure what your code was trying to do. All the letters are subtracted by -'a', so "a4" is known to be column 0, row 4."h4" would be column 7, row 4, etc. Digits can be chars as well, since they are very small in their range.
Was This Post Helpful? 1
  • +
  • -

#3 edypol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 12

Re: I need to know how to move chess pieces in c

Posted 08 December 2012 - 07:01 PM

Thank you so much for answering me. I follow your ideas, it's not working for me. Let me clarify that I'm coding in C.
here is what I did :

void controlGame()
{
char move[6];
    printf("enter move : ");
    getchar();
    scanf("%c", &move[6]);
int a, b, c, d;
    if(move[1] >='a' && move[1] <='h')

        if(move[2] >= 1 && move[2] <= 8)

            if(move[4] >='a' && move[4] <='h')

                if(move[5] >=1 && move[5] <=8)

a = move[1] - 'a';
b = move[2] - 'a';
c = move[4] - 'a';
d = move[5] - 'a';

matrixGame[d][c]= matrixGame[b][a];
matrixGame[b][a] = 0;

showGame();
}







Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: I need to know how to move chess pieces in c

Posted 08 December 2012 - 08:06 PM

Line 6:
 scanf("%c", &move[6]);



First, you can't put more than one char into a variable, using the scanf() format %c. Use %s or something else, but not %c.

Second, move[6] is out of bounds. move[] has valid indices of 0 through 5, ONLY.
Was This Post Helpful? 1
  • +
  • -

#5 edypol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 12

Re: I need to know how to move chess pieces in c

Posted 08 December 2012 - 09:10 PM

Thank you once more. you're really helping me out with that. I've made some changes but now I have that problem : When I do : e2e4, it moves from d8 to d6.

char move[4];

while (1){ 

	scanf("%s", &move);  
	showGame();

		 if (move[0] >= 'a' && move[0] <= 'h' && move[1] >= '1' && move[1] <= '8' && move[2] >= 'a' && move[2] <= 'h' && move[3] >= '1' && move[3] <= '8')   { 
		 
		  int a, b, c, d;

		  a = move[0] - 'a';
		  b = move[1] - '1';
		  c = move[2] - 'a';
		  d = move[3] - '1';


		  matrixGame[d][c] = matrixGame[b][a];
		  matrixGame[b][a] = 0;

		  showGame(); 

		 }
		}


Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: I need to know how to move chess pieces in c

Posted 08 December 2012 - 09:55 PM

How are your rows and columns on the board, set up - 1 to 8, or 0 to 7? If you add a
printf("a: %d  b: %d  c: %d  d: %d \n",a,b,c,d); getchar();



line at line #16, you will get a chance to see just what your calculations are giving you.
Was This Post Helpful? 0
  • +
  • -

#7 edypol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 12

Re: I need to know how to move chess pieces in c

Posted 08 December 2012 - 10:44 PM

I could modify the a,b,c,d in order to have a specific position, but I always have the black pieces that are moving. I probably should specify how the pieces must be moved. if you have any idea about that, this could be helpful. thanks once more.
Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: I need to know how to move chess pieces in c

Posted 08 December 2012 - 11:05 PM

Later, yes. Right now you need to test whether your input of the move, is being correctly translated into a move, on the board.

Continue with that. Which side moves, is of no consequence. Be sure and test all the edges (top and bottom of the rows, and left and right columns, as well.

I remember quite vividly once, having a pawn move from the right edge of the board, (the 'h' column), around to the left edge of the board, (the 'a' column) as if by "magic". <smile>

You can't take big steps with chess programming, unless you have a lot of skills coding it. For the rest of us, it's take a small step, and then test it pretty thoroughly. Then take another small step in writing code, and repeat.
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,575
  • Joined: 19-February 09

Re: I need to know how to move chess pieces in c

Posted 08 December 2012 - 11:12 PM

With scanf and a string the string is already a pointer.

scanf("%s", move);  


This post has been edited by #define: 08 December 2012 - 11:15 PM

Was This Post Helpful? 1
  • +
  • -

#10 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: I need to know how to move chess pieces in c

Posted 08 December 2012 - 11:31 PM

Good catch, #define.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1