最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實現(xiàn)簡單控制臺五子棋游戲

 更新時間:2019年11月30日 14:36:41   作者:donggedeboke  
這篇文章主要為大家詳細介紹了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 類是游戲類,主要包含游戲的判斷游戲的結束等等。

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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • StackTraceElement獲取方法調(diào)用棧信息實例詳解

    StackTraceElement獲取方法調(diào)用棧信息實例詳解

    這篇文章主要介紹了StackTraceElement獲取方法調(diào)用棧信息實例詳解,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Spring?BeanFactory工廠使用教程

    Spring?BeanFactory工廠使用教程

    Spring的本質是一個bean工廠(beanFactory)或者說bean容器,它按照我們的要求,生產(chǎn)我們需要的各種各樣的bean,提供給我們使用。只是在生產(chǎn)bean的過程中,需要解決bean之間的依賴問題,才引入了依賴注入(DI)這種技術
    2023-02-02
  • java 算法之歸并排序詳解及實現(xiàn)代碼

    java 算法之歸并排序詳解及實現(xiàn)代碼

    這篇文章主要介紹了java 算法之歸并排序詳解及實現(xiàn)代碼的相關資料,需要的朋友可以參考下
    2017-03-03
  • 如何使用maven-helper插件解決jar包沖突問題

    如何使用maven-helper插件解決jar包沖突問題

    安裝了Maven?Helper插件,只要打開pom文件,就可以打開該pom文件的Dependency?Analyzer視圖,這篇文章主要介紹了使用maven-helper插件解決jar包沖突,需要的朋友可以參考下
    2024-05-05
  • Maven繼承父工程時的relativePath標簽解析用法小結

    Maven繼承父工程時的relativePath標簽解析用法小結

    relativePath 的作用是為了找到父級工程的pom.xml,本文主要介紹了Maven繼承父工程時的relativePath標簽解析用法小結,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • SpringBoot可視化監(jiān)控的具體應用

    SpringBoot可視化監(jiān)控的具體應用

    最近越發(fā)覺得,任何一個系統(tǒng)上線,運維監(jiān)控都太重要了,本文介紹了SpringBoot可視化監(jiān)控的具體應用,分享給大家,有興趣的同學可以參考一下
    2021-06-06
  • Java數(shù)據(jù)導入功能之讀取Excel文件實例

    Java數(shù)據(jù)導入功能之讀取Excel文件實例

    這篇文章主要介紹了Java數(shù)據(jù)導入功能之讀取Excel文件實例,本文給出了jar包的下載地址以及讀取Excel文件的代碼實例,需要的朋友可以參考下
    2015-06-06
  • 詳解Spring的兩種代理方式:JDK動態(tài)代理和CGLIB動態(tài)代理

    詳解Spring的兩種代理方式:JDK動態(tài)代理和CGLIB動態(tài)代理

    這篇文章主要介紹了詳解Spring的兩種代理方式:JDK動態(tài)代理和CGLIB動態(tài)代理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • JVM分析之類加載機制詳解

    JVM分析之類加載機制詳解

    JVM內(nèi)部架構包含類加載器、內(nèi)存區(qū)域、執(zhí)行引擎等。日常開發(fā)中,我們編寫的java文件被編譯成class文件后,jvm會進行加載并運行使用類。本次將對JVM加載部分進行分析,便于大家了解并掌握加載機制
    2022-08-08
  • Java中CyclicBarrier和CountDownLatch的用法與區(qū)別

    Java中CyclicBarrier和CountDownLatch的用法與區(qū)別

    CyclicBarrier和CountDownLatch這兩個工具都是在java.util.concurrent包下,并且平時很多場景都會使用到。本文將會對兩者進行分析,記錄他們的用法和區(qū)別,感興趣的可以了解一下
    2021-08-08

最新評論

泉州市| 潢川县| 金寨县| 临沭县| 黔江区| 杨浦区| 延津县| 新丰县| 德保县| 清丰县| 哈密市| 马边| 嘉定区| 霞浦县| 兴山县| 互助| 夏邑县| 贵溪市| 安国市| 崇文区| 广饶县| 开化县| 封开县| 普定县| 巫溪县| 武定县| 湘潭县| 鹿邑县| 密山市| 丰台区| 云梦县| 敦煌市| 汉中市| 阿图什市| 河东区| 亳州市| 山丹县| 八宿县| 苗栗县| 桦川县| 东莞市|