首页 > 其他 > 详细

三子棋

时间:2021-01-19 00:45:15      阅读:6      评论:0      收藏:0      [点我收藏+]
头文件

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#define ROW 3
#define COL 3
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
//#include <>
//函数的声明
InitBoard(char board[ROW][COL], int row, int col);
DisplayBoard(char board[ROW][COL], int row, int col);
PlayerMove(char board[ROW][COL], int row, int col);
ComputerMove(char board[ROW][COL], int row, int col);

//四种结果 玩家赢 ‘*’
//电脑赢 ‘#‘
//平局   ‘Q‘
//继续   ‘C‘ 
char Iswin(char board[ROW][COL], int row, int col);

定义函数

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

InitBoard(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] = ‘ ‘;
        }
    }
}
//      |   |   
//   ---|---|---
//      |   |   
//   ---|---|---
//      |   | 
DisplayBoard(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < col; i++)
    {
        for (j = 0; j < row; j++)
        {
            printf(" %c ",board[i][j]);
            if (j == row - 1)
            {
                printf("\n");
                break;
            }
            printf("|");

        }
        for (j = 0; j < row; j++)
        {
            if (i == col - 1)
                break;
            printf("---");
            if (j == row - 1)
            {
                printf("\n");
                break;
            }
            printf("|");
        }

    }

}

PlayerMove(char board[ROW][COL], int row, int col)
{
    printf("轮到玩家\n");
    int i = 0;
    int j = 0;
    while (1)
    {
        printf("请输入要下的坐标  \n");
        scanf("%d%d", &i, &j);

        if (i <= row && i >= 1 && j <= col && j >= 1 )
        {
            if (board[i - 1][j - 1] == ‘ ‘)
            {
                board[i - 1][j - 1] = ‘*‘;
                break;
            }
            else
            {
                printf("该坐标已经被占用\n");
            }
        }
        else
            printf("非法坐标,请重新输入\n");
    }

}

ComputerMove(char board[ROW][COL], int row, int col)
{
    printf("电脑下\n");
    int x = 0, y = 0;
    while (1)
    {
        x = rand() % row;
        y = rand() % col;
        if (board[x][y] == ‘ ‘)
        {
            board[x][y] = ‘#‘;
            break;
        }

    }
}
//返回1表示棋盘满了 0表示没满
int Isfull(char board[ROW][COL], int row, int col)
{
    int i = 0, 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;
    int j = 0;

    //横行
    for (i = 0; i < row; i++)
    {
        if (board[i][1] == board[i][2] && board[i][0] == board[i][1] && board[i][0] != ‘ ‘)
        {
            return board[i][1];
        }
    }
    //竖列
    for (j = 0; j < col; j++)
    {
        if (board[1][j] == board[2][j] && board[0][j] == board[1][j] && board[0][j] != ‘ ‘)
        {
            return board[1][j];
        }

    }

    //对角线
    if (board[1][1] == board[2][2] && board[0][0] == board[1][1] && board[1][1] != ‘ ‘)
        return board[1][1];

    if (board[1][1] == board[0][2] && board[2][0] == board[1][1] && board[1][1] != ‘ ‘)
        return board[1][1];
     //判断是否平局
    if (1 == Isfull(board, ROW, COL))
    {
        return "Q";
    }
    return ‘C‘;
}

主函数界面

#define _CRT_SECURE_NO_WARNINGS 1
//搭建框架,建立测试文件和头文件。
//每一个对应的功能都写一个对应功能的函数
#include "game.h"

void menu()
{
    printf("***************************************\n");
    printf("****    1.Play           0.Exit    ****\n");
    printf("***************************************\n");
}
//游戏的实现
void game()
{
    char ret = 0;
    //建立一个数组,作为棋盘
    char board[ROW][COL] = { 0 };

    //棋盘的初始化
    InitBoard(board, ROW, COL);
    //打印棋盘
    DisplayBoard(board, ROW, COL);
    //下棋
    while (1)
    {
        //玩家先下
        PlayerMove(board, ROW, COL);
        DisplayBoard(board, ROW, COL);
        //判断玩家是否赢
        //电脑下棋
        ret = Iswin(board,ROW,COL);
        if (ret != ‘C‘)
        {
            break;
        }
        ComputerMove(board, ROW, COL);
        DisplayBoard(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 test()
{
    srand((unsigned int)time(NULL));
    int input = 0;
    do
    {
        menu();
        printf("三子棋游戏,请选择是否开始:\n");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("游戏开始\n");
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("输入错误,请重新选择\n");
            break;
        }
    } while (input);

}

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

脑阔疼...

三子棋

原文:https://blog.51cto.com/15078858/2596688

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号