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