virtual_pet
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| virtual_pet [2026/02/26 14:00] – [Activity 5] frchris | virtual_pet [2026/03/01 15:02] (current) – [Part C] frchris | ||
|---|---|---|---|
| Line 126: | Line 126: | ||
| </ | </ | ||
| ===== Activity 4 ===== | ===== Activity 4 ===== | ||
| + | ==== Part A: Food ==== | ||
| <file java Food.java> | <file java Food.java> | ||
| /** | /** | ||
| Line 132: | Line 132: | ||
| * @author (your name) | * @author (your name) | ||
| * @version (a version number or a date) | * @version (a version number or a date) | ||
| + | | ||
| + | * A Food object is created with parameters that define the name of the food, | ||
| + | * the increase in energy level, the increase in happiness level, | ||
| + | * and the amount of weight gained if the food is consumed by a | ||
| + | * VirtualPet object. | ||
| + | | ||
| + | * The Food class provides a constructor with the following header: | ||
| + | * public Food(String name, int energyIncrease, | ||
| + | * The Food class also has accessor methods for all instance variables. | ||
| */ | */ | ||
| public class Food | public class Food | ||
| Line 140: | Line 149: | ||
| } | } | ||
| </ | </ | ||
| + | ==== Part B: Game ==== | ||
| <file java Game.java> | <file java Game.java> | ||
| /** | /** | ||
| Line 174: | Line 183: | ||
| - | <file java VirtualPetGUIRunner.java> | + | |
| + | ==== Text Based Runner for Activity 4 ==== | ||
| + | |||
| + | <file java VirtualPetRunner4.java> | ||
| + | |||
| + | import java.util.Scanner; | ||
| + | import java.util.concurrent.Executors; | ||
| + | import java.util.concurrent.ScheduledExecutorService; | ||
| + | import java.util.concurrent.TimeUnit; | ||
| + | |||
| + | public class VirtualPetRunner4 | ||
| + | { | ||
| + | // Prints out menu and returns user choice | ||
| + | public static int getChoice(Scanner input) | ||
| + | { | ||
| + | int selection = 0; | ||
| + | while (selection < 1 || selection > 4) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | return selection; | ||
| + | } | ||
| + | |||
| + | // Prints out food options and returns user choice | ||
| + | public static int getPantry(Scanner input) | ||
| + | { | ||
| + | int selection = 0; | ||
| + | while (selection < 1 || selection > 5) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | selection = input.nextInt(); | ||
| + | } | ||
| + | return selection; | ||
| + | |||
| + | } | ||
| + | |||
| + | // Prints out game options and returns user choice | ||
| + | public static int getGame(Scanner input) | ||
| + | { | ||
| + | int selection = 0; | ||
| + | while (selection < 1 || selection > 4) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | selection = input.nextInt(); | ||
| + | } | ||
| + | return selection; | ||
| + | } | ||
| + | |||
| + | // Displays a picture of the pet | ||
| + | public static void printPet(String emo) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | public static void main(String[] args) | ||
| + | { | ||
| + | // CHANGE THIS VARIABLE VALUE TO TEST AT A DIFFERENT SPEED | ||
| + | final int INTERVAL_IN_SECONDS = 10; | ||
| + | |||
| + | // Sets up Scanner for user input | ||
| + | Scanner input = new Scanner(System.in); | ||
| + | |||
| + | VirtualPet4 myPet = new VirtualPet4(" | ||
| + | |||
| + | // Instantiates Food objects | ||
| + | Food apple = new Food(" | ||
| + | Food cupcake = new Food(" | ||
| + | Food broccoli = new Food(" | ||
| + | Food potato = new Food(" | ||
| + | |||
| + | // Instantiates Game objects | ||
| + | Game coinToss = new Game(" | ||
| + | Game hoopJumping = new Game(" | ||
| + | Game simonSays = new Game(" | ||
| + | |||
| + | // Sets up a ScheduledExecutorService object that will call updateStatus | ||
| + | // every 1 minute. | ||
| + | ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | ||
| + | scheduler.scheduleAtFixedRate(() -> { myPet.updateStatus(); | ||
| + | |||
| + | System.out.println(myPet); | ||
| + | printPet(" | ||
| + | |||
| + | int choice = getChoice(input); | ||
| + | while (choice != 4) | ||
| + | { | ||
| + | if(choice == 1) | ||
| + | { | ||
| + | System.out.println(myPet); | ||
| + | } | ||
| + | else if (choice == 2) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | int food = getPantry(input); | ||
| + | Food f = null; | ||
| + | if (food == 1) | ||
| + | f = apple; | ||
| + | else if (food == 2) | ||
| + | f = cupcake; | ||
| + | else if (food == 3) | ||
| + | f = broccoli; | ||
| + | else if (food == 4) | ||
| + | f = potato; | ||
| + | if (f != null) | ||
| + | { | ||
| + | myPet.feed(f); | ||
| + | System.out.println(" | ||
| + | + " 1 " + f.getName()); | ||
| + | } | ||
| + | } | ||
| + | else if (choice == 3) | ||
| + | { | ||
| + | System.out.println(" | ||
| + | int game = getGame(input); | ||
| + | Game g = null; | ||
| + | if (game == 1) | ||
| + | g = coinToss; | ||
| + | else if (game == 2) | ||
| + | g = hoopJumping; | ||
| + | else if (game == 3) | ||
| + | g = simonSays; | ||
| + | if (g != null) | ||
| + | { | ||
| + | boolean hasWon = myPet.play(g); | ||
| + | System.out.println(" | ||
| + | + " with " + myPet.getName()); | ||
| + | if (hasWon) | ||
| + | | ||
| + | else | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | if (myPet.getEnergyLevel() >= 5 && myPet.getHappinessLevel() >= 5) | ||
| + | printPet(" | ||
| + | else | ||
| + | printPet(" | ||
| + | System.out.println(myPet.getName().toUpperCase()); | ||
| + | choice = getChoice(input); | ||
| + | } | ||
| + | scheduler.shutdown(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ==== Part C Putting it all together ==== | ||
| + | |||
| + | Now that we have a Game and Food class, let’s revise the VirtualPet class to include interactions with these classes. | ||
| + | - Write the class VirtualPet4. You may copy your code implementation of VirtualPet from Activity 3 but be sure to modify the class header and constructor to match the new class name. | ||
| + | |||
| + | - Modify the '' | ||
| + | |||
| + | - Modify the '' | ||
| + | |||
| + | === GUI Based Runner for Activity 4 === | ||
| + | |||
| + | - [[https:// | ||
| + | - [[https:// | ||
| + | - [[https:// | ||
| + | - [[https:// | ||
| + | |||
| + | |||
| + | <file java VirtualPetGUIRunner4.java> | ||
| /** | /** | ||
| * A GUI class to run the Virtual Pet Lab | * A GUI class to run the Virtual Pet Lab | ||
| Line 182: | Line 368: | ||
| * @November 2024 | * @November 2024 | ||
| */ | */ | ||
| + | |||
| import java.awt.BorderLayout; | import java.awt.BorderLayout; | ||
| import java.awt.Color; | import java.awt.Color; | ||
| Line 197: | Line 384: | ||
| import javax.swing.Box; | import javax.swing.Box; | ||
| import javax.swing.BoxLayout; | import javax.swing.BoxLayout; | ||
| + | import javax.swing.ButtonGroup; | ||
| import javax.swing.ImageIcon; | import javax.swing.ImageIcon; | ||
| import javax.swing.JButton; | import javax.swing.JButton; | ||
| Line 206: | Line 394: | ||
| import javax.swing.JOptionPane; | import javax.swing.JOptionPane; | ||
| import javax.swing.JPanel; | import javax.swing.JPanel; | ||
| + | import javax.swing.JRadioButton; | ||
| import javax.swing.Timer; | import javax.swing.Timer; | ||
| - | public class VirtualPetGUIRunner | + | public class VirtualPetGUIRunner4 extends JFrame |
| { | { | ||
| - | // CHANGE THIS VARIABLE VALUE TO TEST AT A DIFFERENT SPEED | + | // Edit this value to change speed of update |
| private final int INTERVAL_IN_SECONDS = 10; | private final int INTERVAL_IN_SECONDS = 10; | ||
| | | ||
| Line 216: | Line 405: | ||
| private int timerSeconds, | private int timerSeconds, | ||
| private boolean isEating, isPlaying; | private boolean isEating, isPlaying; | ||
| - | private | + | private |
| + | private Food apple, cupcake, broccoli, potato; | ||
| + | private Game coinToss, hoopJumping, | ||
| private ImageIcon imageHappy, imageSad, imageEat, imagePlay; | private ImageIcon imageHappy, imageSad, imageEat, imagePlay; | ||
| private JFrame frame; | private JFrame frame; | ||
| Line 222: | Line 413: | ||
| private JLabel petLabel, | private JLabel petLabel, | ||
| private JButton foodButton, playButton; | private JButton foodButton, playButton; | ||
| + | private JRadioButton appleButton, | ||
| + | private ButtonGroup foodGroup, gameGroup; | ||
| private JMenu newPetMenu; | private JMenu newPetMenu; | ||
| private JMenuItem petRunner3, petRunner4; | private JMenuItem petRunner3, petRunner4; | ||
| private JMenuBar menuBar; | private JMenuBar menuBar; | ||
| | | ||
| - | public | + | public |
| { | { | ||
| - | | + | |
| - | | + | initVirtualPetObjects(name); |
| + | | ||
| | | ||
| // Automatically calls update after INTERVAL_IN_SECONDS time | // Automatically calls update after INTERVAL_IN_SECONDS time | ||
| ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | ||
| - | scheduler.scheduleAtFixedRate(() -> { pet3.updateStatus(); | + | scheduler.scheduleAtFixedRate(() -> { pet4.updateStatus(); |
| | | ||
| // Calls helper methods to initialize components of GUI | // Calls helper methods to initialize components of GUI | ||
| + | initTimerPanel(); | ||
| initPetDisplayPanel(); | initPetDisplayPanel(); | ||
| - | initTimerPanel(); | ||
| initStatsPanel(); | initStatsPanel(); | ||
| - | | + | |
| + | initGameButtons(); | ||
| + | initMenuPanel(); | ||
| fillLayout(); | fillLayout(); | ||
| initMenuBar(); | initMenuBar(); | ||
| Line 245: | Line 441: | ||
| // Initializes the frame | // Initializes the frame | ||
| frame = new JFrame(); | frame = new JFrame(); | ||
| - | frame.setPreferredSize(new Dimension(350, 500)); | + | frame.setPreferredSize(new Dimension(400, 500)); |
| frame.setTitle(" | frame.setTitle(" | ||
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| Line 256: | Line 452: | ||
| } | } | ||
| | | ||
| - | // Initializes | + | // Initializes |
| - | public void initButtons() | + | public void initVirtualPetObjects(String name) |
| { | { | ||
| - | | + | |
| - | | + | |
| - | foodButton | + | |
| - | foodButton.addActionListener(new FoodButtonListener()); | + | |
| - | foodButton.setPreferredSize(new Dimension(80, 30)); | + | |
| - | foodButton.setMaximumSize(new Dimension(80, 30)); | + | |
| - | | + | |
| - | playButton | + | |
| - | playButton.addActionListener(new PlayButtonListener()); | + | |
| - | playButton.setPreferredSize(new Dimension(80, 30)); | + | |
| - | playButton.setMaximumSize(new Dimension(80, 30)); | + | |
| - | | + | |
| - | menuPanel | + | |
| - | menuPanel.setLayout(new BoxLayout(menuPanel, | + | |
| - | menuPanel.add(Box.createVerticalStrut(50)); | + | |
| - | menuPanel.add(foodButton, | + | |
| - | menuPanel.add(Box.createVerticalStrut(10)); | + | |
| - | menuPanel.add(playButton, | + | |
| - | menuPanel.add(Box.createVerticalStrut(10)); | + | |
| - | } | + | |
| - | + | ||
| - | // Initializes the panel that contains the information text | + | |
| - | public void initStatsPanel() | + | |
| - | { | + | |
| - | info = "" + pet3.toString().replaceAll(" | + | |
| - | statsLabel = new JLabel(info); | + | |
| - | statsLabel.setBorder(BorderFactory.createEmptyBorder(0, 15, 15, 0)); | + | |
| - | | + | |
| - | messageLabel | + | |
| - | messageLabel.setForeground(Color.blue); | + | |
| - | messageLabel.setBorder(BorderFactory.createEmptyBorder(0, 15, 15, 0)); | + | |
| - | + | ||
| - | statsPanel = new JPanel(new GridLayout(2,1)); | + | |
| - | | + | |
| - | statsPanel.add(statsLabel); | + | |
| - | } | + | |
| - | + | ||
| - | // Initializes the panel that displays the VirtualPet | + | |
| - | public void initPetDisplayPanel() | + | |
| - | { | + | |
| - | imageHappy | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | + | ||
| - | petPanel = new JPanel(new BorderLayout()); | + | |
| - | petPanel.add(petLabel); | + | |
| } | } | ||
| | | ||
| Line 321: | Line 479: | ||
| int remainingSeconds = timerSeconds % 60; | int remainingSeconds = timerSeconds % 60; | ||
| timerLabel.setText(String.format(" | timerLabel.setText(String.format(" | ||
| - | info = "" | + | info = "< |
| statsLabel.setText(info); | statsLabel.setText(info); | ||
| if(!isPlaying && !isEating) | if(!isPlaying && !isEating) | ||
| { | { | ||
| - | if((ImageIcon)petLabel.getIcon() != imageSad && (pet3.getEnergyLevel() < 5 || pet3.getHappinessLevel() < 5)) | + | if((ImageIcon)petLabel.getIcon() != imageSad && (pet4.getEnergyLevel() < 5 || pet4.getHappinessLevel() < 5)) |
| petLabel.setIcon(imageSad); | petLabel.setIcon(imageSad); | ||
| - | else if((ImageIcon)petLabel.getIcon() != imageHappy && (pet3.getEnergyLevel() >= 5 && | + | else if((ImageIcon)petLabel.getIcon() != imageHappy && (pet4.getEnergyLevel() >= 5 && |
| petLabel.setIcon(imageHappy); | petLabel.setIcon(imageHappy); | ||
| } | } | ||
| Line 336: | Line 494: | ||
| timerPanel.add(timerLabel); | timerPanel.add(timerLabel); | ||
| timerPanel.setLayout(new GridBagLayout()); | timerPanel.setLayout(new GridBagLayout()); | ||
| + | } | ||
| + | | ||
| + | // Initializes the panel that displays the VirtualPet | ||
| + | public void initPetDisplayPanel() | ||
| + | { | ||
| + | imageHappy = new ImageIcon(" | ||
| + | imageSad = new ImageIcon(" | ||
| + | imageEat = new ImageIcon(" | ||
| + | imagePlay = new ImageIcon(" | ||
| + | | ||
| + | petLabel = new JLabel(imageHappy); | ||
| + | petPanel = new JPanel(new BorderLayout()); | ||
| + | petPanel.add(petLabel); | ||
| + | } | ||
| + | | ||
| + | // Initializes the panel that contains the information text | ||
| + | public void initStatsPanel() | ||
| + | { | ||
| + | info = "< | ||
| + | statsLabel = new JLabel(info); | ||
| + | statsLabel.setBorder(BorderFactory.createEmptyBorder(0, | ||
| + | | ||
| + | messageLabel = new JLabel(" | ||
| + | messageLabel.setForeground(Color.blue); | ||
| + | messageLabel.setBorder(BorderFactory.createEmptyBorder(0, | ||
| + | | ||
| + | statsPanel = new JPanel(new GridLayout(2, | ||
| + | statsPanel.add(messageLabel); | ||
| + | statsPanel.add(statsLabel); | ||
| + | } | ||
| + | | ||
| + | // Initializes the Food and food option buttons | ||
| + | public void initFoodButtons() | ||
| + | { | ||
| + | foodButton = new JButton(" | ||
| + | foodButton.addActionListener(new FoodButtonListener()); | ||
| + | foodButton.setPreferredSize(new Dimension(80, | ||
| + | foodButton.setMaximumSize(new Dimension(80, | ||
| + | | ||
| + | appleButton = new JRadioButton(" | ||
| + | cupcakeButton = new JRadioButton(" | ||
| + | broccoliButton = new JRadioButton(" | ||
| + | potatoButton = new JRadioButton(" | ||
| + | | ||
| + | foodGroup = new ButtonGroup(); | ||
| + | foodGroup.add(appleButton); | ||
| + | foodGroup.add(cupcakeButton); | ||
| + | foodGroup.add(broccoliButton); | ||
| + | foodGroup.add(potatoButton); | ||
| + | appleButton.setSelected(true); | ||
| + | } | ||
| + | | ||
| + | // Initializes the Game and game option buttons | ||
| + | public void initGameButtons() | ||
| + | { | ||
| + | playButton = new JButton(" | ||
| + | playButton.addActionListener(new PlayButtonListener()); | ||
| + | playButton.setPreferredSize(new Dimension(80, | ||
| + | playButton.setMaximumSize(new Dimension(80, | ||
| + | coinButton = new JRadioButton(" | ||
| + | hoopButton = new JRadioButton(" | ||
| + | simonButton = new JRadioButton(" | ||
| + | gameGroup = new ButtonGroup(); | ||
| + | gameGroup.add(coinButton); | ||
| + | gameGroup.add(hoopButton); | ||
| + | gameGroup.add(simonButton); | ||
| + | coinButton.setSelected(true); | ||
| + | } | ||
| + | | ||
| + | // Initializes the menu panel that contains all buttons | ||
| + | public void initMenuPanel() | ||
| + | { | ||
| + | GridBagConstraints c = new GridBagConstraints(); | ||
| + | menuPanel = new JPanel(); | ||
| + | menuPanel.setLayout(new BoxLayout(menuPanel, | ||
| + | menuPanel.add(Box.createVerticalStrut(10)); | ||
| + | menuPanel.add(foodButton, | ||
| + | menuPanel.add(appleButton, | ||
| + | menuPanel.add(cupcakeButton, | ||
| + | menuPanel.add(broccoliButton, | ||
| + | menuPanel.add(potatoButton, | ||
| + | menuPanel.add(Box.createVerticalStrut(10)); | ||
| + | menuPanel.add(playButton, | ||
| + | menuPanel.add(coinButton, | ||
| + | menuPanel.add(hoopButton, | ||
| + | menuPanel.add(simonButton, | ||
| + | menuPanel.add(Box.createVerticalStrut(10)); | ||
| } | } | ||
| | | ||
| Line 360: | Line 605: | ||
| c.fill = GridBagConstraints.BOTH; | c.fill = GridBagConstraints.BOTH; | ||
| c.weightx = 60; | c.weightx = 60; | ||
| - | c.weighty = 70; | + | c.weighty = 50; |
| c.gridx = 0; | c.gridx = 0; | ||
| c.gridy = 0; | c.gridy = 0; | ||
| Line 368: | Line 613: | ||
| mainPanel.add(menuPanel, | mainPanel.add(menuPanel, | ||
| c.weightx = 60; | c.weightx = 60; | ||
| - | c.weighty = 30; | ||
| c.gridx = 0; | c.gridx = 0; | ||
| c.gridy = 1; | c.gridy = 1; | ||
| Line 382: | Line 626: | ||
| public void actionPerformed(ActionEvent e) | public void actionPerformed(ActionEvent e) | ||
| { | { | ||
| - | | + | |
| - | messageLabel.setText(" | + | if(cupcakeButton.isSelected()) |
| - | info = "" | + | f = cupcake; |
| + | else if(broccoliButton.isSelected()) | ||
| + | f = broccoli; | ||
| + | else if(potatoButton.isSelected()) | ||
| + | f = potato; | ||
| + | pet4.feed(f); | ||
| + | messageLabel.setText(" | ||
| + | info = "< | ||
| statsLabel.setText(info); | statsLabel.setText(info); | ||
| - | animationTimerSeconds = 0; | ||
| animationTimerSeconds = 0; | animationTimerSeconds = 0; | ||
| isEating = true; | isEating = true; | ||
| Line 397: | Line 647: | ||
| if(animationTimerSeconds >= 2) | if(animationTimerSeconds >= 2) | ||
| { | { | ||
| - | if(pet3.getEnergyLevel() < 5 || pet3.getHappinessLevel() < 5) | + | if(pet4.getEnergyLevel() < 5 || pet4.getHappinessLevel() < 5) |
| petLabel.setIcon(imageSad); | petLabel.setIcon(imageSad); | ||
| else | else | ||
| Line 407: | Line 657: | ||
| }); | }); | ||
| animationTimer.start(); | animationTimer.start(); | ||
| + | animationTimerSeconds = -1; | ||
| } | } | ||
| } | } | ||
| - | | + | |
| // Defines the action when the Play button is clicked | // Defines the action when the Play button is clicked | ||
| class PlayButtonListener implements ActionListener | class PlayButtonListener implements ActionListener | ||
| Line 415: | Line 666: | ||
| public void actionPerformed(ActionEvent e) | public void actionPerformed(ActionEvent e) | ||
| { | { | ||
| - | | + | |
| - | messageLabel.setText(" | + | if(hoopButton.isSelected()) |
| - | info = "" | + | g = hoopJumping; |
| + | else if(simonButton.isSelected()) | ||
| + | g = simonSays; | ||
| + | if(pet4.play(g)) | ||
| + | messageLabel.setText("< | ||
| + | | ||
| + | | ||
| + | info = "< | ||
| statsLabel.setText(info); | statsLabel.setText(info); | ||
| animationTimerSeconds = 0; | animationTimerSeconds = 0; | ||
| Line 429: | Line 687: | ||
| if(animationTimerSeconds >= 2) | if(animationTimerSeconds >= 2) | ||
| { | { | ||
| - | if(pet3.getEnergyLevel() < 5 || pet3.getHappinessLevel() < 5) | + | if(pet4.getEnergyLevel() < 5 || pet4.getHappinessLevel() < 5) |
| petLabel.setIcon(imageSad); | petLabel.setIcon(imageSad); | ||
| else | else | ||
| Line 448: | Line 706: | ||
| { | { | ||
| try | try | ||
| - | { | + | { |
| String input = JOptionPane.showInputDialog(" | String input = JOptionPane.showInputDialog(" | ||
| VirtualPetGUIRunner init = new VirtualPetGUIRunner(input); | VirtualPetGUIRunner init = new VirtualPetGUIRunner(input); | ||
| Line 465: | Line 723: | ||
| public void actionPerformed(ActionEvent e) | public void actionPerformed(ActionEvent e) | ||
| { | { | ||
| - | // " | ||
| - | // VirtualPetGUIRunner4 in your project | ||
| - | /* | ||
| try | try | ||
| { | { | ||
| Line 478: | Line 733: | ||
| System.out.println(err); | System.out.println(err); | ||
| } | } | ||
| - | */ | ||
| } | } | ||
| } | } | ||
| Line 485: | Line 739: | ||
| { | { | ||
| String input = JOptionPane.showInputDialog(" | String input = JOptionPane.showInputDialog(" | ||
| - | | + | |
| } | } | ||
| } | } | ||
| Line 491: | Line 745: | ||
| ===== Activity 5 ===== | ===== Activity 5 ===== | ||
| - | < | + | ==== Adding Your Own Features ==== |
| - | import java.util.Scanner; | + | Copy your code from '' |
| - | import | + | |
| - | import | + | |
| - | import | + | |
| - | public | + | Implement two new features in your '' |
| - | { | + | |
| - | // Prints out menu and returns user choice | + | |
| - | public static int getChoice(Scanner input) | + | |
| - | { | + | |
| - | int selection = 0; | + | |
| - | while (selection < 1 || selection > 4) | + | |
| - | { | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | } | + | |
| - | return selection; | + | |
| - | } | + | |
| - | + | ||
| - | // Prints out food options and returns user choice | + | |
| - | public static int getPantry(Scanner input) | + | |
| - | { | + | |
| - | int selection = 0; | + | |
| - | while (selection < 1 || selection > 5) | + | |
| - | { | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | selection = input.nextInt(); | + | |
| - | } | + | |
| - | return selection; | + | |
| - | + | ||
| - | } | + | |
| - | + | ||
| - | // Prints out game options and returns user choice | + | |
| - | public static int getGame(Scanner input) | + | |
| - | { | + | |
| - | int selection = 0; | + | |
| - | while (selection < 1 || selection > 4) | + | |
| - | { | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | selection = input.nextInt(); | + | |
| - | } | + | |
| - | return selection; | + | |
| - | } | + | |
| - | + | ||
| - | // Displays a picture of the pet | + | |
| - | public static void printPet(String emo) | + | |
| - | { | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | System.out.println(" | + | |
| - | } | + | |
| - | + | ||
| - | public static void main(String[] args) | + | |
| - | { | + | |
| - | // CHANGE THIS VARIABLE VALUE TO TEST AT A DIFFERENT SPEED | + | |
| - | final int INTERVAL_IN_SECONDS = 10; | + | |
| - | + | ||
| - | // Sets up Scanner for user input | + | |
| - | Scanner input = new Scanner(System.in); | + | |
| - | + | ||
| - | VirtualPet4 myPet = new VirtualPet4(" | + | |
| - | + | ||
| - | | + | |
| - | Food apple = new Food(" | + | |
| - | Food cupcake = new Food(" | + | |
| - | Food broccoli = new Food(" | + | |
| - | Food potato = new Food(" | + | |
| - | + | ||
| - | // Instantiates Game objects | + | |
| - | Game coinToss = new Game(" | + | |
| - | Game hoopJumping = new Game(" | + | |
| - | Game simonSays = new Game(" | + | |
| - | + | ||
| - | // Sets up a ScheduledExecutorService object that will call updateStatus | + | |
| - | // every 1 minute. | + | |
| - | ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | + | |
| - | scheduler.scheduleAtFixedRate(() -> { myPet.updateStatus(); | + | |
| - | + | ||
| - | System.out.println(myPet); | + | |
| - | printPet(" | + | |
| - | + | ||
| - | int choice = getChoice(input); | + | |
| - | while (choice != 4) | + | |
| - | { | + | |
| - | if(choice == 1) | + | |
| - | { | + | |
| - | System.out.println(myPet); | + | |
| - | } | + | |
| - | else if (choice == 2) | + | |
| - | { | + | |
| - | System.out.println(" | + | |
| - | int food = getPantry(input); | + | |
| - | Food f = null; | + | |
| - | if (food == 1) | + | |
| - | f = apple; | + | |
| - | else if (food == 2) | + | |
| - | f = cupcake; | + | |
| - | else if (food == 3) | + | |
| - | f = broccoli; | + | |
| - | else if (food == 4) | + | |
| - | f = potato; | + | |
| - | if (f != null) | + | |
| - | { | + | |
| - | myPet.feed(f); | + | |
| - | System.out.println(" | + | |
| - | + " 1 " + f.getName()); | + | |
| - | } | + | |
| - | } | + | |
| - | else if (choice == 3) | + | |
| - | { | + | |
| - | System.out.println(" | + | |
| - | int game = getGame(input); | + | |
| - | Game g = null; | + | |
| - | if (game == 1) | + | |
| - | g = coinToss; | + | |
| - | else if (game == 2) | + | |
| - | g = hoopJumping; | + | |
| - | else if (game == 3) | + | |
| - | g = simonSays; | + | |
| - | if (g != null) | + | |
| - | { | + | |
| - | boolean hasWon = myPet.play(g); | + | |
| - | System.out.println(" | + | |
| - | + " with " + myPet.getName()); | + | |
| - | if (hasWon) | + | |
| - | | + | |
| - | else | + | |
| - | | + | |
| - | } | + | |
| - | } | + | |
| - | if (myPet.getEnergyLevel() >= 5 && myPet.getHappinessLevel() >= 5) | + | |
| - | printPet(" | + | |
| - | else | + | |
| - | printPet(" | + | |
| - | System.out.println(myPet.getName().toUpperCase()); | + | |
| - | choice = getChoice(input); | + | |
| - | } | + | |
| - | scheduler.shutdown(); | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | </ | + | |
| + | The following are ideas for additional features, inspired by the original Tamagotchi: | ||
| + | - Your virtual pet has a hidden health level based on their energy, happiness, sickness, and cleanliness. | ||
| + | - Your virtual pet will randomly use the bathroom and will need to be cleaned. | ||
| + | - Your virtual pet will randomly get sick and need to be given medicine. | ||
| + | - Your virtual pet will notify the user if their energy level is 0, if their happiness level is 0, if they are sick, or if they used the bathroom and need to be cleaned. | ||
| + | - Create a more interactive game in the Game class for the '' | ||
virtual_pet.1772132438.txt.gz · Last modified: by frchris
