C Language console version 2048 games _c language

Source: Internet
Author: User
Tags clear screen function definition rand

The effect is not good, forgive me, nothing has written a!!!

/** * @author Routh * @main. c * @date 2014, 4, * * #include <stdio.h> #include <stdlib.h> #include &l  t;conio.h> #include <windows.h>//console width #define CONSOLE_WIDTH #define Box_width int box[4][4] =
 
{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
 
The console output handle handle c_handle;
  void Setcursorposition (short x, short y) {static COORD C;
  c.x = X;
  C.y = Y;
SetConsoleCursorPosition (C_handle, C);
  } void Drawthegamebox () {printf ("%15s \ n", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s \ n", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", ""); printf ("%15s%8s%8s%8s%8s \ n", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s \ n", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s \ n", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
  printf ("%15s%8s%8s%8s%8s", "", "", "", "", "");
printf ("%15s \ n", "");} /** * Get a random position:r * the X:0xff & (R >> 4) * The y:0x0f & R */int random () {int i =
  0, j = 0, _index = 0, rrr = 0;
  int rand_arr[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  Srand ((unsigned) time (NULL)); Rand () for (i = 0; i < 4; i + +) {for (j = 0; J < 4; J + +) {if (box[i][j] = = 0) {Rand_arr[_index + +] = (0xFF & I << 4) |
      (0xf & J);
  }} if (_index = 0) {return-1;
Return Rand_arr[rand ()% _index];
  }/** the Clean array.**/int* alogs (int item[]) {int i = 0, j = 0;
  int tep[4] = {0, 0, 0, 0}, Tmp[4] = {0, 0, 0, 0};
    for (i = 0; i < 4; i + +) {if (Item[i]!= 0) {tep[j + +] = Item[i];
    for (i = 0; i < 3 i + +) {if (tep[0] = = 0) break;
      if (tep[i] = = tep[i + 1]) {tep[i] *= 2;
    Tep[i + 1] = 0;
  } j = 0;
    for (i = 0; i < 4; i + +) {if (Tep[i]!= 0) {tmp[j + +] = Tep[i];
} return (int *) (&AMP;TMP);
  }/** BOX can move.*/int validate (int item[]) {int i = 0;
    for (i = 0; i < 3; i + +) {if (Item[i]!= 0 && item[i] = = item[i + 1]) return 1;
  if (item[i] = = 0 && item[i + 1]!= 0) return 1;
return 0;
  int keydowncontrolx (int key) {int i = 0, j = 0;
  int *p; int tp[4] = {0, 0, 0, 0};
    Switch (key) {case A://up j = 0;
      for (i = 0; i < 4; i + +) {tp[0] = Box[0][i];
      TP[1] = Box[1][i];
      TP[2] = Box[2][i];
      TP[3] = Box[3][i];
      p = alogs (TP);
      if (!validate (TP)) J + +;
      Box[0][i] = p[0];
      Box[1][i] = p[1];
      Box[2][i] = p[2];
    Box[3][i] = p[3];
  Return J!= 4;
    Case://down j = 0;
      for (i = 0; i < 4; i + +) {tp[0] = Box[3][i];
      TP[1] = Box[2][i];
      TP[2] = Box[1][i];
      TP[3] = Box[0][i];
      p = alogs (TP);
      if (!validate (TP)) J + +;
      Box[3][i] = p[0];
      Box[2][i] = p[1];
      Box[1][i] = p[2];
    Box[0][i] = p[3];
  Return J!= 4;
    Case A://Left j = 0;
      for (i = 0; i < 4; i + +) {tp[0] = box[i][0];
      TP[1] = box[i][1];
      TP[2] = box[i][2];
      TP[3] = box[i][3];
      p = alogs (TP);
      if (!validate (TP)) J + +;
      Box[i][0] = p[0];
      BOX[I][1] = p[1];
      BOX[I][2] = p[2];
    BOX[I][3] = p[3];Return J!= 4;
    Case of://Right j = 0;
      for (i = 0; i < 4; i + +) {tp[0] = box[i][3];
      TP[1] = box[i][2];
      TP[2] = box[i][1];
      TP[3] = box[i][0];
      p = alogs (TP);
      if (!validate (TP)) J + +;
      BOX[I][3] = p[0];
      BOX[I][2] = p[1];
      BOX[I][1] = p[2];
    Box[i][0] = p[3];
  Return J!= 4;
  Default:return 0;
return 0;
  int main () {int i = 0, j = 0, r = 0;
  /** set the cursor, visible or invisible.*/console_cursor_info CCI = {1, 0};
  /** get the console output handle.*/C_handle = GetStdHandle (Std_output_handle);
  Hide the cursor.
  Setconsolecursorinfo (C_handle, &cci);
  Setconsoletextattribute (C_handle, 0x3);
  System ("color 30");
  Drawthegamebox ();
  r = Random ();
  if (rand ()% 2 = 0) {Box[0xff & (R >> 4)][0XF & R] = 2;
  else {Box[0xff & (R >> 4)][0XF & R] = 4; for (i = 0; i < 4; i + +) {for (j = 0; J < 4; J + +) {if (BOX[I][J] = = 0) continue;
      Setcursorposition (+ J * 8 + 2 + (J * 2), I * 4 + (i + 2));
      Setconsoletextattribute (C_handle, box[i][j]);
    printf ("%d", box[i][j]);
    }//Begin while (1) {sleep (100);
    Key down.
      while (_kbhit ()) {//the key down fun.
      if (!keydowncontrolx (_getch ())) continue;
      Clear the console and redraw.
      System ("CLS");
      Setconsoletextattribute (C_handle, 0x3);
      Drawthegamebox ();
          for (i = 0; i < 4; i + +) {for (j = 0; J < 4; J + +) {if (box[i][j] = 0) continue;
          Setcursorposition (+ J * 8 + 2 + (J * 2), I * 4 + (i + 2));
          Setconsoletextattribute (C_handle, box[i][j]);
        printf ("%d", box[i][j]);
      } r = Random ();
        if (r = = 1) {//Game over//setconsoletextattribute (C_handle, 0xff0000);
        Setcursorposition (27, 10);
      printf ("GAME orver!!!!!!!"); } if (rand ()% 2 = 0) {BOX[0xFF & (R >> 4)]
      [0xf & R] = 2;
      else {Box[0xff & (R >> 4)][0XF & R] = 4;
      Sleep (100); Setcursorposition (0xf & R) * 8 + 2 + ((0xf & R) * 2), (0xFF & (R >> 4)) * 4 + (0xFF & (R &G
      T;> 4)) (2));
      Setconsoletextattribute (C_handle, Box[0xff & (R >> 4)][0XF & R]);
 
    printf ("%d", Box[0xff & (R >> 4)][0XF & R]);
} return 0;
 }

Attach another small partner's code

* * Copyright (C) Judge Young * e-mail:yjjtc@126.com * version:1.0 * * * * #include <stdio.h> #include <time. H>/* contains the time () function required to set the seed for random numbers * * * * #include <conio.h> * contains the Getch () function that completes input characters without echo and carriage return confirmation on the Windows platform/#include <wind ows.h>/* Contains functions of setting output cursor position to clear screen function on Windows platform/Void Start_game (); /* Start game/void Reset_game (); 
/* Reset Game *//////////void Move_left () to the left and right four directions.
void Move_right ();
void Move_up ();

void Move_down ();  void Refresh_show ();  /* Refresh Interface display * * void add_rand_num (); /* Generate random number, this program only generates 2 or 4, the probability ratio is set to 2:1/void Check_game_over ();  /* Detect whether the game is lost, set the end of the game logo/int get_null_count ();   /* Get the number of positions above the game panel * * Int board[4][4];      /* Game Digital Panel, abstract as two-dimensional array */INT score;      /* Game of the points/int best; /* Game maximum score/int if_need_add_num;  /* Whether the need to generate a random number of flags, 1 for the need, 0 means that do not need * * int if_game_over; 

/* Whether the game is over flag, 1 means the game is over, 0 is normal////* Main function function definition */int main () {start_game ();}
  /* Start game function definition/void Start_game () {reset_game ();
  char cmd; while (1) {cmd = Getch ();/* receive the standard input stream character command */IF (if_game_over)/* To determine whether you need to have lost the game/{if (cmd = = ' Y ' | | | cmd = = ' y ')/* re-play Game * * Reset_game ();
      Continue
      else if (cmd = = ' n ' | |-cmd = = ' n ') * * exit/{return;
      else {continue; } if_need_add_num = 0;  /* First set not the default need to generate random numbers, when needed again set to 1 * */switch (CMD)/* command resolution, W,S,A,D character representative up and down about the command * * case ' a ': Case ' a ':
      75:move_left ();
    Break
      Case "s": Case ' s ': Case 80:move_down ();
    Break
      Case ' W ': Case ' W ': Case 72:move_up ();
    Break
      Case ' d ': Case ' d ': Case 77:move_right ();
    Break } score > Best? best = score:1;
      /* Break score Record */if (if_need_add_num)/* defaults to the need to generate random numbers also need to refresh the display, and vice versa * * Add_rand_num ();
    Refresh_show ();
  }}/* Reset game function definition */void Reset_game () {score = 0;
  If_need_add_num = 1;
  
  If_game_over = 0; * * To understand the game initialization when the number of two, there must be a 2, theTo randomly generate a 2, others are 0 */int n = rand ()% 16;
    for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {Board[i][j] = (n--= = 0? 2:0);
  
  }/* Front has generated a 2, where the regeneration into a random 2 or 4, and set the probability of generating 2 is 4 times * * Add_rand_num ();
  * When the interface is refreshed and displayed, the interface has two digits by default, while the rest is empty (value 0)/System ("CLS");
Refresh_show ();
  /* Generate random number function definition/void Add_rand_num () {Srand (time (0)); int n = rand ()% get_null_count ();/* Determines where the random number is generated in the empty position * * (int i = 0; i < 4; i++) {for (int j = 0; J < 4; j + +) {if (board[i][j] = = 0 && n--= 0)/* Locate the location to be generated */{BOARD[I][J] = (rand ()% 3? 2:4
      )/* Determine what value to generate, set the probability of generating 2 is 4 of the probability of twice times * * return;
  Get empty position quantity function definition/int get_null_count () {int n = 0;
    for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J +) {Board[i][j] = = 0 n++: 1;
} return N; }/* Check whether the game ends function definition/void Check_game_over () {for (int i = 0; i < 4; i++) {for (int j = 0; J &Lt 3; J + +) {/* Horizontal and vertical comparisons are equal to two elements next to each other, and if there is equal, the game does not end. */if (board[i][j] = = Board[i][j+1] | | board[j][i] = = Board[j+1][i
        ] {if_game_over = 0;
      Return
}} if_game_over = 1;
 * * * The following four functions, to achieve the next move up and down the digital panel change algorithm * left and right move the essence of the same, the difference is just the traversal direction of the column item the same as the nature of the move on and off, the difference being just the way the line item is traversing, the essence of the left and the up movement is the same, the difference is only the Diachronic row and column swaps ///////////////* Left function definition/void Move_left () {/* variable i is used to traverse the subscript of the line item, and all rows are independent of each other when moving, and do not affect/for (int i = 0; i < 4; i++) * Variable j is the column subscript, the variable k is the subscript of the item to be compared (merged), K&LT;J * for (int j = 1, k = 0; J < 4; J + +) {if (Board[i][j] > 0)/*
          Find the first item that is not empty after K, the subscript is J, then three kinds of cases * * * (board[i][k] = = Board[i][j])/* Condition 1:k and J are equal, then merge the squares and score * *
          Score + = board[i][k++] <<= 1;
          BOARD[I][J] = 0; If_need_add_num = 1;
          /* need to generate random number and Refresh interface/else if (board[i][k] = = 0)/* Condition 2:k Item is empty, then the J is assigned to K, the equivalent of J Block moved to K Square * *
          BOARD[I][K] = Board[i][j];
          BOARD[I][J] = 0; If_need_add_num = 1;
          else//Condition 3:K item is not empty, and the J item is not equal, at this time the J item is assigned to the K+1 item, which is equivalent to moving to the k+1 position * * * board[i][++k] = board[i][j];
            if (j!= k)/* To determine whether the J and K items are previously together, if it is not to assign a value of J is null (value 0)/{board[i][j] = 0;
          If_need_add_num = 1; /}}}}/* Right shift function definition/void Move_right () {//* in the left-shift operation, the difference is just that J and K are all backwards traversal * * for (int i = 0; i < 4 ;  i++) {for (int j = 2, k = 3; j >= 0; j--) {if (Board[i][j] > 0) {if (board[i][k) = =
          Board[i][j]) {score + = board[i][k--] <<= 1;
          BOARD[I][J] = 0;
        If_need_add_num = 1;
          else if (board[i][k] = = 0) {Board[i][k] = board[i][j];
          BOARD[I][J] = 0;
        If_need_add_num = 1;
          else {Board[i][--k] = board[i][j];
            if (j!= k) {board[i][j] = 0;
          If_need_add_num = 1;
    }
        }  The UP function definition/void move_up () {*/* is modeled as the left shift operation, and the difference is only the row and column interchange after traversal/for (int i = 0; i < 4; i++) {A for ( Int J = 1, k = 0; J < 4; J + +) {if (Board[j][i] > 0) {if (board[k][i] = = Board[j][i]) {score = = Boar
          D[k++][i] <<= 1;
          Board[j][i] = 0;
        If_need_add_num = 1;
          else if (board[k][i] = = 0) {Board[k][i] = Board[j][i];
          Board[j][i] = 0;
        If_need_add_num = 1;
          else {Board[++k][i] = Board[j][i];
            if (j!= k) {board[j][i] = 0;
          If_need_add_num = 1; }}}}}//Down function definition */void Move_down () {/* follows the left shift operation, the difference is only the row and column interchange after traversal, and J and K are all backwards traversal * * for (int i = 0; I < 4;  i++) {for (int j = 2, k = 3; j >= 0; j--) {if (Board[j][i] > 0) {if (board[k][i) = = Board[j][i]) {score = Board[k--][i] <<= 1;
          Board[j][i] = 0;
        If_need_add_num = 1;
          else if (board[k][i] = = 0) {Board[k][i] = Board[j][i];
          Board[j][i] = 0;
        If_need_add_num = 1;
          else {Board[--k][i] = Board[j][i];
            if (j!= k) {board[j][i] = 0;
          If_need_add_num = 1; "}}}}}/* Refresh interface function definition/void Refresh_show () {/* Reset cursor Output position way clear screen can reduce flicker, system (" CLS ") is alternate screen command, all win
  Dows Platform Related * * COORD pos = {0, 0};
  
  SetConsoleCursorPosition (GetStdHandle (Std_output_handle), POS);
  printf ("\n\n\n\n");
  printf ("game:2048 SCORE:%06d best:%06d\n", SCORE, Best);
  
  printf ("--------------------------------------------------\ n \ nthe");
  * * Draw table and digital/printf ("┌──┬──┬──┬──┐\n");
    for (int i = 0; i < 4; i++) {printf ("│"); for (int j = 0; J < 4; J +) {if (Board[i][j]!= 0) {if (BOARD[I][J] <) {printf ("%d│", Board[i][j]);
        else if (Board[i][j] <) {printf ("%d│", Board[i][j]);
        else if (Board[i][j] < 1000) {printf ("%d│", Board[i][j]);
        else if (Board[i][j] < 10000) {printf ("%4d│", Board[i][j]);
          else {int n = board[i][j];
            for (int k = 1; k < k++) {n >>= 1;
            if (n = = 1) {printf ("2^%02d│", k);//* More than four digits are represented by a power form of 2, such as the 2^13 form/break;
    else printf ("│");
    } if (I < 3) {printf ("\n├──┼──┼──┼──┤\n");
    else {printf ("\n└──┴──┴──┴──┘\n");
  printf ("\ n");
  printf ("--------------------------------------------------\ n"); printf ("w↑a← →d↓s ");
    if (get_null_count () = = 0) {check_game_over (); if (if_game_over)/* To determine whether to lose the game/{printf ("\ r game over! TRY the GAME AGAIN?
    [y/n] "); }
  }
}

The above is the entire contents of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.