java控制臺(tái)實(shí)現(xiàn)拼圖游戲
本文實(shí)例為大家分享了java控制臺(tái)實(shí)現(xiàn)拼圖游戲的具體代碼,供大家參考,具體內(nèi)容如下
1、首先對(duì)原始的拼圖進(jìn)行多次無規(guī)律的移動(dòng),將拼圖打亂
2、然后進(jìn)行游戲,在游戲移動(dòng)同時(shí)對(duì)拼圖順序進(jìn)行判斷
①如果拼圖成功,則跳出循環(huán),結(jié)束游戲;
②如果拼圖失敗,陷入死循環(huán),繼續(xù)移動(dòng)拼圖,直至拼圖成功為止。
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)) {// 先判斷是否能夠移動(dòng),開始移動(dòng)拼圖
yiDon(x, y,a);
if (resultOfGrame(a)) {
System.out.println("游戲完成,恭喜你取得勝利!!");
printfResult(a);
break;
}
continue;
} else {
System.out.println("無法移動(dòng),請(qǐng)重新輸入移動(dòng)數(shù)字!");
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;
}
/**
* 移動(dòng)拼圖
*/
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;
}
/**
* 判斷是否能移動(dòng)
*/
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;
}
/**
* 產(chǎn)生[0,a)的一個(gè)隨機(jī)整數(shù)
*/
public static int runNum(int a) {
return random.nextInt(a);
}
/**
* 獲取要移動(dòng)數(shù)字的坐標(biāo)
*/
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;
}
}
/**
* 鍵盤接收要移動(dòng)的數(shù)字
* @return 1~8
*/
public static int event() {
System.out.println("請(qǐng)輸入要移動(dòng)的數(shù)字");
String output = scanner.nextLine();// 輸入失敗重寫輸入
if (output.length() == 1) {// 只能輸入一個(gè)字符
char s = output.charAt(0);// 只能輸入1~8
if (s > '0' && s < '9')
return (Integer.parseInt(output));
}
System.out.println("輸入不合法,請(qǐng)重新輸入??!");
return event();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java日志相關(guān)技術(shù)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java日志相關(guān)技術(shù)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理的相關(guān)資料,需要的朋友可以參考下2017-07-07
劍指Offer之Java算法習(xí)題精講鏈表與二叉樹專項(xiàng)訓(xùn)練
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03
Java圖形化界面設(shè)計(jì)之容器(JFrame)詳解
這篇文章主要介紹了Java圖形化界面設(shè)計(jì)之容器(JFrame)詳解,條理清晰,依次介紹了Java基本類(JFC),AWT和Swing的區(qū)別,Swing基本框架,圖形化設(shè)計(jì)步驟以及組件容器的使用等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Spring/Spring Boot 中優(yōu)雅地做參數(shù)校驗(yàn)拒絕 if/else 參數(shù)校驗(yàn)
這篇文章主要介紹了Spring/Spring Boot 中優(yōu)雅地做參數(shù)校驗(yàn)拒絕 if/else 參數(shù)校驗(yàn),本文使用最新的 Spring Boot 版本 2.4.5,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-04-04
Java實(shí)現(xiàn)讀取html文本內(nèi)容并按照格式導(dǎo)出到excel中
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)讀取html文本提取相應(yīng)內(nèi)容按照格式導(dǎo)出到excel中,文中的示例代碼講解詳細(xì),需要的可以參考下2024-02-02

