Today - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Today

Description:

Perfect Nim Strategy. Finish up some fundamental C concepts. Static ... Perfecting a Nim Strategy. Convert Piles to binary. Examine columns of binary digits ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: Today


1
Lecture 10
  • Today
  • Discuss Project2
  • Perfect Nim Strategy
  • Finish up some fundamental C concepts
  • Static class members
  • The Big Three
  • Exam
  • Thursday Oct 3, 8pm,
  • Location ten Hoor, Room 125
  • Assignments
  • Project Three is due Friday, Oct. 4, at 9pm

2



Association of Computing Machinery
The First Society in Computing
First Meeting of the Year Thursday, September
26, 2002 Time and Place EE 119
at 600 p.m.
Join us on Thursday for FREE FOOD, and
announcements of upcoming ACM events including a
programming contest. Guest Speaker Shane
Merritt, a UA CS grad, currently Director of
Network and Computing Support at Seebeck
For more info, visit us at acm.eng.ua.edu
3
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

4
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

5
Perfect Nim Strategy (Contd)
  • 1(2) OO
  • 2(5) 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?

6
Perfect Nim Strategy (Contd)
  • 1(2) OO
  • 2(5) OOOOO
  • 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

7
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

8
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

9
Class Exercises
  • The program in snippit1 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.

10
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

11
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)

12
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

13
Class Exercises
  • Download the code in snippit2 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?

14
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

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

16
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
17
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

18
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

19
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

20
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

21
Class Exercises
  • Write a copy constructor for the class from the
    exercises for snippit2.

22
Project Three QA
  • Make sure you understand
  • Use of Reset method
  • How to break a tie (Weight function)
  • How many games to play (250)
  • How to keep track of individual strategies
  • Strategy Ptrs100
  • Ptrs0 new Strategy1(BothY, OneY, OneN,
    BothN)
  • Ptrs1 new Strategy2(BothY, OneY, OneN,
    BothN)

23



Association of Computing Machinery
The First Society in Computing
First Meeting of the Year Thursday, September
26, 2002 Time and Place EE 119
at 600 p.m.
Join us on Thursday for FREE FOOD, and
announcements of upcoming ACM events including a
programming contest. Guest Speaker Shane
Merritt, a UA CS grad, currently Director of
Network and Computing Support at Seebeck
For more info, visit us at acm.eng.ua.edu
Write a Comment
User Comments (0)
About PowerShow.com