2048游戏(未完成)

来源:互联网 发布:手机桌游软件 编辑:程序博客网 时间:2024/06/07 23:12

//2048游戏
//Bug: 在无法向左移动时,仍会随机生成数字

#include <iostream>#include <stdlib.h>#include <conio.h>#include <windows.h>using namespace std;#define NUM 4static int TotalScore = 0;void moveLeft(int *a){    for (int i = 0; i < NUM; i++)    {        int index = 0;        for (int j = 0; j< NUM; j++)        {            if (*(a + i * NUM + j) != 0)            {                *(a + i * NUM + index) = *(a + i * NUM  + j);                index++;            }        }        for (int j = index; j < NUM; j++ )        {            *(a + i * NUM + j) = 0;        }    }    for(int i = 0; i < NUM; i++)    {        for(int j = 0; j < NUM-1; j++)        {            if(*(a+i*NUM+j) == *(a+i*NUM+j+1))            {                *(a+i*NUM+j) = *(a+i*NUM+j) << 1;                TotalScore+=*(a+i*NUM+j);                *(a+i*NUM+j+1) = 0;            }        }    }    for (int i = 0; i < NUM; i++)    {        int index = 0;        for (int j = 0; j< NUM; j++)        {            if (*(a + i * NUM + j) != 0)            {                *(a + i * NUM + index) = *(a + i * NUM  + j);                index++;            }        }        for (int j = index; j < NUM; j++ )        {            *(a + i * NUM + j) = 0;        }    }}void moveRight(int *a){    for (int i = 0; i < NUM; i++)    {        int index = NUM - 1;        for (int j = NUM - 1; j >= 0; j--)        {            if (*(a + i * NUM + j) != 0)            {                *(a + i * NUM + index) = *(a + i * NUM  + j);                index--;            }        }        for (int j = index; j >= 0; j-- )        {            *(a + i * NUM + j) = 0;        }    }    for(int i = 0; i < NUM; i++)    {        for(int j = NUM-1; j > 0; j--)        {            if(*(a+i*NUM+j) == *(a+i*NUM+j-1))            {                *(a+i*NUM+j) = *(a+i*NUM+j) <<1;                TotalScore+=*(a+i*NUM+j);                *(a+i*NUM+j-1) = 0;            }        }    }    for (int i = 0; i < NUM; i++)    {        int index = NUM - 1;        for (int j = NUM - 1; j >= 0; j--)        {            if (*(a + i * NUM + j) != 0)            {                *(a + i * NUM + index) = *(a + i * NUM  + j);                index--;            }        }        for (int j = index; j >= 0; j-- )        {            *(a + i * NUM + j) = 0;        }    }}void moveTop(int *a){    for (int i = 0; i < NUM; i++)    {        int index = 0;        for (int j = 0; j< NUM; j++)        {            if (*(a + j * NUM + i) != 0)            {                *(a + index * NUM + i) = *(a + j * NUM + i);                index++;            }        }        for (int j = index; j < NUM; j++ )        {            *(a + j * NUM + i) = 0;        }    }    for(int i = 0; i < NUM-1; i++)    {        for(int j = 0; j < NUM; j++)        {            if(*(a+i*NUM+j) == *(a+(i+1)*NUM+j))            {                *(a+i*NUM+j) = *(a+i*NUM+j) <<1;                TotalScore+=*(a+i*NUM+j);                *(a+(i+1)*NUM+j) = 0;            }        }    }    for (int i = 0; i < NUM; i++)    {        int index = 0;        for (int j = 0; j< NUM; j++)        {            if (*(a + j * NUM + i) != 0)            {                *(a + index * NUM + i) = *(a + j * NUM + i);                index++;            }        }        for (int j = index; j < NUM; j++ )        {            *(a + j * NUM + i) = 0;        }    }   }void moveDown(int *a){    for (int i = 0; i < NUM; i++)    {        int index = NUM - 1;        for (int j = NUM - 1; j >= 0; j--)        {            if (*(a + j * NUM + i) != 0)            {                *(a + index * NUM + i) = *(a + j * NUM  + i);                index--;            }        }        for (int j = index; j >= 0; j-- )        {            *(a + j * NUM + i) = 0;        }    }    for(int i = NUM-1; i > 0; i--)    {        for(int j = 0; j < NUM; j++)        {            if(*(a+i*NUM+j) == *(a+(i-1)*NUM+j))            {                *(a+i*NUM+j) = *(a+i*NUM+j) <<1;                TotalScore+=*(a+i*NUM+j);                *(a+(i-1)*NUM+j) = 0;            }        }    }    for (int i = 0; i < NUM; i++)    {        int index = NUM - 1;        for (int j = NUM - 1; j >= 0; j--)        {            if (*(a + j * NUM + i) != 0)            {                *(a + index * NUM + i) = *(a + j * NUM  + i);                index--;            }        }        for (int j = index; j >= 0; j-- )        {            *(a + j * NUM + i) = 0;        }    }}bool checkWin(int *a){    bool flag = true;    for(int i = 0; i < NUM; i++)        for(int j = 0; j < NUM; j++)            if(*(a+i*NUM+j) == 1024)                return true;    return false;}bool checkArray(int *a){    bool flag = true;    for(int i = 0; i < NUM; i++)        for(int j = 0; j < NUM; j++)            if(*(a+i*NUM+j) == 0)                return true;    return false;}int myRand(){    int x = rand()%100;    if(x < 70)        return 2;    else        return 4;}int GetDirection(){    int ret = 0;    do     {        int ch = getch();        if(isascii(ch))            continue;        ch = getch();        switch(ch)        {        case 72:                ret = 2; // top            break;        case 75:                ret = 1; // left             break;        case 77:                ret = 3; // right            break;        case 80:                ret = 4; // down            break;        default:                break;        }    } while (ret == 0);    return ret;}int main(){    bool isWin = false;    int a[NUM][NUM] = {0};    while(checkArray((int *)a))    {        int i = rand()%NUM;        int j = rand()%NUM;        if(a[i][j] == 0)            a[i][j] = myRand();        else            continue;        for(int m = 0; m < NUM; m ++)        {            for(int n = 0; n < NUM; n++)                cout << a[m][n] << "\t";            cout << endl;        }        cout << "======================" << endl;        cout << "Score : \t" << TotalScore << endl;        int k = GetDirection();        switch(k)        {            case 1:                cout << "LEFT" << endl;                moveLeft((int *)a);                break;            case 2:                cout << "TOP" << endl;                moveTop((int *)a);                break;            case 3:                cout << "RIGHT" << endl;                moveRight((int *)a);                break;            case 4:                cout << "DOWN" << endl;                moveDown((int *)a);                break;            default:                break;        }    if(checkWin((int *)a))    {        isWin = true;        break;    }    system("cls");    }    if(isWin)        cout << "\n\n=============\n\n" << endl;    else        cout << "\n\n=======FAILED=======\n\n" << endl;    system("pause");    return 0; } 

之后会将bug修正

0 0