本方案是基於控制檯寫的一個代碼
沒有花裏胡哨的界面,只爲研究算法
僅僅用了200行代碼html
遊戲運行結果
這裏我就很簡單的複製了一個結果算法
第9回合,下子方:玩家2(白) 請輸入你要下的位置(空格隔開) 例如 10 5 9 5 玩家2(白)贏得了勝利 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 · · · · · · · · · · · · · · · · 1 · · · · · · · · · · · · · · · · 2 · · · · · · · · · · · · · · · · 3 · · · · · · · · · · · · · · · · 4 · · · · · · · · · · · · · · · · 5 · · · · · 白 黑 · · · · · · · · · 6 · · · · · 白 · · · 黑 · · · · · · 7 · · · · · 白 黑 · · · · · · · · · 8 · · · · · 白 黑 · · · · · · · · · 9 · · · · · 白 · · · · · · · · · · 10 · · · · · · · · · · · · · · · · 11 · · · · · · · · · · · · · · · · 12 · · · · · · · · · · · · · · · · 13 · · · · · · · · · · · · · · · · 14 · · · · · · · · · · · · · · · · 15 · · · · · · · · · · · · · · · · 遊戲結束
有什麼地方還有問題歡迎評論緩存
import java.util.Scanner; /** * 控制檯五子棋遊戲 */ public class Gobang { private boolean gameover = false; //15*15棋盤 private char[][] table = new char[16][16]; //兩個玩家 private Player p1,p2; //回合 private int huihe = 0; public void createGame(Player p1,Player p2){ this.p1=p1; this.p2=p2; } /** 展現棋局 **/ private void show(){ int xx =0; System.out.println(" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 "); for(char[] cs :table){ System.out.print(" "+xx+(xx>9?"":" ")+" "); for(char c : cs){ if(c==0) System.out.print("·"); System.out.print(c+" "); } System.out.println(); xx++; } } /** 獲取下一個走棋的 **/ private Player getPlayer(){ //黑子先走 if (huihe==0) return p1.color=='黑'? p1 : p2; //根據回合判斷棋子顏色 if (huihe%2!=0) return p1.color=='白'? p1 : p2; return p1.color=='黑'? p1 : p2; } /** 判斷是否獲勝 **/ private boolean isWin(int x,int y,char c){ /* * 7 8 9 * 4 5 6 * 1 2 3 */ int xx = 0,yy=0; for (int i =1 ;i<9 ;i++ ){ switch (i){ case 1: xx=-1;yy=-1; break; case 2: xx=-1;yy=1; break; case 3: xx=1;yy=-1; break; case 4: xx=1;yy=1; break; case 5: xx = 0;yy = 1; break; case 6: xx = 1 ;yy = 0; break; case 7: xx = 0 ;yy = -1; break; case 8: xx = -1;yy= 0; break; } if ( (x<4&&xx==-1) || (y<4&&yy==-1)){ }else if((x>12&&xx==1) || (y>12&&yy==1)){ }else if(x<4&&y<4&&(xx!=-1&&yy!=-1)){ if (ishas(x, y, xx, yy, 4, c)) return true; }else if(x>12&&y>12&&(xx!=1&&yy!=1)){ if (ishas(x, y, xx, yy, 4, c)) return true; }else if(xx==0||yy==0){ if (ishas(x, y, xx, yy, 4, c)) return true; } if(x>2&&y>2&&x<14&&y<14) { if (ishas(x, y, xx, yy, 2, c)&&ishas(x, y, -xx, -yy, 2, c)) { return true; } } if(x>3&&y>3&&x<15&&y<15) { if (ishas(x, y, xx, yy, 3, c)&&ishas(x, y, -xx, -yy, 1, c)) { return true; } } if(x>1&&y>1&&x<13&&y<13) { if (ishas(x, y, xx, yy, 1, c)&&ishas(x, y, -xx, -yy, 3, c)) { return true; } } } return false; } /** * 檢測是否有棋子 * @param x x座標 * @param y y座標 * @param xx x方向 * @param yy y方向 * @param num 緩存 * @return */ private boolean ishas(int x,int y,int xx,int yy,int num,char c){ if(num==1){ if(table[x+xx][y+yy] == c)return true; }else if(table[x+xx][y+yy] == c){ return ishas(x+xx,y+yy,xx,yy,num-1,c); } return false; } /** 下棋 **/ public boolean put(int x,int y,Player p){ if (table[x][y]==0) { table[x][y] = p.getColor(); if(isWin(x,y,p.color)){ gameover = true; System.out.println(p.username+"("+p.color+")贏得了勝利"); } return true; } return false; } /** 遊戲運行 **/ public void start(){ Player p = null; Scanner scan = new Scanner(System.in); String[] strArr = new String[2]; while (!gameover){ if(p==null)p=getPlayer(); System.out.println("第"+(huihe/2+1)+"回合,下子方:"+p.getUsername()+"("+p.getColor()+")"); System.out.println("請輸入你要下的位置(空格隔開) 例如 10 5"); //下棋失敗從新開始本回合 try { String in = scan.nextLine().trim(); if ("exit".equals(in)) break; strArr = in.split(" "); if (!put( Integer.parseInt(strArr[0]),Integer.parseInt(strArr[1]),p))continue; }catch (Exception e){ e.printStackTrace(); } show(); p=null; huihe++; } System.out.println("遊戲結束"); } /** 遊戲入口 */ public static void main(String[] args) { Gobang game = new Gobang(); Player p1 = new Player("玩家1",'黑'); Player p2 = new Player("玩家2",'白'); game.createGame(p1,p2); game.start(); } } /** 玩家類 **/ class Player{ String username; char color; public Player(String username, char color) { this.username = username; this.color = color; System.out.println(username+"攜帶"+color+"顏色的棋子加入遊戲"); } public String getUsername() { return username; } public char getColor() { return color; } }