• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C语言控制台版2048小游戏

C语言控制台版2048小游戏

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c语言控制台,c语言控制台界面,c语言控制台程序,c语言控制台颜色,c语言控制台输入等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

效果不好,见谅,没事就写了一个!!!

/**
 * @author Routh
 * @main.c
 * @date 2014, 4, 26
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
// console width
#define CONSOLE_WIDTH 80
#define BOX_WIDTH 10
 
 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■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■■■■■■■■■■■■■■■■■■■■■\n", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■■■■■■■■■■■■■■■■■■■■■\n", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■■■■■■■■■■■■■■■■■■■■■\n", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  printf("%15s■%8s■%8s■%8s■%8s■\n", "", "", "", "", "");
  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 *)(&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 72: // 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 80: // 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 75: // 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 77: // 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(17 + 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(17 + 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(17 + (0xf & r) * 8 + 2 + ((0xf & r) * 2), (0xff & ( r >> 4)) * 4 + ((0xff & ( r >> 4)) + 2));
      //SetConsoleTextAttribute(c_handle, BOX[0xff & ( r >> 4)][0xf & r]);
      printf("%d", BOX[0xff & ( r >> 4)][0xf & r]);
 
    }
  }
 
  return 0;
}
</div>

附上另外一个小伙伴的代码

/*
 * Copyright (C) Judge Young
 * E-mail: yjjtc@126.com
 * Version: 1.0
 */

#include <stdio.h>
#include <time.h>  /* 包含设定随机数种子所需要的time()函数 */
#include <conio.h>  /* 包含Windows平台上完成输入字符不带回显和回车确认的getch()函数 */
#include <windows.h> /* 包含Windows平台上完成设定输出光标位置达到清屏功能的函数 */ 

void start_game(); /* 开始游戏 */
void reset_game(); /* 重置游戏 */

/* 往左右上下四个方向移动 */
void move_left(); 
void move_right();
void move_up();
void move_down();

void refresh_show();  /* 刷新界面显示 */
void add_rand_num();  /* 生成随机数,本程序中仅生成2或4,概率之比设为2:1 */
void check_game_over(); /* 检测是否输掉游戏,设定游戏结束标志 */
int get_null_count();  /* 获取游戏面板上空位置数量 */

int board[4][4];   /* 游戏数字面板,抽象为二维数组 */
int score;      /* 游戏的分 */
int best;      /* 游戏最高分 */
int if_need_add_num; /* 是否需要生成随机数标志,1表示需要,0表示不需要 *



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C语言之实现控制台光标随意移动的实例代码
  • C语言控制台版2048小游戏
  • 原创的C语言控制台小游戏

相关文章

  • 2017-05-28关于C++ string和c类型字符数组的对比
  • 2017-05-28深入理解C/C++中的写时拷贝
  • 2017-05-28浅析char 指针变量char *=p 这个语句的输出问题
  • 2017-05-28C 语言基础教程(我的C之旅开始了)[四]
  • 2017-05-28C语言中sizeof()与strlen()函数的使用入门及对比
  • 2017-05-28C++实现自顶向下的归并排序算法
  • 2017-05-28实例讲解C++编程中对设计模式中的原型模式的使用
  • 2017-05-28你必须知道的C语言预处理的问题详解
  • 2017-05-28用c语言实现2000内既能被3整除又能被7整除的个数
  • 2017-05-28VC实现屏幕截词功能的方法详解

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 剖析C++中的常量表达式与省略号的相关作用
    • wince禁止程序标题栏上的退出按钮示例
    • C++标准库中sstream与strstream的区别详细解析
    • 线程池的原理与实现详解
    • VC自定义消息响应函数postmessage用法示例
    • C++算法之海量数据处理方法的总结分析
    • C++实现数组的排序/插入重新排序/以及逆置操作详解
    • VC6.0如何创建以及调用动态链接库实例详解
    • c语言中十六进制转二进制显示的实现方法
    • C中qsort快速排序使用实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有