无聊,学习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;
}
}