Separating the result

hello i have this code, its chess engine by Andre Adrian
im having trouble with Separating the result and save it into tow char for example : (e2e4) to first char =e2 and second char = e4
any help ? and thnx

chess_program.ino (11.2 KB)

are you literally meaning a char ? ie 0xE2 and 0xE4 (a byte) or do you mean strings "E2" and "E4"?

Is it this piece of code? If not, point out where it is in your code.

void loop() {
  int r;
  // Take move from human
  while (stringComplete == false) {
    getserialchar();
  }
  Serial.print(mn);
  Serial.print(". ");
  Serial.print(inputString.substring(0,4));
  c[0] = inputString.charAt(0);
  c[1] = inputString.charAt(1);
  c[2] = inputString.charAt(2);
  c[3] = inputString.charAt(3);
  c[4] = 0;

If so, change the below first

char *p, c[5], Z;            /* p=pointer to c, c=user input, computer output, Z=recursion counter */

to

char *p, c1[5], c2[5], Z;            /* p=pointer to c, c=user input, computer output, Z=recursion counter */

And next change the code that I pointed out

void loop() {
  int r;
  // Take move from human
  while (stringComplete == false) {
    getserialchar();
  }
  Serial.print(mn);
  Serial.print(". ");
  Serial.print(inputString.substring(0,4));
  c1[0] = inputString.charAt(0);
  c1[1] = inputString.charAt(1);
  c1[2] = 0;
  c2[0] = inputString.charAt(2);
  c2[1] = inputString.charAt(3);
  c2[2] = 0;

1. e2e4 is a valid move (Fig-1) -- why do you want to separate it into these two parts: e2 and e4?

chess.png
Figure-1:

chess.png

int
main ()
{
    const char *s = "e2e4";
    char  t [4];
    char  q [4];

    sscanf (s, "%2s%2s", t, q);
    printf (" %s %s\n", t, q);
}

To separate e2e4 into two parts (e2 and e4), you may include the following codes at the appropriate place of your sketch.

char initialPos[3];
char finalPos[3];
char myData[5];
byte n = Serial.available();
if(n == 4)
{
    for(int i=0; i<4; i++)
   {
       myData[i] = Serial.read();
   }
   initialPos[0] = myData[0];
   initialPos[1] = myData[1];    //char initialPos[] array holds e2
   //------------------------------------------------------------------
   finalPos[0] = myData[2];
   finalPos[1] = myData[3];    //char finalPos[] array holds e4
   //------------------------------------------------------------------
}

@GolamMostafa - you might want to force the 3rd byte to '\0' if initialPos and finalPos are not a global variables. better safe than sorry :slight_smile:

J-M-L:
@GolamMostafa - you might want to force the 3rd byte to '\0' if initialPos and finalPos are not a global variables. better safe than sorry :slight_smile:

I have reserved 1-byte extra in the array in case the OP wants to print the content by inserting a null character.

I saw that, would have been probably good to mention as it seems OP is a beginner. hence my point.

J-M-L:
I saw that, would have been probably good to mention as it seems OP is a beginner. hence my point.

I always enjoy your interaction as it keeps me very inspired.:slight_smile:

:confused:

You definitely have something in mind when you allocated 3 bytes instead of 2 that were needed - so felt good to explain what was in your mind (and ensure there was not this belief that everything is always zero initialized automagically)

GolamMostafa:
1. e2e4 is a valid move (Fig-1) -- why do you want to separate it into these two parts: e2 and e4?

chess.png
Figure-1:

hello i ant to separate the result b8c6
and send it to another arduino

sterretje:
Is it this piece of code? If not, point out where it is in your code.

void loop() {

int r;
  // Take move from human
  while (stringComplete == false) {
    getserialchar();
  }
  Serial.print(mn);
  Serial.print(". ");
  Serial.print(inputString.substring(0,4));
  c[0] = inputString.charAt(0);
  c[1] = inputString.charAt(1);
  c[2] = inputString.charAt(2);
  c[3] = inputString.charAt(3);
  c[4] = 0;



If so, change the below first


char p, c[5], Z;            / p=pointer to c, c=user input, computer output, Z=recursion counter */



to


char p, c1[5], c2[5], Z;            / p=pointer to c, c=user input, computer output, Z=recursion counter */



And next change the code that I pointed out


void loop() {
  int r;
  // Take move from human
  while (stringComplete == false) {
    getserialchar();
  }
  Serial.print(mn);
  Serial.print(". ");
  Serial.print(inputString.substring(0,4));
  c1[0] = inputString.charAt(0);
  c1[1] = inputString.charAt(1);
  c1[2] = 0;
  c2[0] = inputString.charAt(2);
  c2[1] = inputString.charAt(3);
  c2[2] = 0;

sory i f you got me wrong but i want separate the output not the input.
in my project i mad another programe in another arduino to control a CNC to move the peace,