减少EXE大小

时间:2015-02-21 07:19:36

标签: c space-efficiency coding-efficiency

我在C中写了一个TicTacToe程序,在我的硬盘上读取68.3 KB,我想知道是否有任何类型的优化技术可用于减少.exe文件中的字节数。

例如,我的代码中是否有任何内容可以更改以使生成的可执行文件“更小”?

代码:

#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;
}

0 个答案:

没有答案