Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cs1.*;
- public class TreasureFinderGameDeluxe
- {
- public static void main(String[] args)
- {
- /** Constants for readability/flexibility*******/
- final int TREASURE = 999;
- final int GUESSED = -2;
- boolean resume = false;
- int gamemode = 0;
- while (!resume) {
- System.out.print("Easy(0) Medium(1) Hard(2): ");
- gamemode = Keyboard.readInt();
- if (gamemode < 0 || gamemode > 2) {
- System.out.println("Please select a valid game mode");
- resume = false;
- }
- else {
- resume = true;
- }
- }
- final int GAMEMODE = gamemode;
- System.out.print("\f");
- int boardsize = 0;
- resume = false;
- while (!resume) {
- System.out.print("How large would you like the board to be? ");
- boardsize = Keyboard.readInt();
- if (boardsize <= 1) {
- System.out.println("Please select a board size larger than one");
- resume = false;
- }
- else {
- resume = true;
- }
- }
- final int BOARDSIZE = boardsize;
- final int CLOSE = -1;
- final int FAR = -3;
- /** VARIABLES **********************************/
- int[][] board = new int[BOARDSIZE][BOARDSIZE];
- int randRow = (int)(Math.random()*BOARDSIZE);
- int randCol = (int)(Math.random()*BOARDSIZE);
- board[randRow][randCol] = TREASURE; //TREASURE PLACED AT RANDOM LOCATION, MARKED WITH 999
- int guesses = 1; //Keeps track of # user guesses (they only get 3)
- int row = 0;
- int col = 0; //Keeps track of user's row/col guess
- boolean winner = false; //Determines if user has won yet
- boolean close = false;
- boolean realNum = false;
- int howFar = 0;
- /** MAIN LOOP **********************************/
- while (guesses <= BOARDSIZE && winner == false)
- {
- printBoard(board,false);
- //GET GUESS row/col
- System.out.println("Guess #" + guesses);
- realNum = false;
- while (!realNum) {
- System.out.print("r: ");
- row = Keyboard.readInt();
- if (row >= BOARDSIZE || row < 0) {
- System.out.println("Please select a valid row");
- realNum = false;
- }
- else {
- realNum = true;
- }
- }
- realNum = false;
- while (!realNum) {
- System.out.print("c: ");
- col = Keyboard.readInt();
- if (col >= BOARDSIZE || col < 0) {
- System.out.println("Please select a valid column");
- realNum = false;
- }
- else {
- realNum = true;
- }
- }
- //IF CORRECT END GAME, OTHERWISE MARK SPOT GUESSED
- if (board[row][col] == TREASURE)
- winner = true;
- else
- {
- board[row][col] = GUESSED;
- guesses++;
- if (Math.abs(randCol - col) < Math.abs(randRow - row)) howFar = Math.abs(randRow - row);
- else howFar = Math.abs(randCol - col);
- if (howFar == 1 && GAMEMODE == 1) {
- board[row][col] = CLOSE;
- }
- else if (GAMEMODE == 0) {
- if (Math.abs(randCol - col) < Math.abs(randRow - row)) howFar = Math.abs(randRow - row);
- else howFar = Math.abs(randCol - col);
- board[row][col] = howFar;
- }
- }
- } //END WHILE
- printBoard(board,true);
- if (winner && guesses == 1)
- System.out.println("\nCongratulations, you won in " + guesses + " guess!");
- else if (winner)
- System.out.println("\nYou won in " + guesses + " guesses!");
- else
- System.out.println("You lost!");
- }//END MAIN
- //HELPER METHOD TO REDUCE CODE IN MAIN
- //NOTICE - can't use constants here. We will discuss better game designs soon!
- //Post-Condition: THE BOARD HAS NOT BEEN ALTERED IN ANY WAY!!!
- private static void printBoard(int[][] arr, boolean isEnd)
- {
- //PRINT BOARD
- System.out.print("\f");
- for (int r = 0; r < arr.length; r++)
- {
- for (int c = 0; c < arr[0].length; c++)
- {
- if (arr[r][c] == -2)
- System.out.print(" \t");
- else if (arr[r][c] == -1)
- System.out.print("CLOSE\t");
- else if (isEnd && arr[r][c] == 999)
- System.out.print("$$$$$\t");
- else if (arr[r][c] > 0 && arr[r][c] != 999)
- System.out.print(arr[r][c] + "\t");
- else
- System.out.print("(" + r + "," + c + ")\t");
- }
- System.out.println();
- }
- }
- }//END CLASS
Add Comment
Please, Sign In to add comment