Download presentation
Presentation is loading. Please wait.
Published byThomas Lang Modified over 9 years ago
1
CS 151: Object-Oriented Design October 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak
2
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 2 Assignment #4 Due Friday, October 25. Human players of the Rock Paper Scissors game generally do not make random choices. Humans try to develop strategies to beat the opponent. Humans exhibit patterns that a computer can exploit. Continually record the last N choices between the human and the computer player. For this game application, choose N = 3. After each human’s throw choice: Add the sequence consisting of the last 3 choices (human+computer+human) to your record.
3
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 3 Assignment #4 For example, suppose the record contains: SRP, PRS, RPR, PSR, SRP, SPS, PSS (The human’s choices are underlined.) Suppose the last pair of throws is SR. What can the computer predict the human will choose next to throw? The record contains two instances of SRP, and so based on this small sample, the computer can predict the human will next throw Paper. Therefore, the computer should next choose Scissors to win. _
4
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 4 Assignment #4 The longer the matches are, the more throw choice sequences the computer has recorded during the match, and the harder it will be for the human player to win. This is a very rudimentary example of machine learning, a branch of the computer science discipline artificial intelligence. You can initialize a fresh record of choice sequences each time you restart your application. In later assignments, you can store the record in a disk file so that the application’s “experience” grows over time. Add another command-line parameter to indicate whether the computer should use the random algorithm (from Assignment #3) or the new smart algorithm. In addition to the number of throws per match parameter.
5
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 5 Assignment #4 Before printing what the computer chose to throw, your program should print what the computer predicted that the human will throw. Use good object-oriented techniques to design how your application records throw choice sequences of length N = 3 and how your throw calculator looks up sequences to predict the human’s next choice. Make your design flexible enough to handle larger (odd) values of N. Include three JUnit test classes. Each test class should contain at least two test cases. _
6
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 6 Review Quiz #2.
7
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 7 Quiz 2013Oct3
8
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 8 Animation By now we know how to draw a shape and respond to timer ticks. Therefore, we can do simple animation! Move a shape horizontally across the screen. At each timer tick, erase the shape and redraw it slightly offset from its previous position. _
9
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 9 Class ShapeIcon Class ShapeIcon wraps up the shape as an icon: public class ShapeIcon implements Icon { private MoveableShape shape;... public ShapeIcon(MoveableShape shape, int width, int height) { this.shape = shape;... }... public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g; shape.draw(g2); }
10
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 10 Class JLabel MoveableShape is an interface. Class CarShape implements that interface. _ private static final int CAR_WIDTH = 100; private static final int ICON_WIDTH = 400; private static final int ICON_HEIGHT = 100; MoveableShape shape = new CarShape(0, 0, CAR_WIDTH); ShapeIcon icon = new ShapeIcon(shape, ICON_WIDTH, ICON_HEIGHT); JLabel label = new JLabel(icon); Put the icon inside of a Swing JLabel object:
11
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 11 Interface MoveableShape In order to do animation, you must be able to draw a shape and then move it. Move a shape = redraw the shape in a different location AKA translate the shape. public interface MoveableShape { void draw(Graphics2D g2); void translate(int dx, int dy); }
12
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 12 Class CarShape public class CarShape implements MoveableShape {... public void translate(int dx, int dy) { x += dx; y += dy; } public void draw(Graphics2D g2) { Rectangle2D.Double body =... ; Ellipse2D.Double frontTire =... ; Ellipse2D.Double rearTire =... ;... } } public interface MoveableShape { void draw(Graphics2D g2); void translate(int dx, int dy); }
13
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 13 Animation We’ll use a Timer object to drive the animation. At each timer tick: Move the shape (which we had put inside the JLabel object) Repaint the label object final int DELAY = 10; // Milliseconds between timer ticks Timer t = new Timer(DELAY, new ActionListener() { public void actionPerformed(ActionEvent event) { shape.translate(1, 0); label.repaint(); } } ); t.start();
14
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 14 Animation The method calls shape.translate(1, 0) to move the shape one pixel to the right. The method call label.repaint() tells the JLabel object to repaint itself. The label object tells its contents to repaint. Since its contents is an instance of class ShapeIcon which implements the Icon interface, the JLabel object knows to call the content’s paintIcon() method. public void actionPerformed(ActionEvent event) { shape.translate(1, 0); label.repaint(); }
15
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 15 Animation Animation demo
16
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 16 Poor Design! What is wrong with the design this code? MoveableShape shape = new CarShape(0, 0, CAR_WIDTH); ShapeIcon icon = new ShapeIcon(shape, ICON_WIDTH, ICON_HEIGHT); JLabel label = new JLabel(icon);... Timer t = new Timer(DELAY, new ActionListener() { public void actionPerformed(ActionEvent event) { shape.translate(1, 0); label.repaint(); } ); AnimationTester.main()
17
SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 17 Poor Design! Hint: Can you improve the design? _ public class ShapeIcon implements Icon { private MoveableShape shape;... } ShapeIcon
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.