首页 > 其他分享 >2048 Fixed by Lowloon Lowloon

2048 Fixed by Lowloon Lowloon

时间:2024-10-20 13:47:57浏览次数:1  
标签:return mat int Lowloon 2048 else break Fixed merged

Copied from 2048
Created by VastUniverse_Hory
Fixed by Lowloon

#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
#define endl "\n"
using namespace std;
char old;
unsigned long long mat[100][100];
int n, m;
int olld(char o){
    if(o=='w')return 0;
    else if(o=='a')return 1;
    else if(o=='s')return 2;
    else return 3;
}
char randc(char old){
    srand(time(0));
    int a=rand()%4;
    if(olld(old)==a)a=(a+1)%4;
    if(a==0)return 'w';
    else if(a==1)return 'a';
    else if(a==2)return 's';
    else return 'd';
}
void print()
{
    system("cls");
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (mat[i][j] == 0)
            {
                printf("    .");
            } 
            else
            {
                printf("%5d", mat[i][j]);
            }
            // printf(" ");
        }
        printf("\n");
    }
}

int main()
{
    srand(time(0));
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    cout.tie(0);
    cout<<"How many rows and columns? (More than 2)"<<endl;
    cin >> n >> m;
    cout<<"Need Autoplay? Press Y(es) or N(o)."<<endl;
    char que;
    que=getch();
    n = max(n, 2);
    m = max(m, 2);
    n = min(n, 50);
    m = min(m, 50);
    memset(mat, 0, sizeof mat);
    int score = 0;
    while (true)
    {
        vector<pair<int, int> > v;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (mat[i][j] == 0)
                {
                    v.push_back(make_pair(i, j));
                }
                if (mat[i][j] == 2048)
                {
                    printf("YOU WIN!\nScore: %d\n", score);
                    // cout << "YOU WIN!" << endl;
                    // cout << "Score: " << score << endl;
                    system("pause");
                    return 0; 
                }
            }
        }
        if (v.empty())
        {
            printf("GAME OVER!\nScore: %d\n", score);
            // cout << "GAME OVER!" << endl;
            // cout << "Score: " << score << endl;
            while (true);
            return 0;
        }
        int rd = rand() % v.size();
        mat[v[rd].first][v[rd].second] = 2;
        // mat[1][1] = max((long long) mat[1][1], (long long) 2);
        print();
        printf("Score: %d\n", score);
        // cout << "Score: " << score << endl;
        char c;
        do
        {
            if(que=='Y'||que=='y'){
                c = randc(old);
                old = c;
            }else{
                c=getchar();
            }
        } while (!(c == 'w' || c == 'a' || c == 's' || c == 'd'));
        if (c == 'w')
        {
            set<pair<int, int> > merged;
            for (int i = 2; i <= n; i++)
            {
                for (int j = 1; j <= m; j++)
                {
                    for (int k = i - 1; k >= 1; k--)
                    {
                        if (mat[k][j] == 0)
                        {
                            mat[k][j] = mat[k + 1][j];
                            mat[k + 1][j] = 0;
                        }
                        else if (mat[k][j] == mat[k + 1][j])
                        {
                            if (merged.count(make_pair(k, j)))
                            {
                                break;
                            }
                            mat[k][j] *= 2;
                            mat[k + 1][j] = 0;
                            score += mat[k][j];
                            merged.insert(make_pair(k, j));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
        else if (c == 's')
        {
            set<pair<int, int> > merged;
            for (int i = n - 1; i >= 1; i--)
            {
                for (int j = 1; j <= m; j++)
                {
                    for (int k = i + 1; k <= n; k++)
                    {
                        if (mat[k][j] == 0)
                        {
                            mat[k][j] = mat[k - 1][j];
                            mat[k - 1][j] = 0;
                        }
                        else if (mat[k][j] == mat[k - 1][j])
                        {
                            if (merged.count(make_pair(k, j)))
                            {
                                break;
                            }
                            mat[k][j] *= 2;
                            mat[k - 1][j] = 0;
                            score += mat[k][j];
                            merged.insert(make_pair(k, j));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
        else if (c == 'a')
        {
            set<pair<int, int> > merged;
            for (int i = 1; i <= n; i++)
            {
                for (int j = 2; j <= m; j++)
                {
                    for (int k = j - 1; k >= 1; k--)
                    {
                        if (mat[i][k] == 0)
                        {
                            mat[i][k] = mat[i][k + 1];
                            mat[i][k + 1] = 0;
                        }
                        else if (mat[i][k] == mat[i][k + 1])
                        {
                            if (merged.count(make_pair(i, k)))
                            {
                                break;
                            }
                            mat[i][k] *= 2;
                            mat[i][k + 1] = 0;
                            score += mat[i][k];
                            merged.insert(make_pair(i, k));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
        else if (c == 'd')
        {
            set<pair<int, int> > merged;
            for (int i = 1; i <= n; i++)
            {
                for (int j = m - 1; j >= 1; j--)
                {
                    for (int k = j + 1; k <= m; k++)
                    {
                        if (mat[i][k] == 0)
                        {
                            mat[i][k] = mat[i][k - 1];
                            mat[i][k - 1] = 0;
                        }
                        else if (mat[i][k] == mat[i][k - 1])
                        {
                            if (merged.count(make_pair(i, k)))
                            {
                                break;
                            }
                            mat[i][k] *= 2;
                            mat[i][k - 1] = 0;
                            score += mat[i][k];
                            merged.insert(make_pair(i, k));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
//        score++;
    }
	return 0;
}

标签:return,mat,int,Lowloon,2048,else,break,Fixed,merged
From: https://www.cnblogs.com/grm-in-kowlon/p/18487197

相关文章

  • CF1988C. Increasing Sequence with Fixed OR
    链接:    https://codeforces.com/problemset/problem/1988/Chttps://codeforces.com/problemset/problem/1988/C大意:    给定一个n,找一个最长的正整数递增序列,并满足相邻或等于n思路:    1、显然是要分析二进制方面的规律        2、首先......
  • JavaScript Number研究03_实例方法_toExponential_toFixed_toPrecision_toString_valu
    JavaScriptNumber研究03:实例方法——toExponential、toFixed、toPrecision、toString、valueOf、toLocaleString在JavaScript中,Number对象不仅包含了许多有用的静态属性,还提供了一系列实例方法,帮助我们在不同场景下处理和转换数值。这些方法包括:toExponential()toFixed()......
  • 易优CMS出现:Allowed memory size of 134217728 bytes exhausted (tried to allocate 2
    当你遇到“Allowedmemorysizeof134217728bytesexhausted(triedtoallocate20480bytes)”的错误时,这意味着PHP的内存限制已经耗尽。这种错误通常发生在处理大量数据或执行复杂计算时。为了解决这个问题,可以采取以下几种方法:方法1:修改 php.ini 文件(推荐)找到 php......
  • 易优CMS登录后台报Allowed memory size of 134217728 bytes ex hausted (tried to alo
    当你在登录后台时遇到“Allowedmemorysizeof134217728bytesexhausted(triedtoallocate20480bytes)”的错误提示时,通常是由于PHP的内存限制不足导致的。以下是一些具体的解决步骤:步骤1:检查PHP配置登录宝塔面板登录宝塔面板。在左侧菜单栏选择“软件商店”。......
  • 《如 何 速 通 一 套 题》5.1bug fixed
    好吧,那天晚上才发现数据错了,只能咕一下了,现在咕完了前情提要:5.0D补幺梨\(1\lem\le10^8\),\(1\len\le10^7\),一看就不是背包。那么,以背包的形式出现,但是不是背包,还能是什么呢?同余最短路。只能是同余最短路。然后由于\(n\)太大了,所以不同的值的数量估计不会很多,最大......
  • 为何有时出现:Allowed memory size of 134217728 bytes exhausted (tried to allocate
    出现“Allowedmemorysizeof134217728bytesexhausted(triedtoallocate20480bytes)”这样的错误,意味着PHP脚本运行时消耗的内存超过了PHP配置允许的最大内存限制。这个错误通常是因为PHP脚本处理的数据量过大、内存消耗较高的任务或配置不当引起的。以下是几种解决......
  • 在ARM开发板上实现2048小游戏
     event.h屏幕点击事件.h文件:获取屏幕的xy坐标,获取手指滑动的方向,获取点击事件。#ifndef__EVENT_H_#define__EVENT_H_#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<dirent.h>#inclu......
  • C#——fixed用法
    1.fixed语句*固定用于指针操作的变量;*可防止垃圾回收器重新定位可移动变量,并声明指向该变量的指针;*固定变量的地址,在语句的持续时间内不会更改*fixed语句中,只能使用声明的指针,声明的指针是只读的,无法修改*fixed语句只能在不安全的上下文中使用staticvoidMain(str......
  • P2048
    goodrmq#include<bits/stdc++.h>usingnamespacestd;structnode{ intnum; intlid,rid; intw; intdata; booloperator<(nodei)const{ returndata<i.data; }};intc[500005],dp[500005][20],lg[500005];intnum[500005][20];intp[5000......
  • 洛谷 P4829 kry loves 2048——题解
    洛谷P4829题解传送锚点摸鱼环节kryloves2048题目背景kls是一个人赢。题目描述kls最近在玩一款类似2048的游戏,规则是这样的:一开始,有\(n\)个方块,每个方块上有一个\(1\)到\(m\)的整数。kls可以进行两种操作:选择两个数字相同的方块(不一定要相邻),将它们合并成一个数字为......