小游戏:2048 代码(乱)

#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <curses.h>
#define width 5
#define n 4
#define Gnormal 0
#define Gwin 1
#define Glose 2
#define Gquit 3
using namespace std;

class game2048
{
public:
	game2048() :status(Gnormal) {
		setdata();
	}

	int getstatus()
	{
		return status;
	}

	void iprocess()
	{
		char ch = getch();
		if ('a' <= ch&&ch <= 'z')
		{
			ch -= 32;
		}
		if(status==Gnormal)
		{ 
			if (ch == 'A')
			{
				moveleft();
			}
			else if (ch == 'W')
			{
				rotate();
				moveleft();
				rotate();
				rotate();
				rotate();
			}
			else if (ch == 'D')
			{
				rotate();
				rotate();
				moveleft();
				rotate();
				rotate();
			}
			else if (ch == 'S')
			{
				rotate();
				rotate();
				rotate();
				moveleft();
				rotate();
			}
			if (!ifwin())
			{
				if (!randnew())
					status = Glose;
				randnew();
			}
		}
		if (ch == 'Q')
		{
			status = Gquit;
		}
		else if (ch == 'R')
		{
			restart();
		}
	}

	void draw()
	{
		clear();
		const int offset = 12;
		for (int i = 0; i <= n; i++)
			for (int j = 0; j <= n; j++)
				drawitem(i * 2, offset + j*width, '+');
		for (int i = 0; i < n; i++)
			for (int j = 0; j <= n; j++)
				drawitem(1 + i * 2, offset + j*width, '|');
		for (int i = 0; i <= n; i++)
			for (int j = 0; j < n; j++)
				for (int k = 1; k < 5; k++)
					drawitem(2 * i, offset + k + j*width, '-');
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				drawnum(i * 2 + 1, j*width + 4 + offset, data[i][j]);
		mvprintw(2 * n + 2, offset / 2, "W(UP),S(DOWN),A(LEFT),D(RIGHT),R(RESTART),Q(QUIT)");
		mvprintw(2 * n + 3, offset + 10, "my first game");
		if (status == Gwin) {
			mvprintw(n, offset, " YOU WIN,PRESS R TO CONTINUE ");
		}
		else if (status == Glose) {
			mvprintw(n, offset, " YOU LOSE,PRESS R TO CONTINUE ");
		}
	}
	void setdata()
	{
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				data[i][j] = 16 << i << j;
	}
	void drawitem(int row, int col, char ch)
	{
		move(row, col);
		addch(ch);
	}
	void drawnum(int row, int col, int num)
	{
		while (num != 0)
		{
			move(row, col--);
			addch(num % 10 + '0');
			num /= 10;
		}
	}
	void restart()
	{
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				data[i][j] = 0;
		randnew();
		randnew();
		status = Gnormal;
	}
	bool randnew()
	{
		vector<int> emptyxy;
		for (int i = 0; i<n; i++)
			for (int j = 0; j < n; j++)
			{
				if (data[i][j] == 0)
					emptyxy.push_back(i*n + j);
			}
		if (emptyxy.size() == 0)
			return false;
		int value = emptyxy[rand() % emptyxy.size()];
		data[value / n][value%n] = rand() % 10 == 1 ? 4 : 2;
		return true;
	}

	void moveleft()
	{
		int pvalue = 0;
		int cpos = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (data[i][j] == 0)
				{
					continue;
				}
				else
				{
					if (pvalue == 0)
					{
						pvalue = data[i][j];
						data[i][j] = 0;
					}
					else
					{
						if (pvalue == data[i][j])
						{
							data[i][cpos] = pvalue * 2;
							++cpos;
							pvalue = 0;
							data[i][j] = 0;
						}
						else
						{
							data[i][cpos] = pvalue;
							pvalue = data[i][j];
							data[i][j] = 0;
							++cpos;
						}
					}
				}
			}
			if (pvalue != 0)
				data[i][cpos] = pvalue;
			cpos = 0;
			pvalue = 0;
		}
	}

	bool ifwin()
	{
		bool res = false;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
			{
				if (data[i][j] == 1024)
				{
					res = true;
					status = Gwin;
				}
			}
		return res;
	}

	void rotate()
	{
		int tmp[n][n] = { 0 };
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < n; ++j)
				tmp[i][j] = data[j][n-i-1];
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < n; ++j)
				data[i][j] = tmp[i][j];
	}

private:
	int data[n][n];
	int status;
};


void initialize()
{
	initscr();
	cbreak();
	noecho();
	curs_set(0);
	srand(time(NULL));
}

void shutdown()
{
	endwin();
}

int main() {
	initialize();
	game2048 game;
	do {
		game.draw();
		game.iprocess();
	} while (Gquit != game.getstatus());
	shutdown();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很抱歉,我无法提供具体的GitHub链接,但是我可以为您介绍一下Java小游戏2048的源代码实现。 以下是一个简单的Java实现2048游戏的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Game2048 extends JFrame { private static final int SIZE = 4; private static final int TILE_SIZE = 100; private int[][] board; public Game2048() { setTitle("2048"); setSize(SIZE * TILE_SIZE, SIZE * TILE_SIZE); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); board = new int[SIZE][SIZE]; addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_UP) { // 处理向上移动的逻辑 } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { // 处理向下移动的逻辑 } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { // 处理向左移动的逻辑 } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { // 处理向右移动的逻辑 } repaint(); } }); setVisible(true); } @Override public void paint(Graphics g) { super.paint(g); for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { int value = board[row][col]; int x = col * TILE_SIZE; int y = row * TILE_SIZE; g.setColor(getTileColor(value)); g.fillRect(x, y, TILE_SIZE, TILE_SIZE); g.setColor(getTextColor(value)); g.drawString(String.valueOf(value), x + TILE_SIZE / 2, y + TILE_SIZE / 2); } } } private Color getTileColor(int value) { // 根据方块的值返回对应的颜色 } private Color getTextColor(int value) { // 根据方块的值返回对应的文字颜色 } public static void main(String[] args) { new Game2048(); } } ``` 这是一个简单的2048游戏的实现,使用了Java的Swing库来创建窗口和绘制游戏界面。游戏逻辑部分需要根据按键事件进行相应的处理,例如向上、向下、向左、向右移动方块。在绘制界面时,根据方块的值来确定方块的颜色和文字颜色。 如果您想查看更详细的源代码或者其他实现方式,建议您在GitHub上搜索相关关键词,例如"java 2048 game",会有很多开源项目提供源代码参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值