欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java控制臺版五子棋的簡單實現(xiàn)方法

 更新時間:2021年01月31日 13:48:05   作者:錢多多不多余  
這篇文章主要給大家介紹了關(guān)于Java控制臺版五子棋的簡單實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

設(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+"獲勝?。?!");
  		 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+"獲勝?。?!");
  		 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+"獲勝?。?!");
  		 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+"獲勝!??!");
  		 break;
 		   }
  		count=1;
  		}
 	 }
 	 
 	 
  } 
 }
 }

總結(jié)

到此這篇關(guān)于Java控制臺版五子棋的簡單實現(xiàn)方法的文章就介紹到這了,更多相關(guān)Java控制臺版五子棋內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IntelliJ IDEA下SpringBoot如何指定某一個配置文件啟動項目

    IntelliJ IDEA下SpringBoot如何指定某一個配置文件啟動項目

    這篇文章主要介紹了IntelliJ IDEA下SpringBoot如何指定某一個配置文件啟動項目問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • java算法之二分查找法的實例詳解

    java算法之二分查找法的實例詳解

    這篇文章主要介紹了java算法之二分查找法的實例詳解的相關(guān)資料,這里提供簡單實例幫助大家學習理解這部分內(nèi)容,需要的朋友可以參考下
    2017-08-08
  • Spring boot 總結(jié)之跨域處理cors的方法

    Spring boot 總結(jié)之跨域處理cors的方法

    本篇文章主要介紹了Spring boot 總結(jié)之跨域處理cors的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • logback使用MDCFilter日志過濾源碼解讀

    logback使用MDCFilter日志過濾源碼解讀

    這篇文章主要介紹了logback使用MDCFilter日志過濾源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Spring中的@ComponentScan注解使用詳解

    Spring中的@ComponentScan注解使用詳解

    這篇文章主要介紹了Spring中的@ComponentScan注解使用詳解,@ComponentScan 注解的作用就是根據(jù)指定的掃描路徑,把路徑中符合掃描規(guī)則的類裝配到 Spring 容器中,需要的朋友可以參考下
    2024-01-01
  • Java基本類型包裝類概述與Integer類、Character類用法分析

    Java基本類型包裝類概述與Integer類、Character類用法分析

    這篇文章主要介紹了Java基本類型包裝類概述與Integer類、Character類用法,結(jié)合實例形式分析了java基本數(shù)據(jù)類型與字符串轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • 由淺到深帶你詳談Java實現(xiàn)數(shù)組擴容的三種方式

    由淺到深帶你詳談Java實現(xiàn)數(shù)組擴容的三種方式

    這篇文章主要詳細介紹了Java實現(xiàn)數(shù)組擴容的三種方式,新建一個數(shù)組,把原來數(shù)組的內(nèi)容搬到新數(shù)組中,使用system.arraycopy(),使用java.util.Arrays.copyOf()這三種方式,具有一定的參考價值,需要的朋友可以借鑒一下
    2023-06-06
  • 詳解javaweb中jstl如何循環(huán)List中的Map數(shù)據(jù)

    詳解javaweb中jstl如何循環(huán)List中的Map數(shù)據(jù)

    這篇文章主要介紹了詳解javaweb中jstl如何循環(huán)List中的Map數(shù)據(jù)的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • Java如何使用正則表達式從字符串中提取數(shù)字

    Java如何使用正則表達式從字符串中提取數(shù)字

    這篇文章主要介紹了Java如何使用正則表達式從字符串中提取數(shù)字問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java動態(tài)代理靜態(tài)代理實例分析

    Java動態(tài)代理靜態(tài)代理實例分析

    這篇文章主要介紹了Java動態(tài)代理靜態(tài)代理實例分析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03

最新評論