1. 程式人生 > >用java做了個控制檯版的2048

用java做了個控制檯版的2048


玩幾局2048,理解下程式執行過程,程式碼挺簡單
但是創造2048這個遊戲的人很牛。

import java.util.Random;
import java.util.Scanner;


public class game2048 {
	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) {
			System.out.print("Input>");
			String line = scanner.nextLine();
			char direction = line.charAt(0);
			if ("WASD".indexOf(direction) != -1)
				return direction;
		}
	}

	/*static char getInput() {
		System.out.print("Input>");
		Scanner scanner = new Scanner(System.in);
		String input = scanner.nextLine();
		return input.charAt(0);
	}*/

	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) {
		int[][] board = init();
		while (true) {
			showBoard(board);
			char direction = acceptInput();
			process(board, direction);
		}
	}

}