Implementing Rules. Java rules ok

Similar documents
CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Determining responsibilities. What object should be responsible for what behavior?

Writing your own Exceptions. How to extend Exception

School of Engineering and Computing INTRODUCTION TO PROGRAMMING 2 M1G /01/11 JP

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Written by John Bell for CS 342, Spring 2018

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

CS 11 java track: lecture 3

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

1. (5 points) In your own words, describe what an instance is.

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Exceptions. What exceptional things might our programs run in to?

DarkRift Server Plugin Tutorial

Address book. Presenting the Java assignment about an Address Book v

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

Recreation. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Foundations of object orientation

2.6 Error, exception and event handling

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

P2: Advanced Java & Exam Preparation

CS3901 Intermediate Programming and Data Structures Winter 2008 (Q2 AY 08) Assignment #5. Due: noon Tuesday, 3 March 2008

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Lecture 11 Unbounded Arrays

Basic Object-Oriented Concepts. 5-Oct-17

Traditional Error Handling

About This Lecture. Repetition Control Structures - while. Outline. Repetition

Tools for Unit Test - JUnit

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

CS211 Lecture: Modeling Dynamic Behaviors of Systems; Interaction Diagrams and Statecharts Diagrams in UML

DOT NET TRAINING PROGRAM

About This Lecture. Repetition Control Structures - while. Repetition. Outline. Semantics - while. Java Syntax: while Statement

Multiple Inheritance, Abstract Classes, Interfaces

Page 1. Human-computer interaction. Lecture 1b: Design & Implementation. Building user interfaces. Mental & implementation models

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS 251, Intermediate Programming Midterm Exam October 9, 2013

CPS122 Lecture: Detailed Design and Implementation

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Assertions and Exceptions Lecture 11 Fall 2005

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Classes, interfaces, & documentation. Review of basic building blocks

CS211 Lecture: Modeling Dynamic Behaviors of Systems; Interaction Diagrams in UML

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

A - 1. CS 494 Object-Oriented Analysis & Design. UML Class Models. Overview. Class Model Perspectives (cont d) Developing Class Models

Basic Keywords Practice Session

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Model answers/marking scheme in bold, additional comments in bold italic.

CSC Java Programming, Fall Java Data Types and Control Constructs

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Lecture 19 GUI Events

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

Exceptions. Examples of code which shows the syntax and all that

Object-Oriented Principles and Practice / C++

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

CS Exam 1 Review Suggestions

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Chapter 11: Exception Handling

Page 1. Human-computer interaction. Lecture 2: Design & Implementation. Building user interfaces. Users and limitations

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

No SVN checkout today. Object-Oriented Design

Object-Oriented Software Engineering Practical Software Development using UML and Java

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Do not turn to the next page until the start of the exam.

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

CSE 143 Lecture 12 Inheritance

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

16 Multiple Inheritance and Extending ADTs

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

SRM INSTITUTE OF SCIENCE & TECHNOLOGY Faculty of Science and Humanities Department of Computer Science UNIT I - INTRODUCTION TO JAVA

Ready for the Real World. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 8 09/18/2007

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

CS 251 Intermediate Programming Exceptions

Java Programming Lecture 23

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Exceptions Handeling

Title. Java Just in Time. John Latham. February 8, February 8, 2018 Java Just in Time - John Latham Page 1(0/0)

Core Competency DOO (Design Object-Oriented)

OBJECT-ORIENTED DESIGN

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Object Oriented Programming Exception Handling

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Lecture Topics. Administrivia

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Spring % of course grade

CS101 Lecture 30: How Search Works and searching algorithms.

Class diagrams. Modeling with UML Chapter 2, part 2. Class Diagrams: details. Class diagram for a simple watch

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

Check out FilesAndExceptions from SVN. Exam 2 Review File I/O, Exceptions Vector Graphics Project

CS 61B Discussion 5: Inheritance II Fall 2014

GUI Event Handlers (Part I)

1.00 Lecture 8. Using An Existing Class, cont.

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Transcription:

Implementing Rules Java rules ok

This is a design discussion lecture for Sprint 2 of the game

What types of Rule:s exist in the Game? After analysing the old specification, it seems that there are two main types of Rule:s in the game: ThingRule:s - Rules about picking up Thing:s RoomRule:s - Rules about what happens with the Room

ThingRule:s identified Bird: Affected by the Rod (you must put it down in order to pick up the Bird) Can t be picked up, unless we have the Cage in our inventory Pirate Chest Can t be picked up (opened) unless we have the four keys

RoomRule:s identified Snake room: Snake means no SOUTH exit Snake disappears if we drop down Cage and Bird Without the Snake, SOUTH room is unblocked (and sets to a new Room) Dragon room: Dragon keeps Glass Key from appearing Dragon disappears if we drop down Gold, Jewels, Diamonds and Silver Without the Dragon, WEST exit disappears Bird room: The bird gets scared if we have the Rod in our inventory

Game over Rooms Two Room:s will kill the Player (Game Over) We suggest that we make these Room:s special, when reading them from the Database - The description of theses Room:s should describe that the Player dies and it s Game Over. This could be accomplished as simply as setting all exits from this Room to null (meaning there s no way out of there) - The User playing the game can t continue, since all navigation buttons are disabled.

ThingRule What behavior is affected by the ThingRules? Look at the descriptions again. The verb we re after is pick up.

ThingRule What behavior is affected by the ThingRules? Look at the descriptions again. The verb we re after is pick up. The behavior affected is the Player s takething(thing) method.

ThingRule So, we need a lot of IF-statements in the takething(thing) method?

ThingRule So, we need a lot of IF-statements in the takething(thing) method? Not necessarily. What we need, is to check the ThingRule before completing the operation of taking a thing. If the ThingRule decides it is allowed to pick up the Thing, we ll procede as usual (add to inventory, remove from room). Otherwise we ll throw an exception (so that the GUI, and therefore the User playing, notices that a rule was violated.

ThingRule How can we implement the ThingRule? Requirements/dependencies: There are more than one ThingRule The ThingRule depends on the Thing to be picked up and the Player s inventory We don t want to hard code rules using if statements The Player doesn t know the Rules, they have to be looked up

Possible solution - The Things carry the rules One way of implementing this would be that the Thing:s have a rule connected to them. Feels intuitive?

Possible solution - The Player carries the rules Another way of implementing this would be that the Player have a rules for the Thing:s. Feels intuitive? When Playing the game, the Player can t know the Rules - they constitute part of what makes the game harder to solve.

Possible solution - The (G)UI carries the rules Another way of implementing this would be that the (G)UI have a rules for the Thing:s. Feels intuitive? But what if we have more than one UI for the game? Should each type and instance of a UI have a list of ThingRule:s?

Possible solution - There s some kind of rule book Another way of implementing this would be that there s some kind of RuleBook to query when picking up Thing:s. This could be implemented as a static method which returns some kind of result with permit/prevent and a message. Another way could be that the RuleBook returns a ThingRule for a particular Thing (provided as an argument when querying).

Investigating - There s some kind of rule book Let s play with the idea that the RuleBook exists and can return a ThingRule provided we give it a Thing: +getrulefor(thing : Thing) : ThingRule As you see (underline!), the method is a static method. The RuleBook must have some kind of internal map between Thing and ThingRule, so that if we do this: ThingRule rule = RuleBook.getRuleFor(thing); then we get the correct rule for the provided thing.

Investigating - There s some kind of rule book Let s play with the idea that the RuleBook exists and can return a ThingRule provided we give it a Thing: +getrulefor(thing : Thing) : ThingRule But most things don t have any rules for picking them up. What would the method return for e.g. a Key? Well, a rule which permits picking it up, of course!

Investigating - There s some kind of rule book Let s play with the idea that the RuleBook exists and can return a ThingRule provided we give it a Thing: +getrulefor(thing : Thing) : ThingRule So, what is a ThingRule, then?

Investigating - There s some kind of rule book Let s play with the idea that the RuleBook exists and can return a ThingRule provided we give it a Thing: +getrulefor(thing : Thing) : ThingRule So, what is a ThingRule, then? An object which can either permit picking the Thing up, or convey some kind of disruption with a message if it doesn t permit it.

Investigating - There s some kind of rule book The behavior of the ThingRule objects could be this: +apply() : void { exceptions=ruleviolationexception } If RuleViolationException is a checked exception, the Player s takething() method must handle it (or pass it on the the UI).

Investigating - There s some kind of rule book The behavior of the ThingRule objects could be this: +apply() : void { exceptions=ruleviolationexception } So how do we create all the ThingRule objects, then, so that they have different behavior (rules) for different Thing:s? Let s look at ThingRule from the Player s perspective!

Investigating - ThingRule from the Player s view The Player, in the takething(thing thing) method, must query the RuleBook if the Thing to be taken is allowed. If not, an Exception will be thrown. The method doesn t know what the Thing is (it is not hard coded, but rather just a parameter to takething(thing thing). The Player doesn t want to or need to know what actual class the ThingRule is, just how to query it for applying the rule for the Thing. How can we write code which operates on a number of classes sharing a common method?

Investigating - ThingRule from the Player s view The ThingRule could be a Java Interface, declaring just one simple method: +apply() : void {exceptions=ruleviolationexception} (note the italics - what do you think it means?) Now, the +getrulefor(thing : Thing) : ThingRule method will simply return some object of some class implementing ThingRule. This object could be created as a local inner anonymous class or a Java 8 lambda expression.

Investigating - ThingRule from the Player s view The ThingRule could be a Java Interface, declaring just one simple method: +apply() : void {exceptions=ruleviolationexception} Knowing that ThingRule is just an interface with just the above method, the takething(thing thing) method could do this: public void takething(thing thing) throws RuleViolationException { RuleBook.getRuleFor(thing).apply(); // here an exception might happen currentroom.removething(thing); inventory.add(thing); }

Investigating - ThingRule from the UI s view The ThingRule could be a Java Interface, declaring just one simple method: +apply() : void {exceptions=ruleviolationexception} The (G)UI is of course mandated by the checked exception to handle it: Thing thing = ((JList<Thing>)event.getSource()).getSelectedValue(); try { Player.getInstance().takeThing(thing); updatemodels(); // make the UI discover the change and // fix the UI lists (inventory and room things) } catch (RuleViolationException e) { messages.settext(e.getmessage()); // notify the User of the denial }

RuleViolationException Inherit Exception and create a constructor which takes a String message, call super(message) in the constructor;

RuleBook Create a static Map<Thing, ThingRule>. We ll populate the map from the CaveInitializer code, so that all the Thing:s with rules are in the map. Make a method, static public ThingRule getrulefor(thing thing) which Looks up the Thing in the map and if it exists, return the corresponding ThingRule otherwise return a new ThingRule which doesn t throw an exception

Alternative approach for apply() We could make the apply() method of ThingRule return a boolean, to make the code in Player more clear: public void takething(thing thing) throws RuleViolationException { if(rulebook.getrulefor(thing).apply()) { currentroom.removething(thing); inventory.add(thing); } } // ThingRule interface: +apply() : boolean {exceptions=ruleviolationexception}

Alternative approach for apply() Our code, which populates the map in RuleBook could create a rule for a Thing with a rule (bird requires cage) like this: RuleBook.addThingRule(thing, () -> { if(! Player.getInstance().inventory().contains(Things.get("Cage")) ) { throw new RuleViolationException("Bird requires a Cage"); } return true; });

Alternative approach for apply() Our code, which populates the map in RuleBook could create a rule for a Thing without a rule like this: RuleBook.addThingRule(thing, () -> { return true; }); When applying that rule, apply() simply returns true and no exception is thrown. You don t have to worry about this code, we ll provide it in CaveInitializer.

Things without rules Or, better still... Let the getrulefor(thing) method of RuleBook return a rule which just allows if it can t find any rule for the thing asked for: public static getrulefor(thing thing) { ThingRule rule = rules.get(thing); if(rule == null) { rule = () -> { return true; }; } return rule; }

Alternative approach for apply() Let s go for the boolean apply() throws RuleViolation approach.

UML updates Player gets a different notation for the takething(thing thing) method: +takething(thing : Thing) : void {exceptions=ruleviolationexception} The interface ThingRule has the following: +apply() : boolean {exceptions=ruleviolationexception} RuleBook: +getrulefor(thing : Thing) : ThingRule RuleViolationException just extends java.lang.exception and is thus a checked exception.

Next sprint - RoomRule:s! Next, we ll have to implement the RoomRule:s. We ll do that in the next sprint!