Three-game analysis and implementation --C language

Three-game as a classic game for the beginner C language proficiency if we can understand and can implement its gaming capabilities, the future for a more systematic study of C language has a good help. Down I share my insights and codes when programming

First, tell us about the three-game rule: as long as your chess even as a line (row, column, diagonal), that is to win
as shown:

Three-game analysis and implementation --C language

The first and FIGS play roughly and ideas:

1. We can checkerboard seen as a two-dimensional array of three rows and three columns, each column and each row separated by lines to distinguish
2 into computer chess and chess players (computer players at first and the first under)
3. determine winning or losing (the player wins, a draw and win computer)

The above analysis is the general idea, let's start writing code:

Creating tset.c, game.c and head.h three documents

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

Here I am into computers to the next and the first player, 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));//产生随机数
    do
    {
        menu();  
        printf("请选择:");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf("输入错误请重新输入.\n");
            break;
        }
    } while (choice);
    return 0;
}

2. game.c down gradually to achieve the desired function and call function in game

Print out the board, and the board is initialized to spaces. code show as below;

void show_board(char board[ROWS][COLS], int rows, int cols)//打印棋盘
{
    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)//将数组初始化为空格
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = ' ';
        }
    }
}

Results shown in Figure
Three-game analysis and implementation --C language

3. Write code players Lazi

The number of players enter the coordinates of two-dimensional array, then need to determine whether the player is reasonable coordinate input on this coordinate, and whether there are pieces, as follows;

void player_move(char board[ROWS][COLS], int rows, int cols)//玩家落子
{
    int x, y;
    printf("玩家落子:\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("位置已被占用!请再试一次。\n");
        }
        else printf("输入错误!请重新输入.\n");
    }
}

4. play into the computer zi

Here is a computer-generated random Lazi achieved in a space array. code show as below;

void computer_move(char board[ROWS][COLS], int rows, int cols)// 电脑落子
{

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

5. Finally, it only determines winners and losers

First discuss the winning player or computer; code is as follows;

char check_win(char board[ROWS][COLS], int rows, int cols)//判断输赢
{
    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 considered a event of a tie, a draw was the next full board are not winning or losing, you can traverse the entire array (the board) if there is no space on a draw. code show as below;

static int is_full(char board[ROWS][COLS], int rows, int cols)//检测是否为平局
{
    int i, j;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            if (board[i][j] == ' ')
                return 0;
        }
    }
    return 1;
}

Preliminary test results are as follows

Three-game analysis and implementation --C language

In the figure we can see that the computer can have a chance to win it at random Now, this game is no challenge. Should therefore be modified under erupted computer code, make it intelligent. We determined if the computer zi pieces in the same peer, or column with a slash, the computer at which the spaces preferentially Zi unpaired one line, so that simple intelligent

code show as below;

void computer_move(char board[ROWS][COLS], int rows, int cols)//智能化电脑落子
{
    int x, y, i;
    printf("电脑落子:\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:;
}

FIG effect;
Three-game analysis and implementation --C language

Detect all results

Computer wins
Three-game analysis and implementation --C language
player wins
Three-game analysis and implementation --C language
a draw
Three-game analysis and implementation --C language

All codes

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("请选择");
    scanf("%d", &choice);
    switch (choice)
    {
    case 1: //电脑先落子
        do
        {
            computer_move(board, ROWS, COLS);   //电脑落子
            show_board(board, ROWS, COLS);      //打印棋盘
            win = check_win(board, ROWS, COLS); 
            if (win != 0)  //没赢
                break;
            player_move(board, ROWS, COLS);     //玩家落子
            show_board(board, ROWS, COLS);      // 打印棋盘
            win = check_win(board, ROWS, COLS);
        } while (win == 0);
        if (win == '#')
            printf("很遗憾,你输了!\n");
        if (win == '*')
            printf("恭喜,你赢了!\n");
        if (win == 'q')
            printf("平局\n");
        break;
    case 2: //玩家先落子
        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("很遗憾,你输了!\n");
        if (win == '*')
            printf("恭喜,你赢了!\n");
        if (win == 'q')
            printf("平局\n");
        break;
    default:
        printf("输入错误,请重新输入\n");
        goto flag;
    }
}
int main()
{
    int choice;
    srand((unsigned int)time(NULL));//产生随机数
    do
    {
        menu();  
        printf("请选择:");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf("输入错误请重新输入.\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)//打印棋盘
{
    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)//将数组初始化为空格
{
    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)// 电脑落子
//{
//
//  int x, y;
//  printf("电脑落子:\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)//智能化电脑落子
{
    int x, y, i;
    printf("电脑落子:\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)//玩家落子
{
    int x, y;
    printf("玩家落子:\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("位置已被占用!请再试一次。\n");
        }
        else printf("输入错误!请重新输入.\n");
    }
}

static int is_full(char board[ROWS][COLS], int rows, int cols)//检测是否为平局
{
    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)//判断输赢
{
    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);//打印棋盘
void init_board(char board[ROWS][COLS], int rows, int cols);//初始化棋盘为空格
void computer_move(char board[ROWS][COLS], int rows, int cols);//电脑落子
void player_move(char board[ROWS][COLS], int rows, int cols);//玩家落子
char check_win(char board[ROWS][COLS], int rows, int cols);//判断输赢

Guess you like

Origin blog.51cto.com/14441430/2424922