#include "my_getch.h"
#include "math.h"
#include "time.h"
#define SPACE () printf ("")
#define RED_NUM (N) printf ("\033[;31m%4d", (n))
#define GREEN_NUM (N) printf ("\033[;32m%4d", (n))
#define BLUE_NUM (N) printf ("\033[;34m%4d", (n))
#define YELLOW_NUM (N) printf ("\033[;33m%4d", (n))
#define PURPLE_NUM (N) printf ("\033[;35m%4d", (n))
#define DEEPGREEN_NUM (N) printf ("\033[;36m%4d", (n))
/* According to different number for different macro replacement, the output of different colors/
void printnum (const int num)
{
if (num==0)
Space ();
else if (num==1024 | | num==32)
Red_num (NUM);
else if (num==2 | | num==64)
Blue_num (NUM);
else if (num==4 | | num==128)
Green_num (NUM);
else if (num==8 | | num==256)
Yellow_num (NUM);
else if (num==16 | | num==512)
Purple_num (NUM);
Else
Deepgreen_num (NUM);
}
Enum Game_stat{playing,failed,exited,done};
Enum Cmd{up,down,left,right,quit,invalid};
enum cmd direction;
Short empty[16];
struct Game
{
int box[16];
Enum Game_stat Stat;
int step;
unsigned long int point;
}game;
void Init_game ()
{
int i;
for (i=0;i<16;i++)
game.box[i]=0;
game.stat=playing;
Game.step=0;
game.point=0;
}
* Check whether the game can continue * *
void Check_fail ()
{
int i,j;
for (i=0;i<4;i++)
for (j=0;j<3;j++)
if (game.box[i*4+j]==game.box[i*4+j+1])
Return
for (j=0;j<4;j++)
for (i=0;i<3;i++)
if (game.box[i*4+j]==game.box[(i+1) *4+j])
Return
game.stat=failed;
}
The probability ratio of/*2 and 4 appears to be 3/1*/
int Get2or4 ()
{
int X=rand ()%4;
Return x>3? 4:2;
}
/* Receive keyboard typing, the arrow keys are represented by three char type characters:
On: 27 91 65
Under: 27 91 66
Right: 27 91 67
Left: 27 91 68
*/
void Inputcmd ()
{
Char C=my_getch ();
if (c==27)
{
C=my_getch ();
if (c==91)
{
C=my_getch ();
if (c==65)
Direction=up;
if (c==66)
Direction=down;
if (c==67)
Direction=right;
if (c==68)
Direction=left;
printf ("%d", c);
}
}
else if (c== ' \ n ')
Direction=quit;
Else
Direction=invalid;
}
/* Retrieve empty, that is, the value of 0
*/
int Findpos ()
{
int i,count=0,pos;
for (i=0;i<16;i++)
Empty[i]=-1;
for (i=0;i<16;i++)
{
if (game.box[i]==0)
Empty[count++]=i;
}
if (count==0)
Return-1;//game over
Pos=empty[rand ()%count];
printf ("pos=%d\n", POS);
return POS;
}
int merge ()//Can not be moved when return-1
{
int box_4x4[4][4];
int i,j,deep;
int has_move=-1,merge_line=0;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
BOX_4X4[I][J]=GAME.BOX[I*4+J];
if (direction==right)
{
printf ("right");
for (i=0;i<4;i++)
{
deep=3;
merge_line=0;
for (j=3;j>=0;j--)
{
if (box_4x4[i][j]!=0)
{
BOX_4X4[I][DEEP]=BOX_4X4[I][J];
if (deep!=j) box_4x4[i][j]=0;
if (deep<3 && merge_line==0 && box_4x4[i][deep]==box_4x4[i][deep+1])
{game.point+=box_4x4[i][deep+1]; box_4x4[i][deep+1]*=2;
box_4x4[i][deep]=0; merge_line=1; }
else deep--;
}
}
}
}
if (direction==left)
{
printf ("left");
for (i=0;i<4;i++)
{
Deep=0;
merge_line=0;
for (j=0;j<4;j++)
{
if (box_4x4[i][j]!=0)
{
BOX_4X4[I][DEEP]=BOX_4X4[I][J];
if (deep!=j) box_4x4[i][j]=0;
if (deep>0 && merge_line==0 && box_4x4[i][deep]==box_4x4[i][deep-1])
{game.point+=box_4x4[i][deep-1]; box_4x4[i][deep-1]*=2;
box_4x4[i][deep]=0; merge_line=1; }
else deep++;
}
}
}
}
if (direction==up)
{
printf ("Up");
for (j=0;j<4;j++)
{
Deep=0;
merge_line=0;
for (i=0;i<4;i++)
{
if (box_4x4[i][j]!=0)
{
BOX_4X4[DEEP][J]=BOX_4X4[I][J];
if (deep!=i) box_4x4[i][j]=0;
if (deep>0 && merge_line==0 && box_4x4[deep][j]==box_4x4[deep-1][j])
{GAME.POINT+=BOX_4X4[DEEP-1][J]; box_4x4[deep-1][j]*=2;
box_4x4[deep][j]=0; merge_line=1; }
else deep++;
}
}
}
}
if (Direction==down)
{
printf ("Down");
for (j=0;j<4;j++)
{
merge_line=0;
deep=3;
for (i=3;i>=0;i--)
{
if (box_4x4[i][j]!=0)
{
BOX_4X4[DEEP][J]=BOX_4X4[I][J];
if (deep!=i) box_4x4[i][j]=0;
if (deep<3 && merge_line==0 && box_4x4[deep][j]==box_4x4[deep+1][j])
{game.point+=box_4x4[deep+1][j]; box_4x4[deep+1][j]*=2;
box_4x4[deep][j]=0; merge_line=1; }
else deep--;
}
}
}
}
for (i=0;i<4;i++)
for (j=0;j<4;j++)
if (Game.box[i*4+j]!=box_4x4[i][j])
{
GAME.BOX[I*4+J]=BOX_4X4[I][J];
has_move=1;
if (game.box[i*4+j]==2048) Game.stat=done;
}
return has_move;
}
void Drawbox ()
{
int *box=game.box;
printf ("\033[2j");//Clear screen
printf ("\033[2h");//Cursor Reset
printf ("\033[?25l");//Hide cursor
printf ("Steps:%8d points:%10lu \ n", game.step,game.point);
printf ("\033[;30m---------------------------------\ n");
printf ("\033[;30m| | | | |\n ");
printf ("| %4d | %4d | %4d | %4d |\n ", box[0],box[1],box[2],box[3]);
printf ("\033[;30m|");
Printnum (Box[0]); printf ("\033[;30m |");
Printnum (box[1]); printf ("\033[;30m |");
Printnum (box[2]); printf ("\033[;30m |");
Printnum (Box[3]); printf ("\033[;30m |\n");
printf ("\033[;30m| | | | |\n ");
printf ("\033[;30m+-------+-------+-------+-------+\n");
printf ("\033[;30m| | | | |\n ");
printf ("| %4d | %4d | %4d | %4d |\n ", box[4],box[5],box[6],box[7]);
printf ("\033[;30m|");
Printnum (Box[4]); printf ("\033[;30m |");
Printnum (Box[5]); printf ("\033[;30m |");
Printnum (Box[6]); printf ("\033[;30m |");
Printnum (Box[7]); printf ("\033[;30m |\n");
printf ("\033[;30m| | | | |\n ");
printf ("\033[;30m+-------+-------+-------+-------+\n");
printf ("\033[;30m| | | | |\n ");
printf ("| %4d | %4d | %4d | %4d |\n ", box[8],box[9],box[10],box[11]);
printf ("\033[;30m|");
Printnum (Box[8]); printf ("\033[;30m |");
Printnum (box[9]); printf ("\033[;30m |");
Printnum (box[10]); printf ("\033[;30m |");
Printnum (box[11]); printf ("\033[;30m |\n");
printf ("\033[;30m| | | | |\n ");
printf ("\033[;30m+-------+-------+-------+-------+\n");
printf ("\033[;30m| | | | |\n ");
printf ("| %4d | %4d | %4d | %4d |\n ", box[12],box[13],box[14],box[15]);
printf ("\033[;30m|");
Printnum (box[12]); printf ("\033[;30m |");
Printnum (box[13]); printf ("\033[;30m |");
Printnum (box[14]); printf ("\033[;30m |");
Printnum (box[15]); printf ("\033[;30m |\n");
printf ("\033[;30m| | | | |\n ");
printf ("\033[;30m---------------------------------\ n");
if (game.stat==failed)
printf ("oh,failed! Try again.\n ");
else if (game.stat==done)
printf ("Yeah,you won! \ n ");
Else
}
int main (int argc,char** argv)
{
int pwdlen=10;
int Newpos;
int has_merge=1;
init_game ();
srand (Time (0));
fflush (stdin);
while (1)
{
//printf ("\033[2j");//Clear Screen
//printf ("\033[2h");//cursor Reset
//printf ("\033[?25l");//Hide Cursor
Newpos=findpos ();
if (newpos==-1)
{
Check_fail ();
if (game.stat==failed)
break;
}
if (has_merge!=-1)
{
Game.box[newpos]=get2or4 () ;
}
Drawbox ();
Inputcmd (direction);
if (direction==quit)
Break
else if (direction==invalid)
Continue
Else
{
Has_merge=merge ();
if (game.stat==done) break;
if (has_merge!=-1) game.step++;
}
}
Drawbox ();
printf ("\033[;30m");
}
/*---------------------------------------
* * Copyright (c) 2013-3-2 Deltayang
* * e-mail:deltayang89@gmail.com
* * GETCH.C: Analog implementation getch ()
**--------------------------------------*/
#ifndef Mygetch_h
#define Mygetch_h
#include <stdio.h>
#include <termios.h>/Operation terminal
#include <unistd.h>
#include <assert.h>
#include <string.h>
Char My_getch ()
{
int c=0;
struct Termios org_opts, new_opts;
int res=0;
Reserved Terminal original settings
Res=tcgetattr (Stdin_fileno, &org_opts);
ASSERT (res==0);
Set terminal parameters from new
memcpy (&new_opts, &org_opts, sizeof (new_opts));
New_opts.c_lflag &= ~ (Icanon | ECHO | Echoe | Echok | echonl | Echoprt | Echoke | ICRNL);
Tcsetattr (Stdin_fileno, Tcsanow, &new_opts);
C=getchar ();
Restore Interrupt Settings
Res=tcsetattr (Stdin_fileno, Tcsanow, &org_opts); assert (res==0);
return C;
}
#endif