三子棋游戏

纯c语言三子棋游戏

电脑下棋部分斜线判断的算法有点问题,暂时没想到解决方案,想到会来改,也欢迎在下面评论指出错误

看这篇博客的人已经超过300了。。真的出乎我的意料,为了方便大家直接下载我的代码,你可以点击这里前往我的github下载整个打包文件

game.h

#ifndef __GAME_H__
#define __GAME_H__

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define ROW 9
#define COL 9

void InitBoard(char board[ROW][COL], int row, int col);
void PrintBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
int CheckWin(char board[ROW][COL], int row, int col);

#endif // !__GAME_H__

code.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

int row = 3;
int col = 3;

int main()
{
    int choice = 0;
    char board[ROW][COL] = { 0 };
    srand((unsigned)time(0));
    while (1)
    {
        printf("1.玩一把 0.退出\n");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            InitBoard(board, row, col);
            PrintBoard(board, row, col);
            while (1)
            {
                PlayerMove(board, row, col);
                PrintBoard(board, row, col);
                if (CheckWin(board, row, col) != 0)
                {
                    break;
                }
                ComputerMove(board, row, col);
                PrintBoard(board, row, col);
                if (CheckWin(board, row, col) != 0)
                {
                    break;
                }
            }
            break;
        case 0:
            exit(0);
            break;
        }
    }
    return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

int i = 0;
int j = 0;

void HorizontalSearch(char board[ROW][COL], char a, int col, int x);
void LengthwaysSearch(char board[ROW][COL], char a, int row, int y);
int XiexiaSearch(char board[ROW][COL], char a, int row, int col, int x, int y);

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

void PrintBoard(char board[ROW][COL], int row, int col)
{
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)//打印每一行的棋子
        {
            if (j == col - 1)
            {
                printf(" %c \n", board[i][j]);
                break;
            }
            printf(" %c |", board[i][j]);
        }
        if (i != row - 1)//打印棋盘中间的横线,如果已经倒数第一行,不打印
        {
            for (j = 0; j < col; j++)
            {
                printf("----");
            }
            printf("\n");
        }
    }
    printf("\n");
}

void PlayerMove(char board[ROW][COL], int row, int col)
{
    printf("输入想要下的位置:>");
    while (1)
    {
        scanf("%d%d", &i, &j);
        if ((i > row) || (j > col))
        {
            printf("坐标错误,重新输入:>");
        }
        else
            break;
    }
    board[i - 1][j - 1] = 'X';
}

void HorizontalSearch(char board[ROW][COL], char a, int col, int x)//横向查找空位
{
    int y = 0;
    for (y = 0; y < col; y++)
    {
        while (board[x][y] == 0)
        {
            board[x][y] = a;
            return;
        }
    }
}

void LengthwaysSearch(char board[ROW][COL], char a, int row, int y)//纵向查找空位
{
    int x = 0;
    for (x = 0; x < row; x++)
    {
        while (board[x][y] == 0)
        {
            board[x][y] = a;
            return;
        }
    }
}

int XiexiaSearch(char board[ROW][COL], char a, int row, int col, int x, int y)//斜下检测空位
{
    while (x >= 0 && y >= 0)
    {
        if (board[x][y] == 0)
        {
            board[x][y] = a;
            return 1;
        }
        x--;
        y--;
    }
    return 0;
}

int XieshangSearch(char board[ROW][COL], char a, int row, int col, int x, int y)//斜上检查空位
{
    while (x < row && y >= 0)
    {
        if (board[x][y] == 0)
        {
            board[x][y] = a;
            return 1;
        }
        x++;
        y--;
    }
    return 0;
}

void ComputerMove(char board[ROW][COL], int row, int col)
{

    for (i = 0; i < row; i++)//横向判断
    {
        int countX = 0;
        int count0 = 0;
        int count = 0;
        for (j = 0; j < col; j++)
        {
            if (board[i][j] == 'X')
            {
                countX++;
                count++;
            }
            if (board[i][j] == '0')
            {
                count0++;
                count++;
            }
        }
        if (count == 3)
        {
            continue;
        }
        if (countX == 2)
        {
            HorizontalSearch(board, '0', col, i);
            return;
        }
        if (count0 == 2)
        {
            HorizontalSearch(board, '0', col, i);
            return;
        }
    }
    for (j = 0; j < col; j++)//纵向判断
    {
        int countX = 0;
        int count0 = 0;
        int count = 0;
        for (i = 0; i < row; i++)
        {
            if (board[i][j] == 'X')
            {
                countX++;
                count++;
            }
            if (board[i][j] == '0')
            {
                count0++;
                count++;
            }
        }
        if (count == 3)
        {
            continue;
        }
        if (countX == 2)
        {
            LengthwaysSearch(board, '0', row, j);
            return;
        }
        if (count0 == 2)
        {
            LengthwaysSearch(board, '0', row, j);
            return;
        }
    }
    for (i = 0; i < row; i++)//判断斜线
    {
        int x = i;
        int y = 0;
        int countX = 0;
        int count0 = 0;
        int count = 0;
        while ((y < col) && (x < row))//斜向下部分
        {
            if (board[x][y] == 'X')
            {
                countX++;
                count++;
            }
            if (board[x][y] == '0')
            {
                count0++;
                count++;
            }
            x++;
            y++;
        }
        while (count != 3)
        {
            if (countX == 2 && XiexiaSearch(board, '0', row, col, --x, --y))
            {
                return;
            }
            if (count0 == 2 && XiexiaSearch(board, '0', row, col, --x, --y))
            {
                return;
            }
            break;
        }
        countX = 0;
        count0 = 0;
        x = i;
        y = 0;
        count = 0;
        while ((y < col) && (x >= 0))//斜向上
        {
            if (board[x][y] == 'X')
            {
                countX++;
                count++;
            }
            if (board[x][y] == '0')
            {
                count0++;
                count++;
            }
            x--;
            y++;
        }
        while (count != 3)
        {
            if (countX == 2 && XieshangSearch(board, '0', row, col, ++x, --y))
            {
                return;
            }
            if (count0 == 2 && XieshangSearch(board, '0', row, col, ++x, --y))
            {
                return;
            }
            break;
        }
    }
    do
    {
        i = rand() % 3;
        j = rand() % 3;
    } while (board[i][j] != 0);
    board[i][j] = '0';
}

int CheckWin(char board[ROW][COL], int row, int col)
{
    int count = 0;
    for (i = 0; i < row; i++)//横向判断
    {
        int countX = 0;
        int count0 = 0;
        for (j = 0; j < col; j++)
        {
            if (board[i][j] == 'X')
            {
                countX++;
            }
            if (board[i][j] == '0')
            {
                count0++;
            }
            if (countX == 3)
            {
                printf("玩家赢\n");
                return 1;
            }
            if (count0 == 3)
            {
                printf("电脑赢\n");
                return 2;
            }
        }
    }
    for (j = 0; j < col; j++)//纵向判断
    {
        int countX = 0;
        int count0 = 0;
        for (i = 0; i < row; i++)
        {
            if (board[i][j] == 'X')
            {
                countX++;
            }
            if (board[i][j] == '0')
            {
                count0++;
            }
            if (countX == 3)
            {
                printf("玩家赢\n");
                return 1;
            }
            if (count0 == 3)
            {
                printf("电脑赢\n");
                return 2;
            }
        }
    }
    for (i = 0; i < row; i++)//判断斜线
    {
        int x = i;
        int y = 0;
        int countX = 0;
        int count0 = 0;
        while ((y < col) && (x < row))//斜向下部分
        {
            if (board[x][y] == 'X')
            {
                countX++;
            }
            if (board[x][y] == '0')
            {
                count0++;
            }
            if (countX == 3)
            {
                printf("玩家赢\n");
                return 1;
            }
            if (count0 == 3)
            {
                printf("电脑赢\n");
                return 2;
            }
            x++;
            y++;
        }
        countX = 0;
        count0 = 0;
        while ((y < col) && (x >= 0))
        {
            if (board[x][y] == 'X')
            {
                countX++;
            }
            if (board[x][y] == '0')
            {
                count0++;
            }
            if (countX == 3)
            {
                printf("玩家赢\n");
                return 1;
            }
            if (count0 == 3)
            {
                printf("电脑赢\n");
                return 2;
            }
            x--;
            y++;
        }
    }
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (board[i][j] != 0)
            {
                count++;
            }
            if (count == 9)
            {
                printf("平局\n");
                return 3;
            }
        }
    }
    return 0;
}
    原文作者:无聊星期三
    原文地址: https://blog.csdn.net/Boring_Wednesday/article/details/78746868
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞