java实现简单五子棋
2016-06-17 09:52
531 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunt1921/article/details/51697743
阅读更多
package crazyit; /**Description: * <br>CopyRight,stc * <br>program 简单五子棋的设计 * <br>Date:6.17 * @author sunt * @version 1.0 */ import java.io.BufferedReader; import java.io.InputStreamReader; public class Gobang { //定义棋盘大小 private static int BOARD_SIZE = 15; //定义一个二维数组来充当棋盘 private String[][] board; public void initBoard(){ //初始化棋盘数组 board = new String[BOARD_SIZE][BOARD_SIZE]; //把每个元素赋值为“+” for (int i = 0 ; i < BOARD_SIZE ; i++) { for (int j = 0 ; j< BOARD_SIZE ; j++) { board[i][j] = "+"; } } } //在控制台输出棋盘的方法 public void printBoard() { for (int i =0 ; i < BOARD_SIZE ; i++) { { for (int j = 0 ; j < BOARD_SIZE ; j++) System.out.print(board[i][j]); } System.out.print("\n"); } } //判断插入的棋子是否合法 public boolean pd(String[][] pos , int x , int y ) { boolean isKey = true; if (pos[x][y] == "&" || pos[x][y] == "●") { isKey = false; } return isKey; } //封装一个可以判断五子在同一条直线上的方法 public boolean isWin(String[][] pos , int x , int y , String z) { boolean isWin = false; int countY =0, countX =0; int temp; //判断在y轴是否 为五子,利用county计数但实际会多加一次自身,所以最后判断是否有5子的时候要减一 temp = y; while (pos[x][temp] == z) { if (temp <= 0 || temp >= 14) { break; } --temp; ++countY; } temp = y; while (pos[x][temp] == z) { if (temp <= 0 || temp >= 14) { break; } ++temp; ++countY; } //判断在x轴是否为五子 temp = x; while (pos[temp][y] == z) { if (temp <= 0 || temp >= 14) { break; } --temp; ++countX; } temp = x; while (pos[temp][y] == z) { if (temp <= 0 || temp >= 14) { break; } ++temp; ++countX; } //判断从左上到右下 int i = x , j = y; int countXL = 0, countXR = 0; while (pos[i][j] == z) { if (i <= 0 || i >= 14 || j <= 0 || j >= 14) { break; } --i; --j; ++countXL; } i = x ; j = y ; while (pos[i][j] == z) { if (i <= 0 || i >= 14 || j <= 0 || j >= 14) { break; } ++i; ++j; ++countXL; } //判断从右上到坐下 while (pos[i][j] == z) { if (i <= 0 || i >= 14 || j <= 0 || j >= 14) { break; } ++i; --j; ++countXR; } while (pos[i][j] == z) { if (i <= 0 || i >= 14 || j <= 0 || j >= 14) { break; } --i; ++j; ++countXR; } if (countX == 5 || countY == 5 || countXL == 5 || countXR == 5) { isWin = true; } return isWin; } public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Gobang gb = new Gobang(); gb.initBoard(); gb.printBoard(); String inputstr = null; //读取键盘输入的方法 InputStreamReader isr = new InputStreamReader(System.in); BufferedReader bfr = new BufferedReader(isr); //bfr.readLine()读取键盘每一行 System.out.println("请输入您下棋的坐标,应以x,y的格式:"); while ((inputstr = bfr.readLine()) != null) { //将用户输入的字符串以逗号作为分隔符,分隔成2个字符串 String[] posStrArr = inputstr.split(","); int xPos = Integer.parseInt(posStrArr[0]); int yPos = Integer.parseInt(posStrArr[1]); /**电脑随机生成2个整数,作为电脑下棋的坐标,赋给board数组 * 1、坐标的有效性,只能是数字,不能超出棋盘范围 * 2、下的棋的点,不能重复下棋 * 3、每次下棋后,需要扫描谁赢了 * */ int xcPos = (int)(Math.random() * 15); int ycPos = (int)(Math.random() * 15); boolean pd = gb.pd(gb.board, xPos - 1, yPos - 1); if (pd) { gb.board [xPos - 1][yPos - 1] = "●"; if (gb.isWin(gb.board, xPos - 1, yPos - 1, "●")) { System.out.println("恭喜你赢了"); continue; } else { System.out.println("请重新输入您下棋的坐标,应以x,y的格式:"); } } boolean pd2 = gb.pd(gb.board, xcPos - 1, ycPos - 1); while (pd2 == false) { xcPos = (int)(Math.random() * 15); ycPos = (int)(Math.random() * 15); pd2 = gb.pd(gb.board, xcPos - 1, ycPos - 1); } gb.board [xcPos - 1][ycPos - 1] = "&"; if (gb.isWin(gb.board, xcPos - 1, ycPos - 1, "&")) { System.out.println("你输了 "); continue; } gb.printBoard(); } } }
阅读更多
相关文章推荐
- 用Java实现Web服务器 HTTP协议
- 如何在Java中实现远程方法调用
- java语音聊天室原形的实现
- 使用Java实现数据报通讯过程
- 用Java实现Server-Client结构的聊天系统
- Java 实现的24点牌
- 基于Java实现Http服务器之二
- 在 Windows 中实现 Java 本地方法
- Java图形设计中,利用Bresenham算法实现直线线型,线宽的控制(NO 2D GRAPHICS)
- 在JAVA应用程序中如何实现FTP的功能
- 使用Java在Web上实现简易干特图之五
- 应用Java技术实现数据库应用系统
- JAVA中用动态代理类实现记忆功能(一)
- Java中利用散列表实现股票行情的查询
- 数据库连接池Java实现小结
- Java实现POP3服务器
- Java Tip: 用Reflection实现Visitor模式
- 在Java程序,如何实现自适应位置的快捷菜单
- http断点续传简单实现(java)
- 在Java中实现回调过程