JAVA-五子棋实现
来源:互联网 发布:淘宝限制购物怎么解除 编辑:程序博客网 时间:2024/05/23 20:22
无聊,学习JAVA中~,随手编写一个五子棋,其实逻辑真的很简单、很简单,只是用下,熟悉下语法神马哒。等看完swing,写个界面吧。
package fivepiece;import java.util.Scanner;import java.util.*;import java.lang.*;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created by BeckyLu on 15/12/22. */public class FivePiece { private int whiteNum = 0; private int blackNum = 0; private PieceInfo mPieceInfp = new PieceInfo(); public void run() { Scanner myScanner = new Scanner(System.in); int num = 0; while(true) { if(num % 2 == 0) System.out.println("请输入白色棋子的横坐标"); else System.out.println("请输入黑色棋子的横坐标"); int mWidth = myScanner.nextInt() - 1; if(num % 2 == 0) System.out.println("请输入白色棋子的纵坐标"); else System.out.println("请输入黑色棋子的纵坐标"); int mHeight = myScanner.nextInt() - 1; if(num % 2 == 0) { int result = mPieceInfp.setmPieceInfoBase(PieceCom.COL_W, mWidth, mHeight); if(result == -1) { System.out.println("这个位置上已经放置棋子了,请选择其他位置"); continue; } whiteNum += 1; } else { int result = mPieceInfp.setmPieceInfoBase(PieceCom.COL_B, mWidth, mHeight); if(result == -1) { System.out.println("这个位置上已经放置棋子了,请选择其他位置"); continue; } blackNum += 1; } if(mPieceInfp.judgeSuccess(mWidth, mHeight)) { if(num % 2 == 0) { System.out.println("白色赢"); } else { System.out.println("黑色赢"); } break; } num += 1; } }}class PieceCom { public final static int COL_W = 1; public final static int COL_B = 2; public final static int width = 10; public final static int height = 10;}class PieceInfoBase { private int color = 0; private boolean isEmpty = true; private int widthIndex = -1; private int heightIndex = -1; public void setColor(int _col) { if ( _col == PieceCom.COL_W || _col == PieceCom.COL_B ) { color = _col; isEmpty = false; } } public PieceInfoBase(int _widthIndex, int _heightIndex) { widthIndex = _widthIndex; heightIndex = _heightIndex; } public boolean getIsEmpty() { return isEmpty; } public int getColor() { return color; } public void clearColor() { color = 0; isEmpty = true; }}class PieceInfo { private PieceInfoBase [][] mPieceInfo = new PieceInfoBase[PieceCom.width][PieceCom.height]; public PieceInfo() { for (int i = 0; i != PieceCom.height; ++i) { for(int j = 0; j != PieceCom.width; ++j) { mPieceInfo[j][i] = new PieceInfoBase(j, i); } } } public void resetPiece() { for (int i = 0; i != PieceCom.height; ++i) { for (int j = 0; j != PieceCom.width; ++j) { if (!mPieceInfo[j][i].getIsEmpty()) mPieceInfo[j][i].clearColor(); } } } public int setmPieceInfoBase(int _col, int _widthIndex, int _heightIndex) { if (_widthIndex >= 0 && _widthIndex <= 9 && _heightIndex >=0 && _heightIndex <= 9) { if(mPieceInfo[_widthIndex][_heightIndex].getIsEmpty()) { mPieceInfo[_widthIndex][_heightIndex].setColor(_col); return 0; } } return -1; } public boolean judgeSuccess(int _widthIndex, int _heightIndex) { piecePolic mP = new PiecePolicy(); return mP.piecePolicFunc(mPieceInfo, _widthIndex, _heightIndex); }}interface piecePolic{ boolean piecePolicFunc(PieceInfoBase [][] _PieceInfo, int _widthIndex, int _heightIndex);}class PiecePolicy implements piecePolic{ @Override public boolean piecePolicFunc(PieceInfoBase[][] _PieceInfo, int _widthIndex, int _heightIndex) { int color = _PieceInfo[_widthIndex][_heightIndex].getColor(); int leftNum = 0; for(int i = 1; i != 5; ++i) { int widthIndex = _widthIndex - i; if(widthIndex >= 0 && _PieceInfo[widthIndex][_heightIndex].getColor() == color) { leftNum = leftNum + 1; } else{ break; } } for (int j = 1; j != 5; ++j) { int widthIndex = _widthIndex + j; if (widthIndex <= 9 && _PieceInfo[widthIndex][_heightIndex].getColor() == color) leftNum = leftNum + 1; else break; } if (leftNum == 4) { return true; } int rightNum = 0; for(int i = 1; i != 5; ++i) { int heightIndex = _heightIndex - i; if(heightIndex >= 0 && _PieceInfo[_widthIndex][heightIndex].getColor() == color) { System.out.println("color = " + _PieceInfo[_widthIndex][heightIndex].getColor() ); rightNum = rightNum + 1; } else{ break; } } for (int j = 1; j != 5; ++j) { int heightIndex = _heightIndex + j; if (heightIndex <= 9 && _PieceInfo[_widthIndex][heightIndex].getColor() == color) rightNum = rightNum + 1; else break; } if (rightNum == 4) { return true; } int upNum = 0; for(int i = 1; i != 5; ++i) { int widthIndex = _widthIndex - i; int heightIndex = _heightIndex - i; if(widthIndex >= 0 && heightIndex >= 0 && _PieceInfo[widthIndex][heightIndex].getColor() == color) { upNum = upNum + 1; } else{ break; } } for (int j = 1; j != 5; ++j) { int widthIndex = _widthIndex + j; int heightIndex = _heightIndex + j; if (widthIndex <= 9 && heightIndex <= 9 && _PieceInfo[widthIndex][_heightIndex].getColor() == color) upNum = upNum + 1; else break; } if (upNum == 4) { return true; } int downNum = 0; for(int i = 1; i != 5; ++i) { int widthIndex = _widthIndex - i; int heightIndex = _heightIndex + i; if(widthIndex >= 0 && heightIndex <= 9 && _PieceInfo[widthIndex][_heightIndex].getColor() == color) { downNum = downNum + 1; } else{ break; } } for (int j = 1; j != 5; ++j) { int widthIndex = _widthIndex + j; int heightIndex = _heightIndex - j; if (widthIndex <= 9 && heightIndex >= 0 && _PieceInfo[widthIndex][_heightIndex].getColor() == color) downNum = downNum + 1; else break; } if (downNum == 4) { return true; } return false; }}
0 0
- java实现的五子棋
- JAVA-五子棋实现
- java实现简单五子棋
- java五子棋初实现
- java实现控制台五子棋
- 五子棋AI算法 Java实现
- 五子棋AI算法 Java实现
- java实现简单的五子棋
- java控制台实现五子棋功能
- Java程序实现五子棋小游戏
- Java智能五子棋的实现
- Java简单实现的五子棋
- 五子棋的Java实现 详解
- 五子棋网络版JAVA实现(-)
- 五子棋网络版JAVA实现(二)
- 幼稚的五子棋界面(java实现)
- JAVA 五子棋 判断输赢的代码实现
- java基础-简单实现两人五子棋
- android-CTS调试方法
- KMP算法
- RandomAccessFile
- ecelpse网格DEBUG步骤
- 二叉树的先序中序后序遍历问题
- JAVA-五子棋实现
- 本地环境 XAMPP+phpStorm+XDebug+chrome 配置和断点调试
- HDU 1597 find the nth digit 水题
- github优秀开源项目
- IOS 成功失败的Block写法
- 静态变量(static variables)那点事
- Autocomplete
- 【AJAX】学习总结
- 归并排序