亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Java項目實現五子棋小游戲

瀏覽:53日期:2022-09-01 08:59:11

本文實例為大家分享了Java實現五子棋小游戲的具體代碼,供大家參考,具體內容如下

項目名稱

五子棋小游戲

項目描述

可以改變獲勝棋子數,率先連成棋數的人獲勝

代碼實現

測試類

public class Test { public static void main(String[] args) { FiveChess fiveChess = new FiveChess(); fiveChess.start(); }}

主類:實現主方法

public class FiveChess { private static final int CheckerSize = 10; private static final int successSize = 5; private Chess[][] chess; private int xPos; private int yPos; private boolean flag = true; private Scanner scanner = new Scanner(System.in); public FiveChess(){ chess = new Chess[CheckerSize][CheckerSize]; } private void initCheck(){ for(int i=0;i<CheckerSize;i++){ for(int j=0;j<CheckerSize;j++){ chess[i][j] = new Chess('十'); } } } private boolean judge(int xPos,int yPos){ //橫向 if(yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos][yPos-1].getValue()) || yPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos][yPos+1].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (yPos - i >= 0 && chess[xPos][yPos - i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (yPos + i < CheckerSize && chess[xPos][yPos + i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } //縱向 if (xPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos].getValue()) || xPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (xPos- i >= 0 && chess[xPos-i][yPos].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (xPos + i < CheckerSize && chess[xPos+i][yPos].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } //正斜線 if (xPos-1>=0 && yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos-1].getValue()) || xPos+1<CheckerSize && yPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos+1].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (xPos - i >= 0 && yPos - i >= 0 && chess[xPos-i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (xPos + i < CheckerSize && yPos + i < CheckerSize && chess[xPos+i][yPos+i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } //反斜線 if (xPos-1>=0 && yPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos+1].getValue()) || xPos+1<CheckerSize && yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos-1].getValue())){ int count = 1; for (int i = 1; i < successSize; i++) { if (xPos - i >= 0 && yPos + i<CheckerSize && chess[xPos-i][yPos+i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } for (int i = 1; i < successSize; i++) { if (xPos + i < CheckerSize && yPos - i >= 0 && chess[xPos+i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) { count++; } else { break; } } return count >= successSize ? true : false; } return false; } private void runChess(String run){ System.out.println('請輸入'+run+'坐標:'); xPos = scanner.nextInt(); yPos = scanner.nextInt(); if(chess[xPos-1][yPos-1].getValue().equals('十')){ if(run.equals('黑棋')){ chess[xPos-1][yPos-1] = new Chess('●'); } else if(run.equals('白棋')){ chess[xPos-1][yPos-1] = new Chess('?'); } for(int i=0;i<CheckerSize;i++){ for (int j=0;j<CheckerSize;j++){ System.out.print(chess[i][j].getValue()); } System.out.println(); } if(judge(xPos-1,yPos-1)){ flag = false; System.out.println(run+'獲勝'); System.out.println('游戲結束,是否重玩'); String finish = scanner.next(); if (finish.equals('是')){ flag = true; start(); } else if (finish.equals('否')){ System.exit(0); } } }else { System.out.println('該處已存在棋子,請重新選擇'); runChess(run); } } public void start(){ initCheck(); System.out.println('請選擇先走方:黑棋or白棋'); String run = scanner.next(); for(int i=0;i<CheckerSize;i++){ for (int j=0;j<CheckerSize;j++){ System.out.print(chess[i][j].getValue()); } System.out.println(); } while (flag) { switch (run) { case '黑棋': runChess('黑棋'); run = '白棋'; break; case '白棋': runChess('白棋'); run = '黑棋'; break; default: } } }}

結點類

public class Chess { private String value; public Chess(String value){ this.value = value; } public String getValue() { return value; }}

更多有趣的經典小游戲實現專題,分享給大家:

C++經典小游戲匯總

python經典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經典游戲 玩不停

javascript經典小游戲匯總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 国产精品入口在线看麻豆 | 青草草在线观看免费视频 | 久久视频精品a线视频在线观看 | 亚洲色图综合图片 | 亚洲精品日韩中文字幕久久久 | 黄色高清在线观看 | 521香蕉永久播放地址 | 国产72av国片精品jk制服 | 国产一区2区 | 性生a| 日韩爽爽视频爽爽 | 视频在线观看免费播放www | 真实一级一级一片免费视频 | 亚洲综合激情九月婷婷 | 亚洲高清国产一线久久 | 国产精彩视频在线 | 国产亚洲一区二区三区在线观看 | 亚洲成人国产精品 | 青青色在线观看 | 国产特黄一级毛片特黄 | 久久视频精品线视频在线网站 | 亚洲欧美视频网站 | 九九热精品在线观看 | 日本成人片网站 | 在线观看黄色片 | 国产福利区一区二在线观看 | 国产亚洲精品自在线观看 | 无需付费大片免费在线观看 | 成人黄色在线观看视频 | 国产日韩欧美在线观看播放 | 中国护士一级毛片免费版本 | 国产精品1区 | 特级aa一级欧美毛片 | 久久久久久日本一区99 | 国内a级毛片 | 国产成a人亚洲精v品久久网 | 亚洲欧美视频一区二区三区 | 欧美日韩在线精品成人综合网 | xxxvideos3d性动漫| 国产一区二区fc2ppv在线播放 | 毛片大全免费看 |