CPSC 124 | Introduction to Programming | Spring 2024 |
Arrays are useful for keeping track of a collection of things...and sometimes (actually, quite often) the number of things in the collection varies. Furthermore, you may not know how many elements there will be in advance so you cannot guarantee the array will be big enough when you create it.
In this lab you'll modify a program for the casino game Red Dog so it can work with a variable number of players.
Labs and projects are for practice and learning. While it can be very productive to work on problems with your peers, it is also easy to underestimate how much you yourself understand and can do in such situations — so often something looks easy when someone else does it! With this in mind, you should always make the first attempt on a problem or programming task yourself using only the resources provided for the course (textbook, slides and examples posted on the schedule page, other resources linked on the course webpages). After that point, you are encouraged to come to office hours and/or go to Teaching Fellows. You may not collaboratively write solutions or code, and you may not copy solutions or code written by others, even if you contributed ideas.
You can discuss specific exercises with other students in general terms — such as how you might get started on that problem, or how or when to use various programming constructs, or strategies for debugging — but how to use a particular programming construct to solve a specific problem or debugging a particular program should only be discussed in office hours or with the Teaching Fellows.
API documentation for the provided classes can be found below:
You can find API documentation for ArrayList below:
For all exercises:
Unless otherwise indicated in the problem, if specific output is shown to indicate what your program should do, be sure your program's output matches what is shown exactly.
Employ good programming style, including choosing descriptive variable names and autoformatting your code. Use blank lines and comments to group and label statements that together perform a distinct task.
Write Javadoc-style comments including your name and a description of the class for each class, whether it is for constructing objects or containing a main program
Write Javadoc-style comments for each method, subroutine, and function. Also identify and check preconditions, throwing an IllegalArgumentException if a precondition is violated.
Also, you are once again working with multiple files and thus need to make sure that every file needed for the program is compiled. Since you will only be making changes to the main program, it should be sufficient to only compile that file (Java will automatically compile the others needed). Refer back to lab 9 for more on working with multiple files.
In this exercise, you will modify RedDogArray.java so that new players can be added, players can leave the game, and players who run out of money are eliminated.
The first step is to modify the program so that it works with partially-full arrays. Recall that with partially-full arrays, you need to keep track of the number of elements in the array in addition to the array itself:
In main, add a variable numplayers to keep track of the current number of players. Initialize it to reflect the initial number of players in the game.
Add a parameter for the number of players to the subroutines getBets, raiseBets, and play and update all of the loops in those subroutines so they only access elements up to the number of players (not the capacity of the array). Also update everywhere those subroutines are called so that the proper number of players is passed in for that parameter.
You should now be able to compile and run the program and have it work just as it did originally.
Next, write three functions to handle removing players, as described below. Note that all three functions should return the number of players removed — this is necessary because the main program will need to update numplayers (a function cannot update a variable declared in the main program).
Write a function removePlayer which, given an array of players, the number of players in the array, and the position of a player to remove as parameters, removes that player from the array. The order of the remaining players should be preserved. The function should return the number of players removed from the array (which will always be 1).
Write a function removePlayerByName which, given an
array of players, the number of players in the array, and the name
of a player to remove as parameters, removes the named player from
the array. The order of the remaining players should be
preserved. The function should return the number of players
removed from the array. (This will be either zero or one,
depending on whether a player with the specified name was
found.)
Use your removePlayer function to actually
remove the player once you've found the position of the named
player.
Write a function removeBankruptPlayers which, given
an array of players and the number of players in the array as
parameters, removes all of the players who have no money left. The
order of the remaining players should be preserved. The function
should return the number of players removed from the array.
You can use your removePlayer function to remove a player
when you find one with no money left — just be careful not to skip
players after one is removed. (As a test, make sure your function
works correctly if there are two consecutive players with no money
left — this case is a common source of errors.)
For a
bonus, instead figure out how to remove all the players with
no money in a single loop (i.e. no nested loops and no calls
to removePlayer or another function).
Now, update the main program:
Add to the play-the-game loop in the main program so that it does the following after each round of the game:
Remove all players with no money left. (Use the appropriate function.) Make sure you make use of the function's return value to correctly update the number of players in the game.
Prompt for the name of a player who wants to leave the game, and remove that player. (Use the appropriate function.) Keep prompting until - (a dash) is entered. (If the dash is entered first, no players are removed.) Make sure you make use of the function's return value to correctly update the number of players in the game.
Modify the play-the-game loop so that the loop exits when the number of players is 0.
You should now be able to compile and run the program. Test that you can remove players, that players who go bankrupt are removed from the game, and that the game ends when all of the players have been removed/eliminated.
Adding new players means that the array may need to grow:
Write a function grow which, given an array of players as a parameter, grows the array — that is, it should return a new array with twice the capacity of the provided one and with the same elements. Note that this function does not insert any new elements into the array — it just returns an array with a bigger capacity (and the same elements).
Then:
Write a function addPlayer which, given an array of players, the number of players in the array, the name of the new player, and the player's initial amount of money as parameters, adds the new player at the beginning of the array. (The order of the existing players should be preserved.) Assume that there is room in the array to add the player.
Add to the play-the-game loop in the main program so that after each round of the game (and after players are removed), it prompts for the name of a player who wants to join the game, and adds a new player with that name and $100 at the beginning of the array. (Use your addPlayer and, if necessary, grow functions to manipulate the array.) Keep prompting until - (a dash) is entered. (If the dash is entered first, no players are added.) Make sure you update numplayers appropriately.
You should now be able to compile and run the program. Test that you can add players without the program crashing and that the new players are incorporated into the next round of the game.
In this exercise, you will modify RedDogArrayList.java so that new players can be added, players can leave the game, and players who run out of money are eliminated...in other words, the same things you just did in exercise #1. The difference here is that you'll use an ArrayList instead of an array, greatly simplifying the tasks of inserting and removing players — you can just call the appropriate method of ArrayList instead of having to shift elements or grow the array.
The first step is to modify the program so that it works with ArrayList:
Add the necessary import statement so you can use ArrayList.
Modify the main program so that an ArrayList is created and the initial players are added to that instead of an array.
For subroutines getBets, raiseBets, and play, change the parameter to an ArrayList of players instead of an array. Also update all of the loops in those subroutines so they correctly access the ArrayList.
You should now be able to compile and run the program and have it work just as it did originally.
Next, write two subroutines to handle removing players, as described below:
Write a subroutine removePlayerByName which, given an
ArrayList of players and the name of a player to remove
as parameters, removes the named player from the list. The order
of the remaining players should be preserved.
Use the
appropriate ArrayList method to actually remove the
player once you've found the position of the named player.
Write a subroutine removeBankruptPlayers which, given
an ArrayList of players as a parameter, removes all of
the players who have no money left. The order of the remaining
players should be preserved.
Use the
appropriate ArrayList method to remove a player when you
find one with no money left — just be careful not to skip players
after one is removed. (As a test, make sure your function works
correctly if there are two consecutive players with no money
left.)
Then:
Add to the play-the-game loop in the main program so that it does the following after each round of the game:
Remove all players with no money left. (Use the appropriate subroutine.)
Prompt for the name of a player who wants to leave the game, and remove that player. (Use the appropriate subroutine.) Keep prompting until - (a dash) is entered. (If the dash is entered first, no players are removed.)
Modify the play-the-game loop so that the loop exits when the number of players is 0.
You should now be able to compile and run the program. Test that you can remove players, that players who go bankrupt are removed from the game, and that the game ends when all of the players have been removed/eliminated.
Adding players:
Add to the play-the-game loop in the main program so that after each round of the game, it prompts for the name of a player who wants to join the game, and adds a new player with that name and $100 at the beginning of the ArrayList. (Use the appropriate ArrayList method.) Keep prompting until - (a dash) is entered. (If the dash is entered first, no players are added.)
You should now be able to compile and run the program. Test that the new players are incorporated into the next round of the game.
Don't forget to hand in your work when you are done!
As in lab 2 (and all labs), grading will be based on both functionality and style. Style means following the good programming style conventions outlined in lab 2, as well as producing reasonably compact and elegant solutions. "Reasonably compact and elegant" means that your program is not significantly longer or more complex than it needs to be. In particular, you must use loops when loops are appropriate — achieving repetition by copying-and-pasting a bunch of code will not earn credit.