C语言课程设计作业源代码(自存)

#include <algorithm>
#include <iostream>
#include <math.h>
#include <windows.h>
#include <stdio.h>
using namespace std;
char mp[15][15];
const int Len = 15;
const int win_count = 5;
bool Judge(const char map[15][15], char a)//判断游戏是否结束
{
    // printf("##     %c\n", a);
    for (int i = 0; i < Len; i++)
    {
        for (int j = 0; j < Len; j++)
        {
            if (map[i][j] == a)
            {
                int count_a = 1;
                //1. ----------------------------------------------------------------
                //竖直方向
                for (int x = i - 1, times = 0; x >= 0; x--, times++)
                {
                    if (map[x][j] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                for (int y = i + 1, times = 0; y < Len; y++, times++)
                {
                    if (map[y][j] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                if (count_a >= win_count)
                {
                    return true;
                }
                //2. --------------------------------------------------------------------
                //横方向
                count_a = 1;
                for (int x = j - 1, times = 0; x >= 0; x--, times++)
                {
                    if (map[i][x] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                for (int y = j + 1, times = 0; y < Len; y++, times++)
                {
                    if (map[i][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                if (count_a >= win_count)
                {
                    return true;
                }
                //3. -------------------------------------------------------------------
                //上斜下方向
                count_a = 1;
                for (int x = i - 1, y = j - 1, times = 0; i >= 0 && j >= 0; x--, y--, times++)
                {
                    if (map[x][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                for (int x = i + 1, y = j + 1, times = 0; i < Len && j < Len; x++, y++, times++)
                {
                    if (map[x][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                if (count_a >= win_count)
                {
                    return true;
                }
                //4. ------------------------------------------------------------------
                //下斜上方向
                count_a = 1;
                for (int x = i + 1, y = j - 1, times = 0; x < Len && y >= 0; x++, y--, times++)
                {
                    if (map[x][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                for (int x = i - 1, y = j + 1, times = 0; x >= 0 && y < Len; x--, y++, times++)
                {
                    if (map[x][y] == a)
                    {
                        count_a++;
                    }
                    else
                        break;
                    if (times > win_count - 1)
                        break;
                }
                if (count_a >= win_count)
                {
                    return true;
                }
                //-------------------------------------------------------------------
            }
        }
    }
    return false;
}
void f()//打印游戏规则
{
     printf("   --------------------------------------------------------\n");
    printf("   |           _________________________________           |\n");
    printf("   |           |     The Game will begin!!!    |           |\n");
    printf("   |           |_______________________________|           |\n");
    printf("   |                      *游戏规则*                       |\n");
    printf("   |      请两边选手轮流输入放棋子的坐标(以空格隔开)       |\n");
    printf("   |                        黑(@)棋:@                      |\n");
    printf("   |                        白(O)棋:O                      |\n");
    printf("   |                         黑棋先放                      |\n");
    printf("   ---------------------------------------------------------\n");
}
int main()
{
    f();
    for (int i = 10; i >= 1;i--)
        Sleep(1000), printf("                       还有%2d秒开始游戏\n", i);
        system("cls");
        for (int i = 0; i <= 15; i++)
            for (int j = 0; j <= 15; j++)
                mp[i][j] = ' ';
    //打印结果(调试)
    printf("    ");
    for (int i = 1; i <= 15; i++)
        printf("%-4d", i);
    printf("\n");
    printf("    ____________________________________________________________\n");
    for (int i = 0; i < 15; i++)
    {
        if (i >= 0)
            printf("%-3d", i + 1);
        else
            printf("   ");
        printf("|");

        for (int j = 0; j < 15; j++)
            printf(" %-2c|", mp[i][j]);
        printf("\n    ____________________________________________________________\n");
    }
    printf("\n\n");

    int heng, zong, flag = 0;
    printf("   请输入黑(@)棋坐标:\n");
    mp[15][15] = ' ';
    while (scanf("%d%d", &zong, &heng))
    {
        //输入
        // printf("%"
        // system("cls");)
        while (heng < 1 || zong < 1 || heng > Len || zong > Len || mp[zong-1][heng-1] != ' ')
        {
//            printf("h=%d z=%d l=%d %c\n", heng, zong, Len, mp[zong][heng]);
            printf("Input error,input again!!!\n");
            if (flag % 2 == 0)
                printf("请输入黑(@)棋坐标:\n");
            else
                printf("请输入白(O)棋坐标:\n");
            scanf("%d%d", &zong, &heng);
        }
        cout << "Input success!!!\n";
        system("cls");
        if (flag % 2 == 0)
            mp[zong - 1][heng - 1] = '@';
        else
            mp[zong - 1][heng - 1] = 'O';
        //打印新的地图
        printf("    ");
        for (int i = 1; i <= 15; i++)
            printf("%-4d", i);
        printf("\n");
        printf("    ____________________________________________________________\n");
        for (int i = 0; i < 15; i++)
        {
            if (i >= 0)
                printf("%-3d", i + 1);
            else
                printf("   ");
            printf("|");

            for (int j = 0; j < 15; j++)
                printf(" %-2c|", mp[i][j]);

            printf("\n    ____________________________________________________________\n");
        }
        printf("\n\n");
        //进行判断看 那一方获胜
        if (flag % 2 == 0)
        {
            if (Judge(mp, '@'))
            {
                printf("                 黑棋获胜 !!!         \n");
                break;
            }
        }
        else
        {
            if (Judge(mp, 'O'))
            {
                printf("                 白棋获胜 !!!         \n");
                break;
            }
        }
        flag++;
        if (flag % 2 == 0)
            printf("请输入黑(@)棋坐标:\n");
        else
            printf("请输入白(O)棋坐标:\n");
    }
    printf("                        游戏结束!!!\n");
    char p[100];
    gets(p);
    return 0;
}
五子棋小游戏
界面设计
功能实现
游戏说明
棋盘设计
游戏中界面设计
结束界面设计
胜负判断
游戏中的反馈
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值