java如何实现简单控制台五子棋游戏
创始人
2024-12-24 08:21:27
0次

具体内容如下

GobangMain这个类是游戏的主方法,主要用于控制游戏的执行,值得注意的是输入的坐标的格式是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)+"赢了!");          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 类是游戏类,主要包含游戏的判断游戏的结束等等。

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;    }    // 判断落子的地方有没有其他的子    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;  }}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...