TicTacToe – C (procedural style)

This is a simple version of TicTacToe written in C.

I wrote this primarily to help a friend of mine. It has virtually no error checking (primarily in regards to the scanf functions, so don’t mess up while inputting!)

#include <stdio.h>
 
char board[3][3];
 
char turn = 'X';
 
void init(char board[3][3]);
void draw(char board[3][3]);
void move(char board[3][3], char turn);
int isGameOver(char board[3][3]);
 
 
int main() {
    init(board);
    while(!isGameOver(board)) {
        draw(board);
        move(board, turn);
        // change turn
        if(turn == 'X') {
            turn = 'O';
        } else {
            turn = 'X';
        }
    }
}
 
void init(char board[3][3]) {
    int x, y;
    for(y = 0; y < 3; y++) {
        for(x = 0; x < 3; x++) {
            board[x][y] = ' ';
        }
    }
}
 
void move(char board[3][3], char turn) {
    int x, y, moved = 0;
    printf("%c's Turn:\n", turn);
    while(moved == 0) {
        printf("Input column (0-2):\n");
        scanf("%d", &x);
        printf("Input row (0-2):\n");
        scanf("%d", &y);
        if(x >= 0 && x <= 2 && y >= 0 && y <= 2) {
            if(board[x][y] == ' ') {
                board[x][y] = turn;
                moved = 1;
            } else {
                printf("You can not go there!\n");
            }
        } else {
            printf("Row or Column invalid!\n");
        }
    }
}
 
int isGameOver(char board[3][3]) {
    int x, y;
    // check rows
    for(y = 0; y < 3; y++) {
        if(board[0][y] != ' ') {
            if(board[0][y] == board[1][y] && board[1][y] == board[2][y]) {
                printf("Player %c Wins!\n", board[0][y]);
                return 1;
            }
        }
    }
    // check columns
    for(x = 0; x < 3;x++) {
        if(board[x][0] != ' ') {
            if(board[x][0] == board[x][1] && board[x][1] == board[x][2]) {
                printf("Player %c Wins!\n", board[x][0]);
                return 1;
            }
        }
    }
    // check diagonals
    if(board[0][0] != ' ') {
        if(board[x][0] == board[1][1] && board[1][1] == board[2][2]) {
            printf("Player %c Wins!\n", board[0][0]);
            return 1;
        }
    }
    if(board[2][0] != ' ') {
        if(board[2][0] == board[1][1] && board[1][1] == board[0][2]) {
            printf("Player %c Wins!\n", board[2][0]);
            return 1;
        }
    }
    // check cat game
    if(board[0][0] != ' ' && board[1][0] != ' ' && board[2][0] != ' ' &&
       board[0][1] != ' ' && board[1][1] != ' ' && board[2][1] != ' ' &&
       board[0][2] != ' ' && board[1][2] != ' ' && board[2][2] != ' ') {
        printf("Cat Game!");
        return 1;
    }
    // the game is still going
    return 0;
}
 
void draw(char board[3][3]) {
    int x, y;
    for(y = 0; y < 3; y++) {
        for(x = 0; x < 3; x++) {
            printf("%c", board[x][y]);
            if(x < 2) {
                printf("|");
            }
        }
        printf("\n");
        if(y < 2) {
            printf("-----\n");
        }
    }
}
You can leave a response, or trackback from your own site.

4 Responses to “TicTacToe – C (procedural style)”

  1. Scott says:

    Hey, so you used forward declarations of the functions so you could include the array in them?

  2. kenny says:

    Hey Scott, thanks for the question.
    That’s not entirely the case, but close.
    Take this for example, if you try to use a variable name “i” in code but never define it, the code has no idea what it is, so at some point you have to say “int i;”
    or “char i;”
    or whatever type it is correct
    so the same goes for functions
    you have to tell the code to expect a function of a specific name and behavior
    so the things you see at the beginning are telling the code that hey there is a function named init that returns void and excepts a char array [3][3] (and the name is optional i just like it for style)
    so that later on in the code when you call
    init(board); it knows what that means

    so in general before you use a function
    you must at some point tell the compiler about it

    now,
    if you go int add(int a, int b) {return a+b;}
    THEN below that type add(a,b);
    it will work
    since you defined the full function before calling it

    when code gets real big
    or multiple files start to use the same set of codeyou tend to factor these function declarations into header files that are shared then put the function definitions in C files
    but for small examples like this

    i just put the declarations up at top of the c file, this is common practice

  3. Hi Ken
    I really like your TTT C code-thanks!
    I have been trying to get a simple chessboard to display in the console and take user move inputs to move the pieces but my C coding skill is limited! I thought I might try and adapt your code to help my aim. So I will post what I have so far and wonder if you could help me get it working? :-)
    #include

    /*
    * Partly based on KenSoft tictactoe
    * @http://ken-soft.com/2011/07/19/tictactoe-c-procedural-style/
    */

    char board[8][8];
    char turn = ‘W’;

    void init();
    void draw();
    void move(char board[8][8], char turn);
    int isGameOver(char board[8][8]);

    int main() {
    init(board);
    while(!isGameOver(board)) {
    draw(board);
    move(board, turn);
    // change turn
    if(turn == ‘W’) {
    turn = ‘B’;
    } else {
    turn = ‘W’;
    }
    }
    }

    void init() {
    int r;
    int c;

    char board[8][8]={
    {‘r’, ‘n’, ‘b’, ‘q’, ‘k’, ‘b’, ‘n’, ‘r’},
    {‘p’, ‘p’, ‘p’, ‘p’, ‘p’, ‘p’, ‘p’, ‘p’},
    {‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘},
    {‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘},
    {‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘},
    {‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘},
    {‘P’, ‘P’, ‘P’, ‘P’, ‘P’, ‘P’, ‘P’, ‘P’},
    {‘R’, ‘N’, ‘B’, ‘Q’, ‘K’, ‘B’, ‘N’, ‘R’}
    };
    printf(“\n”);
    for (r=0;r<8;r++)
    {
    for (c=0;c= 0 && r = 0 && c <= 8) {
    if(board[r][c] == ' ') {
    board[r][c] = turn;
    moved = 1;
    } else {
    printf("You can not go there!\n");
    }
    } else {
    printf("Row or Column invalid!\n");
    }
    }
    }
    int isGameOver(char board[8][8]) {

    // the game is still going
    return 0;
    }

    void draw() {
    int r;
    int c;
    char board[8][8]={
    {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
    {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
    {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
    {'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}
    };
    printf("\n");
    for (r=0;r<8;r++)
    {
    for (c=0;c<8;c++)
    {
    printf("%c",board[r][c]);

    }
    printf("\n");

    }
    }

    Many thanks…:-)

    • kenny says:

      Glad it helped! I’m a bit swamped at the moment, but if I have some free time i’ll try to help you debug it! In the mean time, keep trying and update me with your status :)

Leave a Reply

Powered by WordPress | Designed by: WordPress Themes | Thanks to best wordpress themes, Find WordPress Themes and Themes Directory