java實現(xiàn)簡單控制臺五子棋游戲
本文實例為大家分享了java實現(xiàn)簡單控制臺五子棋的具體代碼,供大家參考,具體內(nèi)容如下
GobangMain這個類是游戲的主方法,主要用于控制游戲的執(zhí)行,值得注意的是輸入的坐標的格式是3,4的樣式,不能是其他的格式,也不能出現(xiàn)空格。
package com.qf.Gobang; import java.util.Scanner; import org.omg.CORBA.PUBLIC_MEMBER; public class GobangMain { public static String white = "白色"; public static String black = "黑色"; public static boolean color=true; public static String spoint;//存儲坐標 public static void main(String[] args) { Gobang gobang = new Gobang(); Scanner scanner=new Scanner(System.in); while(true){ System.out.println("請"+(color?white:black)+"落子:"); spoint=scanner.next();//獲得坐標 Point point=gobang.analysisPoint(spoint);//解析坐標,并返回坐標對象 if(gobang.luoZi(point,color)){ gobang.printMap(); if(gobang.isWin(point,color)){ System.out.println(""+(color?white:black)+"贏了!"); break; } color=!color; } } } }
Point類
public class Point { public Point(int x, int y) { super(); this.x = x; this.y = y; } int x; int y; }
Gobang 類是游戲類,主要包含游戲的判斷游戲的結(jié)束等等。
package com.qf.Gobang; import java.awt.Event; import java.util.Scanner; public class Gobang { public int n = 20;// 地圖的規(guī)模 public String color;// 確認是白方,還是黑方 public String mark = "╋"; public String white = "○"; public String black = "●"; public String[][] map = new String[n][n];; public String[] coordinate = { "⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖", "⒗", "⒘", "⒙", "⒚", "⒛" }; public Gobang() { // 初始化地圖 init(); } // 初始化地圖 public void init() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == n - 1) { map[i][j] = coordinate[j]; } else if (j == n - 1) { map[i][j] = coordinate[i]; } else { map[i][j] = mark; } } } printMap(); } // 打印地圖 public void printMap() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(map[i][j]); } System.out.println(); } } // 解析坐標 public Point analysisPoint(String point) { String[] points = point.split(","); int x = Integer.parseInt(points[0]) - 1; int y = Integer.parseInt(points[1]) - 1; return new Point(x, y); } // 落子 public boolean luoZi(Point point, Boolean color) { // 判斷是否越界 if (point.x < 0 || point.y > 18 || point.y < 0 || point.y > 18) { return false; } // 判斷落子的地方有沒有其他的子 if (map[point.x][point.y] != mark) { return false; } map[point.x][point.y] = color ? white : black; return true; } // 判斷是否輸贏 public boolean isWin(Point point, boolean color) { // 縱向 int zxS = 0;// 縱向上 for (int i = 0; i < 5; i++) { if (point.x - i < 0) { break; } if (map[point.x - i][point.y].equals(color ? white : black)) { zxS++; } else { break; } } int zxX = 0;// 縱向下 for (int i = 1; i < 5; i++) { if (point.x + i > 18) { break; } if (map[point.x + i][point.y].equals(color ? white : black)) { zxX++; } else { break; } } // 橫向 int hxZ = 0;// 橫向左 for (int i = 0; i < 5; i++) { if (point.y - i < 0) { break; } if (map[point.x][point.y - i].equals(color ? white : black)) { hxZ++; } else { break; } } int hxY = 0;// 橫向右 for (int i = 1; i < 5; i++) { if (point.y + i > 18) { break; } if (map[point.x][point.y + i].equals(color ? white : black)) { hxY++; } else { break; } } // 正斜 int zxxS = 0;// 正斜上 for (int i = 0; i < 5; i++) { if (point.y + i > 18 || point.x - i < 0) { break; } if (map[point.x - i][point.y + i].equals(color ? white : black)) { zxxS++; } else { break; } } int zxxX = 0;// 正斜下 for (int i = 1; i < 5; i++) { if (point.y - i < 0 || point.x + i > 18) { break; } if (map[point.x + i][point.y - i].equals(color ? white : black)) { zxxX++; } else { break; } } // 反斜 int fxxS = 0;// 反斜上 for (int i = 0; i < 5; i++) { if (point.y - i < 0 || point.x - i < 0) { break; } if (map[point.x - i][point.y - i].equals(color ? white : black)) { fxxS++; } else { break; } } int fxxX = 0;// 反斜下 for (int i = 1; i < 5; i++) { if (point.y + i > 18 || point.x + i >18) { break; } if (map[point.x + i][point.y + i].equals(color ? white : black)) { fxxX++; } else { break; } } System.out.println(); System.out.print("反斜上↖:" + fxxS+"\t"); System.out.print("縱向上↑:" + zxS+"\t"); System.out.print("正斜上↗:" + zxxS); System.out.println(); System.out.print("橫向左←:" + hxZ+"\t\t\t"); System.out.print("橫向右→:" + hxY); System.out.println(); System.out.print("正斜下↙:" + zxxX+"\t"); System.out.print("縱向下↓:" + zxX+"\t"); System.out.print("反斜下↘:" + fxxX); System.out.println(); if (zxS + zxX > 4 || hxY + hxZ > 4 || zxxS + zxxX > 4 || fxxS + fxxX > 4) { return true; } return false; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java反射學(xué)習 getClass()函數(shù)應(yīng)用
所謂反射,可以理解為在運行時期獲取對象類型信息的操作,本文將詳細介紹,需要的朋友可以參考下2012-12-12idea沒有services窗口、沒有springboot啟動項問題
這篇文章主要介紹了idea沒有services窗口、沒有springboot啟動項問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05SpringBoot中的五種對靜態(tài)資源的映射規(guī)則的實現(xiàn)
這篇文章主要介紹了SpringBoot中的五種對靜態(tài)資源的映射規(guī)則的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-12-12java生成pdf表格,調(diào)用itext創(chuàng)建的實例
這篇文章主要介紹了java生成pdf表格,調(diào)用itext創(chuàng)建的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Spring Security 自動踢掉前一個登錄用戶的實現(xiàn)代碼
這篇文章主要介紹了Spring Security 自動踢掉前一個登錄用戶的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05java 中InputStream,String,File之間的相互轉(zhuǎn)化對比
這篇文章主要介紹了java 中InputStream,String,File之間的相互轉(zhuǎn)化對比的相關(guān)資料,需要的朋友可以參考下2017-04-04