国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

java如何實(shí)現簡(jiǎn)單控制臺五子棋游戲

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

小編給大家分享一下java如何實(shí)現簡(jiǎn)單控制臺五子棋游戲,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體內容如下

GobangMain這個(gè)類(lèi)是游戲的主方法,主要用于控制游戲的執行,值得注意的是輸入的坐標的格式是3,4的樣式,不能是其他的格式,也不能出現空格。

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)+"贏(yíng)了!");          break;        }        color=!color;      }    }  }}

Point類(lèi)

public class Point {  public Point(int x, int y) {    super();    this.x = x;    this.y = y;  }  int x;  int y;}

Gobang 類(lèi)是游戲類(lèi),主要包含游戲的判斷游戲的結束等等。

package com.qf.Gobang;import java.awt.Event;import java.util.Scanner;public class Gobang {  public int n = 20;// 地圖的規模  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;    }    // 判斷落子的地方有沒(méi)有其他的子    if (map[point.x][point.y] != mark) {      return false;    }    map[point.x][point.y] = color ? white : black;    return true;  }  // 判斷是否輸贏(yíng)  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;  }}

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

国产精品V欧美精品V日韩精品| 午夜无码国产理论在线| 午夜福利国产成人无码GIF动图| 亚洲一本到无码AV中文字幕| 极品少妇的粉嫩小泬视频| 中文字幕在线免费看线人|