writing 2048 game using c language

I'm writing a simple game 2048 in c language. The pull left, pull right, pull up and pull down command is done. However, the most important part, i think, which is terminating the program when there is no room to generate a new random number and pull the number. Also i can't make the game board not generating a new number when there is no room to combine the same number and no room to pull the number.

here is my code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

if (gen_num == 0) {
            row = rand() % 4;
            column = rand() % 4;
            if (board[row][column] == 0) {
                board[row][column] = ((rand() % 2) + 1) * 2;

    valid_move = 0;
    gen_num = 0;

    cmd = read_command();
    if (cmd == 1){
    if (cmd == 2){
    if (cmd == 3){
    if (cmd == 4){
    if (cmd == 5){
        printf("bye :(\n");
    if (cmd < 1 || cmd>5){
        printf("[error] command is between 1 and 5\n\n");

    for (i = 0; i < 4; i++){
        for (j = 0; j < 4; j++){
            if (board[i][j] == check_move[i][j])

    printf("Valid_move = %d\n\n", valid_move);

    if (valid_move >= 15){
        printf("[Error] Invalid direction!!!\n\n");
        gen_num = 1;

pull_up, pull_down, pull_left, pull_right are the functions that i have set before the main function


Your code isn't complete, but from the looks of it you might end up not plaing a new tile at all, especially if the board is already very crowded. You should try to randomly place the tile as long as your position is invalid.

Of course, if there isn't any valid position, you will try over and over without success, so you should test whether the board is full, i.e. whether all tiles are non-zero.

You can combine plecing a new tile and checking whether the board is full. Instead of probing the board repeatedly, create a list of valid tiles and then pick one. If the list of valid tiles is empty, end the game.

A function that does this might look loike this:

int place_tile()
    int pos[16];
    int npos = 0;
    int i, j, p;

    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            if (board[j][i] == 0) pos[npos++] = j*4 + i;

    if (npos == 0) return 0;      // no more space

    p = pos[rand() % npos];
    j = p / 4;
    i = p % 4;

    board[j][i] = (rand() % 2) ? 2 : 4;
    return 1;

The return value indicates whether a tile was successfully placed (1) or not (0).

Need Your Help

Can the content of a email read receipt be set in the original email?

c# smtpclient

I use the following code to send a message and require a read receipt. Then I use outlook to open this message and send back the read receipt. The read receipt is received, my question is could I s...

String array not storing data from text file

java arrays split java.util.scanner

So I have some code that is supposed to use a jfilechooser get a text file use a split and scanner with a colon as a delimeter and store the data into an array. The code and text is as seen below. ...