[C language] Realize the three-pointed chess

The C language implementation of Sanmoku mainly uses the relevant knowledge of two-dimensional arrays, so we use multi-file programming to implement this program, we need to create a header file and two source files to implement:
1.game.h–> function declaration With the macro definition
2.game.c –> the realization of each function of the game
3.main.c –> the definition of the function

Ideas:
1. Put the execution of each function in the game execution function game function, and put the game function in the loop so that we can still play after the game is over.
2. Select the structure to judge whether to continue playing the game or exit the game, there must be a main() function implementation, we put this framework in main.c:

Specific code module
main.c

#include"game.h"
#define _CRT_SECURE_NO_WARNINGS 1

void  Menu ()
 {
     printf ( "#############################\n" );
     printf ( "##Welcome to three Pieces##\n" );
     printf ( "############################\n" );
     printf ( "## 1.Play 2.Exit ##\n" );
     printf ( "#############################\n" );
     printf ( "Please select:> " );
}
int  main ()
 {
     int quit = 0 ;
     while (!quit) { //if not quit
        Menu();
        int select = 0;
        scanf("%d", &select);
        switch (select) {
        case 1:
            Game();
            break;
        case 2:
            quit = 1 ;
             break ;
         default :
             printf ( "You made a mistake, please try again!\n" );
             break ;

        }
    }
    printf("byebye!\n");
    system("pause");
}

game.h

# ifndef __GAEM_H__ 
# define __GAME_H__

# include <stdio.h>
 # include <windows.h>
 # include <time.h>//random numbers
 # pragma  warning (disable: 4996) 
// macro definitions are placed in the header file 
# define ROW 3 
# define COL 3 
# define Player_C 'X'
 # define Computer_C 'O'
 # define INIT   ' '
 //implement 
void  InitBoard ( char board[][COL], int row, int col) in game.c ; //rows can be omitted, columns are not can 
void ShowBoard(char board[][COL], int row, int col);
void ComputerMove(char board[][COL], int row, int col);
char Judge(char board[][COL], int row, int col);


void Game();

#endif

game.c

#include"game.h"

void  InitBoard ( char board[][COL], int row, int col) //The row can be omitted, the column cannot be
 {
     int i = 0 ;
     for (; i < row; i++) {
         int j = 0 ;
         for (; j < col; j++) {
            board[i][j] = INIT;
        }
    }
}
void ShowBoard(char board[][COL], int row, int col)
{
    printf("   1  | 2 | 3 \n");
    printf("---------------\n");
    int i = 0;
    for (; i < row; i++){
    printf("%d |",i+1);
    int j = 0;
    for (; j < col; j++) {
        printf("%c  |", board[i][j]);
    }
    printf("\n");
    printf("---------------\n");
    }
    /*printf("3 |");
    for (int j = 0; j < col; j++) {
        printf("%c  |", board[2][j]);
    }
        printf("\n");
        printf("---------------\n");*/
}
void ComputerMove(char board[][COL], int row, int col)
{
    while (1) {
        int x = rand() % row;
        int y = rand() % col;
        if (board[x][y] == ' ') {
            board[x][y] = Computer_C;
            break;
        }
    }
}

char Judge(char board[][COL], int row, int col)
{
    int i = 0;
    for (; i < row; i++) {
        if(board[i][0]==board[i][1]&&\
            board[i][1]==board[i][2]&&\
            board[i][0] != ' '){
            return board[i][0];
        }
    }
    for (i = 0; i < col; i++) {
        if (board[0][i] == board[1][i] && \
            board[1][i] == board[2][i] && \
            board[0][i] != ' ') {
            return board[i][0];
        }
    }
    if (board[0][0] == board[1][1] && \
        board[1][1] != ' ' && \
        board[1][1] == board[2][2]) {
        return board[1][1];
    }
    if (board[0][2] == board[1][1] && \
        board[1][1] != ' ' && \
        board[1][1] ==board[2][0]) {
        return board[1][1];
    }
    for (i = 0; i < row; i++) {
        int j = 0;
        for (; j < col; j++) {
            if (board[i][j] == INIT) {
                return 'N';
            }
        }
    }
    return 'F';
}

void Game() {
    srand(( unsigned  long )time( NULL )); //seed the next random number 
    char board[ROW][COL];
    InitBoard(board, ROW, COL);
    char result = 0;
    do {
        ShowBoard(board, ROW, COL);
        printf ( "Please move:> " );
         int x = 0 ;
         int y = 0 ;
        scanf_s( "%d %d" , &x, &y);
         if (x <= 0 || x > 3 || y <= 0 || y > 3 ) {
             printf ( "The location you entered is wrong, please try again Enter!\n" );
             continue ;
        }
        if (board[x - 1 ][y - 1 ] != ' ' ) {
             printf ( "This position is already occupied, please re-enter!\n" );
        }
        board[x - 1][y - 1] = Player_C;
        result = Judge(board, ROW, COL);
        if (result != 'N') {//'N'->next,'F'->FULL,'X','O'
            break;

        }

        ComputerMove(board, ROW, COL);
        result = Judge(board, ROW, COL);
        if (result != 'N') {//'N'->next,'F'->FULL,'X','O'
            break;

        }
    } while ( 1 );
     if (result == Player_C) {
         printf ( "Congratulations, you won!\n" );
    }
    else  if (result == Computer_C) {
         printf ( "Sorry, you lost!\n" );
    }
    else {
         printf ( "Not bad, a tie!\n" );
    }
    printf ( "You're having a good time, do you want to play again!\n" );
}

Test effect

Leave a Comment

Your email address will not be published. Required fields are marked *