//Rule book for checking the moves are legal, also extends the cellMatrix public class draughtsMoveCheck extends draughtsMatrix { private boolean isSecondClick = false, previousMoveAllowsBunnyHop = false, isWinner = false; private int currentPlayer = 1; private int selectedRow = 0, selectedColumn = 0; private int attackRow = 0, attackColumn = 0; private String strDisplay = ""; public void draughtsMoveCheck () { } public void reset () { isSecondClick = false; isWinner = false; previousMoveAllowsBunnyHop = false; currentPlayer = 1; strDisplay = defaultMsg(); resetCellMatrix(); } public boolean isWinner () { return isWinner; } public void processMouseClick (int row, int column) { if (isSecondClick) //If choosing where to move { boolean legalMove = checkMove(row, column); //Check move is legal and update the matrix if it is if (isWinner || (legalMove && !previousMoveAllowsBunnyHop)) //If player has won, OR (the move was legal AND no bunny hops are possible) { isSecondClick = false; } } else { if (cellMatrix[row][column] == currentPlayer || cellMatrix[row][column] == (currentPlayer + 2)) //If the selected cell contains a coin of the current player { selectedRow = row; selectedColumn = column; isSecondClick = true; strDisplay = "click the tile you wish to move to, click same cell to deselect it"; } else { strDisplay = defaultMsg(); } } } private boolean checkBunnyHopMove (int currentCoin, int desRow, int desColumn) //This is only checking if a bunny hop is possible, it is not checking for errors { boolean legalBunnyHop = false; int adjCell = 0, nextHopCell = 0; if (currentCoin != 2) //Anything but normal blue { if (desRow > 1 && desColumn > 1) //Check NW move { adjCell = cellMatrix[desRow - 1][desColumn - 1]; nextHopCell = cellMatrix[desRow - 2][desColumn - 2]; if (isBunnyHopMovePossible(adjCell, nextHopCell)) { legalBunnyHop = true; } } if (desRow > 1 && desColumn < 6) //Check NE move { adjCell = cellMatrix[desRow - 1][desColumn + 1]; nextHopCell = cellMatrix[desRow - 2][desColumn + 2]; if (isBunnyHopMovePossible(adjCell, nextHopCell)) { legalBunnyHop = true; } } } if (currentCoin != 1) //Anything but normal red { if (desRow < 6 && desColumn > 1) //Check SW move { adjCell = cellMatrix[desRow + 1][desColumn - 1]; nextHopCell = cellMatrix[desRow + 2][desColumn - 2]; if (isBunnyHopMovePossible(adjCell, nextHopCell)) { legalBunnyHop = true; } } if (desRow < 6 && desColumn < 6) //Check SE move { adjCell = cellMatrix[desRow + 1][desColumn + 1]; nextHopCell = cellMatrix[desRow + 2][desColumn + 2]; if (isBunnyHopMovePossible(adjCell, nextHopCell)) { legalBunnyHop = true; } } } if (legalBunnyHop) { strDisplay = "Bunny hop allowed, click the same cell if you do not want to bunny hop"; } return legalBunnyHop; } private boolean isBunnyHopMovePossible (int adjCell, int nextHopCell) //Checks the adjCell has enemy in it, and the next hop cell is empty { if ((adjCell != 0 && adjCell != currentPlayer && adjCell != (currentPlayer + 2)) && nextHopCell == 0) { return true; } else { return false; } } private boolean checkMove (int desRow, int desColumn) //Rule book for checking if move is allowed { int selectedCoin = cellMatrix[selectedRow][selectedColumn]; boolean legalMove = true; if (desRow == selectedRow && desColumn == selectedColumn) //If deselecting your current coin (by clicking it twice) { if (previousMoveAllowsBunnyHop) //If you are doing a bunny hop, selecting ends your turn { if (currentPlayer == 1) { currentPlayer = 2; } else { currentPlayer = 1; } previousMoveAllowsBunnyHop = false; } strDisplay = defaultMsg(); } else if (cellMatrix[desRow][desColumn] != 0) //Moving to a cell that can not be moved onto { strDisplay = "That tile can not be moved onto"; legalMove = false; } else if (desRow > (selectedRow + 2) || desColumn > (selectedColumn + 2)) { //Trying to move to far strDisplay = "To far, if trying to do a bunny hop, do it in steps"; legalMove = false; } else if (desRow < (selectedRow - 2) || selectedColumn < (selectedColumn - 2)) { strDisplay = "To far, if trying to do a bunny hop, do it in steps"; legalMove = false; } else if (selectedCoin == 1 && desRow > selectedRow) //If red trying to move down { strDisplay = "Normal red coins can only move north"; legalMove = false; } else if (selectedCoin == 2 && desRow < selectedRow) //If blue trying to move up { strDisplay = "Normal blue coins can only move south"; legalMove = false; } else if ((desRow == selectedRow + 1 || desRow == selectedRow - 1) && (desColumn == selectedColumn + 1 || (desColumn == selectedColumn - 1))) { //Moving one tile only performMove(desRow, desColumn, false); //The flag is for if this move will take over an enemy } else if (checkAttackIsPossible(desRow, desColumn)) //Check to see if the attack is legal { performMove(desRow, desColumn, true); } else { legalMove = false; System.out.print("Some error haven't thought off"); } if (legalMove) { return true; } else { return false; } } private boolean checkAttackIsPossible (int desRow, int desColumn) //Make sure the coin can make the attack move (has something to jump over) { boolean legalMove = false; int currentCoin = cellMatrix[selectedRow][selectedColumn]; if (desRow == selectedRow - 2 && desColumn == selectedColumn + 2) //If moved NE { if (checkHopIsPossible(cellMatrix[selectedRow - 1][selectedColumn + 1])) { legalMove = true; attackRow = selectedRow - 1; attackColumn = selectedColumn + 1; } } else if (desRow == selectedRow - 2 && desColumn == selectedColumn - 2) //If moved NW { if (checkHopIsPossible(cellMatrix[selectedRow - 1][selectedColumn - 1])) { legalMove = true; attackRow = selectedRow - 1; attackColumn = selectedColumn - 1; } } if (desRow == selectedRow + 2 && desColumn == selectedColumn + 2) //If moved SE { if (checkHopIsPossible(cellMatrix[selectedRow + 1][selectedColumn + 1])) { legalMove = true; attackRow = selectedRow + 1; attackColumn = selectedColumn + 1; } } else if (desRow == selectedRow + 2 && desColumn == selectedColumn - 2) //If moved SW { if (checkHopIsPossible(cellMatrix[selectedRow + 1][selectedColumn - 1])) { legalMove = true; attackRow = selectedRow + 1; attackColumn = selectedColumn - 1; } } return legalMove; } private boolean checkHopIsPossible (int adjCell) //Make sure you can hop over this coin { if (adjCell != 0 && adjCell != currentPlayer && adjCell != (currentPlayer + 2)) { return true; } else { return false; } } private void performMove (int desRow, int desColumn, boolean isAttack) //update the cellMatrix to reflect the change { int currentCoin = cellMatrix[selectedRow][selectedColumn]; cellMatrix[selectedRow][selectedColumn] = 0; if (desRow == 0 || desRow == 7 || currentCoin == 3 || currentCoin == 4) //If at the top or bottom of the board, turn into king (or keep king if coin is already king) { cellMatrix[desRow][desColumn] = currentPlayer + 2; } else { cellMatrix[desRow][desColumn] = currentPlayer; } if (isAttack) { cellMatrix[attackRow][attackColumn] = 0; previousMoveAllowsBunnyHop = checkBunnyHopMove(cellMatrix[desRow][desColumn], desRow, desColumn); } if (checkWinner()) { isWinner = true; strDisplay = "Congrats player " + currentPlayer + " you win!"; } else if (previousMoveAllowsBunnyHop) { selectedRow = desRow; selectedColumn = desColumn; } else if (!previousMoveAllowsBunnyHop) { if (currentPlayer == 1) { currentPlayer = 2; } else { currentPlayer = 1; } strDisplay = defaultMsg(); } } private boolean checkWinner () //Checks if any more pieces of the enemy exist { int enemyNormal = 0, enemyKing = 0, currentCellContents = 0; if (currentPlayer == 1) { enemyNormal = 2; enemyKing = 4; } else { enemyNormal = 1; enemyKing = 3; } for (int row = 0; row < 8; row++) { for (int column = 0; column < 8; column++) { currentCellContents = cellMatrix[row][column]; if (currentCellContents == enemyNormal || currentCellContents == enemyKing) { return false; } } } return true; } private String defaultMsg () { return "player " + currentPlayer + " turn"; } public boolean isTileSelected () { return isSecondClick; } public String getStatusMsg () { return strDisplay; } public int getSelectedRow () { return selectedRow; } public int getSelectedColumn () { return selectedColumn; } }