Analysis and Implementation of Three-chess-C Language

Keywords: C++ Programming Windows C

As a classic game, Sanziqi is very helpful for us to learn C language more systematically in the future if we can understand and realize its game function skillfully. Let me share my experience and code in programming.

First of all, introduce the rules of tri-chess: as long as you connect your own chess into a line (row, column, diagonal), that is to win.
As shown in the figure:

Firstly, there is a general idea according to the figure and playing method.

1. We can think of a chessboard as a two-dimensional array of three rows and three columns, with lines separating each row from each column to distinguish them.
2. It is divided into computer chess and player chess (computer plays first and player plays first).
3. Judgment of wins and losses (player wins, computer wins and peace)

The general idea is that the above analysis, the following start to write code:

Create three files: tset.c, game.c and head.h

1. Write the main function in test.c and write out the game menu.

Here I am divided into computer first down and player first down, the code is as follows;

void menu()
{
    printf("***************************************\n");
    printf("*      1.play             0.exit      *\n");
    printf("***************************************\n");
}
void first_move()
{
    printf("***************************************\n");
    printf("*  1.computer first   2.player first  *\n");
    printf("***************************************\n");
}
void game()
{
   return 0;
}
int main()
{
    int choice;
    srand((unsigned int)time(NULL));//Generating random numbers
    do
    {
        menu();  
        printf("Please choose:");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf("Input error please re-enter.\n");
            break;
        }
    } while (choice);
    return 0;
}

2. Step by step implement the required function in game.c and call it in game function

Print out the chessboard and initialize the chessboard to a space. The code is as follows.

void show_board(char board[ROWS][COLS], int rows, int cols)//Print chessboard
{
    int i;
    for (int i = 0; i < rows; i++)
    {
        printf("  %c | %c | %c  \n", board[i][0], board[i][1], board[i][2]);
        if (i != rows - 1)
            printf(" ---|---|--- \n");
    }
}

void init_board(char board[ROWS][COLS], int rows, int cols)//Initialize arrays to spaces
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = ' ';
        }
    }
}

The effect is shown in the figure.

3. Write the code of the player's son.

The number of players input the coordinates of the two-dimensional array. At this time, it is necessary to determine whether the input coordinates of players are reasonable and whether there are chess pieces in the coordinates. The code is as follows.

void player_move(char board[ROWS][COLS], int rows, int cols)//Player falls behind
{
    int x, y;
    printf("Player falls behind:\n");
    while (1)
    {
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= rows && y >= 1 && y <= cols)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*';
                break;
            }
            else printf("Location has been occupied! Please try again.\n");
        }
        else printf("Input error!Please re-enter.\n");
    }
}

4. Play computer games

What we have achieved here is that the computer drops randomly in an array of blanks. The code is as follows.

void computer_move(char board[ROWS][COLS], int rows, int cols)// Computer Fall Out
{

    int x, y;
    printf("Computer Fall Out:\n");
    while (1)
    {
        x = rand() % rows;
        y = rand() % cols;
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            break;
        }
    }
}

5. In the end, the only way to decide is to win or lose.

Discuss the wins of players or computers first; the code is as follows.

char check_win(char board[ROWS][COLS], int rows, int cols)//Judging Win or Lose
{
    int i;
    for (i = 0; i < rows; i++)
    {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
            return board[i][1];
    }
    for (i = 0; i < cols; i++)
    {
        if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
            return board[1][i];
    }
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
        return board[1][1];
    else if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
        return board[1][1];
    else if (is_full(board, rows, cols))
        return 'q';
    return 0;
}

This is to consider the situation of a draw. A draw is a draw when there are no wins or losses on the board, and the whole array (chessboard) can be traversed if there are no spaces. The code is as follows.

static int is_full(char board[ROWS][COLS], int rows, int cols)//Check for a draw
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            if (board[i][j] == ' ')
                return 0;
        }
    }
    return 1;
}

The preliminary test results are as follows.

In the picture, we can see that the computer has a chance to win, but at random, such a game is not challenging. So we should modify the code of the computer to make it intelligent. We can judge when computer drops out that if there are the same chess pieces in the same row, in the same row or on the oblique line, the computer will give priority to dropping out in the space where it does not form a line, so as to realize simplicity and intelligence.

The code is as follows.

void computer_move(char board[ROWS][COLS], int rows, int cols)//Intelligent computer system
{
    int x, y, i;
    printf("Computer Fall Out:\n");
    while (1)
    {
        x = rand() % rows;
        y = rand() % cols;
        for (i = 0; i < rows; i++)
        {
            if (board[i][0] == board[i][1] && board[i][0] == '#' && board[i][2] == ' ')
            {
                board[i][2] = '#';
                goto flag1;
            }
            else if (board[i][1] == board[i][2] && board[i][1] == '#' && board[i][0] == ' ')
            {
                board[i][0] = '#';
                goto flag1;
            }
            else if (board[i][0] == board[i][2] && board[i][2] == '#' && board[i][1] == ' ')
            {
                board[i][1] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[1][i] && board[0][i] == '#' && board[2][i] == ' ')
            {
                board[2][i] = '#';
                goto flag1;
            }
            else if (board[1][i] == board[2][i] && board[1][i] == '#' && board[0][i] == ' ')
            {
                board[0][i] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[2][i] && board[2][i] == '#' && board[1][i] == ' ')
            {
                board[1][i] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[1][1] && board[0][0] == '#' && board[2][2] == ' ')
            {
                board[2][2] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][2] && board[1][1] == '#' && board[0][0] == ' ')
            {
                board[0][0] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[2][2] && board[0][0] == '#' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
            else if (board[0][2] == board[1][1] && board[1][1] == '#' && board[2][0] == ' ')
            {
                board[2][0] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][0] && board[1][1] == '#' && board[0][2] == ' ')
            {
                board[0][2] = '#';
                goto flag1;
            }
            else if (board[2][0] == board[0][2] && board[2][0] == '#' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
        }
        for (i = 0; i < rows; i++)
        {
            if (board[i][0] == board[i][1] && board[i][0] == '*' && board[i][2] == ' ')
            {
                board[i][2] = '#';
                goto flag1;
            }
            else if (board[i][1] == board[i][2] && board[i][1] == '*' && board[i][0] == ' ')
            {
                board[i][0] = '#';
                goto flag1;
            }
            else if (board[i][0] == board[i][2] && board[i][2] == '*' && board[i][1] == ' ')
            {
                board[i][1] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[1][i] && board[0][i] == '*' && board[2][i] == ' ')
            {
                board[2][i] = '#';
                goto flag1;
            }
            else if (board[1][i] == board[2][i] && board[1][i] == '*' && board[0][i] == ' ')
            {
                board[0][i] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[2][i] && board[2][i] == '*' && board[1][i] == ' ')
            {
                board[1][i] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[1][1] && board[0][0] == '*' && board[2][2] == ' ')
            {
                board[2][2] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][2] && board[1][1] == '*' && board[0][0] == ' ')
            {
                board[0][0] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[2][2] && board[0][0] == '*' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
            else if (board[0][2] == board[1][1] && board[1][1] == '*' && board[2][0] == ' ')
            {
                board[2][0] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][0] && board[1][1] == '*' && board[0][2] == ' ')
            {
                board[0][2] = '#';
                goto flag1;
            }
            else if (board[2][0] == board[0][2] && board[2][0] == '*' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
        }
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            goto flag1;
        }
    }
flag1:;
}

The effect is shown in the figure.

Detection of all results

Computer wins

Players win

It ends in a draw

All code

tese.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"
void menu()
{
    printf("***************************************\n");
    printf("*      1.play             0.exit      *\n");
    printf("***************************************\n");
}
void first_move()
{
    printf("***************************************\n");
    printf("*  1.computer first   2.player first  *\n");
    printf("***************************************\n");
}
void game()
{
    int choice, win;
    char board[ROWS][COLS];
    init_board(board, ROWS, COLS);
    first_move();
flag:
    printf("Please choose");
    scanf("%d", &choice);
    switch (choice)
    {
    case 1: //Computer First
        do
        {
            computer_move(board, ROWS, COLS);   //Computer Fall Out
            show_board(board, ROWS, COLS);      //Print chessboard
            win = check_win(board, ROWS, COLS); 
            if (win != 0)  //No win
                break;
            player_move(board, ROWS, COLS);     //Player falls behind
            show_board(board, ROWS, COLS);      // Print chessboard
            win = check_win(board, ROWS, COLS);
        } while (win == 0);
        if (win == '#')
            printf("I'm sorry you lost.!\n");
        if (win == '*')
            printf("Congratulations, you won!\n");
        if (win == 'q')
            printf("It ends in a draw\n");
        break;
    case 2: //Player first
        show_board(board, ROWS, COLS);
        do
        {
            player_move(board, ROWS, COLS);
            show_board(board, ROWS, COLS);
            win = check_win(board, ROWS, COLS);
            if (win != 0)
                break;
            computer_move(board, ROWS, COLS);
            show_board(board, ROWS, COLS);
            win = check_win(board, ROWS, COLS);
        } while (win == 0);
        if (win == '#')
            printf("I'm sorry you lost.!\n");
        if (win == '*')
            printf("Congratulations, you won!\n");
        if (win == 'q')
            printf("It ends in a draw\n");
        break;
    default:
        printf("Input error, please re-enter\n");
        goto flag;
    }
}
int main()
{
    int choice;
    srand((unsigned int)time(NULL));//Generating random numbers
    do
    {
        menu();  
        printf("Please choose:");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf("Input error please re-enter.\n");
            break;
        }
    } while (choice);
    return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"

void show_board(char board[ROWS][COLS], int rows, int cols)//Print chessboard
{
    int i;
    for (int i = 0; i < rows; i++)
    {
        printf("  %c | %c | %c  \n", board[i][0], board[i][1], board[i][2]);
        if (i != rows - 1)
            printf(" ---|---|--- \n");
    }
}

void init_board(char board[ROWS][COLS], int rows, int cols)//Initialize arrays to spaces
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = ' ';
        }
    }
}

//void computer_move(char board[ROWS][COLS], int rows, int cols)// computer drop-off
//{
//
//  int x, y;
//  Pritf ("Computer Laozi: n");
//  while (1)
//  {
//      x = rand() % rows;
//      y = rand() % cols;
//      if (board[x][y] == ' ')
//      {
//          board[x][y] = '#';
//          break;
//      }
//  }
//}
void computer_move(char board[ROWS][COLS], int rows, int cols)//Intelligent computer system
{
    int x, y, i;
    printf("Computer Fall Out:\n");
    while (1)
    {
        x = rand() % rows;
        y = rand() % cols;
        for (i = 0; i < rows; i++)
        {
            if (board[i][0] == board[i][1] && board[i][0] == '#' && board[i][2] == ' ')
            {
                board[i][2] = '#';
                goto flag1;
            }
            else if (board[i][1] == board[i][2] && board[i][1] == '#' && board[i][0] == ' ')
            {
                board[i][0] = '#';
                goto flag1;
            }
            else if (board[i][0] == board[i][2] && board[i][2] == '#' && board[i][1] == ' ')
            {
                board[i][1] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[1][i] && board[0][i] == '#' && board[2][i] == ' ')
            {
                board[2][i] = '#';
                goto flag1;
            }
            else if (board[1][i] == board[2][i] && board[1][i] == '#' && board[0][i] == ' ')
            {
                board[0][i] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[2][i] && board[2][i] == '#' && board[1][i] == ' ')
            {
                board[1][i] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[1][1] && board[0][0] == '#' && board[2][2] == ' ')
            {
                board[2][2] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][2] && board[1][1] == '#' && board[0][0] == ' ')
            {
                board[0][0] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[2][2] && board[0][0] == '#' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
            else if (board[0][2] == board[1][1] && board[1][1] == '#' && board[2][0] == ' ')
            {
                board[2][0] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][0] && board[1][1] == '#' && board[0][2] == ' ')
            {
                board[0][2] = '#';
                goto flag1;
            }
            else if (board[2][0] == board[0][2] && board[2][0] == '#' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
        }
        for (i = 0; i < rows; i++)
        {
            if (board[i][0] == board[i][1] && board[i][0] == '*' && board[i][2] == ' ')
            {
                board[i][2] = '#';
                goto flag1;
            }
            else if (board[i][1] == board[i][2] && board[i][1] == '*' && board[i][0] == ' ')
            {
                board[i][0] = '#';
                goto flag1;
            }
            else if (board[i][0] == board[i][2] && board[i][2] == '*' && board[i][1] == ' ')
            {
                board[i][1] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[1][i] && board[0][i] == '*' && board[2][i] == ' ')
            {
                board[2][i] = '#';
                goto flag1;
            }
            else if (board[1][i] == board[2][i] && board[1][i] == '*' && board[0][i] == ' ')
            {
                board[0][i] = '#';
                goto flag1;
            }
            else if (board[0][i] == board[2][i] && board[2][i] == '*' && board[1][i] == ' ')
            {
                board[1][i] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[1][1] && board[0][0] == '*' && board[2][2] == ' ')
            {
                board[2][2] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][2] && board[1][1] == '*' && board[0][0] == ' ')
            {
                board[0][0] = '#';
                goto flag1;
            }
            else if (board[0][0] == board[2][2] && board[0][0] == '*' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
            else if (board[0][2] == board[1][1] && board[1][1] == '*' && board[2][0] == ' ')
            {
                board[2][0] = '#';
                goto flag1;
            }
            else if (board[1][1] == board[2][0] && board[1][1] == '*' && board[0][2] == ' ')
            {
                board[0][2] = '#';
                goto flag1;
            }
            else if (board[2][0] == board[0][2] && board[2][0] == '*' && board[1][1] == ' ')
            {
                board[1][1] = '#';
                goto flag1;
            }
        }
        if (board[x][y] == ' ')
        {
            board[x][y] = '#';
            goto flag1;
        }
    }
flag1:;
}

void player_move(char board[ROWS][COLS], int rows, int cols)//Player falls behind
{
    int x, y;
    printf("Player falls behind:\n");
    while (1)
    {
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= rows && y >= 1 && y <= cols)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = '*';
                break;
            }
            else printf("Location has been occupied! Please try again.\n");
        }
        else printf("Input error!Please re-enter.\n");
    }
}

static int is_full(char board[ROWS][COLS], int rows, int cols)//Check for a draw
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            if (board[i][j] == ' ')
                return 0;
        }
    }
    return 1;
}

char check_win(char board[ROWS][COLS], int rows, int cols)//Judging Win or Lose
{
    int i;
    for (i = 0; i < rows; i++)
    {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
            return board[i][1];
    }
    for (i = 0; i < cols; i++)
    {
        if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
            return board[1][i];
    }
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
        return board[1][1];
    else if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
        return board[1][1];
    else if (is_full(board, rows, cols))
        return 'q';
    return 0;
}

head.h

#include<stdio.h>
#include<time.h>
#include<Windows.h>

#define ROWS 3
#define COLS 3

void show_board(char board[ROWS][COLS], int rows, int cols);//Print chessboard
void init_board(char board[ROWS][COLS], int rows, int cols);//Initialize the chessboard as a space
void computer_move(char board[ROWS][COLS], int rows, int cols);//Computer Fall Out
void player_move(char board[ROWS][COLS], int rows, int cols);//Player falls behind
char check_win(char board[ROWS][COLS], int rows, int cols);//Judging Win or Lose

Posted by isedeasy on Tue, 30 Jul 2019 05:14:30 -0700