java實現(xiàn)連連看游戲
更新時間:2019年01月05日 09:01:40 作者:Rindy_RR
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)連連看游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)連連看游戲的具體代碼,供大家參考,具體內(nèi)容如下
代碼會實現(xiàn)共享的,這個是截圖

代碼:
package com.lr.bean;
import java.util.Scanner;
import java.util.Random;
import com.lr.bean.Point;
public class Link{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("==========================");
System.out.println("\t\t連連看小游戲");
System.out.println("\t\t版權(quán):lr");
System.out.println("==========================");
System.out.println("請先輸入圖片數(shù)量(難度系數(shù) 1~9):");
int picnum=sc.nextInt();
System.out.println("請輸入棋盤的行數(shù):");
int rows=sc.nextInt();
int cols=0; //列數(shù)
int count=0; //計數(shù)器
do{
if (count>0){
System.out.println("輸入有誤,列數(shù)必須大于0!");
}
System.out.println("請輸入棋盤列數(shù):");
cols=sc.nextInt();
count++;
}while( cols<1 || cols%2!=0);
//創(chuàng)建二維數(shù)組,生成棋盤,列數(shù)+2的原因:做出邊框的效果 數(shù)組剛剛生成時,每個元素都是0
int[][] chessboard=new int[ rows+2 ][ cols+2];
//隨機生成的圖片數(shù)值存入這個二維數(shù)組中,注意:邊框不存值,任為0
initBoard( chessboard ,picnum);
System.out.println("初始化后的棋盤為:");
showBoard2( chessboard);
//打亂棋盤
shuffle( chessboard );
//輸出
System.out.println("打亂后的棋盤為:");
showBoard2( chessboard);
//實現(xiàn)消除業(yè)務(wù)
// 1.定義兩個Point對象
Point p1=new Point();
Point p2=new Point();
// 2.游戲狀態(tài) isGameOver
boolean isGameOver=false;
do{
// 3.循環(huán)輸入兩個點 do...while
System.out.println("請輸入兩個點的坐標(biāo)");
p1.x=sc.nextInt();
p1.y=sc.nextInt();
p2.x=sc.nextInt();
p2.y=sc.nextInt();
// 4.判斷這兩個數(shù)是否可以消除
if( isErazeOk( chessboard,p1,p2)){
//如果可以消除,將這兩個點在chessboard 中的值都設(shè)為0
chessboard[p1.x][p1.y]=0;
chessboard[p2.x][p2.y]=0;
if( checkGameOver( chessboard )){
isGameOver=true;
}
}
//顯示消除后的棋盤
showBoard2( chessboard );
}while( !isGameOver );
System.out.println("游戲結(jié)束!");
}
//判斷是否能消除的業(yè)務(wù)
public static boolean isErazeOk(int[][] chessboard ,Point p1,Point p2){
// 1.兩個點不是同一個
if( p1.equals( p2) ){
System.out.println("輸入的兩個點位置不能相同!");
}
// 2。兩個點的值是否相等
if(chessboard[p1.x][p1.y] !=chessboard[p2.x][p2.y]){
System.out.println("輸入的兩個點值不相同!請重新輸入");
return false;
}
// 3.判斷兩個點的連線情況
if( doOneLine(chessboard,p1,p2) || doTwoLine(chessboard,p1,p2) || doThreeLine(chessboard,p1,p2)){
return true;
}
return false;
}
// 1連線
public static boolean doOneLine(int[][] chessboard,Point p1,Point p2){
//定義最大值和最小值
int max=0;
int min=0;
//判斷是循環(huán)行還是循環(huán)列
if( p1.x==p2.x){
//找y的最大值、找y的最小值、循環(huán)從 min+1 至 max-1、判斷是否為0、如果中間有一個不為0,則返回 false
max=p1.y>p2.y?p1.y:p2.y;
min=p1.y<p2.y?p1.y:p2.y;
for(int i=min+1;i<max;i++){
if(chessboard[p1.x][i]!=0){
return false;
}
}
return true;
}else if( p1.y==p2.y){
//找x的最大值、找x的最小值、循環(huán)從 min+1 至 max-1、判斷是否為0、如果中間有一個不為0,則返回 false
max=p1.x>p2.x?p1.x:p2.x;
min=p1.x<p2.x?p1.x:p2.x;
for(int i=min+1;i<max;i++){
if(chessboard[i][p1.y]!=0){
return false;
}
}
return true;
}
return false;
}
// 2連線
public static boolean doTwoLine(int[][] chessboard,Point p1,Point p2){
//定義兩個臨時點
Point t1=new Point();
t1.x=p1.x;
t1.y=p2.y;
Point t2=new Point();
t2.x=p2.x;
t2.y=p1.y;
if( chessboard[t1.x][t1.y]==0 && doOneLine(chessboard, p1,t1 ) && doOneLine(chessboard, t1,p2) ){
return true;
}
if( chessboard[t2.x][t2.y]==0 && doOneLine(chessboard, p1,t2 ) && doOneLine(chessboard, t2,p2) ){
return true;
}
return false;
}
// 3連線
public static boolean doThreeLine(int[][] chessboard,Point p1,Point p2){
//先循環(huán)行 :x
for( int i=0;i<chessboard.length;i++){
Point t=new Point();
t.x=i;
t.y=p1.y;
if( chessboard[t.x][t.y]==0 && doOneLine(chessboard,t, p1) && doTwoLine(chessboard, t,p2) ){
return true;
}
}
//再循環(huán)列 :y
for( int i=0;i<chessboard.length;i++){
Point t=new Point();
t.x=p1.x;
t.y=i;
if( chessboard[t.x][t.y]==0 && doOneLine(chessboard,t, p1) && doTwoLine(chessboard, t,p2) ){
return true;
}
}
return false;
}
//判斷游戲是否結(jié)束:循環(huán)這個數(shù)組,判斷所有的位置都為0
public static boolean checkGameOver(int[][] chessboard){
for(int i=1;i<chessboard.length-1;i++){
for(int j=1;i<chessboard[i].length-1;j++){
if( chessboard[i][j]!=0 ){
return false;
}
}
}
return true;
}
public static void initBoard(int[][] chessboard,int picnum){
Random r=new Random();
for( int i=1;i<chessboard.length-1;i++){
for(int j=1;j<chessboard[i].length-1;j=j+2){
int pic=r.nextInt( picnum )+1;
chessboard[i][j]=pic;
chessboard[i][j+1]=pic;
}
}
}
//打亂棋盤,只能對中間的值打亂,而不能打擾邊框
//交換數(shù)組的兩個值 ,隨機生成的四個下標(biāo),每個下標(biāo)表示一個數(shù) x1,y1 x2,y2 =》 chessboard【x2】【y1】 將上面兩個數(shù)交換
//概率:棋盤越大,交換越多
public static void shuffle(int[][] chessboard ){
Random r=new Random();
int x1=0;
int y1=0;
int x2=0;
int y2=0;
int temp=0;
for(int i=0;i<chessboard.length*chessboard[0].length*10;i++){
//生成的四個下標(biāo),不能為0,也不能為 length-1
x1=r.nextInt( chessboard.length-2 )+1;
y1=r.nextInt( chessboard[0].length-2 )+1;
x2=r.nextInt( chessboard.length-2 )+1;
y2=r.nextInt( chessboard[0].length-2 )+1;
//完成交換
temp=chessboard[x1][y1];
chessboard[x1][y1]=chessboard[x2][y2];
chessboard[x2][y2]=temp;
}
}
//簡單的輸出
public static void showBoard(int[][] chessboard){
for(int i=0;i<chessboard.length;i++){
for(int j=0;j<chessboard[i].length;j++){
System.out.print(chessboard[i][j]+"\t");
}
System.out.println();
}
}
//私有方法:專門用來輸出棋盤最上面和最下面要出現(xiàn)的列號
private static void showColsNum( int[][] chessboard ){
for(int i=0;i<chessboard[0].length;i++){
if( i==0 || i==chessboard[i].length-1){
System.out.print("\t");
}else{
System.out.print("*"+i+"\t");
}
}
System.out.println();
}
//帶行列提示的輸出
public static void showBoard2( int[][] chessboard){
showColsNum( chessboard );//輸出error:列號
//中間完成棋盤
for(int i=0;i<chessboard.length;i++ ){
//加入前面行號的輸出
if( i==0 || i==chessboard[i].length-1){
System.out.print(" ");
}else{
System.out.print(""+i);
}
for(int j=0;j<chessboard[i].length;j++){
//邊框要用 * 來修飾
if( i==0 || j==0 || i==chessboard.length-1 || j==chessboard[i].length-1){
if( j==chessboard[i].length-1){
System.out.print(" # ");
}else{
System.out.print(" #\t");
}
}else{//不是邊框,就輸出數(shù)組中對應(yīng)的數(shù)字
if( chessboard[i][j]==0){
System.out.print(" \t");
}else{
System.out.print(" "+chessboard[i][j]+"\t");
}
}
}
//加入后面的行號
if( i==0 || i==chessboard.length-1){
System.out.print(" ");
}else{
System.out.print(""+i);
}
System.out.println();
}
showColsNum( chessboard );//輸出列號
}
}
Point類沒有寫出來了,比較簡單,里面就存了兩個數(shù)據(jù),表示數(shù)字的行和列,就不上圖了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC 中HttpMessageConverter簡介和Http請求415 的問題
本文介紹且記錄如何解決在SpringMVC 中遇到415 Unsupported Media Type 的問題,并且順便介紹Spring MVC的HTTP請求信息轉(zhuǎn)換器HttpMessageConverter2016-07-07
java實現(xiàn)word文件轉(zhuǎn)html文件
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)word文件轉(zhuǎn)html文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
SpringBoot+微信小程序?qū)崿F(xiàn)文件上傳與下載功能詳解
這篇文章主要為大家介紹了SpringBoot整合微信小程序?qū)崿F(xiàn)文件上傳與下載功能,文中的實現(xiàn)步驟講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧2022-03-03

