1 / 16

CS 120 Day 2

CS 120 Day 2. More Introduction Computer Science Software Engineering Integrated development environments (IDE) BlueJ Continue the discussion of the MemoryGame program. Computer Science. Processes Algorithms Control Complexity Abstraction. Software Engineering.

orenda
Download Presentation

CS 120 Day 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 120 Day 2 • More Introduction • Computer Science • Software Engineering • Integrated development environments (IDE) • BlueJ • Continue the discussion of the MemoryGame program

  2. Computer Science • Processes • Algorithms • Control Complexity • Abstraction

  3. Software Engineering • Apply Engineering Principles to the development of software. • Definition given by the IEEE • The application of a systematic, disciplined, quantifiable approach to the development, operation, and maintenance of software; that is, the application of engineering to software

  4. Software Engineering • Analysis • Design • Implementation • Testing • Maintenance

  5. Memory Game • Three classes • MemoryGame • MemoryGameWindow • ColorRectangle

  6. MemoryGame import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.util.Random; //Controller for the memory game public class MemoryGame implements ActionListener { MemoryGameWindow gameWindow; Random rNum; ColorRectangle items[]; int playBackItem; int toggleCount; Timer timer; int numItems; int nextGuess;

  7. MemoryGame public MemoryGame() { gameWindow = new MemoryGameWindow(10, 10, this); rNum = new Random(); timer = new Timer(1000, this); timer.setCoalesce(false); } private void generateItems() { int i = 0; int next; items = new ColorRectangle[numItems]; while (i < numItems) { next = rNum.nextInt(3); items[i] = gameWindow.getRectangle(next); i = i + 1; } }

  8. MemoryGame public void startGame(int n) { numItems = n; generateItems(); playBackItem = 0; toggleCount = 0; timer.start(); } public void actionPerformed(ActionEvent e) { if (playBackItem == numItems-1 && toggleCount == 1) { timer.stop(); nextGuess = 0; gameWindow.showResponse("YOUR TURN"); } items[playBackItem].toggleColor(); playBackItem = playBackItem + toggleCount; toggleCount = (toggleCount + 1) % 2; }

  9. MemoryGame public void nextMove(ColorRectangle r) { if (items[nextGuess] == r) { nextGuess = nextGuess + 1; if (nextGuess == numItems) { gameWindow.showResponse("YOU WIN"); } } else { gameWindow.showResponse("ERROR IN MOVE "+ (nextGuess +1)); } } public static void main(String args[]) { new MemoryGame(); } }

  10. MemoryGameWindow import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; //Implements the user interface for the memory game public class MemoryGameWindow extends JFrame implements ChangeListener, ActionListener, MouseListener { MemoryGame control; ColorRectangle rects[]; JButton start; JSlider numItemsSlider; int numItems; JLabel label;

  11. MemoryGameWindow public MemoryGameWindow(int x, int y, MemoryGame c) { setBounds(x, y, 400, 600); setTitle("Memory Game"); getContentPane().setLayout(null); setVisible(true); control = c; rects = new ColorRectangle[3]; rects[0] = new ColorRectangle(30, 20, 100, 75, Color.red, Color.black); rects[0].addMouseListener(this); add(rects[0]); rects[1] = new ColorRectangle(150, 20, 100, 75, Color.yellow, Color.black); rects[1].addMouseListener(this); add(rects[1]); rects[2] = new ColorRectangle(270, 20, 100, 75, Color.blue, Color.black); rects[2].addMouseListener(this); add(rects[2]);

  12. MemoryGameWindow numItems = 5; numItemsSlider = new JSlider(JSlider.HORIZONTAL, 2, 22, numItems); numItemsSlider.setBounds(20, 140, 360, 40); numItemsSlider.setMajorTickSpacing(4); numItemsSlider.setMinorTickSpacing(1); numItemsSlider.setPaintTicks(true); numItemsSlider.setPaintLabels(true); numItemsSlider.addChangeListener(this); add(numItemsSlider); start = new JButton("Start"); start.setBounds(150, 240, 100, 40); start.addActionListener(this); add(start); repaint(); }

  13. MemoryGameWindow public ColorRectangle getRectangle(int i) { //PRE: i >= 0 AND i <= 2 return rects[i]; } public void clearResponse() { if (label != null) { remove(label); repaint(); } } public void showResponse(String s) { clearResponse(); label = new JLabel(s, SwingConstants.CENTER); label.setBounds(125, 300, 150, 40); add(label); repaint(); }

  14. MemoryGameWindow public void stateChanged(ChangeEvent e) { JSlider s = (JSlider) e.getSource(); numItems = s.getValue(); } //This method is called when the start button is clicker public void actionPerformed(ActionEvent e) { clearResponse(); control.startGame(numItems); } //This method is called when one of the rectangles is clicked public void mouseClicked(MouseEvent e) { control.nextMove((ColorRectangle) e.getSource()); } }

  15. ColorRectangle import javax.swing.*; import java.awt.*; import java.awt.event.*; //Implements a ColorRectangle that responds to mouse clicked public class ColorRectangle extends JComponent { Color current; Color next; public ColorRectangle(int x, int y, int w, int h, Color c1, Color c2) { super(); setBounds(x, y, w, h); current = c1; next = c2; setBackground(current); }

  16. ColorRectangle public void paint(Graphics g) { g.setColor( getBackground() ); g.fillRect(0, 0, getWidth()-1, getHeight()-1); paintChildren(g); } public void toggleColor() { Color temp = current; current = next; next = temp; setBackground(current); repaint(); } }

More Related