enter image description here import java.util.Scanner;
public class EnglishCheckers {
// Global constants
public static final int RED = 1;
public static final int BLUE = -1;
public static final int EMPTY = 0;
public static final int SIZE = 8;
// You can ignore these constants
public static final int MARK = 3;
public static EnglishCheckersGUI grid;
public static Scanner getPlayerFullMoveScanner = null;
public static Scanner getStrategyScanner = null;
public static final int RANDOM = 1;
public static final int DEFENSIVE = 2;
public static final int SIDES = 3;
public static final int CUSTOM = 4;
public static void main(String[] args) {
// ******** Don't delete *********
// CREATE THE GRAPHICAL GRID
grid = new EnglishCheckersGUI(SIZE);
// *******************************
//showBoard(example);
//printMatrix(example);
//interactivePlay();
//twoPlayers();
/* ******** Don't delete ********* */
if (getPlayerFullMoveScanner != null){
getPlayerFullMoveScanner.close();
}
if (getStrategyScanner != null){
getStrategyScanner.close();
}
/* ******************************* */
}
public static int[][] createBoard() {
int[][] board = null;
board=new int[8][8]; // defines the new length of the array
for (int j=0; j<8; j=j+1)
{
for (int m=0; m<8; m=m+1) // these two "for" loops will set the value of each square of the board according to the instructions regarding it's location.
{
if (j==0|j==2)
{
if (m%2==0)
board[j][m]=1;
else
board[j][m]=0;
}
if (j==1)
{
if (m%2!=0)
board[j][m]=1;
else
board[j][m]=0;
}
if (j>2&j<5)
board[j][m]=0;
if (j==5|j==7)
{
if (m%2!=0)
board[j][m]=-1;
else
board[j][m]=0;
}
if (j==6)
{
if (m%2==0)
board[j][m]=-1;
else
board[j][m]=0;
}
}
}
return board;
}
public static int howManyDiscs (int[][] board, int player){ // this function will return the number of discs a player has on the board
int positive=0;
int negative=0;
for (int i=0; i<8; i=i+1)
{
for (int j=0; j<8; j=j+1)
{
if (board[i][j]>0) positive=positive+1;
if (board[i][j]<0) negative=negative+1;
}
}
if (player>0) return positive;
else return negative;
}
public static int[][] playerDiscs(int[][] board, int player) {
int[][] positions = null;
if (howManyDiscs(board,player)>0)
{
positions=new int[howManyDiscs(board,player)][2]; // defines the new length of the array
int line=0;
for (int i=0; i<8; i=i+1)
{
for (int j=0; j<8; j=j+1)
{
if (player>0&&board[i][j]>0) // will update the array if the player is 1
{
positions[line][0]=i;
positions[line][1]=j;
line=line+1;
}
if (player<0&&board[i][j]<0) // will update the array if the player is (-1)
{
positions[line][0]=i;
positions[line][1]=j;
line=line+1;
}
}
}
}
return positions;
}
public static boolean isBasicMoveValid(int[][] board, int player, int fromRow, int fromCol, int toRow, int toCol) {
boolean ans = false;
if (fromRow>=0&&fromRow<8&&fromCol>=0&&fromCol<8&&toRow>=0&&toRow<8&&toCol>=0&&toCol<8) // makes sure the coordinates are legal
{
if (board[fromRow][fromCol]==player|board[fromRow][fromCol]==player*2) // checks if a disc of the player exists in the origin square
{
if(board[toRow][toCol]==0) // checks if the destination square is legal
{
if (toCol==fromCol+1||toCol==fromCol-1) // checks if the destination column is legal
{
if (toRow==fromRow+1&&player!=-1) // makes sure the move is legal for a player that isn't (-1)
{
ans=true;
}
if (toRow==fromRow-1&&player!=1) // makes sure the move is legal for a player that isn't 1
{
ans=true;
}
}
}
}
}
return ans;
}
public static int[][] getAllBasicMoves(int[][] board, int player) {
int[][] moves = null;
int line=0;
for (int i=0; i<8; i=i+1)
{
for (int j=0; j<8; j=j+1) // these 2 "for" loops will add 1 to the counter "line" for each legal move a square has
{
if (isBasicMoveValid(board,player,i,j,i+1,j+1)) line=line+1;
if (isBasicMoveValid(board,player,i,j,i+1,j-1)) line=line+1;
if (isBasicMoveValid(board,player,i,j,i-1,j+1)) line=line+1;
if (isBasicMoveValid(board,player,i,j,i-1,j-1)) line=line+1;
}
}
moves=new int[line][4]; // creating the length of the output array according to the amount of possibilities found
line=0;
for (int i=0; i<8; i=i+1)
{
for (int j=0; j<8; j=j+1) // the if's below will insert every move that exists for each square to the array
{
if (isBasicMoveValid(board,player,i,j,i+1,j+1))
{
moves[line][0]=i;
moves[line][1]=j;
moves[line][2]=i+1;
moves[line][3]=j+1;
line=line+1;
}
if (isBasicMoveValid(board,player,i,j,i+1,j-1))
{
moves[line][0]=i;
moves[line][1]=j;
moves[line][2]=i+1;
moves[line][3]=j-1;
line=line+1;
}
if (isBasicMoveValid(board,player,i,j,i-1,j+1))
{
moves[line][0]=i;
moves[line][1]=j;
moves[line][2]=i-1;
moves[line][3]=j+1;
line=line+1;
}
if (isBasicMoveValid(board,player,i,j,i-1,j-1))
{
moves[line][0]=i;
moves[line][1]=j;
moves[line][2]=i-1;
moves[line][3]=j-1;
line=line+1;
}
}
}
return moves;
}
public static boolean isBasicJumpValid(int[][] board, int player, int fromRow, int fromCol, int toRow, int toCol) {
boolean ans = false;
if (fromRow>=0&&fromRow<8&&fromCol>=0&&fromCol<8&&toRow>=0&&toRow<8&&toCol>=0&&toCol<8) // makes sure the coordinates are legal
{
if (board[fromRow][fromCol]==player|board[fromRow][fromCol]==player*2) // checks if a disc of the player exists in the origin square
{
if(board[toRow][toCol]==0) // checks if the destination square is legal
{
if (toRow==fromRow+2)
{
if (toCol==fromCol+2)
{
if (player==1|player==2)
{
if (board[fromRow+1][fromCol+1]<0)
ans=true;
}
if (player==-2)
{
if (board[fromRow+1][fromCol+1]>0)
ans=true;
}
}
if (toCol==fromCol-2)
{
if (player==1|player==2)
{
if (board[fromRow+1][fromCol-1]<0)
ans=true;
}
if (player==-2)
{
if (board[fromRow+1][fromCol-1]>0)
ans=true;
}
}
}
if (toRow==fromRow-2)
{
if (toCol==fromCol+2)
{
if (player==-1|player==-2)
{
if (board[fromRow-1][fromCol+1]>0)
ans=true;
}
if (player==2)
{
if (board[fromRow-1][fromCol+1]<0)
ans=true;
}
}
if (toCol==fromCol-2)
{
if (player==-1|player==-2)
{
if (board[fromRow-1][fromCol-1]>0)
ans=true;
}
if (player==2)
{
if (board[fromRow-1][fromCol-1]<0)
ans=true;
}
}
}
}
}
}
return ans;
}
public static int [][] getRestrictedBasicJumps(int[][] board, int player, int row, int col) {
int[][] moves = null;
int line=0;
for (int i=row-2; i<=row+2; i=i+2)
{
for (int j=col-2; j<=col+2; j=j+2) // these 2 "for" loops will add 1 to the counter "line" for each legal move a square has
{
if (isBasicJumpValid(board,player,row,col,i,j)) line=line+1;
}
}
moves=new int[line][4]; // creating the length of the output array according to the amount of possibilities found
line=0;
for (int i=row-2; i<=row+2; i=i+2)
{
for (int j=col-2; j<=col+2; j=j+2)
{
if (isBasicJumpValid(board,player,row,col,i,j)) // will insert every possible jump to the array
{
moves[line][0]=row;
moves[line][1]=col;
moves[line][2]=i;
moves[line][3]=j;
line=line+1;
}
}
}
return moves;
}
public static int[][] getAllBasicJumps(int[][] board, int player) {
int [][] moves = null;
int [][] discs = playerDiscs(board,player); // this array holds a list of the discs the player has on the board currently
int counter=0;
for (int i=0; i<discs.length; i=i+1) // will add each disc's amount of possible moves to the counter
counter=counter+getRestrictedBasicJumps(board,player,discs[i][0],discs[i][1]).length;
moves=new int[counter][4]; // defining the array's size according to the sum of all discs possible jumps
int line=0;
for (int a=0; a<discs.length; a=a+1)
{
int [][] jumps = getRestrictedBasicJumps(board,player,discs[a][0],discs[a][1]); // creates the array "jumps" that holds the possible jumps for the current "for" disc
for (int i=0; i<jumps.length; i=i+1) // will insert each line of the jumps array to the moves array
{
moves[line][0]=jumps[i][0];
moves[line][1]=jumps[i][1];
moves[line][2]=jumps[i][2];
moves[line][3]=jumps[i][3];
line=line+1;
}
}
return moves;
}
public static boolean canJump(int[][] board, int player) {
boolean ans = false;
if (getAllBasicJumps(board,player).length>0) ans=true;
return ans;
}
public static boolean isMoveValid(int[][] board, int player, int fromRow, int fromCol, int toRow, int toCol) {
boolean ans = false;
if (isBasicJumpValid(board,player,fromRow,fromCol,toRow,toCol))
ans=true;
else
if (isBasicMoveValid (board,player,fromRow,fromCol,toRow,toCol))
ans=true;
return ans;
}
public static boolean hasValidMoves(int[][] board, int player) {
boolean ans = false;
if (getAllBasicMoves(board,player).length>0) ans=true;
if (canJump(board,player)) ans=true;
return ans;
}
public static int[][] playMove(int[][] board, int player, int fromRow, int fromCol, int toRow, int toCol) {
if (isBasicJumpValid(board,player,fromRow,fromCol,toRow,toCol)) // in case it's a jump- will replace the eaten player with 0
{
if (fromRow>toRow)
{
if (fromCol>toCol) board[fromRow-1][fromCol-1]=0;
else board[fromRow-1][fromCol+1]=0;
}
else
{
if (fromCol>toCol) board[fromRow+1][fromCol-1]=0;
else board[fromRow+1][fromCol+1]=0;
}
}
if (player==1&toRow==7)
{
board[toRow][toCol]=2;
board[fromRow][fromCol]=0;
}
else
{
if (player==-1&toRow==0)
{
board[toRow][toCol]=-2;
board[fromRow][fromCol]=0;
}
else
{
board[toRow][toCol]=player;
board[fromRow][fromCol]=0;
}
}
return board;
}
public static boolean gameOver(int[][] board, int player) {
boolean ans = false;
if (!hasValidMoves(board,player)) ans=true;
if (playerDiscs(board,1)==null | playerDiscs(board,-1)==null) ans=true;
return ans;
}
public static int findTheLeader(int[][] board) {
int ans = 0;
int player1=0, player2=0;
for (int i=0; i<8; i=i+1)
{
for (int j=0; j<8; j=j+1)
{
if (board[i][j]==1) player1=player1+1;
if (board[i][j]==2) player1=player1+2;
if (board[i][j]==-1) player1=player2+1;
if (board[i][j]==-2) player1=player2+2;
}
}
if (player1>player2)
ans=1;
else
if (player2>player1)
ans=-1;
else
ans=0;
return ans;
}
public static int[][] randomPlayer(int[][] board, int player) {
if (hasValidMoves(board,player))
{
if (getAllBasicMoves(board,player).length>0 && isMoveValid(board,player,getAllBasicMoves(board,player)[0][0],getAllBasicMoves(board,player)[0][1],getAllBasicMoves(board,player)[0][2],getAllBasicMoves(board,player)[0][3]))
board=playMove(board,player,getAllBasicMoves(board,player)[0][0],getAllBasicMoves(board,player)[0][1],getAllBasicMoves(board,player)[0][2],getAllBasicMoves(board,player)[0][3]);
else
if (getAllBasicJumps(board,player).length>0 && isMoveValid(board,player,getAllBasicJumps(board,player)[0][0],getAllBasicJumps(board,player)[0][1],getAllBasicJumps(board,player)[0][2],getAllBasicJumps(board,player)[0][3]))
board=playMove(board,player,getAllBasicJumps(board,player)[0][0],getAllBasicJumps(board,player)[0][1],getAllBasicJumps(board,player)[0][2],getAllBasicJumps(board,player)[0][3]);
}
return board;
}
public static int[][] defensivePlayer(int[][] board, int player) {
if (hasValidMoves(board,player))
{
if (canJump(board,player))
board=playMove(board,player,getAllBasicJumps(board,player)[0][0],getAllBasicJumps(board,player)[0][1],getAllBasicJumps(board,player)[0][2],getAllBasicJumps(board,player)[0][3]);
else
{
boolean goodMove=false;
int i=0;
int [][] arr=board;
while (i<getAllBasicMoves(board,player).length && !goodMove)
{
arr=playMove(board,player,getAllBasicMoves(board,player)[i][0],getAllBasicMoves(board,player)[i][1],getAllBasicMoves(board,player)[i][2],getAllBasicMoves(board,player)[i][3]);
if (!canJump(arr,-player))
goodMove=true;
i=i+1;
}
board=arr;
}
}
return board;
}
public static int[][] sidesPlayer(int[][] board, int player) {
if (hasValidMoves(board,player))
{
if (canJump(board,player))
board=playMove(board,player,getAllBasicJumps(board,player)[0][0],getAllBasicJumps(board,player)[0][1],getAllBasicJumps(board,player)[0][2],getAllBasicJumps(board,player)[0][3]);
else
{
if (getAllBasicMoves(board,player).length>1)
{
int bestMove=0, minHefresh=7;
for (int i=1; i<getAllBasicMoves(board,player).length; i=i+1)
{
if (Math.abs(0-getAllBasicMoves(board,player)[i][3])<minHefresh)
{
bestMove=i;
minHefresh=Math.abs(0-getAllBasicMoves(board,player)[i][3]);
}
if (Math.abs(0-getAllBasicMoves(board,player)[i-1][3])<minHefresh)
{
bestMove=i-1;
minHefresh=Math.abs(0-getAllBasicMoves(board,player)[i-1][3]);
}
if (Math.abs(7-getAllBasicMoves(board,player)[i][3])<minHefresh)
{
bestMove=i;
minHefresh=Math.abs(7-getAllBasicMoves(board,player)[i][3]);
}
if (Math.abs(7-getAllBasicMoves(board,player)[i-1][3])<minHefresh)
{
bestMove=i-1;
minHefresh=Math.abs(7-getAllBasicMoves(board,player)[i-1][3]);
}
}
board=playMove(board,player,getAllBasicMoves(board,player)[bestMove][0],getAllBasicMoves(board,player)[bestMove][1],getAllBasicMoves(board,player)[bestMove][2],getAllBasicMoves(board,player)[bestMove][3]);
}
else
board=playMove(board,player,getAllBasicMoves(board,player)[0][0],getAllBasicMoves(board,player)[0][1],getAllBasicMoves(board,player)[0][2],getAllBasicMoves(board,player)[0][3]);
}
}
return board;
}
//******************************************************************************//
/* ---------------------------------------------------------- *
* Play an interactive game between the computer and you *
* ---------------------------------------------------------- */
public static void interactivePlay() {
int[][] board = createBoard();
showBoard(board);
System.out.println("Welcome to the interactive Checkers Game !");
int strategy = getStrategyChoice();
System.out.println("You are the first player (RED discs)");
boolean oppGameOver = false;
while (!gameOver(board, RED) && !oppGameOver) {
board = getPlayerFullMove(board, RED);
oppGameOver = gameOver(board, BLUE);
if (!oppGameOver) {
EnglishCheckersGUI.sleep(200);
board = getStrategyFullMove(board, BLUE, strategy);
}
}
int winner = 0;
if (playerDiscs(board, RED).length == 0 | playerDiscs(board, BLUE).length == 0){
winner = findTheLeader(board);
}
if (winner == RED) {
System.out.println();
System.out.println("\t *************************");
System.out.println("\t * You are the winner !! *");
System.out.println("\t *************************");
}
else if (winner == BLUE) {
System.out.println("\n======= You lost :( =======");
}
else
System.out.println("\n======= DRAW =======");
}
/* --------------------------------------------------------- *
* A game between two players *
* --------------------------------------------------------- */
public static void twoPlayers() {
int[][] board = createBoard();
showBoard(board);
System.out.println("Welcome to the 2-player Checkers Game !");
boolean oppGameOver = false;
while (!gameOver(board, RED) & !oppGameOver) {
System.out.println("\nRED's turn");
board = getPlayerFullMove(board, RED);
oppGameOver = gameOver(board, BLUE);
if (!oppGameOver) {
System.out.println("\nBLUE's turn");
board = getPlayerFullMove(board, BLUE);
}
}
int winner = 0;
if (playerDiscs(board, RED).length == 0 | playerDiscs(board, BLUE).length == 0)
winner = findTheLeader(board);
System.out.println();
System.out.println("\t ************************************");
if (winner == RED)
System.out.println("\t * The red player is the winner !! *");
else if (winner == BLUE)
System.out.println("\t * The blue player is the winner !! *");
else
System.out.println("\t * DRAW !! *");
System.out.println("\t ************************************");
}
/* --------------------------------------------------------- *
* Get a complete (possibly a sequence of jumps) move *
* from a human player. *
* --------------------------------------------------------- */
public static int[][] getPlayerFullMove(int[][] board, int player) {
// Get first move/jump
int fromRow = -1, fromCol = -1, toRow = -1, toCol = -1;
boolean jumpingMove = canJump(board, player);
boolean badMove = true;
getPlayerFullMoveScanner = new Scanner(System.in);//I've modified it
while (badMove) {
if (player == 1){
System.out.println("Red, Please play:");
} else {
System.out.println("Blue, Please play:");
}
fromRow = getPlayerFullMoveScanner.nextInt();
fromCol = getPlayerFullMoveScanner.nextInt();
int[][] moves = jumpingMove ? getAllBasicJumps(board, player) : getAllBasicMoves(board, player);
markPossibleMoves(board, moves, fromRow, fromCol, MARK);
toRow = getPlayerFullMoveScanner.nextInt();
toCol = getPlayerFullMoveScanner.nextInt();
markPossibleMoves(board, moves, fromRow, fromCol, EMPTY);
badMove = !isMoveValid(board, player, fromRow, fromCol, toRow, toCol);
if (badMove)
System.out.println("\nThis is an illegal move");
}
// Apply move/jump
board = playMove(board, player, fromRow, fromCol, toRow, toCol);
showBoard(board);
// Get extra jumps
if (jumpingMove) {
boolean longMove = (getRestrictedBasicJumps(board, player, toRow, toCol).length > 0);
while (longMove) {
fromRow = toRow;
fromCol = toCol;
int[][] moves = getRestrictedBasicJumps(board, player, fromRow, fromCol);
boolean badExtraMove = true;
while (badExtraMove) {
markPossibleMoves(board, moves, fromRow, fromCol, MARK);
System.out.println("Continue jump:");
toRow = getPlayerFullMoveScanner.nextInt();
toCol = getPlayerFullMoveScanner.nextInt();
markPossibleMoves(board, moves, fromRow, fromCol, EMPTY);
badExtraMove = !isMoveValid(board, player, fromRow, fromCol, toRow, toCol);
if (badExtraMove)
System.out.println("\nThis is an illegal jump destination :(");
}
// Apply extra jump
board = playMove(board, player, fromRow, fromCol, toRow, toCol);
showBoard(board);
longMove = (getRestrictedBasicJumps(board, player, toRow, toCol).length > 0);
}
}
return board;
}
/* --------------------------------------------------------- *
* Get a complete (possibly a sequence of jumps) move *
* from a strategy. *
* --------------------------------------------------------- */
public static int[][] getStrategyFullMove(int[][] board, int player, int strategy) {
if (strategy == RANDOM)
board = randomPlayer(board, player);
else if (strategy == DEFENSIVE)
board = defensivePlayer(board, player);
else if (strategy == SIDES)
board = sidesPlayer(board, player);
showBoard(board);
return board;
}
/* --------------------------------------------------------- *
* Get a strategy choice before the game. *
* --------------------------------------------------------- */
public static int getStrategyChoice() {
int strategy = -1;
getStrategyScanner = new Scanner(System.in);
System.out.println("Choose the strategy of your opponent:" +
"\n\t(" + RANDOM + ") - Random player" +
"\n\t(" + DEFENSIVE + ") - Defensive player" +
"\n\t(" + SIDES + ") - To-the-Sides player player");
while (strategy != RANDOM & strategy != DEFENSIVE
& strategy != SIDES) {
strategy=getStrategyScanner.nextInt();
}
return strategy;
}
/* --------------------------------------- *
* Print the possible moves *
* --------------------------------------- */
public static void printMoves(int[][] possibleMoves) {
for (int i = 0; i < 4; i = i+1) {
for (int j = 0; j < possibleMoves.length; j = j+1)
System.out.print(" " + possibleMoves[j][i]);
System.out.println();
}
}
/* --------------------------------------- *
* Mark/unmark the possible moves *
* --------------------------------------- */
public static void markPossibleMoves(int[][] board, int[][] moves, int fromRow, int fromColumn, int value) {
for (int i = 0; i < moves.length; i = i+1)
if (moves[i][0] == fromRow & moves[i][1] == fromColumn)
board[moves[i][2]][moves[i][3]] = value;
showBoard(board);
}
/* --------------------------------------------------------------------------- *
* Shows the board in a graphic window *
* you can use it without understanding how it works. *
* --------------------------------------------------------------------------- */
public static void showBoard(int[][] board) {
grid.showBoard(board);
}
/* --------------------------------------------------------------------------- *
* Print the board *
* you can use it without understanding how it works. *
* --------------------------------------------------------------------------- */
public static void printMatrix(int[][] matrix){
for (int i = matrix.length-1; i >= 0; i = i-1){
for (int j = 0; j < matrix.length; j = j+1){
System.out.format("%4d", matrix[i][j]);
}
System.out.println();
}
}
}
喜。刚开始编码,安装ide intellij但我无法运行代码。 非常感谢!
如您所见,运行命令的绿色箭头已禁用。此外,当我尝试从工具栏的运行菜单运行时,它只是说编辑配置。
答案 0 :(得分:1)
只需将光标放在“主”方法行上,然后按向右键并选择运行。
答案 1 :(得分:0)
右键单击主方法代码,然后点击“运行”。