top of page

Battle Game I Java Coursework Help

Updated: Dec 28, 2021

This is a Battle game sample assignment done by Codersarts Java Experts.


Battle is a simple two-player card game played with a standard 52-card deck of playing cards. The objective of the game is to win "battles" against the other player. For each battle, the two players place one card face up on the table, and the player that has the highest rank card wins the battle. If the cards have the same rank, the battle is a draw. For the Core and Completion, you are to complete a program that implements a modified version of the game in which the player is playing against the computer. This version of the game starts by shuffling the deck of cards, and giving a hand of five cards to the player. The player then repeatedly "battles" the computer by placing a card on the table. The computer places a card at the same time. The program compares the ranks of the card to see who won, and keeps score of how many battles the player has won and how many battles the computer has won. The first player to win 7 battles is the winner. If the player's hand is not full, they can pick up the next card from the deck at any time. Up to three times in a game, the player can replace a card in their hand by the next card in the deck. The player can reorder the cards in their hand, but must always play by placing the card at the left end of their hand on the table. The computer doesn't have a hand, and always plays by taking and placing the next card from the deck.


Template Program

  • The template for the Battle Game program includes some code that is already completed for you. This code declares constants and fields, sets up the GUI, handles some of the display and allows the user to select a card (for moving or replacing). You are to complete the methods that perform the "core mechanics" of the game that display the cards and respond to the buttons:

  • Battle, which performs a "battle": putting the card at the left end of the player's hand onto the table, putting the computer's card on the table, and updating the score. If the left end of the hand is empty, then it does nothing.

  • Pickup, which picks up the next card in the deck to fill an empty space in the hand.

  • Replace, which replaces the selected card in the hand with the card from the top of the deck. (There is a limit of three replace actions in any game.)

  • Left to move the selected card one step to the left (swapping it with card (or the space) in the adjacent position.

  • Restart, which clears the table, gets a new deck, and restocks the hand,

  • The template has four fields to keep track of the cards:

  • deck: an Array List containing a shuffled deck of cards.

  • hand: an array that can hold up to 5 Cards. It will contain null in any position that does not have a Card.

  • table Computer: an Array List of the Cards that have been placed on the table by the computer.

  • table Player: an Array List of the Cards that have been placed on the table by the player.

  • It has four fields to keep track of the scores, the index of the selected card, and how many cards have been replaced. Your program should display

  • the cards (and gaps) in the player's hand

  • (displayed in a rectangle at the bottom of the window)

  • the cards that the computer and the player have placed on the table.

  • (displayed in two rows in the middle of the window.

  • The cards should be overlapped so that only the most recent "battle" is visible)

  • The current scores, at the top of the window.

  • Here is a screenshot of the layout of the "hand" and the "table", showing a hand with four cards, and a "table" with five battles (ten cards). The player won the first three battles, the computer won the fourth and the fifth is a draw.



  • There is a completed Card class which represents playing cards. It contains

  • a draw(left, top) method for drawing a Card at a specified position,

  • constants specifying the size of the cards,

  • get Suit() and get Rank() methods to get the suit ("clubs", "diamonds", "hearts", or "spades"), and rank (1..13) of the card, and

  • a static get Shuffled Deck() method that returns an Array List of Card containing a shuffled deck of 52 cards. You can call this method on the Card class: Card get Shuffled Deck();

Core

  • Write the following methods:

  • The restart() method so that it

  • gets a new shuffled deck

  • sets the comp Score and play Score fields to their initial values

  • sets the table Computer and table Player fields to be empty lists

  • sets the hand field to have 5 cards in it (taken from the front of the deck)

  • redraws the hand, the table, and the score (use the redraw() method!)

  • The pickup Card() method so that it

  • looks for an empty position in the hand array, and if there is one, and fills it with the card at the front of the deck.

  • redraws the hand, the table, and the score.

  • The draw Hand Cards() method so that it draws each card in the hand array.

  • The drawTableCards() method so that it draws the cards on the "table" for both the computer (on the top) and the player (below).

  • The playBattle() method so that it checks if

  • there is a card in the leftmost place in the player's hand. places the card on the table takes the top card from the deck for the computer player and places it on the table. compares the ranks of the two cards and awards a point to the player with the highest card. Neither player gets a point if the two cards have the same rank. redraws the hand, the table, and the score if the player or the computer have reached the target, end the game Test your program carefully, to make sure that it works correctly in all cases: when the hand is empty when the hand is full when the hand is only part full

Completion

  • Complete the replace Card() method so that a player can throw away the selected card in their hand (if there is one) and replace it with a new card from the deck. A player can only replace a card three times during a game. (Use the remaining Replaces field.)

  • Complete the move Left() method so that it swaps the contents of the currently selected position on the hand with the contents of one position to the left. (Note that it should not attempt to move something beyond the ends of the hand.)

  • Extend the draw Table Cards() method so that it highlights the card with the highest rank on the last battle.


Challenge

  • Implement the true version of Battle Game (see the Wikipedia page about Battle). Note: if you do the Challenge, you must submit BattleGame.java with the core/completion version, and a different file (eg FullBattleGame.java) with your challenge version. It is not possible to combine the simplified version and the full version in the same file.

For Complete solution of this sample assignment mail us on contact@codersarts.com


Also, If you are looking for any kind of coding help in programming languages, Contact us for instant solution












bottom of page