Skip to content

Commit

Permalink
almost finish.
Browse files Browse the repository at this point in the history
Add vertical movement, mearge tile and score system.
  • Loading branch information
Silica163 committed May 6, 2023
1 parent ed98689 commit 4140d82
Showing 1 changed file with 100 additions and 21 deletions.
121 changes: 100 additions & 21 deletions 2048.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,79 @@
#define ROW 4
#define COL 4

void print_state(int (*state)[ROW][COL]);
void print_state(int (*state)[ROW][COL],int *score);
void createMap(int (*state)[ROW][COL]);
void move(int (*st)[ROW][COL],unsigned int dir);
void addEqual(int (*st)[ROW][COL],unsigned int dir,int * score);

int main(){
int game_state[ROW][COL];
createMap(&game_state);
print_state(&game_state);
int score = 0;
while(1){
unsigned int dir;
print_state(&game_state);
print_state(&game_state,&score);
printf("up[1] down[2] right[3] left[4] : ");
scanf("%u",&dir);
move(&game_state,dir);
print_state(&game_state,&score);
addEqual(&game_state,dir,&score);
print_state(&game_state,&score);
move(&game_state,dir);
}
return 0;
}

void print_state(int (*state)[ROW][COL]){
printf("+------+------+------+------+\n");
void addEqual(int (*st)[ROW][COL],unsigned int dir,int *score){

// up down right left
// 1 2 3 4
//move left

int up = dir ==1;
int down = dir == 2;
int right = dir == 3;
int left = dir == 4;

int cEdge1 = (COL - 1) * right;
int cEdge2 = (COL * left) - left + right ;
int rEdge1 = (ROW - 1) * down;
int rEdge2 = (ROW * up) - up + down;

// add horizon
for(int r = 0;r < ROW;r++){
for(
int c = cEdge1;
(c < cEdge2 && left) || (c >= cEdge2 && right);
c += left-right
){
if((*st)[r][c] == 0)continue;
if((*st)[r][c] == (*st)[r][c+left-right]){
(*st)[r][c] += (*st)[r][c+left-right];
(*st)[r][c+left-right] = 0;
*score += (*st)[r][c];
}
}
}

// add vertical
for(int c = 0;c < COL; c++){
for(
int r = rEdge1;
(r < rEdge2 && up) || (r >= rEdge2 && down);
r += up - down
){
if((*st)[r][c] == 0)continue;
if((*st)[r][c] == (*st)[r+up-down][c]){
(*st)[r][c] += (*st)[r+up-down][c];
(*st)[r+up-down][c] = 0;
*score += (*st)[r][c];
}
}
}
}
void print_state(int (*state)[ROW][COL],int *score){
printf("score : %d \n+------+------+------+------+\n",*score);
for(int r = 0;r < ROW;r++){
for(int c = 0 ;c < COL ; c++){
printf("|%6d",(*state)[r][c]);
Expand All @@ -34,9 +87,15 @@ void print_state(int (*state)[ROW][COL]){


void createMap(int (*state)[ROW][COL]){
int map[ROW][COL];
for(int r =0;r < ROW;r++)
for(int c = 0;c < COL;c++)
map[r][c] = 0;
map[1][1] = 2;
map[2][2] = 2;
for(int r = 0;r < ROW;r++){
for( int c = 0;c < COL;c++ ){
(*state)[r][c] = ( c << r) ^ (r << c);
(*state)[r][c] = map[r][c];
}
}
}
Expand All @@ -54,26 +113,46 @@ void move(int (*st)[ROW][COL],unsigned int dir){

int cEdge1 = (COL-1) * right;
int cEdge2 = COL * left ;
int rEdge1 = (ROW - 1) * down;
int rEdge2 = ROW * up;

// move horizontal
// move horizon
for(int r = 0 ; r < ROW ;r++){
for(
int c = cEdge1;
( c < cEdge2 && left) || (c >= cEdge2 && right);
c += (-1 * right) + (1 * left)
int c = cEdge1;
( c < cEdge2 && left) || (c >= cEdge2 && right);
c += (-1 * right) + (1 * left)
){
int *now = &(*st)[r][c];
if(*now != 0 && c != cEdge1 ){
for(
int c2 = cEdge1 ;
(c2 < c && left) || (c2 >= c && right );
c2 += (-1 * right) + (1 * left)
)if((*st)[r][c2] == 0){
(*st)[r][c2] = *now;
*now = 0;
break;
}
}
if(*now != 0 && c != cEdge1 )for(
int c2 = cEdge1 ;
(c2 *left) + (c * right) < (c2 * right) + (c * left);
c2 += (-1 * right) + (1 * left)
)if((*st)[r][c2] == 0){
(*st)[r][c2] = *now;
*now = 0;
break;
};
}
}

// move vertical
for(int c = 0; c < COL;c++){
for(
int r = rEdge1 ;
( r < rEdge2 && up) || (r >= rEdge2 && down);
r+= ( -1 * down) + ( 1 * up )
){
int *now = &(*st)[r][c];
if(*now != 0 && r != rEdge1)for(
int r2 = rEdge1;
(r2 * up) + (r * down) < (r2 * down) + (r * up);
r2 += (-1 * down) + (1 * up)
)if((*st)[r2][c] == 0){
(*st)[r2][c] = *now;
*now = 0;
break;
};
}
}
}
Expand Down

0 comments on commit 4140d82

Please sign in to comment.