Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Input for a chess game

Posted on 2003-11-27
6
369 Views
Last Modified: 2012-05-04
This should be a fairly simple question; I have a chessboard, which is an 8x8 array. (There are no graphics, this is text only). The program begins with displaying the board on screen. I need to ask the user which piece to move, and which piece to move it, in the format x1,y1:x2,y2. Should I use gets() to get the move?
Here is the board:
 BR   BN   BB   BQ   BK   BB   BN   BR
 BP   BP   BP   BP   BP   BP   BP   BP
      []        []        []        []
 []        []        []        []      
      []        []        []        []
 []        []        []        []      
 WP   WP   WP   WP   WP   WP   WP   WP
 WR   WN   WB   WQ   WK   WB   WN   WR

That'd probably look a lot better in notepad.
So for instance, I enter 2,2:2:4 to move a WP foward two spaces. How would I do that? I'm not too familiar with string functions to know how to dissect a string into usable numbers. Any help?
0
Comment
Question by:Tabris42
6 Comments
 

Author Comment

by:Tabris42
ID: 9834124
It has also occurred to me that the coordinates for the chessboard wouldn't actually match the coordinates for the array. What can I do to effectively 'spin' the board around, so that the entered coordinates would actually apply to the array coordinates?
0
 

Expert Comment

by:pgiusto
ID: 9834169
You can use scanf to get the user input into strings.
For example this code asks the user for four numbers separated by spaces and store them on x1, y1, x2, y2.

int x1;
int y1;
int x2;
int y2;

scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

You can refer to scanf documentation to get all the benefits it has.

Hope it helps.


0
 

Author Comment

by:Tabris42
ID: 9834664
I don't think that would work with the colon in the middle.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:Tabris42
ID: 9834868
Ok, I've solved that problem. Here is my chess game so far:

#include <stdio.h>

void printboard(char* board[8][8]);
int main(){
      FILE *f;
      char move[7];
      int whitesturn; //if whitesturn = 1, it's white's turn, if 0 it's black's
      int y1, x1, y2, x2;
      char *chessboard[8][8] ={{"BR ","BN ","BB ","BQ ","BK ","BB ","BN ","BR "},
                               {"BP ","BP ","BP ","BP ","BP ","BP ","BP ","BP "},
                               {"   ","[] ","   ","[] ","   ","[] ","   ","[] "},
                               {"[] ","   ","[] ","   ","[] ","   ","[] ","   "},
                               {"   ","[] ","   ","[] ","   ","[] ","   ","[] "},
                               {"[] ","   ","[] ","   ","[] ","   ","[] ","   "},
                               {"WP ","WP ","WP ","WP ","WP ","WP ","WP ","WP "},
                               {"WR ","WN ","WB ","WQ ","WK ","WB ","WN ","WR "}};
     f = fopen("moves.txt","w");
     while(1){
          printboard(chessboard);
          gets(move);
          if (strcmp(move, "exit") == 0){
               break;
          }
          y1 = ((int)move[0] - 48)-1;
          x1 = 8-((int)move[2] - 48);
          y2 = ((int)move[4] - 48)-1;
          x2 = 8-((int)move[6] - 48);
          fprintf(f, "%s\n", move);
          chessboard[x2][y2] = chessboard[x1][y1];
          if(((x1%2 > 0)&&(y1%2 > 0)) || ((x1%2 == 0)&&(y1%2 == 0))){
               chessboard[x1][y1] = "   ";
          }else{
               chessboard[x1][y1] = "[] ";
          }
     }
     fclose(f);
     printf("\nThanks for playing!\n");
     system("PAUSE");
}
void printboard(char *chessboard[8][8]){
     int i, j, a=8;
     for (i=0; i<8; i++){
          printf("%d  ", a);
          a--;
          for (j=0; j<8; j++){
               printf(chessboard[i][j]);
          }
          printf("\n");
     }
     printf("\n   1  2  3  4  5  6  7  8  \n");
     printf("\nMOVE: ");
}

What I would like to know, is how can I validate whether it's white or black's turn to go? Additionally, how can I check for invalid data, such as bad coordinates, or trying to move a black piece on white's turn, or data not in int,int:int,int format?
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 9835005


Validating the data is pretty easy.

To keep track of which player is to move, just keep a counter of the total number of moves.  Then mask off the bottom bit to determine odd or even.

while (1)
{
  MoveNumber++;

  if (MoveNumber & 1)  /*  Odd number means black's move  */
    MoveBlack ();
  else
    MoveWhite ();
}

You can also keep a "logical" variable to do the same thing:

BlacksMove = 1;
while (1)
{
  if (BlacksMove)
    MoveBlack ();
  else
    MoveWhite ();
  BlacksMove = !BlacksMove;
}


The player should input coordinates from 1 to 8.  scanf() will cease the scan if it encounters an error so it's easy to detect a badly formatted input.

  fgets (Buffer, BUFFER_LENGTH, stdin);
  NewY = 0;
  sscanf (Buffer, "%d,%d:%d,%d", &OldX, &OldY, &NewX, &NewY);
  if (OldX < 1 || OldX > 8 || OldY < 1 || OldY > 8 || NewX < 1 || NewX > 8 || NewY < 1 || NewY > 8)
    IllegalInput ();
  else
    TryMove ();  /*  Check that the move is valid and make it  */

To check that a player is moving his own piece, use either of the "Change Turn" methods above and add this:

  if (BlacksMove)
  {
    if (*ChessBoard[OldX][OldY] == 'B')  /*  Black's move and the piece is black  */
      MakeMove ();
  }
  else
    if (*ChessBoard[OldX][OldY] == 'W')  /*  White's move and the piece is white  */
      MakeMove ();



Good Luck
Kent
0
 
LVL 16

Expert Comment

by:imladris
ID: 9839512
You could also consider accepting logical notation. A common form of move notation designates the rows on the board from 1 to 8 and the columns from a to h. So KP - P4 (your example) would be written e2 - e4 and the standard response would be written e7 - e5. This would be much easier for chess players to use.

It would not be hard to interpret, in fact, you could rid of the need for keeping track of whose move it is. Translate a - h to 0 - 7 for the column coordinates and 1 - 8 to 0 - 7 for the rows, and they're ready to use as indexes.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 109
c language help - file paths 7 150
C hashtable library 3 100
C++ finding a sting in a char* string from a text file 3 120
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question