设为首页 加入收藏

TOP

c语言----<项目>_小游戏<2048>(一)
2019-01-26 00:09:01 】 浏览:180
Tags:语言 ----< 项目 > 小游戏 < 2048>

2048 小游戏 主要是针对逻辑思维的一个训练.

主要学习方面:1.随机数产生的概率.2.行与列在进行移动的时候几种情况.3.MessageBox的使用

#include <iostream>
#include <time.h>
#include <conio.h>
#include <windows.h>
using namespace std;
int board[4][4] = {0};                          //二维数组
int if_need_rand;                               //是否生成随机数
int if_game_over;                               //游戏结束
void showGame() {                               //打印边框
    system("cls");                              //刷新屏幕
    printf("┏━━━┳━━━┳━━━┳━━━┓\n");
    for (int i = 0; i < 4; i++) {
        printf("┃");
        for (int j = 0; j < 4;j++) {
            if (board[i][j]==0) {
            printf("   ┃");
            }
            else {
                printf("%3d┃",board[i][j]);
            }
        }
        if (i<3) {
            printf("\n┣━━━╋━━━╋━━━╋━━━┫\n");
        }
        else {
            printf("\n┗━━━┻━━━┻━━━┻━━━┛\n");
        }
    }
}
//设计随机数
void addRand() {
    //随机数种子
    srand((unsigned)time(NULL));
    int i, j;//行列
    while (1) {
        i = rand() % 4;
        j = rand() % 4;
        if (board[i][j]==0) {
            //三目运算符,几率时2分之一
            board[i][j] = (rand() % 3 ? 2 : 4);
            break;
        }
        else {
            continue;
        }
    }
}
//初始化
void initGame() {
    if_need_rand = 1;
    if_game_over = 0;
    int i, j;
    i = rand() % 4;
    j = rand() % 4;
    board[i][j] = 2;
    //将数组内值,赋值为0;重新初始化
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; j++) {
            board[
		    

i][j] = 0; } } addRand(); showGame(); } //方向分为上下左右 //左移 void moveleft() { for (int i = 0; i < 4;i++) { for (int j = 1, k = 0; j < 4;++j) { //先找到K项后面第一个不为0的项 if (board[i][j]!=0) { //分为三个情况.相等,k项是0,不等 //相等 if (board[i][k]==board[i][j]) { board[i][k++] <<= 1; //k++先赋值给k而后k++到下一个位置,进行判断 board[i][j] = 0; if_need_rand = 1; }else //k项是0 if (board[i][k]==0) { board[i][k] = board[i][j]; board[i][j] = 0; if_need_rand = 1; } //k项与j项不等,有两种情况,j项就在k项后边, //所以不能将j项赋值为0 else { board[i][++k] = board[i][j]; if (j!=k) { board[i][j] = 0; if_need_rand = 1; } } } } } } //右移 void moveRinght() { for (int i = 0; i < 4; i++) { for (int j = 2, k = 3; j >= 0; j--) { //先找到K项后面第一个不为0的项 if (board[i][j] != 0) { //分为三个情况.相等,k项是0,不等 //相等 if (board[i][k] == board[i][j]) { board[i][k--] <<= 1; board[i][j] = 0; if_need_rand = 1; } else //k项是0 if (board[i][k] == 0) { board[i][k] = board[i][j]; board[i][j] = 0; if_need_rand = 1; } //k项与j项不等,有两种情况,j项就在k项后边, //所以不能将j项赋值为0 else { board[i][--k] = board[i][j]; if (j != k) { board[i][j] = 0; if_need_rand = 1; } } } } } } //上移 void moveup() { for (int i = 0; i < 4; i++) { for (int j = 1, k = 0; j < 4; ++j) { //先找到K项后面第一个不为0的项 if (board[j][i] != 0) { //分为三个情况.相等,k项是0,不等 //相等 if (board[k][i] == board[j][i]) { board[k++][i] <<= 1; board[j][i] = 0; if_need_rand = 1; } else //k项是0 if (board[k][i] == 0) { board[k][i] = board[j][i]; board[j][i] = 0; if_need_rand = 1; } //k项与j项不等,有两种情况,j项就在k项后边, //所以不能将j项赋值为0 else { board[++k][i] = board[j][i]; if (j != k) { board[j][i] = 0; if_need_rand = 1; } } } } } } //下移 void moveDown() { for (int i = 0; i < 4; i++) { for (int j = 2, k = 3; j >=0; --j) { //先找到K项后面第一个不为0的项 if (board[j][i] != 0) { //分为三个情况.相等,k项是0,不等 //相等 if (board[k][i] == board[j][i]) { board[k--][i] <<= 1; board[j][i] = 0; if_need_rand = 1; } else //k项是0 if (board[k][i] == 0) { board[k][i] = board[j][i]; board[j][i] = 0; if_need_rand = 1; } //k项与j项不等,有两种情况,j项就在k项后边, //所以不能将j项赋值为0 else { board[--k][i] = board[j][i]; if (j != k) { board[j][i] = 0; if_need_rand = 1; } } } } } } //查看有没有空着的格子 int getNull() { int n = 0; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (board[i][j] == 0) { n++; } } } return n; } //比较相邻的两个数,是否相等 void gameOv
编程开发网
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇野指针与'关键字'NULL 下一篇Telnet模拟系统(Linux c)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }