9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Learning C the hard way
@ 2023-08-20 10:00 plan6
  2023-08-20 12:05 ` [9fans] " adr via 9fans
  0 siblings, 1 reply; 3+ messages in thread
From: plan6 @ 2023-08-20 10:00 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 5292 bytes --]

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);
}



------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tcb666367f7ec3af5-M5a95ba86fc8531e7668aa924
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 11924 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [9fans] Re: Learning C the hard way
  2023-08-20 10:00 [9fans] Learning C the hard way plan6
@ 2023-08-20 12:05 ` adr via 9fans
  2023-08-20 12:48   ` plan6
  0 siblings, 1 reply; 3+ messages in thread
From: adr via 9fans @ 2023-08-20 12:05 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 423 bytes --]

Hi, plan6?

Your newlines are feeding the next getchar. You don't want to read chars, you want to read strings.
Read the documentation, don't use stdio.h, explore the 9 way...

Regards,
adr
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tcb666367f7ec3af5-Me54e289c46e8d4a3f30e66dc
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 1009 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [9fans] Re: Learning C the hard way
  2023-08-20 12:05 ` [9fans] " adr via 9fans
@ 2023-08-20 12:48   ` plan6
  0 siblings, 0 replies; 3+ messages in thread
From: plan6 @ 2023-08-20 12:48 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 408 bytes --]

Thanks adr and Frank (who answered my email) :)

I will explore your solutions and read the documentation.
You pointed my problem with accuracy and I'm very thankful for that :)
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tcb666367f7ec3af5-M23752d18ca6544f985de852c
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #2: Type: text/html, Size: 951 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-20 12:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-20 10:00 [9fans] Learning C the hard way plan6
2023-08-20 12:05 ` [9fans] " adr via 9fans
2023-08-20 12:48   ` plan6

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).