Well, I made a mistake.
Holiday time and I just took my 9front x230 with me, thinking it will be enough to learn C the hard way.
With man pages and sources.
I was wrong :/
nsurf is cool, but kind of slow and I'm stuck with my very first program. A very ridiculous chess UI (in text mode) I intend to develop to play with my daughters on an SSH connection on the same user space.
Don't mind the chess context. I just want to move chars in a double array and print the array.
I made what I thought a logic approach but it works only with the first move.
I know it's not the place to ask this kind of help, because, obviously it won't help 9front or the community in any way and it's just a personal problem due to the fact that I'm not the sharpest tool in the shed and that asking questions about C when it's not ANSI makes people turn crazy.
But maybe someone is bored and have some time to debug this very short program :)
(Suppr or Del to quit).
Here we go (and thanks for reading) :
*************************************************************************
#include <u.h>
#include <libc.h>
#include <stdio.h>
/* define the rows of the chessboard */
char chess[8][10] =
{"8 RCBQKBCR", "7 bbbbbbbb", "6 ........", "5 ........",
"4 ........", "3 ........", "2 wwwwwwww", "1 rcfqkfcr"};
/* print the chessboard */
void
printboard(void) {
for(int n=0; n<8; n++) {
for(int i=0; i<10; i++) {
print(" %c", chess[n][i]);
}
print("\n");
}
print("\n");
}
/* array for the move coordinates */
int coord[] = {0, 0, 0, 0};
int player = 1;
/* get the move coordinates from the player */
/* exceptions will be treated later */
void
coordinates(void) {
if (player == 1) {
player = 2;
print("Whites Enter Coordinates: "); }
else {
player = 1;
print("Blacks Enter Coordinates: "); }
for(int i = 0; i < 4; i++) {
char x = getchar();
if ((x >= 'A' && x <= 'H') || (x >= 'a' && x <= 'h'))
switch (x) {
case 'a': coord[i] = 2; break;
case 'b': coord[i] = 3; break;
case 'c': coord[i] = 4; break;
case 'd': coord[i] = 5; break;
case 'e': coord[i] = 6; break;
case 'f': coord[i] = 7; break;
case 'g': coord[i] = 8; break;
case 'h': coord[i] = 9; break;
}
else if (x >= '1' && x <= '8')
switch (x) {
case '1': coord[i] = 7; break;
case '2': coord[i] = 6; break;
case '3': coord[i] = 5; break;
case '4': coord[i] = 4; break;
case '5': coord[i] = 3; break;
case '6': coord[i] = 2; break;
case '7': coord[i] = 1; break;
case '8': coord[i] = 0; break;
}
}
}
/* ************************************************************* */
void
main() {
print("\n");
printboard();
while(1) {
coordinates();
/* 0-1 and 2-3 inverted because rows before columns */
char last = chess[coord[1]][coord[0]];
chess[coord[1]][coord[0]] = '.'; /* moved pieces make the case empty, so '.' */
chess[coord[3]][coord[2]] = last;
print("\n%c\n", last); /* just to check last */
print("\n");
printboard();
}
exits(0);
}