Vous êtes sur la page 1sur 7

Jan Patrick Camaclang

February 24, 2017


CST 338
Final Project

Take Away Game

Main Objective:

Create a GUI to play Take Away. This game involves 3 piles of 5 groots in each pile. The
player starts by selecting a pile to remove at least one groot from that pile. The computer is next
and removes groots from a pile of their choice. The player and computer switches turns until
there are no more groots to remove. The last player to remove a groot from a pile wins.

Phase One: GameModel

Create a class called GameModel. This class tracks the number of groots in each pile.

Data:

private int [] piles Array of piles holding number of groots. There are 3 piles in this
game.
private static final int GROOTS = 5 Initial number of groots in each pile at the start of
a game.

Methods:

Default constructor initializes piles array with number of GROOTS.


getNumGroots(int pileNum) Accessor for number of groots in piles[].
setNumGroots(int pileNum, int numGroots) Sets new number of groots in the pile by
subtracting the number of groots chosen by the player.
getNumPiles() Accessor for number of piles in the array.

Phase Two: GameController

This class communicates between the GameModel and GameView classes to adjust game data.

Data:

private static GameModel gameModel

Methods:

public GameController(GameModel gameModel) Constructor that sets gameModel.


public static boolean validPile(int pile) Takes in the pile to inspect and determines if
the pile exist in the pile array.
public static boolean validGroots(int pile, int groots) Determines if the number of
groots to remove is valid by inspecting and comparing the number of groots in the
selected pile.
public static void removeFromPile(int pile, int groots) Removes number of groots
from selected pile.
public static int computerPile() Returns a valid pile for the computer to remove from.
Ensures the pile is not empty.
public static int computerGroots(int computerPile) Returns a random number of
groots to remove from selected pile.
public static boolean isGameOver() Determines if the game is finished by checking if
there is anything in the array.
public static int numGroots(int pile) This returns the number of groots in the indicated
pile.
public static void resetGame() Resets the game array by creating a new GameModel().

Phase Three: GameView

This class main objective is to present the game to the player with a GUI. I went with a simple
design like below:

pile1:
JLabel J L a b e l

pile2:
JLabel

pile3:
JLabel

gameStatus: JLabel

whichPile: inputPile: howMany: inputHowMany: Submit:


JLabel JTextField JLabel JTestField JButton

Data:

private ImageIcon gameIcon Holds the image to represents your items in the piles
private JLabel pile1, pile2, pile3, gameStatus, whichPile, howMany Labels for the
piles. Show current game status for the player. Labels for JTextFileds.
private JTextField inputPile, inputHowMany Get input from the player.
private JButton submit Submit player input.
private JMenuBar menuBar
private JMenu menu
private JMenuItem newGame, quit
private int groots = 5 Used to initialize groot images to panel
private Font gameFont Main font for labels.
private Font statusFont Different font size to stand out more.
private JPanel pile1Panel, pile2Panel, pile3Panel Panels for each pile.
private String gameIntro An intro dialogue for game instructions and welcome
message.

Methods:

GameView() Constructor creates the GUI and formats the layout. Creates a menu bar
with one menu and two menu items. Each menu item has an ActionListener. The submit
button is given an ActionListener.
public void addGroots(JPanel currentPanel, int numGroots) Adds the icons to the
current JPanel with the correct number of groots.
public class playAgain implements ActionListener Resets the game by calling
GameController.resetGame(). Updates the panels and gameStatus JLabel.
public class quitGame implements ActionListener Quits out of the game.
public class playRound implements ActionListener Runs the players round with their
input fields for which pile and how many groots. Checks for error input and outputs error
messages. Adjust pile array to reflect players moves and checks for winning conditions.
Also plays computers turn and checks for winning conditions. Changes gameStatus and
pile panels to reflect the current round.
private void updatePanel(int pile) Updates the panel the current player is taking from to
reflect how many groots are in each pile after the round.
Critical Screen Shots of GUI working

Game start display showing introduction game status string. I input 3 in pile and 2 in how many
groots. Then select the Submit button.
The game goes through the first round and remove the correct amount of groots from the pile I
selected. The computer also takes groots from a pile and is displayed in the game status JLabel.
After taking turns for each round I win by removing the last item from the piles.
Error handling catches invalid inputs from the user and presents error message. Does not change
current state of the game.

Vous aimerez peut-être aussi