Java實(shí)現(xiàn)簡單無界面五子棋
本文項(xiàng)目為大家分享了Java實(shí)現(xiàn)無界面五子棋的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目介紹:
本次設(shè)計(jì)是基于知識(shí)點(diǎn)Java類和對(duì)象以及數(shù)組開發(fā)的一個(gè)小型五子棋游戲程序。游戲開始時(shí),選擇黑棋、白棋開局,將一枚棋子落在棋盤一坐標(biāo)上,然后輪番落子,如此輪流下子,直到某一方首先在棋盤的豎、橫或兩斜四方向上的五子連成線,則該方該局獲勝。
項(xiàng)目實(shí)現(xiàn)思路:
1、棋盤設(shè)計(jì)為10*10格,棋盤類型Chess[][] 二維數(shù)組,所含屬性String chessType; 棋盤首先chessType值是”?”。
2、初始化二維數(shù)組
3、玩家選擇黑白圈后,開始下棋。輸入要下棋子的行列坐標(biāo),黑白棋子輪流落子,當(dāng)一方連成五子或下滿棋盤時(shí),游戲結(jié)束(連成五子的一方獲勝,下滿棋盤為和棋)。
4、每一次落子成功后,馬上判斷以該位置為中心的八個(gè)方向:上、下、左、右、左上、左下、右上、右下是否有相同顏色的棋子連成五子,如果連成五子,則游戲結(jié)束,輸出相應(yīng)的信息。
5、當(dāng)游戲一方勝利后顯示勝利信息。從程序表面看,這是一個(gè)二維平面圖,所以數(shù)據(jù)用二維數(shù)組來表示,數(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.測試
**
?* @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;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡單了解SpringMVC緩存對(duì)靜態(tài)資源有什么影響
這篇文章主要介紹了簡單了解SpringMVC緩存對(duì)靜態(tài)資源有什么影響,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java如何實(shí)現(xiàn)讀取txt文件內(nèi)容并生成Word文檔
本文主要介紹了通過Java實(shí)現(xiàn)讀取txt文件中的內(nèi)容,并將內(nèi)容生成Word文檔。文章的代碼非常詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2021-12-12
java中List去除重復(fù)數(shù)據(jù)的5種方式總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于java中List去除重復(fù)數(shù)據(jù)的5種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java中g(shù)etSuperclass()方法的使用與原理解讀
文章介紹了Java中的getSuperclass()方法,該方法用于獲取一個(gè)類的直接父類,通過理解其使用方式、工作原理以及實(shí)際應(yīng)用場景,可以更好地利用反射機(jī)制處理類的繼承關(guān)系,實(shí)現(xiàn)動(dòng)態(tài)類型檢查、類加載以及序列化等功能2025-01-01

