Java實(shí)現(xiàn)簡(jiǎn)單無(wú)界面五子棋
本文項(xiàng)目為大家分享了Java實(shí)現(xiàn)無(wú)界面五子棋的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目介紹:
本次設(shè)計(jì)是基于知識(shí)點(diǎn)Java類和對(duì)象以及數(shù)組開(kāi)發(fā)的一個(gè)小型五子棋游戲程序。游戲開(kāi)始時(shí),選擇黑棋、白棋開(kāi)局,將一枚棋子落在棋盤一坐標(biāo)上,然后輪番落子,如此輪流下子,直到某一方首先在棋盤的豎、橫或兩斜四方向上的五子連成線,則該方該局獲勝。
項(xiàng)目實(shí)現(xiàn)思路:
1、棋盤設(shè)計(jì)為10*10格,棋盤類型Chess[][] 二維數(shù)組,所含屬性String chessType; 棋盤首先chessType值是”?”。
2、初始化二維數(shù)組
3、玩家選擇黑白圈后,開(kāi)始下棋。輸入要下棋子的行列坐標(biāo),黑白棋子輪流落子,當(dāng)一方連成五子或下滿棋盤時(shí),游戲結(jié)束(連成五子的一方獲勝,下滿棋盤為和棋)。
4、每一次落子成功后,馬上判斷以該位置為中心的八個(gè)方向:上、下、左、右、左上、左下、右上、右下是否有相同顏色的棋子連成五子,如果連成五子,則游戲結(jié)束,輸出相應(yīng)的信息。
5、當(dāng)游戲一方勝利后顯示勝利信息。從程序表面看,這是一個(gè)二維平面圖,所以數(shù)據(jù)用二維數(shù)組來(lái)表示,數(shù)組兩個(gè)下標(biāo)可以表示棋盤上的位置,數(shù)組元素的值代表棋格上的狀態(tài),共有三種情況,分別是,?代表白棋,●代表黑棋,?代表格子。
源代碼
1.棋子
/** ?* @author hudongsheng ?* @date 2020/10/29 - 9:28 ?*/ public class ChessType { ? ? private String chessType; ? ? private int x; ? ? private int y; ? ? public int getX() { ? ? ? ? return x; ? ? } ? ? public void setX(int x) { ? ? ? ? this.x = x; ? ? } ? ? public int getY() { ? ? ? ? return y; ? ? } ? ? public void setY(int y) { ? ? ? ? this.y = y; ? ? } ? ? public ChessType() { ? ? } ? ? public String getChessType() { ? ? ? ? return chessType; ? ? } ? ? public void setChessType(String chessType) { ? ? ? ? this.chessType = chessType; ? ? } }
2.下棋
/** ?* @author hudongsheng ?* @date 2020/10/29 - 9:27 ?*/ public class Gobang { ? ? private int size = 1; ? ? private ChessType[][] chessTypes; ? ? private int row; ? ? private int colum; ? ? private int x; ? ? private int y; ? ? //創(chuàng)建一個(gè)棋盤 ? ? public Gobang(int row,int colum){ ? ? ? ? this.row = row; ? ? ? ? this.colum = colum; ? ? ? ? chessTypes = new ChessType[row][colum]; ? ? } ? ? //初始化棋盤 ? ? public void initChessType(){ ? ? ? ? for(int i = 0; i< chessTypes.length; i++){ ? ? ? ? ? ? for (int j = 0; j< chessTypes[i].length; j++){ ? ? ? ? ? ? ? ? chessTypes[i][j] = new ChessType(); ? ? ? ? ? ? ? ? chessTypes[i][j].setChessType("?"); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? //下白棋 ? ? public void setWhiteChess(int x,int y){ ? ? ? ? chessTypes[x][y].setChessType("?"); ? ? } ? ? //下黑棋 ? ? public void setBlackChess(int x,int y){ ? ? ? ? chessTypes[x][y].setChessType("●"); ? ? } ? ?//判斷是否勝利 ? ? ? ? public boolean checkWin(int i,int j) { ? ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? ? boolean flag = false; ? ? ? ? ? ? //判斷縱向是否有五個(gè)棋子是相同的顏色 ? ? ? ? ? ? int count1 = 1;//相同顏色棋子的個(gè)數(shù) ? ? ? ? ? ? String color = chessTypes[i][j].getChessType(); //剛下的棋子的顏色 ? ? ? ? ? ? int a = 1; ?//棋子索引的增量 ? ? ? ? ? ? while((i+a)<row && color == chessTypes[i+a][j].getChessType()){ ? ? ? ? ? ? ? ? count1++; ? ? ? ? ? ? ? ? a++; ? ? ? ? ? ? } ? ? ? ? ? ? a = 1; ? ? ? ? ? ? while((i-a)>=0 && color == chessTypes[i-a][j].getChessType()){ ? ? ? ? ? ? ? ? count1++; ? ? ? ? ? ? ? ? a++; ? ? ? ? ? ? } ? ? ? ? ? ? if(count1 >= 5){ ? ? ? ? ? ? ? ? flag = true; ? ? ? ? ? ? } ? ? ? ? ? ? //判斷縱向是否有五個(gè)棋子是相同的顏色 ? ? ? ? ? ? int count2 = 1; ? ? ? ? ? ? a = 1; ? ? ? ? ? ? while((j+a)<colum && color == chessTypes[i][j+a].getChessType()){ ? ? ? ? ? ? ? ? count2++; ? ? ? ? ? ? ? ? a++; ? ? ? ? ? ? } ? ? ? ? ? ? a = 1; ? ? ? ? ? ? while((j-a)>=0 && color == chessTypes[i][j-a].getChessType()){ ? ? ? ? ? ? ? ? count2++; ? ? ? ? ? ? ? ? a++; ? ? ? ? ? ? } ? ? ? ? ? ? if(count2 >= 5){ ? ? ? ? ? ? ? ? flag = true; ? ? ? ? ? ? } ? ? ? ? ? ? //右上 ? ?左下 是否有五個(gè)棋子是相同的顏色 ? ? ? ? ? ? int count3 = 1; ? ? ? ? ? ? a = 1; ? ? ? ? ? ? while((i+a)<row && (j-a)>=0 && color == chessTypes[i+a][j-a].getChessType()){ ? ? ? ? ? ? ? ? count3++; ? ? ? ? ? ? ? ? a++; ? ? ? ? ? ? } ? ? ? ? ? ? a = 1; ? ? ? ? ? ? while((i-a)>=0 && (j+a)<colum && color == chessTypes[i-a][j+a].getChessType()){ ? ? ? ? ? ? ? ? count3++; ? ? ? ? ? ? ? ? a++; ? ? ? ? ? ? } ? ? ? ? ? ? if(count3 >= 5){ ? ? ? ? ? ? ? ? flag = true; ? ? ? ? ? ? } ? ? ? ? ? ? //左上 ?右下 ?是否有五個(gè)棋子是相同的顏色 ? ? ? ? ? ? int count4 = 1; ? ? ? ? ? ? a = 1; ? ? ? ? ? ? while((i-a)>0 && (j-a)>=0 && color == chessTypes[i-a][j-a].getChessType()){ ? ? ? ? ? ? ? ? count4++; ? ? ? ? ? ? ? ? a++; ? ? ? ? ? ? } ? ? ? ? ? ? a = 1; ? ? ? ? ? ? while((i+a)<row && (j+a)<colum && color == chessTypes[i+a][j+a].getChessType()){ ? ? ? ? ? ? ? ? count4++; ? ? ? ? ? ? ? ? a++; ? ? ? ? ? ? } ? ? ? ? ? ? if(count4 >= 5){ ? ? ? ? ? ? ? ? flag = true; ? ? ? ? ? ? } ? ? ? ? ? ? return flag; ? ? ? ? } ? ? ? ? //落子后打印棋盤 ? ? public void print(){ ? ? ? ? for(int i = 0; i< chessTypes.length; i++){ ? ? ? ? ? ? for (int j = 0; j< chessTypes[i].length; j++){ ? ? ? ? ? ? ? ? System.out.print(chessTypes[i][j].getChessType()); ? ? ? ? ? ? } ? ? ? ? ? ? System.out.println(); ? ? ? ? } ? ? } }
3.測(cè)試
** ?* @author hudongsheng ?* @date 2020/10/29 - 9:27 ?*/ public class Test { ? ? public static void main(String[] args) { ? ? ? ? boolean flag = true; ? ? ? ? int x; ? ? ? ? int y; ? ? ? ? Gobang gobang = new Gobang(10,10); ? ? ? ? Scanner scanner = new Scanner(System.in); ? ? ? ? gobang.initChessType(); ? ? ? ? //下棋 ? ? ? ? System.out.println("黑棋執(zhí)先"); ? ? ? ? while (true){ ? ? ? ? ? ? gobang.print(); ? ? ? ? ? ? System.out.println("請(qǐng)輸入下黑棋的坐標(biāo):"); ? ? ? ? ? ? x = scanner.nextInt(); ? ? ? ? ? ? y = scanner.nextInt(); ? ? ? ? ? ? gobang.setBlackChess(x,y); ? ? ? ? ? ? if(gobang.checkWin(x,y)){ ? ? ? ? ? ? ? ? gobang.print(); ? ? ? ? ? ? ? ? System.out.println("黑棋勝!"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? gobang.print(); ? ? ? ? ? ? System.out.println("請(qǐng)輸入下白棋的坐標(biāo):"); ? ? ? ? ? ? x = scanner.nextInt(); ? ? ? ? ? ? y = scanner.nextInt(); ? ? ? ? ? ? gobang.setWhiteChess(x,y); ? ? ? ? ? ? if(gobang.checkWin(x,y)){ ? ? ? ? ? ? ? ? gobang.print(); ? ? ? ? ? ? ? ? System.out.println("白棋勝!"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud遠(yuǎn)程服務(wù)調(diào)用三種方式及原理
本文給大家介紹SpringCloud遠(yuǎn)程服務(wù)調(diào)用實(shí)戰(zhàn)筆記,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-12-12MyBatis自定義映射resultMap的實(shí)現(xiàn)
本文主要介紹了MyBatis自定義映射resultMap的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Java?pdf文件書簽承前縮放驗(yàn)證的設(shè)置方法
很多朋友不知道是什么是書簽承前縮放,簡(jiǎn)單說(shuō)就是可以任意改變當(dāng)前pdf文檔縮放比例,點(diǎn)擊書簽后不影響其縮放比率,本文給大家介紹下Java?pdf文件書簽承前縮放驗(yàn)證的設(shè)置方法,感興趣的朋友一起看看吧2022-02-02使用JDBC4.0操作XML類型的字段(保存獲取xml數(shù)據(jù))的方法
jdbc4.0最重要的特征是支持xml數(shù)據(jù)類型,接下來(lái)通過(guò)本文重點(diǎn)給大家介紹如何使用jdbc4.0操作xml類型的字段,對(duì)jdbc4.0 xml相關(guān)知識(shí)感興趣的朋友一起看下吧2016-08-08