Today - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Today

Description:

Perfecting a Nim Strategy. Convert Piles to binary. Examine columns of binary digits ... Perfect Nim Strategy. 1(3): OOO. 2(5): OOOOO. 3(7): OOOOOOO. 1(1): O. 2 ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 28
Provided by: davidc73
Category:
Tags: perfecting | today

less

Transcript and Presenter's Notes

Title: Today


1
Lecture 12
  • Today
  • Nim
  • Exam
  • Thursday, Febuary 20, 630-?
  • Location HC-126 (MIB Auditorium)
  • Assignments
  • Project Three due Thursday Feb 13

2
The Strategy Pattern
  • One basic operation needed by the system
  • Lots of potential implementations for the method
  • Each implementation uses a different strategy
  • Based on efficiency
  • Based on accuracy of results
  • Based on other (external) factors
  • Pick one implementation based on your needs
  • Example Register allocation in compiler
  • Different machines have unique architectures
  • Pick implementation appropriate for architecture

3
Basic Idea of Strategy Pattern
  • Define a base class that interacts with world
  • This is a generic version of algorithm
  • Declares all methods in our class
  • Define specific implementations as subclasses
  • Methods for each potential subclass are specific
    to the needs/requirements for that instance
  • Simply insert the appropriate subclass into an
    application

4
Strategy Pattern Games
  • Game has specific rules procedures
  • Single application
  • Standard interface for all players
  • Lots of different strategies for playing game
  • Insert an appropriate strategy when you play
  • This strategy is how you want to play the game

5
Class Exercises
  • Identify three different games you play
  • What is the interface for how you play the game
  • How do you communicate with game/other players
  • These are the rules of the game
  • What are different strategies that people can
    employ when playing the game?

6
Brief classification of games
  • Zero-sum games
  • Winner and loser for the game
  • Tic-tac-toe
  • Basketball, baseball, etc.
  • Non-zero-sum games
  • No specific winner and loser
  • Allows for cooperation
  • options exist that benefit both players

7
Project 3 - Nim
  • Start with 3 piles of stones (size 3, 5 and 7
  • Take turns removing stones from any single pile
    (limit 3 stones)
  • Pick up last stone and lose

8
The Algorithm for Standard Nim
  • Not very intuitive
  • You do not have to use this in project 3
  • Standard Nim
  • 3 piles containing 3, 5, and 7 stones
  • Players may remove any number of stones from any
    pile -- not limited to 3

9
Perfecting a Nim Strategy
  • Convert Piles to binary
  • Examine columns of binary digits
  • Compute special sum -- odd columns and even
    columns
  • To win make special sum all zeroes
  • EXAMPLE
  • (2) OO gt 0 1 0
  • (3) OOO gt 0 1 1
  • (5) OOOOO gt 1 0 1
  • Special Sum gt 1 0 0

10
Perfect Nim Strategy
  • 1(3) OOO
  • 2(5) OOOOO
  • 3(7) OOOOOOO
  • 1(1) O
  • 2(4) OOOO
  • 3(5) OOOOO
  • 0 1 1 3
  • 1 0 1 5
  • 1 1 1 7
  • 0 0 1 so take away one
  • 0 0 1 1
  • 1 0 0 4
  • 1 0 1 5
  • 0 0 0 (lost!) so lose slowly

11
Perfect Nim Strategy (Contd)
  • 1(2) OO
  • 2(3) OOO
  • 3(6) OOOOOO
  • 1(1) O
  • 2(4) OOOO
  • 3(7) OOOOOOO
  • 0 1 0 2
  • 0 1 1 3
  • 1 1 0 6
  • 1 1 1 so take away 7?
  • 0 0 1 1
  • 1 0 0 4
  • 1 1 1 7
  • 0 1 0 so take away 2?

12
Perfect Nim Strategy (Contd)
  • 1(2) OO
  • 2(3) OOO
  • 3(6) OOOOOO
  • 1(1) O
  • 2(4) OOOO
  • 3(7) OOOOOOO
  • 0 1 0 xor 111 101 5
  • 0 1 1 xor 111 100 4
  • 1 1 0 xor 111 001 1 (ok)
  • 1 1 1
  • 0 0 1 xor 010 011 3
  • 1 0 0 xor 010 110 6
  • 1 1 1 xor 010 101 5 (ok)
  • 0 1 0

13
Static class members
  • Some methods/members of a class are associated
    with the class itself (not the individual
    objects)
  • A count of the number of objects that exist
  • A function to retrieve a unique ID for a new
    object

14
Using static class members
  • Declare a variable count, only one instance
    exists of this in program
  • Declare a routine to access this variables value
  • Initialize the variable (only done once)
  • Use this construct
  • class Pair int x,ystatic int count
  • publicPair() count static int
    GetCount(void) return count
  • int Paircount 0
  • int main( ) Pair x, ycout ltlt
    PairGetCount() ltlt endl

15
Class Exercises
  • The program static uses static class members
    (along with templates, inheritance, and virtual
    functions). Try to figure out the output of this
    program WITHOUT running the program.
  • Run the program to check your output.

16
The Big Three
  • The big three are three basic concepts that all
    programmers should understand
  • Otherwise you get strange bugs in your software
  • The big three are
  • Destructor
  • Copy Constructor
  • Assignment Operator
  • Talked about these briefly last semester, want to
    make sure everyone understands

17
Destructor
  • If an object does not have an explicit
    destructor, system builds one on its own
  • Can handle destruction of simple data types (int,
    double, float, char, etc.)
  • Does not know how to delete dynamically allocated
    data
  • If you dont delete an objects data as it goes
    away, you can get into trouble (memory leak)

18
Consider the following example
  • Each node object contains 100 doubles
  • List class
  • Adds at front
  • Removes from front
  • Main routine
  • int main( ) List afor (int b0 blt100 b)
    a.add((double)b.025) a.DumpFirst()
  • class Node double dataNode next
  • publicNode() datanew double100 next0
    friend class List
  • class List Node head
  • public List() head 0 void add(double d)
    Nodepnew Node()p-gtdata1d
    p-gtnextheadheadp void DumpFirst(void) if
    (head) headhead-gtnext

19
Class Exercises
  • Download the code badlist and run it (the program
    from the previous page). Change the number of
    times you execute the main program loop, try
  • 1,000
  • 10,000
  • 100,000
  • 1,000,000
  • 10,000,000
  • What happens?

20
Node destructor
  • As you delete a node from the list, make sure you
    physically de-allocate the space the node was
    using
  • System can handle the de-allocation of simple
    integers, doubles, pointers
  • System cannot handle de-allocation of more
    complex dynamically-allocated items
  • Node() delete data
  • In DumpFirst( ) routine, call delete p where p
    is the node that you are deleting

21
Class Exercise
  • Fix the code so that it can handle any number of
    insertions and deletions.

22
Copy Constructor Assignment
  • Problem with dynamic data structures
  • List a, b
  • a.add(10) a.add(20) a.add(30) a.add(40)
  • b a // is this what we want?
  • a.remove(20) // what does b look like?

a
40
30
20
10
b
23
Assignment Operator
  • Assignment operator for Pair class works OK
  • Pair contains two items (int, double, etc.)
  • Compiler simply copies these bit-by-bit
  • Cannot rely on compiler to do things correctly
    when you have a dynamically-allocated data
    structure (such as a list)
  • Compiler just points head of other list to same
    thing as current lists head
  • This results in aliasing (two pointers to same
    object), changes to one impact the other

24
Problems w/ assignment
  • Prints 20 twice (no destructor)
  • class List Node head
  • publicList() head 0
    void add(double d) double DumpFirst(void)
    double z head-gtdata5 if (head)
    headhead-gtnext return z
  • int main( ) List a, b double ca.add(10)
    a.add(20) b aca.DumpFirst()coutltltcltltendl
    cb.DumpFirst()coutltltcltltendl
  • Segmentation Fault (destructor)
  • class List Node head
  • publicList() head 0
    void add(double d) double DumpFirst(void)
    double z head-gtdata5 Node p head
    if (head) headhead-gtnextdelete p
    return z
  • int main( ) List a, b double ca.add(10)
    a.add(20) b aca.DumpFirst()coutltltcltltendl
    cb.DumpFirst()coutltltcltltendl

25
Class Exercises
  • The assignment operator is shown below. Fully
    comment all lines.
  • List operator(const List a) if (this a)
    return thisNode p a.head, t1, t2while
    (p) t1 new Node() for (int z0 zlt100
    z) t1-gtdataz p-gtdataz if (p a.head)
    this-gthead t1 else t2-gtnext
    t1 pp-gtnext t2 t1

26
Copy Constructor
  • List(const List a)
  • Makes a new list with its initial contents the
    same as what is contained in a
  • Need this to be a new copy of a
  • Same basic logic as the assignment operator

27
Class Exercises
  • Write a copy constructor for the class from the
    previous exercise.
Write a Comment
User Comments (0)
About PowerShow.com