15数码游戏简单实现

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

int index1(int *num, int a);
void printMatrix(int *num);
void swap(int *a, int *b);

int main(int argc, char const *argv[])
{
    int num[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    unsigned int seed = time(NULL); // 随机种子
    srand(seed);
    int i, j, temp;
    int T = 500;

    // 通过随机交换数组两位置的元素值来打乱数组元素
    while (T--)
    {
        i = rand() % 16;
        j = rand() % 16;
        swap(num + i, num + j);
    }
    // 打印数组
    printMatrix(num);

    while (1)
    {
        char str[256]; // 获取输入数据
        fgets(str, 256, stdin);
        fflush(stdin);
        int temp = atoi(str);
        int tem;

        if (temp >= 1 && temp <= 15 && ((abs(index1(num, temp) - index1(num, 0)) == 1) || (abs(index1(num, temp) - index1(num, 0)) == 4)))
        { // 判断输入是否合法
            if (index1(num, 0) == 3)
            {
                if (index1(num, temp) == 7)
                    swap(num + 3, num + 7);
                else if (index1(num, temp) == 2)
                    swap(num + 3, num + 2);
                else
                    printf("wrong input!\n\n");
            }
            else if (index1(num, 0) == 7 || index1(num, 0) == 11)
            {
                if (index1(num, temp) + 1 == index1(num, 0) || abs(index1(num, temp) - index1(num, 0)) == 4)
                    swap(num + index1(num, temp), num + index1(num, 0));
                else
                    printf("wrong input!\n\n");
            }
            else if (index1(num, 0) == 4 || index1(num, 0) == 8)
            {
                if (index1(num, temp) - 1 == index1(num, 0) || abs(index1(num, temp) - index1(num, 0)) == 4)
                    swap(num + index1(num, temp), num + index1(num, 0));
                else
                    printf("wrong input!\n\n");
            }
            else if (index1(num, 0) == 12)
            {
                if (index1(num, temp) == 8)
                    swap(num + 8, num + 12);
                else if (index1(num, temp) == 13)
                    swap(num + 12, num + 13);
                else
                    printf("wrong input!\n\n");
            }
            else
                swap(num + index1(num, temp), num + index1(num, 0));
        }
        else
            printf("wrong input!\n\n");

        printMatrix(num);
    }

    return 0;
}

// 由元素值获取索引
int index1(int *num, int a)
{
    int l = 16;
    int flag = 0, t;
    for (int i = 0; i < l; i++)
        if (a == *(num + i))
        {
            flag = 1;
            t = i;
        }

    if (!flag)
        return 0;
    else
        return t;
}

// 打印数组
void printMatrix(int *num)
{
    for (int i = 0; i < 16; i++)
    {
        if (*(num + i) == 0)
            printf(" ⚫\t");
        else
            printf("%3d\t", *(num + i));
        if ((i + 1) % 4 == 0)
            printf("\n\n\n");
    }
    printf("\n");
    printf("input the next step: ");
}

// 交换数组两位置的元素值
void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

猜你喜欢

转载自blog.csdn.net/wayne17/article/details/86519160