0

I wrote a TicTacToe program in C that reads 68.3 KB on my hard disk, and I was wondering if there are any sort of optimization techniques I could use to decrease the amount of bytes in my .exe file.

For instance, is there anything in my code that I could change to make the resulting executable "smaller"?

Code:

#include <stdio.h>
#include <stdbool.h>

void drawBoard(int [3][3]);
bool boardFull(int [3][3]);
bool checkWin(int [3][3], int);

int main()
{
    int board[3][3];
    int marker, turn, row, column, i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
            board[i][j] = -1;
    }

    printf("Player One is X\n");
    printf("Player Two is O\n");
    drawBoard(board);

    while(!boardFull(board))
    {
        if((turn % 2) == 0)
        {
            printf("Player One's turn.\n");
            marker = 1;
        }
        else
        {
            printf("Player Two's turn.\n");
            marker = 0;
        }

        printf("Which row? ");
        scanf("%i", &row);

        printf("Which column? ");
        scanf("%i", &column);

        if(board[row - 1][column - 1] != -1)
        {
            while(board[row - 1][column - 1] != -1)
            {
                printf("Space already taken. Try again.\n");

                printf("Which row? ");
                scanf("%i", &row);

                printf("Which column? ");
                scanf("%i", &column);

                printf("\n");
            }
        }
        board[row - 1][column - 1] = marker;

        drawBoard(board);

        if(checkWin(board, marker))
        {
            if(marker == 1)
                printf("Player One wins!!");
            else
                printf("Player Two wins!!");

            return 0;
        }

        turn++;
    }
    printf("Cat's game!\n");    

    return 0;
}

void drawBoard(int board[3][3])
{
    int i, j;

    printf("\n");
    for(i = 0; i < 3; i++)
    {
        printf("  ");
        for(j = 0; j < 3; j++)
        {   
            if(board[i][j] == 0)
                printf("O");
            else if(board[i][j] == 1)
                printf("X");
            else
                printf(" ");

            if(j == 2)
                continue;
            else
                printf(" | ");
        }
        if(i == 2)
            printf("\n\n");
        else
            printf("\n -----------\n");
    }
}

bool boardFull(int board[3][3])
{
    int i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            if(board[i][j] == -1)
                return false;
        }
    }

    return true;
}

bool checkWin(int board[3][3], int w)
{
    return
        board[0][0] == w && board[1][1] == w && board[2][2] == w
        || board[0][2] == w && board[1][1] == w && board[2][1] == w
        || board[0][0] == w && board[1][0] == w && board[2][0] == w
        || board[0][1] == w && board[1][1] == w && board[2][1] == w
        || board[0][2] == w && board[1][2] == w && board[2][2] == w
        || board[0][0] == w && board[0][1] == w && board[2][2] == w
        || board[1][0] == w && board[1][1] == w && board[2][2] == w
        || board[2][0] == w && board[2][1] == w && board[2][2] == w;
}
Delfino
  • 967
  • 4
  • 21
  • 46
  • You might wanna ask this on [Code Review](http://codereview.stackexchange.com) – Himal Feb 21 '15 at 07:23
  • Also, don't forget to remove this question. – Himal Feb 21 '15 at 07:26
  • 3
    Compile it with tcc. It tends to produce very small executable files. The practicality of that undertaking is an entirely separate question though... – void_ptr Feb 21 '15 at 07:30
  • 1
    Use http://upx.sourceforge.net/ – bhathiya-perera Feb 21 '15 at 07:32
  • 1
    The large size is usually due to the `stdio` library being included in the executable. Some systems, such as UNIX, use shared libraries which results in smaller executables. You can trim the size somewhat by not including debug information and stripping the symbol table from the executable. The exact details depend on **which OS and compiler you are using**. – Nisse Engström Feb 21 '15 at 07:42
  • Use UPX if you want your program to be detected as a virus and to consume more system memory and start up more slowwly. – David Heffernan Feb 21 '15 at 08:12

0 Answers0