Skip to main content
留学咨询

辅导案例-CSCI1110-Assignment 4

By May 15, 2020No Comments

CSCI1110 Assignment 4 Ochos Locos Faciles! Assignment 2 may have felt like being behind the 8-ball and may have driven you a little crazy. Fear Not! Assignment 4 is all about making Ochos Locos easy through the power of object oriented programming. In this assignment you will reimplement Ochos Locos (with a couple minor changes) in an object oriented manner. You will be guided through the Part 1, to create the base project, and the design for the Part 2 and Bonus will be up to you as you will have a solid OO design from Part 1 Note: Remember to follow the steps in the assignment! Part I: Ochos Locos Faciles We will create the game using an OO approach. Recall, the physical parts of the game are: Card: a card has a colour (red, yellow, green, or blue) and a number (1 … 8). Player: A player has 5 or more cards and takes turns playing in the game with other players Game: A game consists of (i) two or more players; (ii) a deck of cards; (iii) a discard card, and follows a set of rules where each player plays in cyclic order until one of the players wins or the deck runs out of cards. Unsurprisingly, you are to create (at least) three classes: Card, Player, and Game. Create the Card Class The Card class should have (at minimum) the following methods: Card(String card) is the constructor and takes a string of the form “LD”, where L is a letter denoting the color such as R, Y, G, or B, and D is a digit from (0 …9), denoting the card number. char getColor() is a getter that returns the card color. int getNumber() is a getter that returns the card number. boolean matches(Card card) returns true if this card and the passed card have either the same colour or the same number. boolean matchesColor(Card card) returns true if this card and the passed card have either the same colour. boolean matchesNumber(Card card) returns true if this card and the passed card have the same number. String toString() returns a string of the same form as the argument to the constructor. Implement this class and test it on Mimir. Be sure all “Card” tests pass before proceeding. 1 CSCI1110 Winter 2020 Assignment 4 Create the Player Class The Player class should have (at minimum) the following methods: Player(String name) is the constructor and takes a string that denotes the name of the player. void addCardToHand(Card card adds the specified card to the player’s hand. Hint: You may want to use an array or an ArrayList to store the player’s hand inside the Player object.) void removeCardFromHand(Card card removes the specified card from the player’s hand. Card findMatch(Card card searches the player’s hand for the first card that matches (either color or number) the specified card. Note: The hand is to be searched in the order the cards were added to it. If a matching card is found, it is returned. Otherwise, null is returned. Note: The matching is a simplification of Assignment 2. The player keeps the cards in the order that they receive them. The matching involves check- ing each card from oldest to newest. The first card matched it the one returned. For example, support Alice received the cards R8, G8, Y4, G2, B1, and the discard card is G4, the first card (left to right) that matches is G8, which is what should be returned. boolean hasWon() returns true if the player is out of cards. String toString() returns the player’s name, followed by all the cards in the player’s hand, with spaces separating the name and the cards. For example, if Alice had the cards R8, Y4, G2, B1 in her hand, the returned string would be: Alice R8 Y4 G2 B1 Hint: you can use the toString() method of Card to get the string for each card. Implement this class and test it on Mimir. Be sure all “Player” tests pass before proceeding. Create the Game Class The Player class should have (at minimum) the following methods: void addPlayer(Player player adds a player object to the game. Hint: You may want to use an array or an ArrayList to store the players in the Game object. void addCard(Card card adds a card to the deck of cards that will be used to play the game. Hint: You may want to use an array or an ArrayList to store the cards in the Game object. void setDiscardCard(Card card sets the discard card. Recall, the discard card is the card that was most recently played in the game. 2 CSCI1110 Winter 2020 Assignment 4 Card getDiscardCard() gets the discard card. Card drawCard() removes and returns a card from the deck of cards. Player getCurrentPlayer() returns the player whose turn it currently is. Note: initially there is no current player until nextPlayer() is invoked. I.e., calling getCurrentPlayer() before ever calling nextPlayer() is un- defined behaviour. Player nextPlayer() changes the current player to the next player and returns the new current player. Card nextPlay() Performs one move (play) and returns the card that was played by the player. This includes moving to the next player, matching the dis- card card with one from the player’s hand. If the player does not have a matching card, then the player draws a card, adds it to their hand. Oth- erwise, if the player does have a matching card, the card is removed from the player’s hand, the card becomes the new discard card, and the card is returned by the method. boolean outOfCards() returns true if all the cards in the deck have been used up. Implement this class and test it on Mimir. Be sure all “Game” tests pass before proceeding. Note, some of these tests are not exhaustive, so there may still be bugs in some of the more complex methods. Implement Ochos Locos Using the Classes You Created Write a Java program called OchosLocos.java that simulates the basic Ochos Locos game. There are a couple small changes from the Assignment 2 version. Input The input to this program consists of several lines of input. • The first line contains an integer P , denoting the number of players. • This is followed by P lines, each of which represents a player. A player line consists of the player’s name and five (5) cards. The format is: Name Card1 Card2 Card3 Card4 Card5 for example: Alice R1 B3 G8 G4 Y3 • The next line contains a single card, denoting the first discard card. • The last line consists of the remaining cards. Since there are a maximum of 32 cards (4 colours times 8 numbers), the total number of remaining cards will be 32− (P ×5)−1. Please see the example at the end of this section. 3 CSCI1110 Winter 2020 Assignment 4 Processing • Initially, your program should create a new game and read in the players, adding the players to the game. The program should then read in the cards and add them to the game as well. • You program should loop performing nextPlay() until one of the players has won or until there are no more cards. • When a player plays, the player matches the discard card with the the first possible match. I.e., the players should keep their hands in the order the cards were added and should scan the cards oldest to newest, until a match is found. This is different from the Assignment 2 version. • The game ends as soon as a player runs out of cards or draws the last card. Output The output is very similar to Assignment 2. Your program should output each players move after they play. The format is: Name Hand: Move where Name is the player’s name, Hand is the player’s hand after the player’s turn is done, and Move is either DRAW or the card played by the player, depending on whether the player had to draw a card or was able to play a card from their hard. For example, if Alice plays B8, and has the hand R2 G3 B5 Y8, output Alice R2 G3 B5 Y8: B8 and if Bob cannot play and has the hand R8, output Bob R8: DRAW After the game ends output Name is the winner! if the player wins, or That was fun! if the game ran out of cards. Hint: The toString() method for the Player class does most of this. Example Input 4 Alice Y3 Y7 Y8 B1 B7 Bob R8 Y1 G4 G8 B8 Carol R3 R4 R7 Y6 B5 Dave R2 G5 B6 Y5 G3 B3 G2 Y4 R1 G1 Y2 G6 B4 R5 G7 R6 B2 4 CSCI1110 Winter 2020 Assignment 4 Example Output Bob R8 G4 G8 B8: Y1 Carol R3 R4 R7 B5: Y6 Dave R2 G5 Y5 G3: B6 Alice Y7 Y8 B7: B
1 Bob R8 G4 G8: B8 Carol R3 R4 R7: B5 Dave R2 Y5 G3: G5 Alice Y7 Y8 B7 G2: DRAW Bob R8 G8: G4 Carol R3 R7: R4 Dave Y5 G3: R2 Alice Y7 Y8 B7: G2 Bob R8: G8 Carol R3 R7 Y4: DRAW Dave Y5: G3 Alice Y7 Y8 B7 R1: DRAW Bob R8 G1: DRAW Carol R7 Y4: R3 Dave Y5 Y2: DRAW Alice Y7 Y8 B7: R1 Bob G1: R8 Carol Y4: R7 Dave Y5 Y2 G6: DRAW Alice Y8 B7: Y7 Bob G1 B4: DRAW Carol: Y4 Carol is the winner! Submit Part 1 in Mimir on using the “Assignment 4 (Part 1)” project. 5 CSCI1110 Winter 2020 Assignment 4 Part II: Diversidad de Ochos Locos We all know that different players have different strategies and preferences when playing. For this part of the assignment, you will implement three additional player strategies by subclassing the Player class. You will then extend the main program to use these new strategies. Note: Your starting point is your code from Part 1. Create the ColorPlayer Class The ColorPlayer is just like the regular player except that it will always prefer to match color over number. For example, if the player has choice of matching color or number, it will always choose color. You will need to override the findMatch(Card card) method so that it does this. Implement a subclass of Player called ColorPlayer that overrides the findMatch(Card card) method. The method should return the first card in the player’s hand that matches by color. If none of the cards match by color, it should attempt a regular match. Create the NumberPlayer Class The NumberPlayer is just like the regular player except that it will always prefer to match number over color. For example, if the player has choice of matching color or number, it will always choose number. You will need to override the findMatch(Card card) method so that it does this. Implement a subclass of Player called NumberPlayer that overrides the findMatch(Card card) method. The method should return the first card in the player’s hand that matches by number. If none of the cards match by number, it should attempt a regular match. Create the StrategicPlayer Class The StrategicPlayer is just like the regular player except that it will always prefer to match in the same way as the rules for Part I in Assignment 2. That is, if it has choice of matching by number or by color it will prefer to match by number. Otherwise, it will prefer to match by color. If it does not have a choice of matching, it will attempt a regular match. You will need to override the findMatch(Card card) method so that it does this. Implement a subclass of Player called StrategicPlayer that overrides the findMatch(Card card) method. If the method has more cards with the same number than with the same color, it will match by number. Otherwise, it will match by color. Implement Ochos Locos Diversity Using the Classes You Created Write a Java program called OchosLocosDiversity.java by making a copy and extending your OchosLocos.java The new addition is that each player will have one of the four strate- 6 CSCI1110 Winter 2020 Assignment 4 gies: player, color player, number player, or strategic player, and your program will need to instantiate the correct player object. All the other code can be reused from Part I. Input Same as Part 1, except each play line also contains the strategy of the player. A player line consists of the player’s name, strategy, and five (5) cards. The format is: Name Strategy Card1 Card2 Card3 Card4 Card5 where Strategy is one of C, N, S, B, where C denotes a ColorPlayer, N denotes a Number- Player, S denotes a StrategicPlayer, and B denotes the basic Player from Part 1. E.g., Alice S R1 B3 G8 G4 Y3 represents a player named Alice who should be represented by a StrategicPlayer object. Please see the example at the end of this section. Processing Same processing as Part 1, except that each player will match according to their strategy. Output The same format as in Part 1. Example Input 4 Alice C Y3 Y7 Y8 B1 B7 Bob N R8 Y1 G4 G8 B8 Carol S R3 R4 R7 Y6 B5 Dave B R2 G5 B6 Y5 G3 B3 G2 Y4 R1 G1 Y2 G6 B4 R5 G7 R6 B2 Example Output Alice Y3 Y7 Y8 B7: B1 Bob R8 G4 G8 B8: Y1 Carol R3 R4 R7 B5: Y6 Dave R2 G5 Y5 G3: B6 Alice Y3 Y7 Y8: B7 Bob R8 G4 G8: B8 Carol R3 R4 R7: B5 Dave R2 Y5 G3: G5 7 CSCI1110 Winter 2020 Assignment 4 Alice Y3 Y7 Y8 G2: DRAW Bob R8 G8: G4 Carol R3 R7: R4 Dave Y5 G3: R2 Alice Y3 Y7 Y8: G2 Bob R8: G8 Carol R3 R7 Y4: DRAW Dave Y5: G3 Alice Y7 Y8: Y3 Bob R8 R1: DRAW Carol R3 R7: Y4 Dave: Y5 Dave is the winner! Submit Part 2 in Mimir on using the “Assignment 4 (Part 2)” project. 8 CSCI1110 Winter 2020 Assignment 4 Part III: Ochos Locos Completos For the last part of this assignment you will add the 8-card and the 2-card rules described in Assignment 2. Note: Your starting point is your code from Part 2. Create the FullGame Class The FullGame class is a subclass of the Game class that implements the rules for the 8-card and 2-card rules. You will need to override the nextPlay() method so that it does this. Implement a subclass of Game called FullGame that overrides the nextPlay() method. The method implement the 2-card and 8-card rules from Assignment 2. Note: You may need to add additional methods to other classes, depending on your approach. Implement Ochos Locos Complete Using the Classes You Created Write a Java program called OchosLocosComplete.java by making a copy and extend- ing your OchosLocosDiversity.java The minor change is that OchosLocosComplete.java should use FullGame rather than Game. I.e., the change to from OchosLocosDiversity.java to OchosLocosComplete.java is literally one line of code, that instantiates FullGame instead of Game. Input Same as Part 2. Processing Same processing as Part 2, with the following rules: • If an 8 is played, the player sets the color to be whatever they have the most of. If there is a tie, the preference order is red, yellow, green, blue. e.g. red is preferred over yellow. • If a 2 is played, the next player should play the first 2 in their hand (if possible). If the player does not have a 2, they miss their turn and need to pick up twice as many cards as the number of twos that have just been played. For example, if Alice plays a 2, Bob plays a 2, Carol must pick up 4 cards and miss her turn if she does not have a 2. For another example, if Alice plays a 2, Bob picks up 2, Carol plays a 2, then Dave only has two pick up 2 because the previous 2s were not consecutive. 9 CSCI1110 Winter 2020 Assignment 4 Output The same format as in Part 1, except the following: If the card played by player is an 8, print out (in parentheses) the color chosen by the player after the card that they played. For example, suppos Bob plays R8, and chooses (G)reen, the corresponding output is: Bob Y1 G4 G8 B8: R8 (G) Example Input 4 Alice C Y3 Y7 Y8 B1 B7 Bob N R8 Y1 G4 G8 B8 Carol S R3 R4 R7 Y6 B5 Dave B R2 G5 B6 Y5 G3 B3 G2 Y4 R1 G1 Y2 G6 B4 R5 G7 R6 B2 Example Output Alice Y3 Y7 Y8 B7: B1 Bob R8 G4 G8 B8: Y1 Carol R3 R4 R7 B5: Y6 Dave R2 G5 Y5 G3: B6 Alice Y3 Y7 Y8: B7 Bob R8 G4 G8: B8 Carol R3 R4 R7 B5 G2: DRAW Dave R2 Y5 G3: G5 Alice Y3 Y7 Y8 Y4: DRAW Bob R8 G8: G4 Carol R3 R4 R7 B5: G2 Dave Y5 G3: R2 Alice Y3 Y7 Y8 Y4 R1 G1 Y2 G6: DRAW Bob G8: R8 Carol R4 R7 B5: R3 Dave Y5: G3 Alice Y3 Y7 Y8 Y4 R1 Y2 G6: G1 Bob: G8 Bob is the winner! Submit Bonus in Mimir on using the “Assignment 4 (Bonus)” project. 10 CSCI1110 Winter 2020 Assignment 4 Things to Remember • Your code must compile. If it does not compile, you will receive a 0 on that portion of the assignment. • Your code must be well commented and indented. Please see the course website (As- signments section) for Code Style Guidelines. • You may assume that all input will be correct. Grading Marks Part 1 Functionality 30 Quality of Solution 30 Code Clarity 15 Part 2 Functionality 10 Quality of Solution 10 Code Clarity 5 Bonus Functionality 4 Quality of Solution 4 Code Clarity 2 Total 100 Each problem on the assignment will be graded based on three criteria: Functionality “Does it work according to specifications?”. This is determined in an automated fashion by running your program on a number of inputs and ensuring that the outputs match the expected outputs. The score is determined based on t
he number of tests that your program passes. So, if your program passes t T tests, you will receive that proportion of the marks. Quality of Solution “Is it a good solution?” This con- siders whether the solution is correct, efficient, covers boundary conditions, does not have any obvious bugs, etc. This is determined by visual inspection of the code. Initially full marks are given to each solution and marks are deducted based on faults found in the solution. Code Clarity “Is it well written?” This considers whether the solution is properly formatted, well documented, and follows coding style guidelines. A single code clar- ity score is assigned for all solutions. If your program does not compile, it is considered non-functional and of extremely poor quality, meaning you will receive 0 for the solution. What to Hand In This assignment must be done in Java and submitted via Mimir by following the links on Brightspace for each of the three parts. 11

admin

Author admin

More posts by admin