溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如是使用Java實現(xiàn)五子棋小游戲

發(fā)布時間:2021-02-01 09:21:47 來源:億速云 閱讀:121 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)如是使用Java實現(xiàn)五子棋小游戲的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

設(shè)計一個10*10的棋盤:

行號、列號單獨輸出

package yu;

import java.util.Scanner;

public class WuZiQi {
	/*● 棋子1
 ○ 棋子2
	 * 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 String [] [] qipan=new String [10] [10];
 //初始化棋盤:
 for(int k=0;k<qipan.length;k++){
 	 for(int q=0;q<qipan[k].length;q++){
 		 qipan[k][q]="+ ";
 		 }
 }
 //輸出棋盤:
 System.out.print(" ");
 for(int i=0;i<10;i++){
 	 System.out.print(i+" ");
 }
 System.out.println();
 for(int k=0;k<qipan.length;k++){
 	 System.out.print(k+" ");
 	 for(int q=0;q<qipan[k].length;q++){
 		 System.out.print(qipan[k][q]);
 		 }
 	 System.out.println();
 }

輸入坐標下棋(x,y),并作容錯處理:

  1. 保證輸入的坐標是(x,y);

  2. 下標越界處理;

  3. 判斷此坐標有無棋子;

  4. 確保坐標輸入為數(shù)字。

int x,y;//儲存下棋坐標:
 Scanner sc=new Scanner(System.in);
 boolean flag=true;//區(qū)分黑白棋;
 while(true){
 System.out.println("請輸入坐標下棋,坐標格式(x,y)");
 String str=sc.nextLine();
 String [] str1=str.split(",");
  //容錯處理1
  if(str1.length!=2){
 	 System.out.println("坐標輸入錯誤,請重新輸入!!");
 	 
  }else{
  //容錯處理3
 	 try{
 		 x=Integer.parseInt(str1[0]);
    y=Integer.parseInt(str1[1]);
 	 }catch(Exception e){
 		 System.out.println("坐標輸入錯誤,請重新輸入!!");
 		 continue;
 	 }
 	 //容錯處理2--下標越界
 	 if(x>=10||y>=10){
 		 System.out.println("坐標輸入錯誤,請重新輸入!!");
 	 }else{
 		 //容錯處理--判斷當前位置是否有棋子:
 		 //黑白棋:
 		 if(qipan[x][y].equals("+ ")){
 			 if(flag){
 				 qipan[x][y]="● ";
 			 }else{
 				 qipan[x][y]="○ ";
 			 }
 			 flag=!flag;
 		 }else{
 			 System.out.println("當前位置已有棋子,請重新輸入坐標!!");
 			 continue;
 		 }
 		 
 		 //輸出棋盤:
 		  System.out.print(" ");
 		  for(int i=0;i<10;i++){
 		 	 System.out.print(i+" ");
 		  }
 		  System.out.println();
 		  for(int k=0;k<qipan.length;k++){
 		 	 System.out.print(k+" ");
 		 	 for(int q=0;q<qipan[k].length;q++){
 		 		 System.out.print(qipan[k][q]);
 		 		 }
 		 	 System.out.println();
 		  
 		  }

判斷是否五子連珠:

8個方向,4條線

  1. 上方&下方

  2. 左方&右方

  3. 左斜上&右斜下

  4. 右斜上&左斜下

//判斷是否五子連珠:
 		  int count=1;
 		  String currentZiQi=qipan[x][y];//儲存當前下的棋子;
 		 //判斷上方:
 		  for(int k=x-1;k>=0;k--){
 		 	 if(qipan[k][y].equals(currentZiQi)){
 		 		 count++;
 		 	 }else{
 		 		 break;
 		 	 }
 		 	 }
 		  if(count>=5){
 		 	System.out.println(currentZiQi+"獲勝?。?!");
 		 	break;
 		  }
 		 //判斷下方:
 		 for(int k=x+1;k<10;k++){
  		 if(qipan[k][y].equals(currentZiQi)){
  		  count++;
  		 }else{
  		  break;
  		 }
  		 }
  		if(count>=5){
  		 System.out.println(currentZiQi+"獲勝!?。?quot;);
  		 break;
 		   }
  		count=1;//重置count;
  	 //判斷左邊:
  		for(int k=y-1;k>=0;k--){
  		 if(qipan[x][k].equals(currentZiQi)){
  		  count++;
  		 }else{
  		  break;
  		 }
  		 }
  		if(count>=5){
  		 System.out.println(currentZiQi+"獲勝?。?!");
  		 break;
 		   }
  	 //判斷右邊:
  		for(int k=y+1;k<10;k++){
  		 if(qipan[x][k].equals(currentZiQi)){
  		  count++;
  		 }else{
  		  break;
  		 }
  		 }
  		if(count>=5){
  		 System.out.println(currentZiQi+"獲勝!??!");
  		 break;
 		   }
  		count=1;  	
  		//判斷左上斜邊:
  		for(int k=x-1,j=y-1;k>=0&&j>=0;k--,j--){
  			if(qipan[k][j].equals(currentZiQi)){
  		  count++;
  		 }else{
  		  break;
  		 }
     		 }
  		if(count>=5){
  		 System.out.println(currentZiQi+"獲勝!?。?quot;);
  		 break;
 		   }
  		//右下斜方:
  		for(int k=x+1,j=y+1;k<10&&j<10;k++,j++){
  			if(qipan[k][j].equals(currentZiQi)){
  		  count++;
  		 }else{
  		  break;
  		 }
  		 }
  		if(count>=5){
  		 System.out.println(currentZiQi+"獲勝?。?!");
  		 break;
 		   }
  		count=1;
  		//左下斜方:
  		for(int k=x-1,j=y+1;k>=0&&j<10;k--,j++){
  			if(qipan[k][j].equals(currentZiQi)){
  		  count++;
  		 }else{
  		  break;
  		 }
  		 }
  		if(count>=5){
  		 System.out.println(currentZiQi+"獲勝!?。?quot;);
  		 break;
 		   }  		
  		//右上斜方:
  		for(int k=x+1,j=y-1;k<10&&j>=0;k++,j--){
  			if(qipan[k][j].equals(currentZiQi)){
  		  count++;
  		 }else{
  		  break;
  		 }
  		 }
  		if(count>=5){
  		 System.out.println(currentZiQi+"獲勝?。。?quot;);
  		 break;
 		   }
  		count=1;
  		}
 	 }
 	 
 	 
  } 
 }
 }

感謝各位的閱讀!關(guān)于“如是使用Java實現(xiàn)五子棋小游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI