C9, a case study: Solitaire

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Black Jack in Objective-C
1 CSC 222: Computer Programming II Spring 2004 HW1 review arrays vs. vectors class design object-oriented design data objects + functionality example:
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Directions: Form into Teams (up to 7 team members) Clear your tables and move chairs against the wall Only 4 people will participate per iteration Four.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Card Tricks Arranged By Liam Saliba Source – Mismag822Mismag822.
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Abstract Classes We often define classes as subclasses of other classes – First, define the superclass – In some cases, a superclass may not have enough.
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
Design Example. Requirements Make a program that simulates the game of blackjack For now, we ignore money/betting…. just simulate game play But… this.
Elevens Lab Student Material – on website
Midterm 2 Practice CIS 113 Spring public class Card { public int rank; public String suit; Card(){rank = -1; suit = "-";} Card(Card card){rank =
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann.
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
Ch 16. Case Study A Card Game
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Mau Mau (mow- mow) A card game
OOP Week 4 1 Object Oriented Programming in Java Monday, Week 4 Last week’s asst. –rating…. This week’s asst. OOP Concepts Inheritance & Substitutability.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
o Simulate a deck of playing cards o Shuffle the deck o Deal (5) cards into the hand o Turn over the first card o The user must guess whether the next.
o Simulate a deck of playing cards o Shuffle the deck o Deal 5 cards into the hand o Turn over the first card o The user must guess whether the next card.
VOCABULARY  Deck or pack  Suit  Hearts  Clubs  Diamonds  Spades  Dealer  Shuffle  Pick up  Rank  Draw  Set  Joker  Jack 
Ace to King OBJECTIVE: To increase visual attention, and visual/spatial memory abilities. EQUIPMENT: A deck of ordinary cards. EQUIPMENT: A deck of ordinary.
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
Neal Stublen Key Concepts  Classes  Objects  Inheritance  Polymorphism  Encapsulation.
Class & Object 蔡柏灃.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
More on Classes Pepper With help from rs.html.
Computer Science 111 Fundamentals of Programming I More Data Modeling.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
Two Way Tables Venn Diagrams Probability. Learning Targets 1. I can use a Venn diagram to model a chance process involving two events. 2. I can use the.
Catch the Spirit of Mathematical Practices Mathematics Investigations Q-Cards: Rational Numbers The Q-Card game has 8 starter cards, 28 playing cards,
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
World Religions Tabulate Game. wild cards wild cards ordinary cards.
Designing a Card Game Solitaire--Thirteens. Game.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
1 CSC 221: Computer Programming I Fall 2006 Lists, data storage & access  ArrayList class  methods: add, get, size, remove, contains, set, indexOf, toString.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Welcome back!. Object Oriented Programming – Encapsulation Classes encapsulate state (fields) and behavior (methods) – Polymorphism Signature Polymorphism.
EOBoard Assignment 2: 8-off Solitaire 1 Design Data Structures Suit Pip Card Deck EOBoard Foundations Columns Reserves EODeal Shuffle Seed (Int) EOBoard.
Chance We will base on the frequency theory to study chances (or probability).
Click to edit Master text styles Stacks Data Structure.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Shlomo Hershkop Basics overview. Shlomo Hershkop Basic Review - Overview Practice coding Practice coding finger guessing game finger guessing.
Section 5.5 Application: The Card Game of War. 5.5 Application: The Card Game of War A deck of cards is shuffled and dealt to two players The players.
How to do a card trick.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
null, true, and false are also reserved.
Pedro Freitas MathsJam 2017
Basics of OOP A class is the blueprint of an object.
Presentation transcript:

C9, a case study: Solitaire

The class Card Accessor methods (getters only) for suit and rank: prevent unwanted modification Good encapsulation: Card knows its rank and suit, how to draw or flip itself Lots of constants: public final static …

The Game: Klondike 52 cards: 28 in in 7 tableau piles (1-7), only top card is face up initially Suit piles (foundations) built from aces to kings in suit: won if all 52 in suit piles Deck: 24 cards initially, drawn one by one, and put, face up, onto discard pile Deck pile empty = game over Tableau pile: next-higher rank and opposite color, empty spaces filled with kings only; top-most card is always face-up can move complete builds, if they fit the target

Card Piles: Inheritance in Action SuitPile DeckPile DiscardPile TableauPile includes x canTake addCard display select

CardPile methods includes: are coordinates inside the pile? Overridden in tableau piles canTake: can a pile take a card? Default: no; overridden in suit and tableau piles addCard: adds a card to a list of cards; refined in discard pile to make sure cards face up display: displays top card (default); overridden in the tableau class select: called by the MouseListener; default is no action; overridden in table, deck, and discard piles to play the topmost card if possible

Some CardPile methods public final Card pop() { try { return (Card) thePile.pop(); } catch (EmptyStackException e) {return null;} } public void select(int tx, int ty) {} public boolean canTake(Card aCard) {return false;} CardPile (int xl, int yl) { x = xl; y = yl; thePile = new Stack(); }

The Suit Piles Simplest class Only: Trivial constructor: call super canTake: an ace, if empty, otherwise the next higher card of same suit All other behavior is inherited from Card

The Deck Pile Constructor generates a deck of cards and shuffles these: … Random generator = new Random(); for(int i=0; i<52; i++) { int j = Math.abs(generator.nextInt()) % 52; Object temp = thePile.elementAt(i); thePile.setElementAt(thePile.elementAt(j),i); thePile.setElementAt(temp,j); } public void select(int tx, int ty) { if (isEmpty()) return; Solitaire.discardPile.addCard(pop());

The Discard Pile addCard is refined: public void addCard(Card aCard) { if( !aCard.faceUp()) aCard.flip(); super.addCard(aCard); } Select is replaced; rather sophisticated: tries to add the topCard (if any) to any of the suitPiles first, otherwise to any of the tableauPiles, if possible.

The Tableau Pile Most complex CardPile subclass Constructor takes cards off the deck, finally displaying the top card canTake: takes kings if empty, or the next smaller rank of the opposite color includes: does not check bottom border select: if face-down, flip, otherwise try to add to suitPiles or other tableauPiles (buggy?)

The Application Class Relatively standard application class various static arrays for holding piles separate inner class subclassing Frame Interesting constructor “factoring out” most functionality into a separate “init” method, allows for elegant implementation of the restart functionality

Using factored-out “init” public init() { … /* initialize piles */ } public Solitaire() { window = new SolitaireFrame(); init(); window.show(); } private class RestartButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { repaint();

Playing the Polymorphic Game All the various piles are hold together in one array: CardPile[] allPiles = new CardPile[13]; Will call the appropriate method according to the actual type of pile (called virtual method in C++) public void paint(Graphics g) { for(int I =0; i<13; i++) allPiles[i].display(g); }

MouseKeeper private class MouseKeeper extends MouseAdapter { public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); for( int i=0; i<13; i++) if(allPiles[i].includes(x,y) { allPiles[i].select(x,y); // WHY x,y here? repaint(); }

Building a more complete game Described version is minimal (also in terms of GUI) and hard to win. Possible extensions: select: should allow “builds”, i.e. the movement of blocks of stuff Add restart: if deck is empty, reshuffle discardPile and move to deck More options see exercises

Summary Many features and benefits of inheritance Various card piles specialized from one common parent class Default behavior overridden less than half the time Overriding can replace or refine Substitutability: ties together inheritance and polymorphism Polymorphic variable: runtime-type determines which overridden method executes (called “virtual” method in C++, in Java all non-static methods are virtual)