2048源码

本文分享了一段使用C语言编写的控制台2048小游戏源码,包括游戏初始化、显示、操作及判断等功能,并介绍了游戏结束条件。玩家通过方向键进行操作,当无空格且无法进行合并时游戏结束。
摘要由CSDN通过智能技术生成
自己写的一个控制台2048小游戏
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>  //提供 getch()
#include <windows.h>
#include <time.h> //提供 time() 用作随机数种子

int arr[4][4], copy[4][4]; //copy数组用于确认一次操作之后是否发生变化,如果没有变化则不生成新的数
int temp[4];
int empty;  //空格数,如果 empty = 0 那么将结束本次游戏

void init();  //初始化游戏
void show();  //将游戏画面输出
int isDifferent();  //判断输入的操作是否改变数组的值,若不改变,则不添加新的数
void op_up();  //上移操作
void op_down();  //下移操作
void op_left();  //左移操作
void op_right();  //右移操作
void addNum();  //添加新的数
void play();  //提供游戏操作输入

int main()
{
    printf("Enter any key to play game\n");
    getch();
    while (1)
    {
        system("cls");
        init();
        show();
        while (empty) play();
        printf("Game Over\n Please enter 'q' to quit, or any other keys to continue\n");
        if (getch() == 'q') exit(0);
    }
    return 0;
}

void init()
{
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            copy[i][j] = arr[i][j] = 0;
    
    srand(time(0));
    int x = rand() % 4;
    int y = rand() % 4;
    copy[x][y] = arr[x][y] = 2;  //在随机位置生成数字 2 作为开局
    empty = 15;  //更新 empty
}

void show()
{
    for (int i = 0; i < 4; i++)
    {
        printf("_________________________\n");
 
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值