前言:
如今朋友们对“2048c语言源代码解释”都比较关怀,各位老铁们都想要分析一些“2048c语言源代码解释”的相关文章。那么小编同时在网上网罗了一些有关“2048c语言源代码解释””的相关资讯,希望大家能喜欢,姐妹们一起来学习一下吧!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(); } }
————————————————
小伙伴如果有不懂的问题可以随时私信我哈
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #2048c语言源代码解释