1.棋盘类
/**
* 棋盘
*
*/
public class Chessboard {
// 定义一个二维数组来充当棋盘
private String[][] board;
// 定义棋盘的大小
public final int BOARD_SIZE = 15;
/**
* 初始化棋盘
*
* @return void
*/
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");// 每打印完一行数组元素就换行一次
}
}
/**
* 给棋盘位置赋值
*
* @param posX
* X坐标
* @param posY
* Y坐标
* @param chessman
* 棋子
*/
public void setBoard(int posX, int posY, String chessman) {
board[posX][posY] = chessman;
}
/**
* 返回棋盘
*
* @return 返回棋盘
*/
public String[][] getBoard() {
return board;
}
public int getBoardSize() {
return BOARD_SIZE;
}
}
2.棋手类---- 你和电脑
public class ChessMan {
static final String blackChessman="●";
static final String whiteChessman="○";
/**
* @return String 黑棋
*/
public static String getBlackChessman() {
return blackChessman;
}
/**
* @return String 白棋
*/
public static String getWhiteChessman() {
return whiteChessman;
}
}
3.五子棋的具体实现类
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;
/**
* 五子棋游戏类
*
*/
public class GobangGame {
private final int WIN_COUNT = 5;// 定义达到赢条件的棋子数目
private int posX = 0;// 定义用户输入的X坐标
private int posY = 0;// 定义用户输入的X坐标
private Chessboard chessboard;// 定义棋盘
/**
* 构造器,初始化棋盘和棋子属性
*
* @param chessboard
* 棋盘类
*/
public GobangGame(Chessboard chessboard) {
this.chessboard = chessboard;
}
/**
* 检查输入是否合法。
*
* @param inputStr
* 由控制台输入的字符串。
* @return 字符串合法返回true,反则返回false。
*/
public boolean isValid(String inputStr) {
// 将用户输入的字符串以逗号(,)作为分隔,分隔成两个字符串
String[] posStrArr = inputStr.split(",");
try {
posX = Integer.parseInt(posStrArr[0]) - 1;//用户棋子的X坐标
posY = Integer.parseInt(posStrArr[1]) - 1;//用户棋子的Y坐标
} catch (NumberFormatException e) {
chessboard.printBoard();
System.out.println("请以(数字,数字)的格式输入:");
return false;
}
// 检查输入数值是否在范围之内
if (posX < 0 || posX >= chessboard.getBoardSize() || posY < 0
|| posY >= chessboard.getBoardSize() ) {
chessboard.printBoard();
System.out.println("X与Y坐标只能大于等于1,与小于等于" + chessboard.getBoardSize()
+ ",请重新输入:");
return false;
}
// 检查输入的位置是否已经有棋子
String[][] board = chessboard.getBoard();
if (board[posX][posY] != "十") {
chessboard.printBoard();
System.out.println("此位置已经有棋子,请重新输入:");
return false;
}
return true;
}
/**
* 开始下棋
*/
public void start() throws Exception {
chessboard.initBoard();
chessboard.printBoard();
Scanner scan = new Scanner(System.in);
boolean isOver = false;// true为游戏结束
while(!isOver){
System.out.print("请输入棋子的位置:");
String inputStr = scan.nextLine();
//isOver = false;
if (!isValid(inputStr)) {
continue;// 如果不合法,要求重新输入,再继续
}
String chessman =ChessMan.getBlackChessman();
chessboard.setBoard(posX, posY,chessman);
// 判断用户是否赢了
if (isWon(posX, posY,chessman)) {
isOver = true;
} else {
// 计算机随机选择位置坐标
int[] computerPosArr = computerDo();
chessman = ChessMan.getWhiteChessman();
chessboard.setBoard(computerPosArr[0], computerPosArr[1],chessman);
// 判断计算机是否赢了
if (isWon(computerPosArr[0], computerPosArr[1], chessman)) {
isOver = true;
}
}
// 如果产生胜者,询问用户是否继续游戏
if (isOver) {
// 如果继续,重新初始化棋盘,继续游戏
if (isReplay(chessman)) {
isOver = false;
chessboard.initBoard();
chessboard.printBoard();
continue;
}
// 如果不继续,退出程序
break;
}
chessboard.printBoard();
}
}
/**
* 是否重新开始下棋。
*
* @param chessman
* "●"为用户,"○"为计算机。
* @return 开始返回true,反则返回false。
*/
public boolean isReplay(String chessman) throws Exception {
chessboard.printBoard();
String playerWin = "恭喜您,您赢了,";
String computerWin = "很遗憾,您输了,";
String message = chessman.equals(ChessMan.getBlackChessman()) ? playerWin:computerWin ;
System.out.println(message + "再下一局?(y/n)");
Scanner scan = new Scanner(System.in);
if (scan.next().equals("y")) {
// 开始新一局
return true;
}
return false;
}
/**
* 计算机随机下棋
*/
public int[] computerDo() {
Random random = new Random();
int posX = random.nextInt(chessboard.getBoardSize()-1);
int posY = random.nextInt(chessboard.getBoardSize()-1);
String[][] board = chessboard.getBoard();
while (board[posX][posY] != "十" && board[posX][posY]!=ChessMan.getBlackChessman()&& board[posX][posY]!=ChessMan.getWhiteChessman()) {
posX = random.nextInt(chessboard.getBoardSize()-1);
posY = random.nextInt(chessboard.getBoardSize()-1);
}
int[] result = { posX, posY };
return result;
}
/**
* 判断输赢
*
* @param posX
* 棋子的X坐标。
* @param posY
* 棋子的Y坐标
* @param ico
* 棋子类型
* @return 如果有五颗相邻棋子连成一条直接,返回真,否则相反。
*/
public boolean isWon(int posX, int posY, String ico) {
int startX = 0;// 直线起点的X最小坐标
int startY = 0;// 直线起点Y最小坐标
int endX = chessboard.getBoardSize()-1;// 直线结束X最大坐标
int endY = chessboard.getBoardSize()-1;// 直线结束Y最大坐标
int sameCount = 0;// 同条直线上相邻棋子累积数
int temp = 0;
// 计算起点的最小X坐标与Y坐标
temp = posX - WIN_COUNT + 1;
startX = (temp<0)? 0:temp;
temp = posY - WIN_COUNT + 1;
startY = (temp<0)?0:temp;
// 计算终点的最大X坐标与Y坐标
temp = posX + WIN_COUNT - 1;
endX = temp > (chessboard.getBoardSize()-1)?(chessboard.getBoardSize()-1):temp;
temp = posY + WIN_COUNT - 1;
endY = temp >(chessboard.getBoardSize()-1)?(chessboard.getBoardSize()-1):temp;
// 计算一行中相同棋子的数目
String[][] board = chessboard.getBoard();
for (int i = startY; i < endY; i++) {
if (board[posX][i] == ico && board[posX][i + 1] == ico) {
sameCount++;
} else if (sameCount < WIN_COUNT - 1) {
sameCount = 0;
}
}
// 计算一行中相同棋子的数目
if (sameCount == 0) {
for (int i = startX; i < endX; i++) {
if (board[i][posY] == ico && board[i + 1][posY] == ico) {
sameCount++;
} else if (sameCount < WIN_COUNT - 1) {
sameCount = 0;
}
}
}
// 从左上到右下计算相同棋子的数目
if (sameCount == 0) {
int j = startY;
for (int i = startX; i < endX; i++) {
if (j < endY) {
if (board[i][j] == ico && board[i + 1][j + 1] == ico) {
sameCount++;
} else if (sameCount < WIN_COUNT - 1) {
sameCount = 0;
}
j++;
}
}
}
if (sameCount == 0) {
// 从右上到左下计算相同棋子的数目
int j = startX;
for (int i = endY; i > startY; i--) {
if (j < endY) {
if (board[i][j] == ico && board[i-1][j+1] == ico) {
sameCount++;
} else if (sameCount < WIN_COUNT - 1) {
sameCount = 0;
}
j++;
}
}
}
return sameCount >= (WIN_COUNT-1)?true:false;
}
}
4.控制类
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
GobangGame gb = new GobangGame(new Chessboard());
gb.start();
}
}
源代码