日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux控制台单人五子棋简书,Java控制台版五子棋的简单实现方法

發布時間:2025/3/20 linux 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux控制台单人五子棋简书,Java控制台版五子棋的简单实现方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這篇文章主要給大家介紹了關于Java控制臺版五子棋的簡單實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

設計一個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

for(int q=0;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

System.out.print(k+" ");

for(int q=0;q

System.out.print(qipan[k][q]);

}

System.out.println();

}//加入Java開發交流君樣:756584822一起吹水聊天

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

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

下標越界處理;

判斷此坐標有無棋子;

確保坐標輸入為數字。

int x,y;//儲存下棋坐標:

Scanner sc=new Scanner(System.in);

boolean flag=true;//區分黑白棋;

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]="○ ";

}//加入Java開發交流君樣:756584822一起吹水聊天

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

System.out.print(k+" ");

for(int q=0;q

System.out.print(qipan[k][q]);

}

System.out.println();

}

判斷是否五子連珠:

8個方向,4條線

上方&下方

左方&右方

左斜上&右斜下

右斜上&左斜下

//判斷是否五子連珠:

int count=1;

String currentZiQi=qipan[x][y];//儲存當前下的棋子;

//判斷上方://加入Java開發交流君樣:756584822一起吹水聊天

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;

}

}//加入Java開發交流君樣:756584822一起吹水聊天

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;

}

}

}

}

}

[

image

最新2020整理收集的一些高頻面試題(都整理成文檔),有很多干貨,包含mysql,netty,spring,線程,spring cloud、jvm、源碼、算法等詳細講解,也有詳細的學習規劃圖,面試題整理等,需要獲取這些內容的朋友請加Q君樣:756584822

總結

以上是生活随笔為你收集整理的linux控制台单人五子棋简书,Java控制台版五子棋的简单实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。