Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Programming Challenge: Noughts and Crosses

Name: Anonymous 2009-08-11 16:23

I just got finished with my first program (that is, the first one I haven't just copied out of a book). It's a text based version of Noughts and Crosses (aka Tic-tac-toe) without an AI, and it looks pretty ugly to me. Commenting is half-assed, some variables are given nondescript names, and I can tell that my loop structure can use a little work. Still, it runs.

Show me up, /prog/. Write a more elegant version in the language of your choice. Surely it's not a difficult task for [o][u]EXPERT PROGRAMMERS[/o][/u] such as yourselves. If you've got any suggestions for me, those would be nice, too.

Name: Anonymous 2009-08-11 18:58

#include <stdlib.h>
#include <curses.h>
#include <signal.h>

char board[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int x_win = 0, o_win = 0;

void done(int _)
{
    exit(endwin());
}

int play_turn(char c)
{
    mvaddch(3, 14, c);

    switch (getch()) {
        case KEY_A1:
            if (board[0][0] == 0) {
                mvaddch(1, 2, c);
                return 0;
            } else return -1;
        case KEY_UP:
            if (board[0][1] == 0) {
                mvaddch(1, 6, c);
                return 0;
            } else return -1;
        case KEY_A3:
            if (board[0][2] == 0) {
                mvaddch(1, 10, c);
                return 0;
            } else return -1;
        case KEY_LEFT:
            if (board[1][0] == 0) {
                mvaddch(3, 2, c);
                return 0;
            } else return -1;
        case KEY_B2:
            if (board[1][1] == 0) {
                mvaddch(3, 6, c);
                return 0;
            } else return -1;
        case KEY_RIGHT:
            if (board[1][2] == 0) {
                mvaddch(3, 10, c);
                return 0;
            } else return -1;
        case KEY_C1:
            if (board[2][0] == 0) {
                mvaddch(5, 2, c);
                return 0;
            } else return -1;
        case KEY_DOWN:
            if (board[2][1] == 0) {
                mvaddch(5, 6, c);
                return 0;
            } else return -1;
        case KEY_C3:
            if (board[2][2] == 0) {
                mvaddch(5, 10, c);
                return 0;
            } else return -1;
        default:
            return -1;
    }

    return 0;
}

int check_victory()
{
    int i, j, x, o;
    for (i = 0; i < 3; ++i) {
        int x_h, x_v, o_h, o_v;
        x_h = x_v = o_h = o_v = 0;

        for (j = 0; j < 3; ++j) {
            if (board[i][j] == 'X') ++x_h;
            if (board[j][i] == 'X') ++x_v;
            if (board[i][j] == 'O') ++o_h;
            if (board[j][i] == 'O') ++o_h;
        }

        if (x_h == 3 || x_v == 3) return x_win = 1;
        if (o_h == 3 || o_v == 3) return o_win = 1;
    }

    x = o = 0;
    if (board[0][0] == 'X') ++x;
    if (board[0][0] == 'O') ++o;
    if (board[1][1] == 'X') ++x;
    if (board[1][1] == 'O') ++o;
    if (board[2][2] == 'X') ++x;
    if (board[2][2] == 'O') ++o;
    if (x == 3) return x_win = 1;
    if (o == 3) return o_win = 1;

    x = o = 0;
    if (board[0][2] == 'X') ++x;
    if (board[0][2] == 'O') ++o;
    if (board[1][1] == 'X') ++x;
    if (board[1][1] == 'O') ++o;
    if (board[2][0] == 'X') ++x;
    if (board[2][0] == 'O') ++o;
    if (x == 3) return x_win = 1;
    if (o == 3) return o_win = 1;

    return 0;
}

int main(int argc, char **argv)
{
    int i;

    signal(SIGINT, done);

    initscr();
    keypad(stdscr, TRUE);
    nonl();
    cbreak();
    noecho();

    mvaddch(1, 4, '|');
    mvaddch(1, 8, '|');
    mvaddch(3, 4, '|');
    mvaddch(3, 8, '|');
    mvaddch(5, 4, '|');
    mvaddch(5, 8, '|');

    for (i = 1; i < 12; ++i) {
        mvaddch(2, i, '-');
        mvaddch(4, i, '-');
    }

    mvaddch(2, 4, '+');
    mvaddch(2, 8, '+');
    mvaddch(4, 4, '+');
    mvaddch(4, 8, '+');

    for (;;) {
        play_turn('X');
        if (check_victory()) break;
        play_turn('Y');
        if (check_victory()) break;
    }

    done(0);
    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List