Evaluate game-of-the-old code [closed]

-6

I've been programming for a while and would like you to rate my code in C.

It's an old game using a circular data structure.

My code is here :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/*
 *      rows
 *
 * 7   = 000 000 111
 * 56  = 000 111 000
 * 448 = 111 000 000
 *
 *     columns
 *
 * 73  = 001 001 001
 * 146 = 010 010 010
 * 292 = 100 100 100
 *
 *    diagonals
 *
 * 84  = 001 010 100
 * 273 = 100 010 001
 *
 */
int win_code[8] = {7,56,448, 73,146,292, 84,273};

struct _Player{
    int    cod;
    char   symbol;
    struct _Player *next;
};

typedef struct _Player* Player;
Player player;

char board[9]="         ";

//after add on board: player->cod+=pow(2,index);
int add_board(int index);

// check_win()==false: player=player->next;
int check_win();

void print_board();

int main(){
    //Create player 1
    player                  = (Player) malloc(sizeof(Player));

    //player 1 variables
    player->symbol          = 'X';
    player->cod             = 0;

    //Create player 2
    //after player 1 is player 2
    player->next            = (Player) malloc(sizeof(Player));

    //player 2 variables
    player->next->symbol    = 'O';
    player->next->cod       = 0;

    //after player 2 is player 1
    player->next->next      = player;

    /*  one struct control two players.
         _________________|struct _Player*|_________________
        |                                                   |
        |                player=player->next;               |
        |            ___________________________            |
        |           |                           |           |
        |    _______|_______             ______\ /______    |
        |   |               |           |               |   |
        |   |       X       |           |       O       |   |
        |   |_______________|           |_______________|   |
        |          / \                          |           |
        |           |___________________________|           |
        |                                                   |
        |                 player=player->next;              |
        |___________________________________________________|
    */

    int index, turn=1;

    do{
        print_board();
        do{
            printf("Enter a number from 1 to 9 (Valid).\n");
            scanf("%d",&index);
            fflush(stdin);
        //correct range? try add on board.
        }while((index>=1||index<=9)&&!add_board(index));
    }while(!check_win()&&turn++<9);
    return 0;
}

int add_board(int index){
    index--;
    //board is empty.
    if(board[index]==' '){
        //add on board.
        board[index]=player->symbol;
        /*
               |   |
            2^0|2^1|2^2
            ---|---|---
            2^3|2^4|2^5
            ---|---|---
            2^6|2^7|2^8
               |   |
        */
        player->cod+=pow(2,index);
        return 1;
    }
    return 0;
}

int check_win(){
    int x;
    for(x=0;x<9;x++){
        /*
            Example check:
                X| |O
                O|X|   player X cod = 337 = 101010001
                X|O|X
            player->cod:    101010001
                                     &
            win_code[8]:    100010001
                            ---------
                            100010001 == win_code[8]
        */
        if((player->cod&win_code[x])==win_code[x]){
            print_board();
            printf("\n\t   Player: %c Win!\n\n",player->symbol);
            return 1;
        }
    }
    //player did not win, next player.
    player=player->next;
    return 0;
}

void print_board(){
    system("cls");
    printf("\n\n\t   Tic Tac Toe\n\n");
    printf("\t     |     |     \n");
    printf("\t  %c  |  %c  |  %c  \n",board[0],board[1],board[2]);
    printf("\t_____|_____|_____\n");
    printf("\t     |     |     \n");
    printf("\t  %c  |  %c  |  %c  \n",board[3],board[4],board[5]);
    printf("\t_____|_____|_____\n");
    printf("\t     |     |     \n");
    printf("\t  %c  |  %c  |  %c  \n",board[6],board[7],board[8]);
    printf("\t     |     |     \n");
    printf("\n");
}
    
asked by anonymous 21.03.2018 / 04:27

1 answer

2
  • (index>=1||index<=9)&&!add_board(index) should be index < 1 || index > 9 || !add_board(index) .

  • player->cod+=pow(2,index); can be rewritten as player->cod |= 1 << index; . This also eliminates the need to use math.h .

  • Before return 0; of main , enter free(player->next); free(player); . It is not good for you to allocate memory and not deallocate.

  • Instead of int x; for(x=0;x<9;x++){ , use for (int x = 0; x < 8; x++) { . Note that the positions of your array go from 0 to 7, so the <9 is wrong.

  • I would put print_board() inside the do-while external of main , soon after the end of do-while internal. With this, print_board() within check_win() can be removed.

  • 21.03.2018 / 04:57