160 likes | 291 Views
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.
E N D
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 • 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
Software Engineering • Analysis • Design • Implementation • Testing • Maintenance
Memory Game • Three classes • MemoryGame • MemoryGameWindow • ColorRectangle
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;
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; } }
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; }
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(); } }
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;
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]);
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(); }
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(); }
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()); } }
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); }
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(); } }