6 Replies - 913 Views - Last Post: 15 April 2009 - 09:52 AM Rate Topic: -----

#1 situation  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 28-October 08

some troubles about CHESS program

Posted 21 November 2008 - 09:55 PM

I would like to ask,
after I have input the value of the row number and column number of a chess,
what method should I use to put that chess into correct location.

for example, If I input ( r = 5, c = 5)
the chess should be located at ( 5, 5)

that is:
the output should be

______________________
  1  2  3  4  5  6  7  8  9
1
2
3
4
5			 *
6
7
8
9


_____________________

but I do not know how to use array to finish the program
Can anyone give some hints?
thank you for your help :)

the below is my code

#include <stdio.h>

int main()
{
	int row;
	int column;
	int r, c;

	printf("Please input the row no. & column no. of the stone: ");
	scanf("%d %d", &r, &c);

	for ( row = 1; row <= 9; row ++ ){
		printf("  %d", row);
	}
	printf("\n");
	for ( column = 1; column <= 9; column ++ ){
		printf("%d \n", column);
	}

	if ( r > 9 || c > 9 || r < 1 || c < 1 ){
		printf("Wrong number! Input again!\n");
	}
	

	return 0;
}




This post has been edited by situation: 21 November 2008 - 09:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: some troubles about CHESS program

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: some troubles about CHESS program

Posted 21 November 2008 - 10:12 PM

The chess board needs to be a two dimensional array. Essentially a grid. Once you have the row and column you can place the piece at the correct row and column of the array but only if the following things are checked...

1) A piece is not already there which is your own. If it is your opponents piece, then you would do your capture.

2) For pieces that move along the board, you will have to check all spaces between the start and ending spot on the board. This will go for all pieces except the knight which can jump over pieces.

So what you need to do is create a two dimensional array called something like "board" and initialize it with all the pieces to start the game with. Then once you get the row and column you can drop it into the correct spot of this array and the array will keep track of the piece for you.

Look up two dimensional arrays and give it a whirl. If you have trouble, we can jump in and help you out with setting it up. :)
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: some troubles about CHESS program

Posted 21 November 2008 - 10:13 PM

Can i assume all pieces won't be the same asterisk? That would get confusing quick and be discouraging when testing. What sort of implementation is planned for this project?
Was This Post Helpful? 0
  • +
  • -

#4 situation  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 28-October 08

Re: some troubles about CHESS program

Posted 25 November 2008 - 01:07 AM

after I use array to do this project
I have another problem

#include<stdio.h>
int main()
{

	int row,col;
	char board[9][9];
	
	printf("Please input the row no. & column no. of the stone: ");
	scanf("%d %d", &row, &col);
	
	board[row][col] = '*';
	for (row = 0; row < 9; row++) {
		printf("%d", row + 1);
	for (col = 0; col < 9; col++) {
		printf(" %c", board[row][col]);
	}
		putchar('\n');
	}

		return 0;
}



After I modify the mode
I cannot show the correct format of the output

the wrong output
Please input the row no. & column no. of the stone: 5 5
1 ????????
2 ????????
3 ????????
4 ????????
5 ????????
6 ?????* ??
7 ????????
8 ????????
9 ????????



the output should contain the row of no, ie. the first line should be 1 2 3 4 5 6 7 8 9
and no question mark is outputted in this program
what wrong with the code
can anyone help me to adjust the program?
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,627
  • Joined: 16-October 07

Re: some troubles about CHESS program

Posted 25 November 2008 - 05:31 AM

You need to zero out your board first. That is, fill it with blanks. Right now it's full of char 0 or anything else that happened to be there when you declared it. That's where the "?" come from, un printable ascii.

To have 1..9 across the top, just use a loop to print one; it doesn't make sense to try to print that top like with the array. Also, chess boards are 8x8.
Was This Post Helpful? 0
  • +
  • -

#6 lemac raw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-November 08

Re: some troubles about CHESS program

Posted 26 November 2008 - 08:37 AM

int row,col;
   int rowm,colm;
   char board[8][8]; //at this point the board has random trash letters

   //make it all spaces so u can print it
   for (row = 0; row < 8; row++) {
	  for (col = 0; col < 8; col++) {
		 board[row][col] = ' ';
	  }
   }
   
   //now you could put some stone in the board:
   board[2][3]='K';//lets say thats the lonely special runner king of this peacefull kingdom
   
   //now lets show the board
   printf("  1 2 3 4 5 6 7 8\n");
   for (row = 0; row < 8; row++) {
	  printf("%d",row+1);
	  for (col = 0; col < 8; col++) {
		 printf(" %c", board[row][col]);
	  }
	  printf("\n");
   }
   
   printf("Please input the row no. & column no. of the stone to pick: ");
   scanf("%d %d", &row, &col);
   //ok, lets assume the player asked to pick a stone inside the board,
   //but does that stone exist on the square?
   if (board[row-1][col-1] != ' ') {
	  //now ask where to move it to
	  printf("Please input the row no. & column no. where to place it: ");
	  scanf("%d %d", &rowm, &colm);
	  //assuming the player doesnt want to move the king to the twilight zone
	  board[rowm-1][colm-1] = board[row-1][col-1];
	  board[row-1][col-1] = ' ';
	  //show the board again
	  //(i dont know if u know how to make functions so lets paste the code to show)
	  printf("  1 2 3 4 5 6 7 8\n");
	  for (row = 0; row < 8; row++) {
		 printf("%d",row+1);
		 for (col = 0; col < 8; col++) {
			printf(" %c", board[row][col]);
		 }
		 printf("\n");
	  }
   }else{
	  printf("Stop making up pieces!\n");
   }



Ex: try to check if the kings move is legal in terms of moving only 1 square.
Was This Post Helpful? 0
  • +
  • -

#7 Deepglue555  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 116
  • Joined: 13-April 09

Re: some troubles about CHESS program

Posted 15 April 2009 - 09:52 AM

if you are going to use characters to represnet pieces you should start with
k = black king
q = black queen
r = black rook
b = black bishop
n = black knight
p = black pawn

K = white king
etc uppercase for white and lowercase for black.

as far is the array is concerned i would use board[8][8];

0 - 7 ranks and files is mathematically easier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1