亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

java控制臺實現拼圖游戲

瀏覽:7日期:2022-08-08 18:01:39

本文實例為大家分享了java控制臺實現拼圖游戲的具體代碼,供大家參考,具體內容如下

1、首先對原始的拼圖進行多次無規律的移動,將拼圖打亂

2、然后進行游戲,在游戲移動同時對拼圖順序進行判斷

①如果拼圖成功,則跳出循環,結束游戲;②如果拼圖失敗,陷入死循環,繼續移動拼圖,直至拼圖成功為止。

import java.util.Random;import java.util.Scanner;public class GrameOfPingTuTest{ private static Scanner scanner = new Scanner(System.in); private static int h = 2; private static int l = 2; private static int x = 0; private static int y = 0; private static Random random = new Random(); public static void main(String[] args) { int[][] youxi = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; initGrame(youxi); System.out.println('游戲開始!'); playGrame(youxi); } /** * 開始玩游戲 */ public static void playGrame(int[][] a) { int c = 0; while (true) { printfResult(a); c=event(); houQuXY(c,a); if (isDon(x, y)) {// 先判斷是否能夠移動,開始移動拼圖 yiDon(x, y,a); if (resultOfGrame(a)) { System.out.println('游戲完成,恭喜你取得勝利!!'); printfResult(a); break; } continue; } else { System.out.println('無法移動,請重新輸入移動數字!'); continue; } } } /** * 打印拼圖完成情況 * @param a */ public static void printfResult(int[][] a) { for (int[] i : a) { for (int j : i) System.out.print((j != 9 ? j : ' ') + 't'); System.out.println(); } } /** * 初始化游戲 * @param 打亂拼圖 */ public static void initGrame(int[][] a) { int m = 1; while (m <= 100) { while (true) { x = runNum(3); y = runNum(3); if (isDon(x, y)) break; } yiDon(x, y, a); m++; } } /** * 判斷游戲是否成功 * @return TRUE:游戲勝利 */ public static boolean resultOfGrame(int [][] a) { int c = 1; for (int[] b : a) for (int i : b) if (i != c++) return false; return true; } /** * 移動拼圖 */ public static void yiDon(int x, int y,int[][] a) { int m = a[h][l]; a[h][l] = a[x][y]; a[x][y] = m; h = x; l = y; } /** * 判斷是否能移動 */ public static boolean isDon(int x, int y) { if (h == x && Math.abs(l - y) == 1) return true; if (l == y && Math.abs(h - x) == 1) return true; return false; } /** * 產生[0,a)的一個隨機整數 */ public static int runNum(int a) { return random.nextInt(a); } /** * 獲取要移動數字的坐標 */ public static void houQuXY(int c,int[][] a) { for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) if (c == a[i][j]) { x = i; y = j; } } /** * 鍵盤接收要移動的數字 * @return 1~8 */ public static int event() { System.out.println('請輸入要移動的數字'); String output = scanner.nextLine();// 輸入失敗重寫輸入 if (output.length() == 1) {// 只能輸入一個字符 char s = output.charAt(0);// 只能輸入1~8 if (s > ’0’ && s < ’9’) return (Integer.parseInt(output)); } System.out.println('輸入不合法,請重新輸入!!'); return event(); } }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 日本欧美高清 | 天天综合色一区二区三区 | 亚洲一区图片 | 亚洲三级在线看 | 51国产午夜精品免费视频 | 国产97色在线 | 免费 | 国产成人免费片在线观看 | 中文字幕久久乱码一区二区 | 久久久精彩视频 | 日本美女毛片 | 97麻豆精品国产自产在线观看 | 国产精品视频白浆免费视频 | 亚洲第五色综合网 | 欧美一区二区三区在线 | 欧美日韩国产高清 | aaaaaa毛片免费看 | 国产精品视频久久久 | 免费污污视频网站 | 成人久久伊人精品伊人 | 一级a级国产不卡毛片 | 国产精品久久久亚洲456 | 成人满18在线观看网站免费 | 亚洲男人的天堂久久香蕉 | 精品综合久久久久久97超人 | 国产91精品高清一区二区三区 | 黑巨茎大战中国美女 | 青青草在线免费观看 | 在线观看搡女人 | 182午夜在线观看 | 国产黄色毛片视频 | 国产婷婷色一区二区三区深爱网 | 狠狠色丁香婷综合久久 | 美国一级黄色毛片 | 欧美久色| 国产大尺度吃奶无遮无挡 | 91精品影视 | 激情性生活视频在线播放免费观看 | 爱爱成人| 久久亚洲精品国产精品777777 | 国内精品视频在线播放一区 | 诱人的女邻居bd中文字幕 |