9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: plan6@room3420.net
To: 9fans <9fans@9fans.net>
Subject: [9fans] Learning C the hard way
Date: Sun, 20 Aug 2023 06:00:42 -0400	[thread overview]
Message-ID: <16925256420.2CAEa1.581760@composer.9fans.topicbox.com> (raw)

[-- 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 --]

             reply	other threads:[~2023-08-20 10:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-20 10:00 plan6 [this message]
2023-08-20 12:05 ` [9fans] " adr via 9fans
2023-08-20 12:48   ` plan6

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=16925256420.2CAEa1.581760@composer.9fans.topicbox.com \
    --to=plan6@room3420.net \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).