import java.util.List; import java.util.Optional; import java.util.Scanner; /** * QUEEN *
* Converted from BASIC to Java by Aldrin Misquitta (@aldrinm)
*/
public class Queen {
public static final int WINNING_POSITION = 158;
public static final int FORFEIT_MOVE = 0;
// @formatter:off
private static final int[] S = {
81, 71, 61, 51, 41, 31, 21, 11,
92, 82, 72, 62, 52, 42, 32, 22,
103, 93, 83, 73, 63, 53, 43, 33,
114, 104, 94, 84, 74, 64, 54, 44,
125, 115, 105, 95, 85, 75, 65, 55,
136, 126, 116, 106, 96, 86, 76, 66,
147, 137, 127, 117, 107, 97, 87, 77,
158, 148, 138, 128, 118, 108, 98, 88
};
// @formatter:on
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
printWithTab("QUEEN", 33);
printWithTab("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY", 15);
System.out.println("\n\n");
askAndShowInstructions();
boolean anotherGame;
do {
printBoard();
Move firstMove = getUserFirstMove();
if (firstMove.move == 0) {
printWonByForfeit();
}
if (isTopmostRowOrRightmostColumn(firstMove)) {
playOneGame(firstMove);
}
anotherGame = askForAnotherGame();
} while (anotherGame);
System.out.println("\nOK --- THANKS AGAIN.");
}
/**
* Play one game starting with the first move from the user
*/
private static void playOneGame(Move firstMove) {
boolean gameInProgress = true;
Move userMove = firstMove;
while (gameInProgress) {
if (userMove.move == WINNING_POSITION) {
//players wins
printCongratulatoryMessage();
gameInProgress = false;
} else {
ComputerMove computerMove = getComputerMove(userMove);
System.out.printf("COMPUTER MOVES TO SQUARE %d\n", computerMove.move);
if (computerMove.move == WINNING_POSITION) {
printComputerWins();
gameInProgress = false;
} else {
userMove = getValidUserMove(computerMove);
if (userMove.move == FORFEIT_MOVE) {
printWonByForfeit();
gameInProgress = false;
} else if (userMove.move == WINNING_POSITION) {
printCongratulatoryMessage();
gameInProgress = false;
}
}
}
}
}
/**
* Get the user's first move
*/
private static Move getUserFirstMove() {
boolean validMove;
Move move;
do {
System.out.print("WHERE WOULD YOU LIKE TO START? ");
int movePosition = scanner.nextInt();
move = new Move(movePosition);
validMove = false;
if (!isTopmostRowOrRightmostColumn(move)) {
System.out.println("PLEASE READ THE DIRECTIONS AGAIN.");
System.out.println("YOU HAVE BEGUN ILLEGALLY.");
System.out.println();
} else {
validMove = true;
}
scanner.nextLine();
} while (!validMove);
return move;
}
/**
* Prompt and get a valid move from the user. Uses the computer's latest move to validate the next move.
*/
private static Move getValidUserMove(ComputerMove latestComputerMove) {
boolean validUserMove = false;
Move userMove = null;
while (!validUserMove) {
userMove = getUserMove();
if (!validateUserMove(userMove, latestComputerMove)) {
System.out.println("\nY O U C H E A T . . . TRY AGAIN");
} else {
validUserMove = true;
}
}
return userMove;
}
private static void printWonByForfeit() {
System.out.println("\nIT LOOKS LIKE I HAVE WON BY FORFEIT.\n");
}
private static boolean validateUserMove(Move userMove, ComputerMove computerMove) {
if (userMove.move <= computerMove.move) {
return false;
}
if (userMove.move == FORFEIT_MOVE || userMove.move == WINNING_POSITION) {
return true;
}
int tensValueUser = userMove.move / 10;
int unitsValueUser = userMove.move - (tensValueUser * 10);
int unitsValueComputer = computerMove.u;
int tensValueComputer = computerMove.t;
int p = unitsValueUser - unitsValueComputer;
if (p != 0) {
if ((tensValueUser - tensValueComputer) != p) {
return (tensValueUser - tensValueComputer) == 2 * p;
} else {
return true;
}
} else {
int l = tensValueUser - tensValueComputer;
return l > 0;
}
}
private static Move getUserMove() {
System.out.print("WHAT IS YOUR MOVE? ");
int movePosition = scanner.nextInt();
scanner.nextLine();
return new Move(movePosition);
}
private static void printComputerWins() {
System.out.println("\nNICE TRY, BUT IT LOOKS LIKE I HAVE WON.");
System.out.println("THANKS FOR PLAYING.\n");
}
private static boolean askForAnotherGame() {
System.out.print("ANYONE ELSE CARE TO TRY? ");
do {
String response = Queen.scanner.nextLine();
if (response.equals("NO")) {
return false;
} else if (response.equals("YES")) {
return true;
} else {
System.out.println("PLEASE ANSWER 'YES' OR 'NO'.");
}
} while (true);
}
private static boolean isTopmostRowOrRightmostColumn(Move move) {
return move.unitsPlaceValue == 1 || move.unitsPlaceValue == move.tensPlaceValue;
}
private static ComputerMove getComputerMove(Move userMove) {
int unitsValueUser = userMove.unitsPlaceValue;
int tensValueUser = userMove.tensPlaceValue;
List