码迷,mamicode.com
首页 > 编程语言 > 详细

用C语言实现三子棋

时间:2020-08-13 22:17:58      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:棋盘   ext   i++   sig   efault   源文件   参考   输入   需要   

头文件 文件名 game.h

#define ROW 3 //定义行
#define COL 3 //定义列
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>

void chushihua(char board[ROW][COL], int row, int col);
//声明函数chushihua
void xianshiqipan(char board[ROW][COL], int row, int col);
void playwanjia(char board[ROW][COL], int row, int col);
void computerxia(char board[ROW][COL], int row, int col);

//iswin可以告诉我们四种状态 
//玩家赢了 假设返回为*
//电脑赢   假设返回为#
//平局     假设返回为q
//未产生游戏继续  假设返回为c
char iswin(char board[ROW][COL], int row, int col);

源文件 game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void chushihua(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 xianshiqipan(char board[ROW][COL], int row, int col)
{
    int i = 0;
    for ( i = 0; i < row; i++)   //打印行数
    {
        int j = 0; //j是列数
        //这里是打印第i行数
        for ( j = 0; j < col; j++)
        {
            printf(" %c ", board[i][j]);
            if (j < col - 1)   
                //这里判断的是需不需要打印最后一列的|
                printf("|");
        }
        printf("\n");
        //这里是打印分割符
        if (i < row - 1)   
            //row-1的意思是行数-1,这里判断的是需不需要打印最后一行的---|---|---
        {
            for (j = 0; j < col; j++)
            {
                printf("---");
                if (j < row - 1)
                    printf("|");
            }
        }
        printf("\n");
    }
}

void playwanjia(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    printf("玩家走\n");
    while (1)
    {
        printf("请输入要下的坐标:>\n");
        scanf("%d%d", &x, &y);
        if (x >= 1 && x <= row&&y >= 1 && y <= col)
        {
            if (board[x - 1][y - 1] == ‘ ‘)
                //数组下标从0开始,而数轴是从1开始的
            {
                board[x - 1][y - 1] = ‘*‘;
                break;
            }
            else
            {
                printf("该坐标被占用\n");         
            }
        }
        else
        {
            printf("坐标非法,重新输入!\n");     
        }
    }
}

void computerxia(char board[ROW][COL], int row, int col)
{
    int x = 0;
    int y = 0;
    printf("电脑走:>\n");
    while (1)
    {
        x = rand() % row; 
        y = rand() % col;
        //随机生成的任何一个值%3,等到的都是0-2之间的数
        //所以x和y不需要-1
        if (board[x][y] == ‘ ‘)
        {
            board[x][y] = ‘#‘;
            break;
        }
        //假设电脑下棋采用最简单随机下的方式
    }

}

//返回1表示棋盘满了
//返回0表示棋盘没满
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;
    for ( i = 0; i < row; 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 < col; i++)
    {
        if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ‘ ‘)
        {
            return board[i][0];
        }
    }
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ‘ ‘)
    {
        return board[1][1];
    }
    if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ‘ ‘)
    {
        return board[1][1];
    }
    if (1 == isfull(board, ROW, COL))
    {
        return ‘q‘;
    }
    return ‘c‘;
}

源文件 text.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void game()
{
    char ret = 0;
    char board[ROW][COL] = { 0 }; 
    //ROW行 COL列,参考game.h
    chushihua(board, ROW, COL);
    //初始化把九宫格内变成空格
    xianshiqipan(board,ROW,COL);
    //开始下期
    while (1)
    {
        //玩家下棋
        playwanjia(board,ROW,COL);  //玩家走
        system("cls");//清理一下
        xianshiqipan(board, ROW, COL);  //打印一下
        ret = iswin(board, ROW, COL);
        if (ret != ‘c‘)
        {
            break;
        }
        //c是游戏继续
        //电脑下棋
        computerxia(board, ROW, COL);
        system("cls");
        xianshiqipan(board, ROW, COL);
        ret = iswin(board, ROW, COL);
        if (ret != ‘c‘)
        {
            break;
        }
    }
    if (ret==‘*‘)
    {
        printf("玩家赢\n");
    }
    else if (ret == ‘#‘)
    {
        printf("电脑赢\n");
    }
    else
    {
        printf("平局\n");
    }

}

void meue()
{
    printf("==========================\n");
    printf("======1.开始 0.结束=======\n");
    printf("==========================\n");
}

void Text()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do
    {
        meue();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("游戏结束\n");
            break;
        default:
            printf("请重新输入\n");
            break;
        }

    } while (input);
}

int main()
{
    Text();
    return 0;
}

用C语言实现三子棋

标签:棋盘   ext   i++   sig   efault   源文件   参考   输入   需要   

原文地址:https://blog.51cto.com/14893161/2519879

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!