- 分享
分享:更符合OIer体质的2048小游戏
- 2025-8-22 12:21:05 @
将下文代码复制到Dev-c++
注:请在编译选项中加入 -std=c++11
(实在想玩但又不会整的,私信我...)
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<thread>
#define cls() system("cls")
#define colorinit() {HANDLE hd=GetStdHandle(STD_OUTPUT_HANDLE);if(hd==INVALID_HANDLE_VALUE)return GetLastError();DWORD dm=0;if(!GetConsoleMode(hd,&dm))return GetLastError();dm|=0x0004;if(!SetConsoleMode(hd,dm))return GetLastError();}
#define setcolorf(fr,fg,fb) wprintf(L"\x1b[38;2;%d;%d;%dm",fr,fg,fb);
#define setcolorb(br,bg,bb) wprintf(L"\x1b[48;2;%d;%d;%dm",br,bg,bb);
#define setcolory() {setcolorf(204,204,204);setcolorb(0,0,0);}
#define fill(x,y,n) {board[x][y]=n;had[x][y]=1;zs++;}
#define clear() {memset(board,-1,sizeof(board));zs=0;score=0;memset(scodis,0,sizeof(scodis));memset(had,0,sizeof(had));}
using namespace std;
//class data
int board[5][5],rx,ry;
int bf[5][5];
int zs;
bool had[5][5];
int score,scodis[8];
int tilmax,state,scop;
bool able;
//class display
int color[20][4]={
{241,196,15},
{52,152,219},
{142,68,173},
{46,70,140},
{46,70,140},
{46,70,140},
{46,70,140},
{52,73,94},
{231,76,60},
{230,126,34},
{94,185,94},
{244,149,160},
{153,102,0},
{187,187,187},
{241,196,15},
{102,198,255},
{0,0,0},
{178,178,178},
{178,178,178}
};
string tilnames[20]={"CE","Judging","RE","TLE","MLE","ILE","OLE","UKE","WA","PC","AC","PE","DoJ","JF","AU","AK","#pragma","O2","O3"};
string tilbases[20]={"2","4","8","16","32","64","128","256","512","1024","2048","4096","8192","16384","32768","65536","*1","*2","*4"};
string tilxs[20][8]={{
"+--------+","| CCC EEE|","|C E |","|C EEE|","|C E |","| CCC EEE|","+--------+"},{
"+--------+","| ====, |","|== \\,/|","| ^ v |","|/'\\ ==|","| '==== |","+--------+",},{
"+--------+","|RRR EEE|","|R R E |","|RRR EEE|","|R R E |","|R R EEE|","+--------+"},{
"+--------+","|TTTT EEE|","| T E |","|L T EEE|","|L E |","|LLLL EEE|","+--------+",},{
"+--------+","|MMMMM EE|","|M M M E |","| M M EE|","|L E |","|LLLL EE|","+--------+"},{
"+--------+","| III EEE|","| I E |","| III EEE|","|L E |","|LLLL EEE|","+--------+"},{
"+--------+","| OO EEE|","|O O E |","| OO EEE|","|L E |","|LLLL EEE|","+--------+"},{
"+--------+","|U U EEE|","| UU E |","|K K EEE|","|KK E |","|K K EEE|","+--------+"},{
"+--------+","|W W W A |","|W W A A|","|W W AAA|","|W W A A|","|WWWW A A|","+--------+"},{
"+--------+","|PPP CC|","|P P C |","|PPP C |","|P C |","|P CC|","+--------+"},{
"+--------+","| AA CC|","|A A C |","|AAAA C |","|A A C |","|A A CC|","+--------+"},{
"+--------+","|PPP EEE|","|P P E |","|PPP EEE|","|P E |","|P EEE|","+--------+"},{
"+--------+","|DD OO J|","|D D OO J|","|D D J|","|D D J J|","|DD J |","+--------+"},{
"+--------+","| J FFF|","| J F |","| J FFF|","|J J F |","| JJ F |","+--------+"},{
"+--------+","| A U U|","|A A U U|","|AAA U U|","|A A U U|","|A A UU |","+--------+"},{
"+--------+","| AA K K|","|A A KK |","|AAAA K |","|A A KK |","|A A K K|","+--------+"},{
"+--------+","| # # |","|########|","| # # |","|########|","| # # |","+--------+"},{
"+--------+","| OO 222|","|O O 2|","|O O 222|","|O O 2 |","| OO 222|","+--------+"},{
"+--------+","| OO 333|","|O O 3|","|O O 333|","|O O 3|","| OO 333|","+--------+"}};
string sconums[11][6]={
{"000","0 0","0 0","0 0","000",},
{" 1 ","11 "," 1 "," 1 ","111"},
{"222"," 2","222","2 ","222"},
{"333"," 3","333"," 3","333"},
{"4 4","4 4","444"," 4"," 4"},
{"555","5 ","555"," 5","555"},
{"666","6 ","666","6 6","666"},
{"777"," 7"," 7"," 7"," 7"},
{"888","8 8","888","8 8","888"},
{"999","9 9","999"," 9","999"}
};
void setcolor(int BC,int FC){
WORD wColor=((BC&0x0F)<<4)+(FC&0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor);
}
void hidecur(){
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
return;
}
void setsize(int col,int row){
char cmd[64];
sprintf(cmd,"mode con cols=%d lines=%d",col,row);
system(cmd);
return;
}
void gotoxy(int x,int y){
HANDLE hout;
COORD pos;
pos.X=x;
pos.Y=y;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout,pos);
return;
}
void maindisplay(){
gotoxy(0,0);
printf("+----------------------------------------+---------------------------+\n");
for(int i=1;i<5;i++){
for(int j=0;j<7;j++){
printf("|");
for(int k=1;k<5;k++){
if(board[i-1][k-1]<0){
printf(" ");
continue;
}
setcolorf(255,255,255);
setcolorb(color[board[i-1][k-1]][0],color[board[i-1][k-1]][1],color[board[i-1][k-1]][2]);
if(i-1==rx&&k-1==ry&&able){
setcolorb(255,255,255);
setcolorf(color[board[i-1][k-1]][0],color[board[i-1][k-1]][1],color[board[i-1][k-1]][2]);
}
printf("%s",tilxs[board[i-1][k-1]][j].c_str());
setcolory();
}
if((i-1)*7+j+2^7&&(i-1)*7+j+2^9){
printf("|");
}
if((i-1)*7+j+2==7||(i-1)*7+j+2==9){
printf("+---------------------------+");
}
if((i-1)*7+j+2>1&&(i-1)*7+j+2<7){
for(int l=0;l<7;l++){
setcolorf(255,255,255);
printf("%s",sconums[scodis[l]][(i-1)*7+j].c_str());
if(l<6){
printf(" ");
}
setcolory();
}
printf("|");
}
if((i-1)*7+j+2==10){
printf(" Blocks |");
}
if((i-1)*7+j+2>10&&(i-1)*7+j+2<30){
setcolorf(color[(i-1)*7+j-9][0],color[(i-1)*7+j-9][1],color[(i-1)*7+j-9][2]);
if(!color[(i-1)*7+j-9][0]){
setcolorb(204,204,204);
setcolorf(0,0,0);
}
printf("%s",tilnames[(i-1)*7+j-9].c_str());
setcolory();
for(int l=0;l<27-tilnames[(i-1)*7+j-9].size()-tilbases[(i-1)*7+j-9].size();l++){
printf(" ");
}
setcolorf(color[(i-1)*7+j-9][0],color[(i-1)*7+j-9][1],color[(i-1)*7+j-9][2]);
if(!color[(i-1)*7+j-9][0]){
setcolorb(204,204,204);
setcolorf(0,0,0);
}
printf("%s",tilbases[(i-1)*7+j-9].c_str());
setcolory();
printf("|");
}
if((i-1)*7+j+2==8){
printf("MAX:");
setcolorf(color[tilmax][0],color[tilmax][1],color[tilmax][2]);
printf("%s(=%s)",tilnames[tilmax].c_str(),tilbases[tilmax].c_str());
setcolory();
for(int l=0;l<9-tilnames[tilmax].size()-tilbases[tilmax].size();l++){
printf(" ");
}
if(!state){
printf(" ");
}
else if(state==1){
printf(" +");
printf("%d%d%d%d%d%d",scop/100000,scop/10000%10,scop/1000%10,scop/100%10,scop/10%10,scop%10);
}
else if(state==2){
printf("YOU AK IOI!");
}
else{
printf(" AFO...");
}
printf("|");
}
printf("\n");
}
}
printf("+----------------------------------------+---------------------------+");
return;
}
//class function
char getkey(){
int getk;
while(1){
if(_kbhit){
getk=getch();
break;
}
}
switch(getk){
case 'H':
getk='w';
break;
case 'K':
getk='a';
break;
case 'P':
getk='s';
break;
case 'M':
getk='d';
break;
case 'w':
case 'a':
case 's':
case 'd':
getk=' ';
break;
}
return getk;
}
int randint(int a,int b){
return rand()%(b-a+1)+a;
}
int turnpos(int i,int j){
return (i-1)*4+j;
}
pair<int,int> turnxy(int pos){
return make_pair((pos-1)/4,(pos-1)%4);
}
void makerxy(){
int rrr=randint(1,16);
rx=turnxy(rrr).first;
ry=turnxy(rrr).second;
while(had[rx][ry]){
rrr=randint(1,16);
rx=turnxy(rrr).first;
ry=turnxy(rrr).second;
}
return;
}
void resco(){
scodis[0]=score/1000000;
scodis[1]=score/100000%10;
scodis[2]=score/10000%10;
scodis[3]=score/1000%10;
scodis[4]=score/100%10;
scodis[5]=score/10%10;
scodis[6]=score%10;
return;
}
int newblock(){
int get=randint(0,9909);
if(get<7830){
return 0;
}
if(get<(7830+780)){
return 1;
}
if(get<(7830+780+1118)){
return 16;
}
return 17;
}
int main(){
srand(unsigned(time(NULL)));
SetConsoleTitle("AKIOI(2048)-made by shenhan");
colorinit();
hidecur();
setsize(70,2);
cout<<"Game init\nPlease press Up arrow";
while(1){
int cz=getkey();
if(cz=='w'){
break;
}
}
setsize(70,2);
cout<<"Game init\nPlease press Left arrow";
while(1){
int cz=getkey();
if(cz=='a'){
break;
}
}
setsize(70,2);
cout<<"Game init\nPlease press Down arrow";
while(1){
int cz=getkey();
if(cz=='s'){
break;
}
}
setsize(70,2);
cout<<"Game init\nPlease press Right arrow";
while(1){
int cz=getkey();
if(cz=='d'){
break;
}
}
setsize(70,1);
cout<<"Now don't press any key again while the game start...";
Sleep(2000);
setsize(70,12);
for(int i=10;i>-1;i--){
cout<<" _| _| _| _|_|_| _|_| _|_|_|\n";
cout<<"_| _| _|_| _| _| _| _| \n";
cout<<"_|_|_| _| _|_| _| _| _| _| \n";
cout<<"_| _| _|_| _| _| _| _| \n";
cout<<"_| _| _| _| _|_|_| _|_| _|_|_|";
cout<<"\n2048 GAME\nAuther: shenhan(shenhfr on CSDN,Extparxt_Helium on ACwing)\nHow to play:\nPress Arrow keys to control\nPress ` key to give up\nWhen you get AK(65536),you win!\n"<<i;
Sleep(1000);
setsize(70,12);
}
setsize(70,30);
clear();
for(int i=0;i<2;i++){
makerxy();
fill(rx,ry,newblock());
}
maindisplay();
while(1){
int cz=getkey(),last,scobf=score;
state=0;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
bf[i][j]=board[i][j];
}
}
if(cz=='s'){
int li;
for(int j=0;j<4;j++){
last=board[3][j];
li=3;
for(int i=2;i>-1;i--){
if(board[i][j]<0){
continue;
}
if((board[i][j]>15&&board[i+1][j]<16)||(board[i][j]<16&&board[i+1][j]>15)){
bool f=1;
for(int si=i;si<4;si++){
if(board[si][j]<0){
f=0;
}
}
if(f){
able=1;
board[i+1][j]+=(board[i][j]-16);
board[i][j]=-1;
score+=pow(2,board[i+(board[i][j]>15?-1:1)][j]+1);
}
}
if(board[i][j]==last){
able=1;
board[i][j]=-1;
score+=(board[li][j]<16?pow(2,(++board[li][j])+1):0);
}
last=board[i][j];
li=i;
}
}
for(int j=0;j<4;j++){
queue<int> il;
for(int i=3;i>-1;i--){
if(board[i][j]^-1){
il.push(board[i][j]);
board[i][j]=-1;
}
}
for(int i=3;i>-1;i--){
if(il.empty()){
break;
}
board[i][j]=il.front();
il.pop();
}
}
}
else if(cz=='w'){
int li;
for(int j=0;j<4;j++){
last=board[0][j];
li=0;
for(int i=1;i<4;i++){
if(board[i][j]<0){
continue;
}
if((board[i][j]>15&&board[i-1][j]<16)||(board[i][j]<16&&board[i-1][j]>15)){
bool f=1;
for(int si=i;si>-1;si--){
if(board[si][j]<0){
f=0;
}
}
if(f){
able=1;
board[i-1][j]+=(board[i][j]-16);
board[i][j]=-1;
score+=pow(2,board[i+(board[i][j]>15?1:-1)][j]+1);
}
}
if(board[i][j]==last){
able=1;
board[i][j]=-1;
score+=(board[li][j]<16?pow(2,(++board[li][j])+1):0);
}
last=board[i][j];
li=i;
}
}
for(int j=0;j<4;j++){
queue<int> il;
for(int i=0;i<4;i++){
if(board[i][j]^-1){
il.push(board[i][j]);
board[i][j]=-1;
}
}
for(int i=0;i<4;i++){
if(il.empty()){
break;
}
board[i][j]=il.front();
il.pop();
}
}
}
else if(cz=='a'){
int lj;
for(int i=0;i<4;i++){
last=board[i][0];
lj=0;
for(int j=1;j<4;j++){
if(board[i][j]<0){
continue;
}
if((board[i][j]>15&&board[i][j-1]<16)||(board[i][j]<16&&board[i][j-1]>15)){
bool f=1;
for(int sj=j;sj>-1;sj--){
if(board[i][sj]<0){
f=0;
}
}
if(f){
able=1;
board[i][j-1]+=(board[i][j]-16);
board[i][j]=-1;
score+=pow(2,board[i][j+(board[i][j]>15?1:-1)]+1);
}
}
if(board[i][j]==last){
able=1;
board[i][j]=-1;
score+=(board[i][lj]<16?pow(2,(++board[i][lj])+1):0);
}
last=board[i][j];
lj=j;
}
}
for(int i=0;i<4;i++){
queue<int> jl;
for(int j=0;j<4;j++){
if(board[i][j]^-1){
jl.push(board[i][j]);
board[i][j]=-1;
}
}
for(int j=0;j<4;j++){
if(jl.empty()){
break;
}
board[i][j]=jl.front();
jl.pop();
}
}
}
else if(cz=='d'){
int lj;
for(int i=0;i<4;i++){
last=board[i][3];
lj=3;
for(int j=2;j>-1;j--){
if(board[i][j]<0){
continue;
}
if((board[i][j]>15&&board[i][j+1]<16)||(board[i][j]<16&&board[i][j+1]>15)){
bool f=1;
for(int sj=j;sj<4;sj++){
if(board[i][sj]<0){
f=0;
}
}
if(f){
able=1;
board[i][j+1]+=(board[i][j]-16);
board[i][j]=-1;
score+=pow(2,board[i+(board[i][j]>15?-1:1)][j]+1);
}
}
if(board[i][j]==last){
able=1;
board[i][j]=-1;
score+=(board[i][lj]<16?pow(2,(++board[i][lj])+1):0);
}
last=board[i][j];
lj=j;
}
}
for(int i=0;i<4;i++){
queue<int> jl;
for(int j=3;j>-1;j--){
if(board[i][j]^-1){
jl.push(board[i][j]);
board[i][j]=-1;
}
}
for(int j=3;j>-1;j--){
if(jl.empty()){
break;
}
board[i][j]=jl.front();
jl.pop();
}
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(board[i][j]^bf[i][j]){
able=1;
}
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(board[i][j]<0){
had[i][j]=0;
}
else{
had[i][j]=1;
}
}
}
if(able){
makerxy();
fill(rx,ry,newblock());
}
resco();
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(board[i][j]^-1&&board[i][j]<16){
tilmax=max(tilmax,board[i][j]);
}
}
}
if(tilmax==15){
state=2;
}
else if(scobf<score){
scop=score-scobf;
state=1;
}
else if(cz=='`'){
state=3;
}
maindisplay();
able=0;
if(state==2||state==3){
break;
}
}
setsize(70,33);
maindisplay();
return 0;
}
/*
https://apps.ak-ioi.com/oi-2048/
https://apps.ak-ioi.com/oi-2048/wiki/
CE 2 241,196,15-0.783
Judging 4 52,152,219-0.078
RE 8 142,68,173
TLE 16 46,70,140
MLE 32 46,70,140
ILE 64 46,70,140
OLE 128 46,70,140
UKE 256 52,73,94
WA 512 231,76,60
PC 1024 230,126,34
AC 2048 94,185,94
PE 4096 244,149,160
DoJ 8192 153,102,0
JF 16384 187,187,187
AU 32768 241,196,15
AK 65536 102,198,255
#pragma *1 0,0,0 -0.1118
O2 *2 178,178,178-0.0182
O3 *4 178,178,178
*/
0 条评论
目前还没有评论...