Last active
March 29, 2022 12:43
-
-
Save Rdilorenzo73/d65d24dfb0984f2e3fc30ffbfc31f109 to your computer and use it in GitHub Desktop.
Revisions
-
Rdilorenzo73 revised this gist
Nov 4, 2018 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,8 @@ A Java assignment taken from the course: [Software Development Fundamentals](https://www.edx.org/course/software-development-fundamentals-pennx-sd1x) _I did not create this assignment. The coded solution is mine. This was a Java practice project which helped me remember how to work with a 2D array._ <hr> Create a class called WhackAMole. -
Rdilorenzo73 revised this gist
Nov 4, 2018 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,10 +20,7 @@ public WhackAMole(int numAttempts, int gridDimension) { } } boolean place(int x, int y) { if(moleGrid[x][y] == '*') { -
Rdilorenzo73 revised this gist
Nov 2, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ A Java assignment taken from the course: [Software Development Fundamentals](https://www.edx.org/course/software-development-fundamentals-pennx-sd1x) <hr> Create a class called WhackAMole. It contains three integer instance variables called `score`, `molesLeft`, and `attemptsLeft`. Make one more instance variable called `moleGrid` which is a 2-dimensional array of chars. -
Rdilorenzo73 revised this gist
Nov 2, 2018 . 1 changed file with 8 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,9 @@ A Java assignment taken from the course: [Software Development Fundamentals](https://www.edx.org/course/software-development-fundamentals-pennx-sd1x) Create a class called WhackAMole. It contains three integer instance variables called `score`, `molesLeft`, and `attemptsLeft`. Make one more instance variable called `moleGrid` which is a 2-dimensional array of chars. We will also have you create the following methods in this class. @@ -9,16 +12,16 @@ We will also have you create the following methods in this class. `WhackAMole(int numAttempts, int gridDimension)` - Constructor for the game, specifies total number of whacks allowed, and the grid dimension. After reading through the assignment, make sure to initialize the moleGrid with the appropriate character. `boolean place(int x, int y)` – Given a location, place a mole at that location. Return true if you can. (Also update number of moles left.) `void whack(int x, int y)` - Given a location, take a whack at that location. If that location contains a mole, the score, number of attemptsLeft, and molesLeft is updated. If that location does not contain a mole, only attemptsLeft is updated. `void printGridToUser()` – Print the grid without showing where the moles are. For every spot that has recorded a “whacked mole,” print out a W, or * otherwise. `void printGrid()` - Print the grid completely. This is effectively dumping the 2d array on the screen. The places where the moles are should be printed as an ‘M’. The places where the moles have been whacked should be printed as a ‘W’. The places that don’t have a mole should be printed as *. Putting it all together - main method -
Rdilorenzo73 revised this gist
Nov 2, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ It contains three integer instance variables called score, molesLeft, and attemp We will also have you create the following methods in this class. `WhackAMole(int numAttempts, int gridDimension)` - Constructor for the game, specifies total number of whacks allowed, and the grid dimension. After reading through the assignment, make sure to initialize the moleGrid with the appropriate character. boolean place(int x, int y) – Given a location, place a mole at that location. Return true if you can. (Also update number of moles left.) -
Rdilorenzo73 revised this gist
Nov 2, 2018 . 1 changed file with 29 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ Create a class called WhackAMole. It contains three integer instance variables called score, molesLeft, and attemptsLeft. Make one more instance variable called moleGrid which is a 2-dimensional array of chars. We will also have you create the following methods in this class. WhackAMole(int numAttempts, int gridDimension) - Constructor for the game, specifies total number of whacks allowed, and the grid dimension. After reading through the assignment, make sure to initialize the moleGrid with the appropriate character. boolean place(int x, int y) – Given a location, place a mole at that location. Return true if you can. (Also update number of moles left.) void whack(int x, int y) - Given a location, take a whack at that location. If that location contains a mole, the score, number of attemptsLeft, and molesLeft is updated. If that location does not contain a mole, only attemptsLeft is updated. void printGridToUser() – Print the grid without showing where the moles are. For every spot that has recorded a “whacked mole,” print out a W, or * otherwise. void printGrid() - Print the grid completely. This is effectively dumping the 2d array on the screen. The places where the moles are should be printed as an ‘M’. The places where the moles have been whacked should be printed as a ‘W’. The places that don’t have a mole should be printed as *. Putting it all together - main method In order to play this game you need to create a main method. Begin by creating a 10 by 10 grid where you randomly place the moles. Place a total of 10 moles. Now allow the user (remember how to use Scanner?) to enter the x and y coordinates of where they would like to take a whack. Tell them they have a maximum of 50 attempts to get all the moles. At any point in the game, they can input coordinates of -1, -1 in order to indicate that they are giving up. If the user gives up they get to see the entire grid. The game ends if the user is able to uncover all the moles or if the user runs out of attempts. -
Rdilorenzo73 created this gist
Nov 2, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,134 @@ import java.util.Scanner; public class WhackAMole { int score; int molesLeft; int attemptsLeft; char[][] moleGrid; public WhackAMole(int numAttempts, int gridDimension) { attemptsLeft = numAttempts; moleGrid = new char[gridDimension][gridDimension]; //construct empty grid for(int i = 0; i < gridDimension; i++) { for(int j = 0; j < gridDimension; j++) { moleGrid[i][j] = '*'; } } } /** * Given a location, place a mole at that location. * Return true if you can. (Also update number of moles left.) */ boolean place(int x, int y) { if(moleGrid[x][y] == '*') { moleGrid[x][y] = 'M'; molesLeft++; return true; } else{ return false; } } void whack(int x, int y) { if(x == -1 && y == -1) { attemptsLeft = 0; printGrid(); } else if(moleGrid[x][y] == 'M') { moleGrid[x][y] = 'W'; score++; attemptsLeft--; molesLeft--; printGridToUser(); } else { attemptsLeft--; printGridToUser(); } } void printGridToUser() { for(int i = 0; i < moleGrid.length; i++) { for(int j = 0; j < moleGrid.length; j++) { if(moleGrid[i][j] == 'W') { System.out.print("W" +" "); } else { System.out.print("*" + " "); } } System.out.print("\n"); } } void printGrid() { for(int i = 0; i < moleGrid.length; i++) { for(int j = 0; j < moleGrid.length; j++) { System.out.print(moleGrid[i][j] + " "); } System.out.println(""); } } public String toString(){ String status = ""; status += "Score: " + score; status += "\n"; status += "Moles Left: " + molesLeft; status += "\n"; status += "Attempts Left:" + attemptsLeft; return status; } public static void main(String[] args) { WhackAMole game = new WhackAMole(50, 10); //randomly place ten moles int placedMoles = 0; while(placedMoles < 10) { int randomGuessX = (int)(Math.random()*10); int randomGuessY = (int)(Math.random()*10); if(game.place(randomGuessX, randomGuessY) == true) { placedMoles++; } } while(game.attemptsLeft > 0 && game.molesLeft > 0) { Scanner userInput = new Scanner(System.in); System.out.println("Enter the x and y coordinates of where you would like to take a whack." +"\n"+ "You have a maximum of 50 attempts to get all the moles."); System.out.print("x coordinate of the mole: "); int userGuessX = userInput.nextInt(); System.out.println(""); System.out.print("y coordinate of the mole: "); int userGuessY = userInput.nextInt(); game.whack(userGuessX,userGuessY); } } }