栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java项目实现五子棋小游戏

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java项目实现五子棋小游戏

本文实例为大家分享了Java实现五子棋小游戏的具体代码,供大家参考,具体内容如下

项目名称

五子棋小游戏

项目描述

可以改变获胜棋子数,率先连成棋数的人获胜

代码实现

测试类

public class Test {
 public static void main(String[] args) {
 FiveChess fiveChess = new FiveChess();
 fiveChess.start();
 }
}

主类:实现主方法

public class FiveChess {
 private static final int CheckerSize = 10;
 private static final int successSize = 5;
 private Chess[][] chess;
 private int xPos;
 private int yPos;
 private boolean flag = true;
 private Scanner scanner = new Scanner(System.in);
 public FiveChess(){
 chess = new Chess[CheckerSize][CheckerSize];
 }
 private void initCheck(){
 for(int i=0;i=0 && chess[xPos][yPos].getValue().equals(chess[xPos][yPos-1].getValue()) ||
  yPos+1= 0 && chess[xPos][yPos - i].getValue().equals(chess[xPos][yPos].getValue())) {
   count++;
  } else {
   break;
  }
  }
  for (int i = 1; i < successSize; i++) {
  if (yPos + i < CheckerSize &&
   chess[xPos][yPos + i].getValue().equals(chess[xPos][yPos].getValue())) {
   count++;
  } else {
   break;
  }
  }
  return count >= successSize ? true : false;
 }
 //纵向
 if (xPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos].getValue()) ||
  xPos+1= 0 && chess[xPos-i][yPos].getValue().equals(chess[xPos][yPos].getValue())) {
   count++;
  } else {
   break;
  }
  }
  for (int i = 1; i < successSize; i++) {
  if (xPos + i < CheckerSize &&
   chess[xPos+i][yPos].getValue().equals(chess[xPos][yPos].getValue())) {
   count++;
  } else {
   break;
  }
  }
  return count >= successSize ? true : false;
 }
 //正斜线
 if (xPos-1>=0 && yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos-1].getValue()) ||
  xPos+1= 0 && yPos - i >= 0 &&
   chess[xPos-i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) {
   count++;
  } else {
   break;
  }
  }
  for (int i = 1; i < successSize; i++) {
  if (xPos + i < CheckerSize && yPos + i < CheckerSize &&
   chess[xPos+i][yPos+i].getValue().equals(chess[xPos][yPos].getValue())) {
   count++;
  } else {
   break;
  }
  }
  return count >= successSize ? true : false;
 }
 //反斜线
 if (xPos-1>=0 && yPos+1=0 &&
   chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos-1].getValue())){
  int count = 1;
  for (int i = 1; i < successSize; i++) {
  if (xPos - i >= 0 && yPos + i= 0 &&
   chess[xPos+i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) {
   count++;
  } else {
   break;
  }
  }
  return count >= successSize ? true : false;
 }
 return false;
 }
 private void runChess(String run){
 System.out.println("请输入"+run+"坐标:");
 xPos = scanner.nextInt();
 yPos = scanner.nextInt();
 if(chess[xPos-1][yPos-1].getValue().equals("十")){
  if(run.equals("黑棋")){
  chess[xPos-1][yPos-1] = new Chess("●");
  }
  else if(run.equals("白棋")){
  chess[xPos-1][yPos-1] = new Chess("〇");
  }
  for(int i=0;i

结点类

public class Chess {
 private String value;
 public Chess(String value){
 this.value = value;
 }
 public String getValue() {
 return value;
 }
}

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

Javascript经典游戏 玩不停

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/133960.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号