2048——Java控制台版本

2022-11-29 14:24:40 浏览数 (2)

前情提要,祝2021年的蓝桥杯小伙伴成绩突出。

贪心、杂凑、递归、分类、评分、哈希、共识、聚类、分治、启发式、粒子群、最大期望、最小完成时间、序列最小优化。。。。算法无数,但是基础类似,无非坐标处理。那么开启一个坐标小游戏2048吧~

一个简单的2048小游戏的操作提示——Java控制台版本:

代码语言:javascript复制
import java.util.Random;
import java.util.Scanner;

/**
 * 
 * @author 老师付 2021年5月29日15:05:21
 */
public class Action {
	static Random random = new Random();
	static Scanner scanner = new Scanner(System.in);

	private static boolean isNotFull(int[][] board) {
		for (int[] ia : board)
			for (int i : ia)
				if (i == 0)
					return true;
		return false;
	}

	private static void generate(int[][] board) {
		assert isNotFull(board);
		while (true) {
			int number = random.nextInt(16);
			int row = number / 4;
			int col = number % 4;
			if (board[row][col] == 0) {
				board[row][col] = System.currentTimeMillis() % 2 == 0 ? 2 : 4;
				break;
			}
		}
	}

	static int[][] init() {
		int[][] numArray = new int[4][4];
		generate(numArray);
		generate(numArray);
		return numArray;
	}

	static void showBoard(int[][] board) {
		for (int[] numArray : board) {
			for (int i : numArray) {
				if (i == 0) {
					System.out.printf("%4s", ".");
				} else {
					System.out.printf("%4s", i);
				}
			}
			System.out.println();
		}
	}

	static char acceptInput() {
		while (true) {
			String line = scanner.nextLine().toLowerCase();
			char direction = line.charAt(0);
			if ("wasdWASD".indexOf(direction) != -1) {
				return direction;
			}else {
				System.out.println("请按要求输入操作指令:");
				System.out.println("up:w/W");
				System.out.println("left:a/A");
				System.out.println("down:s/S");
				System.out.println("right:d/D");
			}
		}
	}

	static void downBlock(int[][] numArray) {
		for (int i = 0; i < 4; i  ) {
			for (int k = 0; k < 3; k  ) {
				for (int j = 3; j > 0; j--) {
					if (numArray[j][i] == 0) {
						numArray[j][i] = numArray[j - 1][i];
						numArray[j - 1][i] = 0;
					}
				}

				for (int j = 3; j > 0; j--) {
					if (numArray[j][i] == numArray[j - 1][i]) {
						numArray[j][i] = 2 * numArray[j][i];
						numArray[j - 1][i] = 0;
					}
				}
			}
		}
		generate(numArray);
	}

	static void upBlock(int[][] numArray) {
		for (int i = 0; i < 4; i  ) {
			for (int k = 0; k < 3; k  ) {
				for (int j = 0; j < 3; j  ) {
					if (numArray[j][i] == 0) {
						numArray[j][i] = numArray[j   1][i];
						numArray[j   1][i] = 0;
					}
				}

				for (int j = 0; j < 3; j  ) {
					if (numArray[j][i] == numArray[j   1][i]) {
						numArray[j][i] = 2 * numArray[j][i];
						numArray[j   1][i] = 0;
					}
				}
			}
		}
		generate(numArray);
	}

	static void leftBlock(int[][] numArray) {
		for (int i = 0; i < 4; i  ) {
			for (int k = 0; k < 3; k  ) {
				for (int j = 0; j < 3; j  ) {
					if (numArray[i][j] == 0) {
						numArray[i][j] = numArray[i][j   1];
						numArray[i][j   1] = 0;
					}
				}

				for (int j = 0; j < 3; j  ) {
					if (numArray[i][j] == numArray[i][j   1]) {
						numArray[i][j] = 2 * numArray[i][j];
						numArray[i][j   1] = 0;
					}
				}
			}
		}
		generate(numArray);
	}

	static void rightBlock(int[][] numArray) {
		for (int i = 0; i < 4; i  ) {
			for (int k = 0; k < 3; k  ) {
				for (int j = 3; j > 0; j--) {
					if (numArray[i][j] == 0) {
						numArray[i][j] = numArray[i][j - 1];
						numArray[i][j - 1] = 0;
					}
				}

				for (int j = 3; j > 0; j--) {
					if (numArray[i][j] == numArray[i][j - 1]) {
						numArray[i][j] = 2 * numArray[i][j];
						numArray[i][j - 1] = 0;
					}
				}
			}
		}
		generate(numArray);
	}

	static void process(int[][] board, char direction) {
		switch (direction) {
		case 'w':
			upBlock(board);
			break;
		case 'a':
			leftBlock(board);
			break;
		case 's':
			downBlock(board);
			break;
		case 'd':
			rightBlock(board);
			break;
		}
	}

	public static void main(String[] args) {
		System.out.println("游戏开始:请按照操作提示游戏:");
		System.out.println("up:w/W");
		System.out.println("left:a/A");
		System.out.println("down:s/S");
		System.out.println("right:d/D");
		int[][] board = init();
		while (true) {
			showBoard(board);
			char direction = acceptInput();
			process(board, direction);
		}
	}
}

玩法示例:输入大小写均可。

0 人点赞