第一次写博客,问大家一个问题吧。
2018-02-03 16:58
246 查看
本人编辑了一个java五子棋程序,但是运行时出现了问题
当黑棋输入第一个棋子时,就会出现错误警告,然后程序就会莫名结束。
![](https://img-blog.csdn.net/20180203165602233?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvREZWU0VEMzMzMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
import java.util.Scanner; public class FIve { public static void main(String[] args) { new FIve().run(); } public void run() { Scanner scanner = new Scanner(System.in); System.out.println("请输入黑棋昵称"); String nameBlack = scanner.nextLine(); System.out.println("请输入白棋昵称"); String nameWhite = scanner.nextLine(); int size = 15; int[][] table = new int[size][size]; printTable(table, size); int win = 0; int currentPlayer = 0; do { // 下棋 String playerName = currentPlayer == 0 ? nameBlack : nameWhite; System.out.println("该" + playerName + "下棋"); int x, y; boolean canPlace = true; do{ if (!canPlace){ System.out.println("这个位置不能放棋子,请重新输入"); } String code = scanner.nextLine(); y = code.charAt(0) - 'A'; x = Integer.valueOf(code.substring(1)) - 1; canPlace = true; canPlace = canPlace && x >= 0 && x < size; canPlace = canPlace && y >= 0 && y < size; canPlace = canPlace && table[y][x] == 0; }while (!canPlace); table[y][x] = currentPlayer == 0 ? 1 : 2; currentPlayer++; currentPlayer %= 2; // 打印棋盘 printTable(table, size); // 检测输赢 win = check(table, x, y ); } while (win == 0) ; if (win == 1) { System.out.println(nameBlack + "胜利"); } else if (win == 2) { System.out.println(nameWhite + "胜利"); } else if (win == 3) { System.out.println("平局"); } } public void printTable(int[][] table, int size) { System.out.print(" "); for (int i = 0; i < size; i++) { int value = i + 1; System.out.print(value < 10 ? value + " " : value + " "); } System.out.println(); for (int i = 0; i < size; i++) { char title = (char) ('A' + i); System.out.print(title + " "); for (int j = 0; j < size; j++) { int value = table[i][j]; char c = ' '; switch (value) { case 0: c = '.'; break; case 1: c = 'x'; break; case 2: c = 'o'; break; } System.out.print(c + " "); } System.out.println(); } } public int check(int[][] table, int x, int y){ boolean test = false; test = test || moreThaFive(table, x, y, 0, -1); test = test || moreThaFive(table, x, y, 1, -1); test = test || moreThaFive(table, x, y, 1, 0); test = test || moreThaFive(table, x, y, 1, 1); if (test){ return table[y][x]; } return 0; } public boolean moreThaFive(int[][] table, int x, int y, int dx, int dy){ int count = 0; count += count(table, x, y, dx, dy); count += count(table, x, y, -dx, -dy); count -= 1; return count >= 5; } public int count(int[][] table, int originX, int originY, int dx, int dy){ int originValue = table[originY][originX]; int count = 0; int x = originX; int y = originY; int value; do { count ++; originX += dx; originY += dy; value = table[originY][originX]; } while (value == originValue); return count; } }
当黑棋输入第一个棋子时,就会出现错误警告,然后程序就会莫名结束。
相关文章推荐
- 好久没更新博客了,今天给大家分享一下Android中的资源与国际化的问题,通常我们新建一个Android工程,目录结构如下图所示: 我们主要看一下layout与values目录,layou
- 一个刚到此吧的IT新人,第一次发博客,望大家多多指教
- 端口占用问题,今天学习的时候又出现了这个问题,找了度娘,特此记录以下。第一次写博客,希望自己能坚持下来。大家共同进步
- 最近做了一个Mqtt消息推送,和大家分享分享心得,以防以后搞忘了,新人第一次写博客,大神勿喷。
- 一个组策略无法打开的问题(已解决)和大家分享下经
- 关于web,我有一个尴尬的问题想请大家帮忙!
- 第一次遭遇脚本: 利用xsd自动生成cs的一个问题
- 如何备考CCIE/HCIE,一个源自于美帝学员的问题,我会在下午的直播分享会上为大家分享一下经验
- 关于csdn博客的一个小问题
- 问大家一个没有什么技术水平的问题!
- 开发中碰到过的一些问题总结,给自己也给大家一个参考!
- CSDN博客:对于一个程序员,写程序就是实现功能,遇到具体问题,解决这个问题,并记录问题到CSDN上,从而有所提高和互相帮助。
- mysql left( right ) join使用on 与where 筛选的差异 博客分类: mysql MySQLSQL数据结构 有这样的一个问题mysql查询使用mysql中left(
- (技术小白)最近搭建了一个博客小站,希望大家多多提出建议。谢谢大家
- git 第一次 clone 远程项目时的 一个小问题
- 思绪良久,终于决定还是来做一个自己的博客,希望能够通过这里把我的理念传递给大家。
- 请大家帮帮我吧:一个jdbc连接sqlserver的问题
- 冒犯了,问大家一个问题,会linux的进来帮我解决一下
- 请教大家一个有关移位运算的问题。
- 第一次写博客,欢迎大家!