SAI || Sommersemester 1997 || Systemnahe Software II || Übungen

<- Alle Module

Lösung zu Blatt 5 (Aufgabe 6): gbtest.c

Aus Aufgabe 3: Testanwendung für Ein- und Ausgabefunktionen.

gbtest.c

/*
 *      gbtest.c        - Test suite for gobang user interface modules
 *
 *      Martin Hasch, University of Ulm, May 1997
 */

#include        <stdio.h>
#include        <stdlib.h>
#include        <unistd.h>
#include        "gbdisp.h"
#include        "gbuser.h"

static char *comment[3] = {
        "game over",
        "next move: black",
        "next move: white"
};

static void game(int size)
{
        int whoseturn;
        int x, y;

        if ( gb_reset(size) != GB_OK ) {
                fprintf(stderr, "%d: invalid board size\n", size);
                exit(EXIT_FAILURE);
        }
        whoseturn = 1;
        x = y = (gb_size()+1) >> 1;
        (void) gb_goto(x, y);
        for (;;) {
                gb_status(comment[whoseturn]);
                if ( !whoseturn )
                        return;
                gb_turn(whoseturn);
                switch ( gb_getcommand() ) {
                case GBU_QUIT:
                        whoseturn = 0;
                        break;
                case GBU_LEFT:
                        if ( gb_goto(x-1, y) == GB_OK )
                                --x;
                        break;
                case GBU_DOWN:
                        if ( gb_goto(x, y-1) == GB_OK )
                                --y;
                        break;
                case GBU_UP:
                        if ( gb_goto(x, y+1) == GB_OK )
                                ++y;
                        break;
                case GBU_RIGHT:
                        if ( gb_goto(x+1, y) == GB_OK )
                                ++x;
                        break;
                case GBU_REDRAW:
                        gb_redraw();
                        break;
                case GBU_MOVE:
                        (void) gb_set(x, y, whoseturn);
                        /* FALL INTO NEXT CASE */
                case GBU_PASS:
                        whoseturn = 3 - whoseturn;
                        break;
                case GBU_ERASE:
                        (void) gb_set(x, y, 0);
                        break;
                }
        }
}

int main(int argc, char *argv[])
{
        int n;

        n = 0;
        if ( argc > 2 || argc == 2 && !(n = atoi(argv[1])) ) {
                fprintf(stderr, "Usage: %s [size]\n", argv[0]);
                exit(EXIT_FAILURE);
        }
        game(n);
        exit(EXIT_SUCCESS);
}
<- Alle Module
SAI || Sommersemester 1997 || Systemnahe Software II || Übungen

Martin Hasch, Juni 1997