Académique Documents
Professionnel Documents
Culture Documents
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.
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:
This class communicates between the GameModel and GameView classes to adjust game data.
Data:
Methods:
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
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.