1. 程式人生 > >c++寫 2048

c++寫 2048

down cursor 不顯示光標 tle cti syn all ++i bool

#include <stdlib.h> #include <conio.h> #include <windows.h> #include<iostream> #include<ctime> using namespace std; int a[4][4]; void myrand(); void init() { for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { a[i][j] = 0; } } myrand(); } void draw() { int i, j; for (i = 0; i < 4; ++i) { // 一個方格由三根豎線組成 for (j = 0; j < 4; ++j) // 第一排豎線 每個豎線之間占5個格 printf("| "); printf("|\n"); for (j = 0; j < 4; ++j) { // 第二排豎線與數字 if (a[i][j] == 0) printf("| "); else printf("|%4d", a[i][j]); } printf("|\n"); for (j = 0; j < 4; ++j) // 第三排豎線加底線 printf("|____"); printf("|\n"); } } void myrand() { srand(time(0)); bool bo = true; while (bo) { int i = rand() % 4; int j = rand() % 4; int n = rand() % 2; if (a[i][j] == 0) { if (n == 1) { a[i][j] = 2; } else if (n == 0) { a[i][j] = 4; } break; } else { for (int i = 1; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (a[i][j] != 0) { bo = false; } } } continue; } } } /////////////////////////////////////////////////////////////////////////////////////////////// void up() { for (int i = 1; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (a[i][j] != 0) { if (a[i -1][j] == a[i][j]) { a[i - 1][j] += a[i][j]; a[i][j] = 0; } } } } for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 3; j++) { //只要當前為0就把下面一個給它 if (a[i][j] == 0) { a[i][j] = a[i + 1][j]; //下面一個為0 a[i + 1][j] = 0; } } } myrand(); } void down() { for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 3; j++) { if (a[i][j] != 0) { if (a[i+1][j] == a[i][j]) { a[i + 1][j] += a[i][j]; a[i][j] = 0; } } } } for (int i = 1; i <= 3; i++) { for (int j = 0; j <= 3; j++) { //只要當前為0就把上一個給它 if (a[i][j] == 0) { a[i][j] = a[i-1][j]; //上一個為0 a[i-1][j] = 0; } } } myrand(); } void righ() { for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 2; j++) { if (a[i][j] != 0) { if (a[i][j+1] == a[i][j]) { a[i][j + 1] += a[i][j]; a[i][j] = 0; } } } } for (int i = 0; i <= 3; i++) { for (int j = 1; j <= 3; j++) { //只要當前為0就把前一個給它 if (a[i][j] == 0) { a[i][j] = a[i][j -1]; //前一個為0 a[i][j - 1] = 0; } } } myrand(); } void left() { for (int i = 0; i <= 3; i++) { for (int j = 1; j <=3; j++) { //只要有一個不為0就進來 if (a[i][j] !=0 ) { //合並相同的 if (a[i][j-1] == a[i][j]) { a[i][j-1] += a[i][j]; a[i][j] = 0; } } } } for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 2; j++) { //只要當前為0就把後一個給它 if (a[i][j] == 0) { a[i][j] = a[i][j + 1]; //後一個為0 a[i][j + 1] = 0; } } } myrand(); } /////////////////////////////////////////////////////////////////////////////////////// void to_up() { int x, y, i; for (y = 0; y < 4; ++y) { // 從上向下合並相同的方塊 for (x = 0; x < 4; ++x) { if (a[x][y] == 0) ; else { for (i = x + 1; i < 4; ++i) { if (a[i][y] == 0) ; else if (a[x][y] == a[i][y]) { a[x][y] += a[i][y]; a[i][y] = 0; x = i; break; } else { //x = i - 1; break; } } } } } for (y = 0; y < 4; ++y) // 向上移動箱子 for (x = 0; x < 4; ++x) { if (a[x][y] == 0) ; else { for (i = x; (i > 0) && (a[i - 1][y] == 0); --i) { a[i - 1][y] = a[i][y]; a[i][y] = 0; } } } myrand(); } void to_down() { int x, y, i; for (y = 0; y < 4; ++y) // 向下合並相同的方格 for (x = 3; x >= 0; --x) { if (a[x][y] == 0) ; else { for (i = x - 1; i >= 0; --i) { if (a[i][y] == 0) ; else if (a[x][y] == a[i][y]) { a[x][y] += a[i][y]; a[i][y] = 0; x = i; break; } else break; } } } for (y = 0; y < 4; ++y) // 向下移動方格 for (x = 3; x >= 0; --x) { if (a[x][y] == 0) ; else { for (i = x; (i < 3) && (a[i + 1][y] == 0); ++i) { a[i + 1][y] = a[i][y]; a[i][y] = 0; } } } myrand(); } void to_left() { int x, y, i; for (x = 0; x < 4; ++x) // 向左合並相同的方格 for (y = 0; y < 4; ++y) { if (a[x][y] == 0) ; else { for (i = y + 1; i < 4; ++i) { if (a[x][i] == 0) ; else if (a[x][y] == a[x][i]) { a[x][y] += a[x][i]; a[x][i] = 0; y = i; break; } else break; } } } for (x = 0; x < 4; ++x) // 向左移動方格 for (y = 0; y < 4; ++y) { if (a[x][y] == 0) ; else { for (i = y; (i > 0) && (a[x][i - 1] == 0); --i) { a[x][i - 1] = a[x][i]; a[x][i] = 0; } } } myrand(); } void to_right() { int x, y, i; for (x = 0; x < 4; ++x) // 向右合並相同的方格 for (y = 3; y >= 0; --y) { if (a[x][y] == 0) ; else { for (i = y - 1; i >= 0; --i) { if (a[x][i] == 0) ; else if (a[x][y] == a[x][i]) { a[x][y] += a[x][i]; a[x][i] = 0; y = i; break; } else break; } } } for (x = 0; x < 4; ++x) // 向右移動方格 for (y = 3; y >= 0; --y) { if (a[x][y] == 0) ; else { for (i = y; (i < 3) && (a[x][i + 1] == 0); ++i) { a[x][i + 1] = a[x][i]; a[x][i] = 0; } } } myrand(); } //////////////////////////////////////////////////////////////////////////////////////////////////// int main() { cout << "wasd,為上下左右方向鍵!!!" << endl; init(); draw(); //char ch; bool bo = true; while (bo) { switch (getch()) { case ‘w‘: case ‘W‘: up(); //to_up(); system("cls"); draw(); break; case ‘s‘: case ‘S‘: down(); //to_down(); system("cls"); draw(); break; case ‘a‘: case ‘A‘: left(); //to_left(); system("cls"); draw(); break; case ‘d‘: case ‘D‘: righ(); //to_right(); system("cls"); draw(); break; } for (int i = 1; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (a[i][j] >= 2048) { cout << "你牛逼!!!" << endl; bo = false; } } } } }

bug 版

// ConsoleApplication6.cpp : 定義控制臺應用程序的入口點。
//
/有bug
#include "stdafx.h"
#include<windows.h>
#include<time.h>
#include<vector>
using namespace std;
#define KEYDOWN(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?1:0)
#define KEYUP(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?0:1)
#define UP    0
#define DOWN  1
#define LEFT  2
#define RIGHT 3
//
clock_t time1_start = clock();
clock_t time1_end;
clock_t time2_start = clock();
clock_t time2_end;

int map[4][4];

bool WirteChar(int High, int Wide, char * pszChar, int wArr)
{
    CONSOLE_CURSOR_INFO cci;
    cci.dwSize = 1;
    cci.bVisible = FALSE;           //不顯示光標
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
    COORD loc;
    loc.X = Wide * 2*3;
    loc.Y = High*3;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wArr);
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);
    printf(pszChar);       //打印時需要註意有些特殊字符是占兩個字節
    return true;
}

bool SetWindowSize(wchar_t * pTitle, int nX, int nY)
{
    // 設置控制臺標題
    SetConsoleTitle(pTitle);
    HANDLE hStdIn, hStdOut;
    hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    //獲取最大控制臺窗口大小
    COORD pos = GetLargestConsoleWindowSize(hStdOut);
    COORD BufferSize = { pos.X + 1, pos.Y + 1 };
    //設置控制臺緩沖區大小
    if (!SetConsoleScreenBufferSize(hStdOut, BufferSize))
    { //設置控制臺窗口緩沖區失敗
        printf("buffer err(%d,%d)%d\n", BufferSize.X, BufferSize.X, GetLastError());
        return false;
    }
    SMALL_RECT srctWindow = { 0,0,nX,nY };   //控制臺坐標位置
    if (!SetConsoleWindowInfo(hStdOut, true, &srctWindow))
    {  //設置控制臺窗口大小
        printf("size err %d\n", GetLastError());
        return false;
    }
    COORD Buffer = { nX + 1, nY + 1 };   //緩沖區坐標位置
                                         //設置控制臺緩沖區大小
    if (!SetConsoleScreenBufferSize(hStdOut, Buffer))
    { //設置控制臺窗口緩沖區失敗
        printf("buffer err(%d,%d)%d\n", BufferSize.X, BufferSize.Y, GetLastError());
        return false;
    }
    return true;
}

void initMap()
{
    srand((unsigned int)time(NULL));
    int arr[4] = { 0,2,4,8 };

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            int num = rand() % 4;
            map[i][j] = arr[num];
        }
    }
}
int AddNum()
{
    srand((unsigned int)time(NULL));
    int arr[4] = { 0,2,4,8 };
    int num = rand() % 4;
    vector<POINT>Vec;
    POINT point = {};
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            if (map[i][j] == 0)
            {
                point.x = i;
                point.y = j;
                Vec.push_back(point);
            }
        }
    }
    if (Vec.size() >= 3)
    {
        map[Vec[0].x][Vec[0].y] = arr[num];
    }
    return 0;
}

void PrintMap()
{
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            switch (map[i][j])
            {
            case 0:  WirteChar(i+1 , j+1 , "  0  ", 48|14); break;
            case 2:  WirteChar(i+1 , j+1 , "  2  ", 80|11); break;
            case 4:  WirteChar(i+1 , j+1 , "  4  ", 192|1); break;
            case 8:  WirteChar(i+1 , j+1 , "  8  ", 96|0); break;
            case 16: WirteChar(i+1 , j+1 , " 16  ", 176|0); break;
            case 32: WirteChar(i+1 , j+1 , " 32  ", 192|5); break;
            case 64: WirteChar(i+1 , j+1 , " 64  ", FOREGROUND_INTENSITY); break;
            case 128:WirteChar(i+1 , j+1 , " 128 ", 112|9); break;
            }
        }
    }
}

bool MoveUp()
{
    bool IsA = false;
    for (int i = 0; i < 4; i++)//0 1 2 3
    {
        for (int j = 0; j < 4; j++)//0 1 2 3
        {
            if (i == 0) break;
            if (map[i][j] == map[i - 1][j])
            {
                map[i - 1][j] = map[i][j] + map[i][j];
                map[i][j] = 0;
                IsA = true;
                continue;
            }
            if (map[i - 1][j] == 0)
            {
                map[i - 1][j] = map[i][j];
                map[i][j] = 0;
                IsA = true;
                continue;
            }
        }
    }
    if (IsA) return true;
    return false;

}

bool MoveDown()
{
    bool IsA = false;
    for (int i = 3; i >= 0; i--)//3 2 1 0
    {
        for (int j = 0; j < 4; j++) //1 2 3 4
        {
            if (i == 3) break;
            if (map[i][j] == map[i + 1][j])
            {
                map[i + 1][j] = map[i][j] + map[i][j];
                map[i][j] = 0;
                IsA = true;
                continue;
            }
            if (map[i + 1][j] == 0)
            {
                map[i + 1][j] = map[i][j];
                map[i][j] = 0;
                IsA = true;
                continue;
            }
        }
    }
    if (IsA) return true;
    return false;
}

bool MoveLeft()
{
    bool IsA = false;
    for (int i = 0; i < 4; i++)//0 1 2 3
    {
        for (int j = 0; j < 4; j++)//0 1 2 3
        {
            if (j == 0)continue;
            if (map[i][j] == map[i][j - 1])
            {
                map[i][j - 1] = map[i][j] + map[i][j];
                map[i][j] = 0;
                IsA = true;
                continue;
            }
            if (map[i][j - 1] == 0)
            {
                map[i][j - 1] = map[i][j];
                map[i][j] = 0;
                IsA = true;
                continue;
            }
        }
    }
    if (IsA) return true;
    return false;
}

bool MoveRight()
{
    bool IsA = false;
    for (int i = 0; i < 4; i++)//0 1 2 3
    {
        for (int j = 3; j >= 0; j--)//3 2 1 0
        {
            if (j == 3)continue;
            if (map[i][j] == map[i][j + 1])
            {
                map[i][j + 1] = map[i][j] + map[i][j];
                map[i][j] = 0;
                IsA = true;
                continue;
            }
            if (map[i][j + 1] == 0)
            {
                map[i][j + 1] = map[i][j];
                map[i][j] = 0;
                IsA = true;
                continue;
            }
        }
    }
    if (IsA) return true;
    return false;
}

bool Kib()
{
    if (KEYDOWN(‘W‘) || KEYDOWN(‘w‘))
    {
        return MoveUp();
    }
    if (KEYDOWN(‘S‘) || KEYDOWN(‘s‘))
    {
        return MoveDown();
    }
    if (KEYDOWN(‘A‘) || KEYDOWN(‘a‘))
    {
        return MoveLeft();
    }
    if (KEYDOWN(‘D‘) || KEYDOWN(‘d‘))
    {
        return MoveRight();
    }
    return 0;
}

int main()
{
    SetWindowSize(L"hi", 40, 20);
    system("color 7f");
    initMap();
    PrintMap();

    while (1)
    {
        time1_end = clock();
        if (time1_end - time1_start > 30)
        {
            time1_start = time1_end;
            bool IsMove=    Kib();
            if (IsMove)
            {
            PrintMap();
            AddNum();
            PrintMap();
            }
        }
    }
    return 0;
}

c++寫 2048