【C语言】单人棋代码

开门见山:

#include<stdio.h>
#include<windows.h>
int main()
{
    char a[9][9];
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            if (i < 3 || i>5)
            {
                if (j < 3 || j>5)
                    a[i][j] = ' ';
                else
                    a[i][j] = 'O';
            }
            else
                a[i][j] = 'O';
        }
    }
    for (int i = 3; i < 6; i++)
    {
        for (int j = 3; j < 6; j++)
        {
            a[i][j] = '.';
        }
    }
    printf("\n\n\n\n\n\n\n\n    规则是:X是你的坐标.选择一颗棋,AWSD控制它的走向,让他跳过其他棋,被跳过的棋会消失,最后场上只剩一颗棋就算赢\n                    十秒后开始游戏");
    Sleep(10000);
    system("cls");
    int t = 4, x = 4;
    char b, c, e, f = '.';
    int d = 0, j = 0;
    a[t][x] = 'X';
    while (1)
    {
        printf("\n\n");
        for (int i = 0; i < 9; i++)
        {
            printf("\n\n            ");
            for (int j = 0; j < 9; j++)
                printf("%c      ", a[i][j]);
        }
        printf("\n\n\n\n");
        if (j % 2 == 0)
        {
            printf("                        选择你要走的棋,摁下回车键继续.....(摁 x 取消)");
            scanf_s(" %c", &b);
            switch (b)
            {
            case 'a':if (a[t][x - 1] != ' ' && x > 0) { x--; e = a[t][x]; a[t][x + 1] = f; f = e; a[t][x] = 'X'; }
                    else { printf("\n                此操作违规"); d = 1; } break;
            case 's':if (a[t + 1][x] != ' ') { t++; e = a[t][x];  a[t - 1][x] = f;  f = e; a[t][x] = 'X'; }
                    else { printf("\n                此操作违规"); d = 1; } break;
            case 'w':if (a[t - 1][x] != ' ') { t--; e = a[t][x]; a[t + 1][x] = f; f = e; a[t][x] = 'X'; }
                    else { printf("\n                此操作违规"); d = 1; } break;
            case 'd':if (a[t][x + 1] != ' ' && x < 9) { x++; e = a[t][x]; a[t][x - 1] = f; f = e; a[t][x] = 'X'; }
                    else { printf("\n                此操作违规"); d = 1; } break;
            default:x = x, t = t; break;
            }
            system("cls");
            printf("\n\n");
            for (int i = 0; i < 9; i++)
            {
                printf("\n\n            ");
                for (int j = 0; j < 9; j++)
                    printf("%c      ", a[i][j]);
            }
        }
        if (j % 2 == 1)
        {
            if (d == 0)
            {
                printf("\n                        现在想让它跳向哪里:(摁 x 取消)");
                scanf_s(" %c", &c);
                switch (c)
                {
                case 'a':if (a[t][x - 2] == '.' && a[t][x - 1] == 'O')
                {
                    if (e == 'O')
                    {
                        if (x >= 2)
                        {
                            a[t][x - 1] = '.'; a[t][x] = '.'; x -= 2; a[t][x] = 'O'; e = a[t][x]; f = a[t][x]; a[t][x] = 'X';
                        }
                        else printf("\n                此操作违规"); break;
                    }
                    else printf("\n                此操作违规"); break;
                }
                        else printf("\n                此操作违规"); break;
                case 's':if (a[t + 2][x] == '.' && a[t + 1][x] == 'O')
                {
                    if (e == 'O')
                    {
                        if (t <= 6)
                        {
                            a[t + 1][x] = '.'; a[t][x] = '.'; t += 2; a[t][x] = 'O'; e = a[t][x]; f = a[t][x]; a[t][x] = 'X';
                        }
                        else printf("\n                此操作违规"); break;
                    }
                    else printf("\n                此操作违规"); break;
                }
                        else printf("\n                此操作违规"); break;
                case 'w':if (a[t - 2][x] == '.' && a[t - 1][x] == 'O')
                {
                    if (e == 'O')
                    {
                        if (t >= 2)
                        {
                            a[t - 1][x] = '.'; a[t][x] = '.'; t -= 2; a[t][x] = 'O'; e = a[t][x]; f = a[t][x]; a[t][x] = 'X';
                        }
                        else printf("\n                此操作违规"); break;
                    }
                    else printf("\n                此操作违规"); break;
                }
                        else printf("\n                此操作违规"); break;
                case 'd':if (a[t][x + 2] == '.' && a[t][x + 1] == 'O')
                {
                    if (e == 'O')
                    {
                        if (x <= 6)
                        {
                            a[t][x + 1] = '.'; a[t][x] = '.'; x += 2; a[t][x] = 'O'; e = a[t][x]; f = a[t][x]; a[t][x] = 'X';
                        }
                        else printf("\n                此操作违规"); break;
                    }
                    else printf("\n                此操作违规"); break;
                }
                        else printf("\n                此操作违规"); break;
                case 'x':  break;
                }
            }
        }
        else
        {
            d = 0;
        }
        j++;
        Sleep(500);
        system("cls");
    }
    return 0;
}

好了,希望这段代码能对你有帮助~(都是我自己写的)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值