2048游戏编程(2048游戏源码分享)

import java.util.Random; import java.util.Scanner; public class game_2048 { public static int score = 0; public static Random random = new Random(); public static void print(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int i = 0;i < n;i ){ for(int j = 0;j < m;j ){ if(arr[i][j] > 10){ System.out.print(arr[i][j] " "); }else { System.out.print(arr[i][j] " "); } } System.out.println(); } } public static String input(Scanner sc) { System.out.println("please input:(w:up, s:down, a:left, d:right)"); String input = sc.next(); return input; } public static boolean isFinish(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int i = 0;i < n;i ){ for(int j = 0;j < m-1;j ){ if ((arr[i][j] == 0) || (arr[i][j] == arr[i][j 1])) { return false; } } } for(int j = 0;j < m;j ){ for(int i = 0;i < n-1;i ){ if ((arr[i][j] == 0) || (arr[i][j] == arr[i 1][j])) { return false; } } } return true; } public static int makeData(Random random) { int r = random.nextInt(10); if(r >= 5){ return 2; } return 0; } public static void moveUp(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int j = 0;j < m;j ){ //上移 for(int i = 0;i < n;i ){ if(arr[i][j] == 0){ for(int k = i 1;k < n;k ){ if(arr[k][j] != 0){ arr[i][j] = arr[k][j]; arr[k][j] = 0; break; } } } } //归并 for(int i = 0;i < n-1;i ){ if(arr[i][j] == arr[i 1][j]){ arr[i][j] *= 2; arr[i 1][j] = 0; score = arr[i][j]; } } //产生随机数 for(int i = n-1;i >= 0;i--){ if(arr[i][j] == 0){ arr[i][j] = makeData(random); } } } } public static void moveDown(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int j = 0;j < m;j ){ //下移 for(int i = n-1;i >= 0;i--){ if(arr[i][j] == 0){ for(int k = i-1;k >= 0;k--){ if(arr[k][j] != 0){ arr[i][j] = arr[k][j]; arr[k][j] = 0; break; } } } } //归并 for(int i = n-1;i > 0;i--){ if(arr[i][j] == arr[i-1][j]){ arr[i][j] *= 2; arr[i-1][j] = 0; score = arr[i][j]; } } //产生随机数 for(int i = 0;i < n;i ){ if(arr[i][j] == 0){ arr[i][j] = makeData(random); } } } } public static void moveLeft(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int i = 0;i < n;i ){ //左移 for(int j = 0;j < m;j ){ if(arr[i][j] == 0){ for(int k = j 1;k < m;k ){ if(arr[i][k] != 0){ arr[i][j] = arr[i][k]; arr[i][k] = 0; break; } } } } //归并 for(int j = 0;j < m-1;j ){ if(arr[i][j] == arr[i][j 1]){ arr[i][j] *= 2; arr[i][j 1] = 0; score = arr[i][j]; } } //产生随机数 for(int j = m-1;j >= 0;j--){ if(arr[i][j] == 0){ arr[i][j] = makeData(random); } } } } public static void moveRight(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int i = 0;i < n;i ){ //左移 for(int j = m-1;j >= 0;j--){ if(arr[i][j] == 0){ for(int k = j-1;k >= 0;k--){ if(arr[i][k] != 0){ arr[i][j] = arr[i][k]; arr[i][k] = 0; break; } } } } //归并 for(int j = m-1;j > 0;j--){ if(arr[i][j] == arr[i][j-1]){ arr[i][j] *= 2; arr[i][j-1] = 0; score = arr[i][j]; } } //产生随机数 for(int j = 0;j < m;j ){ if(arr[i][j] == 0){ arr[i][j] = makeData(random); } } } } public static void run() { Scanner sc = new Scanner(System.in); int[][] arr = new int[4][4]; while(true){ if (!isFinish(arr)) { String input = input(sc); switch (input) { case "w": moveUp(arr); break; case "s": moveDown(arr); break; case "a": moveLeft(arr); break; case "d": moveRight(arr); break; default: break; } print(arr); }else { System.out.println("score:" score); break; } } } public static void main(String[] args) { run(); } } ,我来为大家科普一下关于2048游戏编程?以下内容希望对你有帮助!

2048游戏编程(2048游戏源码分享)

2048游戏编程

import java.util.Random; import java.util.Scanner; public class game_2048 { public static int score = 0; public static Random random = new Random(); public static void print(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int i = 0;i < n;i ){ for(int j = 0;j < m;j ){ if(arr[i][j] > 10){ System.out.print(arr[i][j] " "); }else { System.out.print(arr[i][j] " "); } } System.out.println(); } } public static String input(Scanner sc) { System.out.println("please input:(w:up, s:down, a:left, d:right)"); String input = sc.next(); return input; } public static boolean isFinish(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int i = 0;i < n;i ){ for(int j = 0;j < m-1;j ){ if ((arr[i][j] == 0) || (arr[i][j] == arr[i][j 1])) { return false; } } } for(int j = 0;j < m;j ){ for(int i = 0;i < n-1;i ){ if ((arr[i][j] == 0) || (arr[i][j] == arr[i 1][j])) { return false; } } } return true; } public static int makeData(Random random) { int r = random.nextInt(10); if(r >= 5){ return 2; } return 0; } public static void moveUp(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int j = 0;j < m;j ){ //上移 for(int i = 0;i < n;i ){ if(arr[i][j] == 0){ for(int k = i 1;k < n;k ){ if(arr[k][j] != 0){ arr[i][j] = arr[k][j]; arr[k][j] = 0; break; } } } } //归并 for(int i = 0;i < n-1;i ){ if(arr[i][j] == arr[i 1][j]){ arr[i][j] *= 2; arr[i 1][j] = 0; score = arr[i][j]; } } //产生随机数 for(int i = n-1;i >= 0;i--){ if(arr[i][j] == 0){ arr[i][j] = makeData(random); } } } } public static void moveDown(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int j = 0;j < m;j ){ //下移 for(int i = n-1;i >= 0;i--){ if(arr[i][j] == 0){ for(int k = i-1;k >= 0;k--){ if(arr[k][j] != 0){ arr[i][j] = arr[k][j]; arr[k][j] = 0; break; } } } } //归并 for(int i = n-1;i > 0;i--){ if(arr[i][j] == arr[i-1][j]){ arr[i][j] *= 2; arr[i-1][j] = 0; score = arr[i][j]; } } //产生随机数 for(int i = 0;i < n;i ){ if(arr[i][j] == 0){ arr[i][j] = makeData(random); } } } } public static void moveLeft(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int i = 0;i < n;i ){ //左移 for(int j = 0;j < m;j ){ if(arr[i][j] == 0){ for(int k = j 1;k < m;k ){ if(arr[i][k] != 0){ arr[i][j] = arr[i][k]; arr[i][k] = 0; break; } } } } //归并 for(int j = 0;j < m-1;j ){ if(arr[i][j] == arr[i][j 1]){ arr[i][j] *= 2; arr[i][j 1] = 0; score = arr[i][j]; } } //产生随机数 for(int j = m-1;j >= 0;j--){ if(arr[i][j] == 0){ arr[i][j] = makeData(random); } } } } public static void moveRight(int[][] arr) { int n = arr.length; int m = arr[0].length; for(int i = 0;i < n;i ){ //左移 for(int j = m-1;j >= 0;j--){ if(arr[i][j] == 0){ for(int k = j-1;k >= 0;k--){ if(arr[i][k] != 0){ arr[i][j] = arr[i][k]; arr[i][k] = 0; break; } } } } //归并 for(int j = m-1;j > 0;j--){ if(arr[i][j] == arr[i][j-1]){ arr[i][j] *= 2; arr[i][j-1] = 0; score = arr[i][j]; } } //产生随机数 for(int j = 0;j < m;j ){ if(arr[i][j] == 0){ arr[i][j] = makeData(random); } } } } public static void run() { Scanner sc = new Scanner(System.in); int[][] arr = new int[4][4]; while(true){ if (!isFinish(arr)) { String input = input(sc); switch (input) { case "w": moveUp(arr); break; case "s": moveDown(arr); break; case "a": moveLeft(arr); break; case "d": moveRight(arr); break; default: break; } print(arr); }else { System.out.println("score:" score); break; } } } public static void main(String[] args) { run(); } }

————————————————

小伙伴如果有不懂的问题可以随时私信我哈,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页