Java 自动下棋
2023-09-11 14:18:49 时间
学习 Java 写的代码片段增加熟练度。
第一步定义一个棋盘(这里用二维数组)和棋盘大小
//初始化棋盘 public void initBoard() { board = new String[BOARD_SIZE][BOARD_SIZE]; for (int i = 0; i < board.length; i++) { // String[] itemArg = board[i]; for (int j = 0; j < board.length; j++) { board[i][j] = "┼"; } } } //打印棋盘 public void printData() { System.out.println("------start-----"); for (int i = 0; i < board.length; i++) { for (String item : board[i]) { System.out.print(item); } System.out.println();//一行结束换行打印 } System.out.print("-------end-------"); System.out.print("\n\n\n\n"); }
随机获取棋子的位置 小棋盘大小用到Math.random(), do{}while(boolean);
Math.random() * 5 随机出来的结果是从 0 - 5 的浮点类型
do{}while(boolean); 先执行代码块,在判断条件 ture 继续循环 false 结束循环。
//随机获取出棋子位置(x,y) private int[] getPosition(ArrayList<int[]> historyPosition) { int[] p = new int[2]; do { p[0] = (int) (Math.random() * BOARD_SIZE);//[0,5) p[1] = (int) (Math.random() * BOARD_SIZE); } while (dataRepeat(p, historyPosition));//判断当前位置是否已有棋子 return p; }
判断当前位置是否有棋子用到 for each{}
//当前位置已有棋子重新获取 private boolean dataRepeat(int[] positionBoard, ArrayList<int[]> hpArg) { boolean repeat = false; for (int[] hp: hpArg) { repeat = Arrays.equals(positionBoard, hp); if(repeat) return repeat; } return repeat; }
全部代码
class Gobang { //定义棋盘大小 private static int BOARD_SIZE = 5; //定义一个二维数组 private static String[][] board; //初始化棋盘 public void initBoard() { board = new String[BOARD_SIZE][BOARD_SIZE]; for (int i = 0; i < board.length; i++) { // String[] itemArg = board[i]; for (int j = 0; j < board.length; j++) { board[i][j] = "┼"; } } } //打印棋盘 public void printData() { System.out.println("------start-----"); for (int i = 0; i < board.length; i++) { for (String item : board[i]) { System.out.print(item); } System.out.println();//一行结束换行打印 } System.out.print("-------end-------"); System.out.print("\n\n\n\n"); } public static void main(String[] arg) { Gobang gobang = new Gobang(); gobang.initBoard(); gobang.printData(); gobang.startRandomPosition(); } //开始下棋 public void startRandomPosition() { int label = 0; //记录已经有棋子的位置 ArrayList<int[]> historyPosition = new ArrayList<>(); String[] boardChild = {"●", "○"};//黑白棋子 int[] positionBlack; do { positionBlack = getPosition(historyPosition); board[positionBlack[0]][positionBlack[1]] = boardChild[label % 2];//获取棋子黑白交替获取 System.out.println(Arrays.toString(positionBlack) + boardChild[label % 2] + (label % 2 == 0 ? "黑子" : "白子")); printData(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } historyPosition.add(positionBlack);//记录位置 label++; //交换棋子 } while (historyPosition.size() < BOARD_SIZE * BOARD_SIZE); } //当前位置已有棋子重新获取 private boolean dataRepeat(int[] positionBoard, ArrayList<int[]> hpArg) { boolean repeat = false; for (int[] hp: hpArg) { repeat = Arrays.equals(positionBoard, hp); if(repeat) return repeat; } return repeat; } //随机获取出棋子位置(x,y) private int[] getPosition(ArrayList<int[]> historyPosition) { int[] p = new int[2]; do { p[0] = (int) (Math.random() * BOARD_SIZE); p[1] = (int) (Math.random() * BOARD_SIZE); } while (dataRepeat(p, historyPosition)); return p; } }
相关文章
- java集合(3)- Java中的equals和hashCode方法详解
- 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制
- 大杂烩 -- Java内存布局【图】以及java各种存储区【详解】
- 【java细节】Java代码忽略https证书:No subject alternative names present
- 实用操作--JAVA项目从8升级至17的步骤及填坑记录
- JAVA础--利用java反射机制绕过编译时的类型检查和访问控制检查
- 【Java】【深拷贝】一行代码搞定深拷贝
- java.lang.NullPointerException
- maven项目的java和resources等文件夹不在Java Resources的文件夹里,并且缺少Deployment...
- 《Java EE 7精粹》—— 2.4 异步支持
- 1. java.util.concurrent - Java 并发工具包
- Java学习-084-java.sql.SQLException: Zero date value prohibited 问题解决
- 在java中使用JMH(Java Microbenchmark Harness)做性能测试
- Java_JSP自定义标签的开发与应用
- 『Java练习生的自我修养』java-se进阶¹ • 初识多线程
- 深入Java设计模式之装饰模式
- 深入Java设计模式之命令模式
- Java并发编程:阻塞队列
- Java流缓冲区问题
- 【JAVA】【NIO】10、Java NIO ServerSocketChannel
- 【Hadoop】:HDFS调用Java API进行操作
- 自动加载,热加载java项目文件
- Java小白入门200例43之while 语句
- Java EE供应商和伦敦Java用户组宣布新的MicroProfile