同时祝福大家
#include<bits/stdc++.h>
#include<Windows.h>
#define int long long
#define size 4
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//#define system("cls") cl
using namespace std;
int a[6][6];
int maxx;
bool IsMouseButtonDown(unsigned int button) {
return GetAsyncKeyState(button) & 0x8000;
}
POINT point;
void color(int ForgC, int BackC) {
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
void gotoxy(int x,int y){
COORD pos;
HANDLE han=GetStdHandle(STD_OUTPUT_HANDLE);
pos.X=y,pos.Y=x;
SetConsoleCursorPosition(han,pos);
}
bool die(){
for(int i=1;i<=size;i++)for(int j=1;j<=size;j++)if(a[i][j]==0)return 0;
return 1;
}
void rands(){
while(1){
int x=rand()%4+1,y=rand()%4+1;
if(a[x][y]==0){
if(rand()%64==1)a[x][y]=3;
if(maxx>=128){
if(rand()%6==2)a[x][y]=16;
else if(rand()%6==1||rand()%6==0)a[x][y]=8;
else a[x][y]=4;
}
else if(rand()%8==3){
a[x][y]=4;
}
else a[x][y]=2;
break;
}
}
}
void print(){
for(int i=1;i<=size;i++){
for(int j=1;j<=size;j++){
if(a[i][j]==0)continue;
if(a[i][j]==3){
color(15,8);
gotoxy(i*3-3+1,j*6-3-1);
cout<<" ";
gotoxy(i*3-3+2,j*6-3-1);
cout<<" ## ";
gotoxy(i*3-3+3,j*6-3-1);
cout<<" ";
}
else{
int x=log2(a[i][j])+1;
if(x>=2)x++;
if(x>=8)x++;
color(15,x);
gotoxy(i*3+1-3,j*6-3-1);
cout<<" ";
gotoxy(i*3+2-3,j*6-3-1);
if(a[i][j]>=10000){
cout<<""<<a[i][j]<<" ";
}else if(a[i][j]>=1000){
cout<<" "<<a[i][j]<<" ";
}else if(a[i][j]>=100){
cout<<" "<<a[i][j]<<" ";
}else if(a[i][j]>=10){
cout<<" "<<a[i][j]<<" ";
}else {
cout<<" "<<a[i][j]<<" ";
}
gotoxy(i*3+3-3,j*6-3-1);
cout<<" ";
}
}
}
}
void out(){
system("cls");
color(2,2);
cout<<" \n";
for(int i=1;i<=12;i++){
cout<<" ";
color(15,0);
cout<<" ";
color(2,2);
cout<<" \n";
}
color(2,2);
cout<<" \n";
color(15,0);
}
void down(){
for(int i=size;i>=1;i--){
for(int j=1;j<=size;j++){
if(a[i][j]==0)continue;
if(a[i][j]==a[i+1][j]||((a[i][j]==3||a[i+1][j]==3)&&!(a[i][j]==3&&a[i+1][j]==3))){
a[i+1][j]*=2;
for(int f=i;f>=1;f--){
a[f][j]=a[f-1][j];
}
}
else {
int f=i;
while(1){
if(a[f+1][j]==0&&f+1<=size)f++;
else break;
}swap(a[i][j],a[f][j]);
}
}
}
}
void up(){
for(int i=1;i<=size;i++){
for(int j=1;j<=size;j++){
if(a[i][j]==0)continue;
if(a[i][j]==a[i-1][j]||((a[i][j]==3||a[i-1][j]==3)&&!(a[i][j]==3&&a[i-1][j]==3))){
a[i-1][j]*=2;
for(int f=i;f<=size;f++){
a[f][j]=a[f+1][j];
}
}
else {
int f=i;
while(1){
if(a[f-1][j]==0&&f-1>=1)f--;
else break;
}swap(a[i][j],a[f][j]);
}
}
}
}
void left(){
for(int i=1;i<=size;i++){
for(int j=1;j<=size;j++){
if(a[i][j]==0)continue;
if(a[i][j]==a[i][j-1]||((a[i][j]==3||a[i][j-1]==3)&&!(a[i][j]==3&&a[i][j-1]==3))){
a[i][j-1]*=2;
for(int f=j;f<=size;f++){
a[i][f]=a[i][f+1];
}
}
else {
int f=j;
while(1){
if(a[i][f-1]==0&&f-1>=1)f--;
else break;
}swap(a[i][j],a[i][f]);
}
}
}
}
void right(){
for(int i=1;i<=size;i++){
for(int j=size;j>=1;j--){
if(a[i][j]==0)continue;
if(a[i][j]==a[i][j+1]||((a[i][j]==3||a[i][j+1]==3)&&!(a[i][j]==3&&a[i][j+1]==3))){
a[i][j+1]*=2;
for(int f=j;f>=1;f--){
a[i][f]=a[i][f-1];
}
}
else {
int f=j;
while(1){
if(a[i][f+1]==0&&f+1<=size)f++;
else break;
}swap(a[i][j],a[i][f]);
}
}
}
}
int x_1,x_2,y_1,y_2;
void get(){
while(!KEY_DOWN(VK_LBUTTON));
if (KEY_DOWN(VK_LBUTTON)) {
GetCursorPos(&point);
x_1=point.y,y_1=point.x;
while(KEY_DOWN(VK_LBUTTON)) ;
GetCursorPos(&point);
x_2=point.y,y_2=point.x;
if(abs(x_1-x_2)<150&&abs(y_1-y_2)<150)return ;
if(x_1<x_2&&abs(x_1-x_2+3)/abs(y_1-y_2)+1>=3)down();
else if(x_1>x_2&&abs(x_1-x_2+3)/(abs(y_1-y_2)+1)>=3)up();
else if(y_1<y_2&&abs(y_1-y_2+3)/(abs(x_1-x_2)+1)>=3)right();
else if(y_1>y_2&&abs(y_1-y_2+3)/(abs(x_1-x_2)+1)>=3)left();
}
}
void DisableMouseSelection(){
//
CONSOLE_CURSOR_INFO ConsoleCursorInfo;
//
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ConsoleCursorInfo);
//
ConsoleCursorInfo.bVisible = FALSE;
//
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ConsoleCursorInfo);
//
HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(hConsole, &mode);
SetConsoleMode(hConsole, mode & (~ENABLE_QUICK_EDIT_MODE));
}
signed main(){
DisableMouseSelection();
//
// Sleep(10000);
cout<<"欢迎来到2048\n鼠标控制(鼠标左键按下移动,松开 标准一点,不然会有问题)\nbug私信dsafhrbghrsbsbsbs,感谢dalao";
char x=getchar();
system("cls");
color(2,2);
cout<<" \n";
for(int i=1;i<=12;i++){
cout<<" ";
color(15,0);
cout<<" ";
color(2,2);
cout<<" \n";
}
color(2,2);
cout<<" \n";
color(15,0);
srand(time(0));
rands();
print();
while(!die()){
get();
out();
rands();
print();
}
return 0;
}