C language realizes three chess (can realize m row and n column chessboard and related victory judgment)

In order to realize the progress of the game of backgammon, there are the following steps:

First of all, we need to design a simplified menu for users to choose to enter the game.

Secondly, if the user enters the game, the writing and implementation of the chessboard is first required.

The user then plays chess and proceeds to print a new board.

The computer generates random numbers to play chess, and continues to print new chessboards.

Determine whether the computer wins the player to win a tie or continue playing.

So far we have completed an idea of a three-moon chess program, the core of which is the application of two-dimensional arrays. Since I find it troublesome, I put all the three-moon chess programs in a source file and implement them through the definition of macros. The checkerboard with m rows and n columns is no longer limited to a chessboard with three rows and three columns in terms of judgment conditions. And backgammon just add two more equal

Writing header files

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:6031)//realize the use of scanf function in vc system
#include <stdio.h>
#include <stdlib.h>
#include<time.h>//Realize the function of rand() function
#define ROW 5
#define COL 5//Take five as an example here

main function

int main()
{
    srand((unsigned int)time(NULL));//Call the srand function and timestamp to realize rand() truly random
    test();
    return 0;
}

test and menu functions

void menu()
{
    printf("**********************\\
");
    printf("**** 1.play 0.exit**\\
");
    printf("**********************\\
");//Game initial menu interface
}
void test()
{
    int input = 0;
    do//Because the game must be executed once, call the do while loop
    {
        menu();//call menu function
        printf("Please choose:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("Enter the game\\
");
            game();//Enter the game main frame game function
            break;
        case 0:
            printf("Exit the game\\
");
            break;
        default:
            printf("Re-enter\\
");
            break;
        }
    }
    while (input);

}

game function (function of the main program)

void game()
{
    char ret = 0;//Receive the return value
    char board[ROW][COL] = { 0 };//Define a two-dimensional array of board
    Initboard(board, ROW, COL);//Initialize the board array
    Displayboard(board,ROW,COL);//print the board
    while (1)//enter the loop
    {
        Playermove(board,ROW,COL);//The player plays chess and calls the Playermove function
        Displayboard(board, ROW, COL);//Print the board after playing chess
        ret=Iswin(board, ROW, COL);//Receive the return value after each chess game, judge the progress of the next step, and call the Iswin function
        //* means player wins, # means computer wins, D means draw, C means continue
        if (ret !='C')
        {
            break;//If it is not C, jump out of the loop directly
        }
        Computermove(board, ROW, COL);//Computer play chess, call Computermove function
        Displayboard(board, ROW, COL);
        ret=Iswin(board, ROW, COL);
        if (ret !='C')
        {
            break;
        }
    }
    if (ret == '*')
    {
        printf("Player wins\\
");
    }
    else if (ret == '#')
    {
        printf("Computer Victory\\
");
    }
    else
    {
        printf("tie");
    }
}

Initialization function Initboard()

void Initboard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0;i < row;i ++ )
    {
        for (j = 0;j < col;j ++ )
            board[i][j] = ' ';//Use two layers of for loops to assign each element of the array as a space to complete the initialization
    }

}

Print the chessboard function Displayboard()

void Displayboard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 1;i <= col;i ++ )
    {
        printf(" %d ", i);
        if (i == col)
            printf("\\
");
    }//print column label
    for (i = 0;i <row;i ++ )
    {
        for (j = 0; j < col; j + + )
        {
           if (j == 0)
            {
                printf("%d ", i + 1);//print line label
             }
            printf(" %c ", board[i][j]);//Accept space array element spaces
            if (j < col - 1)
                printf("|");//The last column does not add a vertical bar
        
        }
        printf("\\
");
        if (i < row - 1)
        {
            for (j = 0;j < col;j ++ )
            { if (j == 0)
                {
                    printf(" ---");
                }
                if (j > 0)
                printf("---");//print split line
                if (j < col - 1)
                    printf("|");//The last line does not print|

            }
            printf("\\
");//line break
        }
    }

}

Player move function Playermove()

void Playermove(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    
    while (1)
    {printf("Player please play chess\\
");
    printf("line:")
    scanf("%d", &x);
    printf("Column:")
    scanf("%d", &y);
        if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*';//Input the corresponding element in the corresponding space position
                break;// Jump out of the loop directly after playing chess
            }
            else
            {
                printf("This coordinate is already occupied, please re-enter\\
");//Some coordinates have already been played
            }

        }
        else
        {
            printf("The coordinates are illegal, please re-enter\\
");//If the coordinates are out of range, please re-enter
        }
    }
}

Computer move function Computermove()

void Computermove(char board[ROW][COL], int row, int col)
{
    printf("\\
");
    int x = 0;
    int y = 0;
    
    while (1)
    {x = rand() % row;//Generate 0-(row-1) random number
    y = rand() % col;//Generate a random number of 0-(col-1)
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            break;//Until the computer goes down to the correct position and jumps out of the loop
        }
    }

}

Judging the winning and losing function Iswin()

char Iswin(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0;i < row - 2;i ++ )
    {
        for (j = 0;j < col;j ++ )
        {//Two layers of nested loops to judge the victory of three in a row
            if (board[i][j] == board[i + 1][j] & amp; & amp; board[i + 1][j] == board[i + 2][j] & amp; & amp; board[i][j] != ' ')
            {
                return board[i][j];
            }
        }
    }
    for (j = 0;j < col - 2;j ++ )
    {
        for (i = 0;i < row;i ++ )
        {//Judge a row of three wins
            if (board[i][j] == board[i][j + 1] & amp; & amp; board[i][j + 1] == board[i][j + 2] & amp; & amp; board[i][j] != ' ')
                return board[i][j];
        }
    }
    for (i = 0;i < row - 2;i ++ )
    {
        for (j = 0;j < row - 2;j ++ )
        {//Judge the victory of the three from the upper left to the lower right
            if (board[i][j] == board[i + 1][j + 1] & amp; & amp; board[i + 1][j + 1] == board[i + 2][j + 2] & amp; & amp; board[i][j] != ' ')
                return board[i][j];
        }
    }
    for (i = 0;i < row-2;i ++ )
    {
        for (j = 2;j < col;j ++ )
        {//Judging three victories from upper right to lower left
            if (board[i][j] == board[i + 1][j - 1] & amp; & amp; board[i + 1][j - 1] == board[i + 2][j - 2] & amp; & amp; board[i][j] != ' ')
                return board[i][j];
        }
    }
    if (Isfull(board, ROW, COL) == 1)//Judge whether there is a tie
    {
        return 'D';//Tie returns D
    }
    return 'C';// None of the above assumptions are true Return to C and continue playing chess
}

Judging whether the draw is full ()

int Isfull(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0;i < row;i ++ )
    {
        for (j = 0;j < col;j ++ )
        {
            if (board[i][j] == ' ')//If there is a space, return 0, which means the board is not full
            {
                return 0;
            }
        }
    }
    return 1;//If the board is full and neither side has won, it means a tie, return 1
}

The corresponding full code is

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:6031)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 5
#define COL 5
int Isfull(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0;i < row;i ++ )
    {
        for (j = 0;j < col;j ++ )
        {
            if (board[i][j] == ' ')
            {
                return 0;
            }
        }
    }
    return 1;
}
char Iswin(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0;i < row - 2;i ++ )
    {
        for (j = 0;j < col;j ++ )
        {
            if (board[i][j] == board[i + 1][j] & amp; & amp; board[i + 1][j] == board[i + 2][j] & amp; & amp; board[i][j] != ' ')
            {
                return board[i][j];
            }
        }
    }
    for (j = 0;j < col - 2;j ++ )
    {
        for (i = 0;i < row;i ++ )
        {
            if (board[i][j] == board[i][j + 1] & amp; & amp; board[i][j + 1] == board[i][j + 2] & amp; & amp; board[i][j] != ' ')
                return board[i][j];
        }
    }
    for (i = 0;i < row - 2;i ++ )
    {
        for (j = 0;j < row - 2;j ++ )
        {
            if (board[i][j] == board[i + 1][j + 1] & amp; & amp; board[i + 1][j + 1] == board[i + 2][j + 2] & amp; & amp; board[i][j] != ' ')
                return board[i][j];
        }
    }
    for (i = 0;i < row-2;i ++ )
    {
        for (j = 2;j < col;j ++ )
        {
            if (board[i][j] == board[i + 1][j - 1] & amp; & amp; board[i + 1][j - 1] == board[i + 2][j - 2] & amp; & amp; board[i][j] != ' ')
                return board[i][j];
        }
    }
    if (Isfull(board, ROW, COL) == 1)
    {
        return 'D';
    }
    return 'C';
}
void Computermove(char board[ROW][COL], int row, int col)
{
    printf("\\
");
    int x = 0;
    int y = 0;
    
    while (1)
    {x = rand() % row;
    y = rand() % col;
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            break;
        }
    }

}
void Playermove(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    
    while (1)
    {printf("Player please play chess\\
");
    printf("The line is:");
    scanf("%d", &x);
    printf("The column is:");
    scanf("%d", &y);
        if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*';
                break;
            }
            else
            {
                printf("The coordinate is already occupied, please re-enter\\
");
            }

        }
        else
        {
            printf("The coordinate is illegal, please re-enter\\
");
        }
    }
}
void Displayboard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 1;i <= col;i ++ )
    {
        printf(" %d ", i);
        if (i == col)
            printf("\\
");
    }
    for (i = 0;i <row;i ++ )
    {
        for (j = 0; j < col; j + + )
        {
            if (j == 0)
            {
                printf("%d ", i + 1);
             }
            printf(" %c ", board[i][j]);
            if (j < col - 1)
                printf("|");
        
        }
        printf("\\
");
        if (i < row - 1)
        {
            for (j = 0;j < col;j ++ )
            {
                if (j == 0)
                {
                    printf(" ---");
                }
                if (j > 0)
                {
                    printf("---");
                }
                if (j < col - 1)
                    printf("|");

            }
            printf("\\
");
        }
    }

}
void Initboard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0;i < row;i ++ )
    {
        for (j = 0;j < col;j ++ )
            board[i][j] = ' ';
    }

}
void game()
{
    char ret = 0;
    char board[ROW][COL] = { 0 };
    Initboard(board, ROW, COL);
    Displayboard(board, ROW, COL);
    while (1)
    {
        Playermove(board,ROW,COL);
        Displayboard(board, ROW, COL);
        ret=Iswin(board, ROW, COL);
        if (ret !='C')
        {
            break;
        }
        Computermove(board, ROW, COL);
        Displayboard(board, ROW, COL);
        ret=Iswin(board, ROW, COL);
        if (ret !='C')
        {
            break;
        }
    }
    if (ret == '*')
    {
        printf("Player wins\\
");
    }
    else if (ret == '#')
    {
        printf("Computer Victory\\
");
    }
    else
    {
        printf("tie");
    }
}
void menu()
{
    printf("**********************\\
");
    printf("**** 1.play 0.exit**\\
");
    printf("**********************\\
");
}
void test()
{
    int input = 0;
    do
    {
        menu();
        printf("Please choose:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("Enter the game\\
");
            game();
            break;
        case 0:
            printf("Exit the game\\
");
            break;
        default:
            printf("Re-enter\\
");
            break;
        }
    }
    while (input);

}
int main()
{
    srand((unsigned int)time(NULL));
    test();
    return 0;
}

The code looks complicated, but it is actually not difficult to understand. Just pay attention to the relative relationship between the arrays. This is just a basic game of backgammon. Of course, the above code can be optimized.

If there are any mistakes or bad writing, please correct me.