package mysrc;
public class Board2
{
// Variable to keep track of the board.
int[][] board;
private int row;
private int column;
// The number of rows and columns of the board.
int boardSize;
private int[][] pointBoard;
/**
* The constructor to create a new Board object.
*
* @param rowInput
* The number of rows.
* @param columnInput
* The number of columns.
*/
public Board2(int rowInput, int columnInput){
// Sets the number of rows and columns to be 2 more than the input
// because the board will have a border of -1s around it.
this.row = rowInput + 2;
this.column = columnInput + 2;
// Creates a new board with 0s to initialise each position.
// A border of -1s will be placed around the board.
board = new int[row][column];
for (int currentRow = 0; currentRow < row; currentRow++)
{
for (int currentColumn = 0; currentColumn < column; currentColumn++)
{
// Adds a -1 if the current space is on the border.
if (currentRow == 0 || currentColumn == 0
|| currentRow == (row - 1)
|| currentColumn == (column - 1))
board[currentRow][currentColumn] = -1;
// Adds a 0 if the current space is not on the border.
else
board[currentRow][currentColumn] = 0;
}
}
}
/**
* Places the piece onto the board.
*
* @param row
* The horizontal position of the piece.
* @param column
* The vertical position of the piece.
* @param piece
* 1 to represent the first player or 2 to represent the second
* player.
*/
public void setPiece(int row, int column, int piece)
{
board[row][column] = piece;
}
/**
* Clears the board.
*/
public void clearBoard()
{
// Clears the board by changing all the values not found on the border
// to 0.
for (int currentRow = 1; currentRow <= 15; currentRow++)
{
for (int currentColumn = 1; currentColumn <= 15; currentColumn++)
{
board[currentRow][currentColumn] = 0;
}
}
}
/**
* Checks to see whether or not there is an empty space.
*
* @return True if there is an empty space, false if there isn't.
*/
public boolean isEmpty()
{
// Goes through every space and checks whether or not there is an empty
// space.
for (int currentRow = 1; currentRow < boardSize - 1; currentRow++)
{
// Returns true is an empty space if found.
for (int currentColumn = 1; currentColumn < boardSize - 1; currentColumn++)
{
if (board[currentRow][currentColumn] == 0)
return true;
}
}
// Returns false if no empty spaces were found.
return false;
}
/**
* Returns the value of the selected piece.
*
* @param row
* The horizontal position of the piece.
* @param column
* The vertical position of the piece.
* @return The value of the piece.
*/
public int getValue(int row, int column)
{
return board[row][column];
}
/**
* Checks to see if there is a winner.
*
* @param rowPlaced
* The row the piece was placed in.
* @param columnPlaced
* The column the piece was placed in.
* @return
*/
public boolean checkForWinner(int rowPlaced, int columnPlaced)
{
{
// Begins checking from the position the piece was placed in.
int lastColumn = columnPlaced;
int lastRow = rowPlaced;
// Keeps track of the value of the pieces being added up.
int pieceValue = board[rowPlaced][columnPlaced];
// The number of pieces found in a row.
int pieceCount = 0;
// Counts how many pieces are in a row horizontally to the right.
while (board[lastRow][lastColumn] == pieceValue)
{
pieceCount++;
if (pieceCount >= 5)
return true;
lastColumn++;
}
// Changes the column value to continue checking horizontally to the
// left.
lastColumn = columnPlaced - 1;
while (board[lastRow][lastColumn] == pieceValue)
{
pieceCount++;
if (pieceCount >= 5)
return true;
lastColumn--;
}
// Resets the number of pieces found in a row to 0.
// Resets the column value to go back to the space the piece was
// placed in.
pieceCount = 0;
lastColumn = columnPlaced;
// Counts how many pieces are in a row vertically downwards.
while (board[lastRow][lastColumn] == pieceValue)
{
pieceCount++;
if (pieceCount >= 5)
return true;
lastRow++;
}
// Changes the row value to continue checking vertically upwards.
lastRow = rowPlaced - 1;
while (board[lastRow][lastColumn] == pieceValue)
{
pieceCount++;
if (pieceCount >= 5)
return true;
lastRow--;
}
// Resets the number of pieces found in a row to 0.
// Resets the row value to go back to the space the piece was placed
// in.
pieceCount = 0;
lastRow = rowPlaced;
// Counts how many pieces are in a row diagonally to the upper
// right.
while (board[lastRow][lastColumn] == pieceValue)
{
pieceCount++;
if (pieceCount >= 5)
return true;
lastColumn++;
lastRow--;
}
// Changes the row and column values to continue checking diagonally
// to the lower left.
lastColumn = columnPlaced - 1;
lastRow = rowPlaced + 1;
while (board[lastRow][lastColumn] == pieceValue)
{
pieceCount++;
if (pieceCount >= 5)
return true;
lastColumn--;
lastRow++;
}
// Resets the number of pieces found in a row to 0.
// Resets the row and column values to go back to the space the
// piece was placed in.
pieceCount = 0;
lastColumn = columnPlaced;
lastRow = rowPlaced;
// Counts how many pieces are in a row diagonally to the upper left.
while (board[lastRow][lastColumn] == pieceValue)
{
pieceCount++;
if (pieceCount >= 5)
return true;
lastColumn++;
lastRow++;
}
// Changes the row and column values to continue checking diagonally
// to the lower right.
lastColumn = columnPlaced - 1;
lastRow = rowPlaced - 1;
while (board[lastRow][lastColumn] == pieceValue)
{
pieceCount++;
if (pieceCount >= 5)
return true;
lastColumn--;
lastRow--;
}
}
return false;
}
/**
* Debugging the point board for the AI system
*/
public void AIdebugger()
{
}
/**
* Debugging the point board for the AI system
*/
public void boardDebugger()
{
}
/**
* Checks the best possible moves for the AI
*
* @param difficulty
* The level of computer AI. Level 3 is the highest, thus using
* the most checks and finds a better move than a Level 1.
* @return the position on where to place the next piece
*/
public int[] AIchecker(int difficulty)
{
// Create the points board for debugging purposes.
pointBoard = new int[boardSize][boardSize];
// Use an array to return both the row and column of the highest points
// location.
int[] position = new int[2];
// Keep track of the highest number of points as well as the
// row / column it was in
int highestPoints = 0;
int highestRow = 1, highestColumn = 1;
// Keeps track if the player has any 4-chains. Only used for a Level 3
// computer.
boolean isFourChain = false;
for (int rowCount = 1; rowCount <= 15; rowCount++)
{
for (int columnCount = 1; columnCount <= 15; columnCount++)
{
// Have a point counter to determine the best position to
// place the piece.
int points = 0;
// Checks if the piece on the board is empty or not.
// (0 = empty. Thus check for total points on that piece.)
if (board[rowCount][columnCount] == 0)
{
// Begins checking from the position the piece was placed
// in.
int lastRow = rowCount;
int lastColumn = columnCount;
// The number of pieces found in a row.
int pieceCount = 0;
// Player # 2 is the human player so it only looks for human
// pieces.
// Piece # 0 means it is an empty square, which is where we
// want to check for moves on.
//
没有合适的资源?快使用搜索试试~ 我知道了~
Connect-Five.rar_connect
共39个文件
class:19个
java:17个
prefs:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 160 浏览量
2022-09-14
21:48:42
上传
评论
收藏 46KB RAR 举报
温馨提示
五子棋对战程序,可以选择人人对战或人机对战,对于初学者很有帮助
资源推荐
资源详情
资源评论
收起资源包目录
Connect-Five.rar (39个子文件)
month1
bin
ai
Sequence.class 4KB
GomokuAI.class 8KB
Direction.class 1KB
mysrc
Board2.class 7KB
GameBoard.class 5KB
UI.class 1KB
UserAction.class 593B
Engine.class 2KB
ConnectFive.class 3KB
Keyboard.class 1KB
util
Pair.class 1KB
ImageResource.class 2KB
gomoku
Move.class 2KB
Piece.class 967B
Square.class 1KB
Player.class 419B
Board.class 4KB
.settings
org.eclipse.jdt.core.prefs 598B
src
ai
GomokuAI.java 10KB
Sequence.java 4KB
Direction.java 1KB
mysrc
ConnectFive.java 5KB
GameBoard.java 10KB
UI.java 966B
UserAction.java 249B
Board2.java 27KB
Keyboard.java 1KB
Engine.java 3KB
util
ImageResource.java 1KB
Pair.java 882B
BoardCell.class 409B
gomoku
Board.java 4KB
Piece.java 376B
Player.java 209B
Square.java 685B
Move.java 1KB
Board.class 5KB
.project 382B
.classpath 301B
共 39 条
- 1
资源评论
邓凌佳
- 粉丝: 65
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- #P0015. 全排列 超级简单
- pta题库答案c语言之排序4统计工龄.zip
- pta题库答案c语言之树结构7堆中的路径.zip
- pta题库答案c语言之树结构3TreeTraversalsAgain.zip
- pta题库答案c语言之树结构2ListLeaves.zip
- pta题库答案c语言之树结构1树的同构.zip
- 基于C++实现民航飞行与地图简易管理系统可执行程序+说明+详细注释.zip
- pta题库答案c语言之复杂度1最大子列和问题.zip
- 三维装箱问题(Three-Dimensional Bin Packing Problem,3D-BPP)是一个经典的组合优化问题
- 以下是一些关于Linux线程同步的基本概念和方法.txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功