C语言简易2048
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string.h>
int b[10];
int a[6][6];
char c[20][7];
void PrintChar(char *ch,UINT count,UINT x,UINT y) //在坐标bai(x,y)处输出字符du串zhich,ch里有count个字符
{
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
ULONG unuse;
pos.X=x;
pos.Y=y;
CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口缓冲区信息
GetConsoleScreenBufferInfo(h, &bInfo );
WriteConsoleOutputCharacterA(h,ch,count,pos,&unuse);
}
int main()
{
int i,j,n,m,p=0,q=0,s,t,row,col,pd,zh,cx,k;
char key;
srand((unsigned int)time(0));//修改种子
i=rand()%4+1;
j=rand()%4+1;
m=rand()%4+1;
n=rand()%4+1;
a[i][j]=2;
a[m][n]=2;
for(i=1;i<=4;i++){
for(j=1;j<=4;j++){
if(a[i][j]!=0) PrintChar(" 2",5,6*j-5,i*3);
}
}
for(i=1;i<=5;i++){
for(j=2;j<=13;j++){
PrintChar("|",1,6*i-6,j);
}
}
for(i=0;i<=24;i++){
for(j=1;j<=14;j=j+3){
if(i%6) PrintChar("_",1,i,j);
}
}
// p:1 7 13 19 q:3 6 9 12
while(1){
key=getch();
if(key=='w'||key==72){
for(i=1;i<=4;i++){
for(j=1;j<=4;j++){
if(a[i][j]!=0){
p=i;
while(p<=3&&(a[p+1][j]==0||a[p+1][j]==a[i][j])){
if(a[p+1][j]==0) p++;
else{
a[i][j]=2*a[i][j];
a[p+1][j]=0;
p++;
}
}
}
}
}
for(i=1;i<=4;i++){
cx=1;
for(j=1;j<=4;j++){
if(a[j][i]!=0){
a[cx++][i]=a[j][i];
if(j!=(cx-1)) a[j][i]=0;
}
}
}
do{
i=rand()%4+1;
j=rand()%4+1;
}
while(a[i][j]!=0);
a[i][j]=(rand()%2+1)*2;
}
if(key=='s'||key==80){
for(i=4;i>=1;i--){
for(j=4;j>=1;j--){
if(a[i][j]!=0){
p=i;
while(p>=2&&(a[p-1][j]==0||a[p-1][j]==a[i][j])){
if(a[p-1][j]==0) p--;
else{
a[i][j]=2*a[i][j];
a[p-1][j]=0;
p--;
}
}
}
}
}
for(i=4;i>=1;i--){
cx=4;
for(j=4;j>=1;j--){
if(a[j][i]!=0){
a[cx--][i]=a[j][i];
if(j!=(cx+1)) a[j][i]=0;
}
}
}
do{
i=rand()%4+1;
j=rand()%4+1;
}
while(a[i][j]!=0);
a[i][j]=(rand()%2+1)*2;
}
if(key=='a'||key==75){
for(i=1;i<=4;i++){
for(j=1;j<=4;j++){
if(a[i][j]!=0){
p=j;
while(p<=3&&(a[i][p+1]==0||a[i][p+1]==a[i][j])){
if(a[i][p+1]==0) p++;
else{
a[i][j]=2*a[i][j];
a[i][p+1]=0;
p++;
}
}
}
}
}
for(i=1;i<=4;i++){
cx=1;
for(j=1;j<=4;j++){
if(a[i][j]!=0){
a[i][cx++]=a[i][j];
if(j!=(cx-1)) a[i][j]=0;
}
}
}
do{
i=rand()%4+1;
j=rand()%4+1;
}
while(a[i][j]!=0);
a[i][j]=(rand()%2+1)*2;
}
if(key=='d'||key==77){
for(i=1;i<=4;i++){
for(j=4;j>=1;j--){
if(a[i][j]!=0){
p=j;
while(p>=2&&(a[i][p-1]==0||a[i][p-1]==a[i][j])){
if(a[i][p-1]==0) p--;
else{
a[i][j]=2*a[i][j];
a[i][p-1]=0;
p--;
}
}
}
}
}
for(i=1;i<=4;i++){
cx=4;
for(j=4;j>=1;j--){
if(a[i][j]!=0){
a[i][cx--]=a[i][j];
if(j!=(cx+1)) a[i][j]=0;
}
}
}
do{
i=rand()%4+1;
j=rand()%4+1;
}
while(a[i][j]!=0);
a[i][j]=(rand()%2+1)*2;
}
for(i=1;i<=4;i++){
for(j=1;j<=4;j++){
c[i*4-4+j][0]=a[i][j]/10000+'0';
c[i*4-4+j][1]=a[i][j]/1000%10+'0';
c[i*4-4+j][2]=a[i][j]/100%10+'0';
c[i*4-4+j][3]=a[i][j]/10%10+'0';
c[i*4-4+j][4]=a[i][j]%10+'0';
k=0;
while(c[i*4-4+j][k]=='0'){
c[i*4-4+j][k]=' ';
k++;
}
PrintChar(c[i*4-4+j],5,6*j-5,i*3);
}
}
}
}