Realization of Three-Mixed Chess with C Language

To realize the man-machine game of sanbang, the file composition is in the form of header file and source file, as follows:
The declaration of the function put into the game.h file, the macro definition used, and the inclusion of the header file.
game.c In the file is the implementation of each part of the function. The
test.c file is the test function, and the main logic implemented by the whole program is placed.
The code of each part is as follows:

game.h

#ifndef     __GANE_H__
#define     __GANE_H__          // 头文件组合形式

#define    ROWS  3              //行数
#define    COLS  3              //列数

#include <stdio.h>              //所用的头文件
#include <stdlib.h> 
#include <time.h>
#include<string.h>


void print_board (char board[ROWS][COLS],int rows,int cols );//打印表格函数

void init_board (char board[ROWS][COLS],int rows,int cols ); //初始化函数

void player_move (char board[ROWS][COLS],int rows,int cols); //玩家移动函数

void computer_move (char board[ROWS][COLS],int rows,int cols); //电脑移动函数

char f_win(char board[ROWS][COLS],int rows,int cols);           //检验函数


#endif      //__GANE_H__

game.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "stdio.h"
#include "game.h"
#include<string.h>

void init_board(char board[ROWS][COLS],int rows,int cols )
{
    memset(board,' ',rows*cols*(sizeof(char)));
}

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

    }
}

void player_move (char board[ROWS][COLS],int rows,int cols)
{
    int x = 0;
    int y = 0;
    printf ("请输入两个下标>:");
    scanf ("%d%d",&x,&y);
    x--;
    y--;
    if ((x>=0)&&(x<=rows-1)&&(y>=0)&&(y<=cols-1))
    {
        if (board[x][y]==' ')
        {
            board[x][y]='X';

        }
        else 
        {
            printf ("下标错误,请重新输入!");
        }
    }
    else 
    {
        printf ("输入有误!");
    }

}

void computer_move (char board[ROWS][COLS],int rows,int cols)
{
    while (1)
    {
        int x=rand()%3;
        int y=rand()%3;
        if (board[x][y]==' ')
        {
            board[x][y]='0';
            break ;
        }

    }

}

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

char f_win(char board[ROWS][COLS],int rows,int cols)
{
    int i =0;
    for (i=0;i<rows;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<cols;i++)
    {
        if ((board[0][i]==board[1][i])&&(board[1][i]==board[2][i])&&(board[0][i])!=' ')
        return board[0][i];
    }


if ((board[0][0]==board[1][1])&&(board[1][1]==board[2][2])&&(board[0][0])!=' ')
return board [0][0];
if ((board[0][2]==board[1][1])&&(board[1][1]==board[2][0])&&(board[1][1])!=' ')
return board [0][2];
if (f_full(board,rows,cols))
{
    return 'p';
}
return ' ';
}

test.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"


void game ()
{
    char board[ROWS][COLS]={
   
   0};
    char ret = 0;
    init_board(board,ROWS,COLS);
    print_board(board,ROWS,COLS);
    srand((unsigned int)time(NULL));
    while (1)
    {
        printf ("玩家走>\n");
        player_move(board,ROWS,COLS);
        print_board(board,ROWS,COLS);
        if ((ret=f_win(board,ROWS,COLS))!=' ')
            break;
        printf ("电脑走>\n");
        computer_move(board,ROWS,COLS);
        print_board(board,ROWS,COLS);
        if ((ret=f_win(board,ROWS,COLS))!=' ')
            break;      
    }


        ret=f_win(board,ROWS,COLS);
        if (ret=='X')
        {
            printf ("恭喜你,赢了!\n");
        }
        else if (ret=='0')
        {
            printf ("不好意思,你输了!\n");
        }
        else if (ret=='p')
        {
            printf ("平局!\n");
        }
        print_board(board,ROWS,COLS);

}
void menu ()
{
    printf ("****************************\n");
    printf ("***** 1.play  0.exit  *****\n");
    printf ("****************************\n");
}

int main()
{

    int input = 0;
    menu();
    do
    {

        printf ("请选择>:");
        scanf ("%d",&input);
        switch (input)
        {
        case 1:
           {
                game();
                break ;
            }
        case 0:
            {
                break;
            }
        default:
            {
                printf("请重新输入>:");
                break ;
            }
        }
    }while (input);
    return 0;
}

The results are as follows:
write picture description here

write picture description here

write picture description here

Hope to help you, continue tomorrow, come on! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324519311&siteId=291194637