1 / 24

Game Programming (Data Structures and Algorithms)

Game Programming (Data Structures and Algorithms). 2006. Spring. Types, Structures, and Classes. The first programming languages Only supported operations on a closed set of data types Assembly language level Didn’t support floating-point values Primitive types

Download Presentation

Game Programming (Data Structures and Algorithms)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Game Programming(Data Structures and Algorithms) 2006. Spring

  2. Types, Structures, and Classes • The first programming languages • Only supported operations on a closed set of data types • Assembly language level • Didn’t support floating-point values • Primitive types • Too limited and creating large S/W app. was impossible with it • Ex) • Structures • User-defined type (similar to the layers of an onion) • Ex) float x, y, z • typedef struct { • float x, y, z; • } point 3D; • point3D p; • p.x = 1;

  3. Types, Structures, and Classes • Need operations to add, subtract, … • Ex) • Problem • Data structure grew  accompanying code would also grew • Naming problem • Solution • Allow each type to include the source code for its own access operations • point3D point3Dadd (point3D, point3D); • point3D point3Dsub (point3D, point3D); • float point3Ddotproduct (point3D, point3D); • point3D point3Dcrossproduct (point3D, point3D);

  4. Types, Structures, and Classes • Classes (become “black boxes”) • Self-sufficient code and data module  classes • Inheritance, operation overload • Object-Oriented Programming (OOP) • Build more structured programs that are easier to maintain • Improve team-based coding • Others • Design patterns • Standard Template Library (STL) • class point3D { • private: • float x, y, z; • public: • point3D (float, float, float); • ~point3D(); • point3D operator+ (point3D); • point3D operator- (point3D); • };

  5. Data Structures • Static Arrays • Store a list of elements that will not change during the program’s life cycle • Definition • Constructor • Destructor • class arrayoftype { • type *data; • int size; • arrayoftype (int); • ~arrayoftype(); • }; • arrayoftype::arrayoftype(int psize) { • size = psize; • data = new type(size); • } • arrayoftype::~arrayoftype() { • if (data!=NULL) delete[] data; • }

  6. Data Structures • Static Arrays • Use a simple but effective algorithms • Advantage • Search speed • Primary search key  O(1) access time • Ordered elements  binary search (O(log2N)) • typedef struct { • int passportid; • char *name; • } person; • class people { • person *data; • int size; • person *seek(int); • }; • person *people::seek(int passid) { • int top=size-1; • int bottom=0; • while (top-bottom>1) { • int mid = (top+bottom)/2; • if (passid>=data[mid.passportid]) bottom=mid; • else top=mid; • } • if (data[bottom].passportid==passid) return (&data[bottom]); • else return NULL; • }

  7. Data Structures • Linked lists • The sequence can grow or shrink dynamically • Accommodating a variable number of elements during the structure’s life cycle • Careful Memory allocation • Deletions  need the previous elements • Access routine can be slower than with arrays • Random access, search • typedef struct { • // here the data for the element • elem *next; • } elem; • class linkedlist { • elem *first; • elem *current; • };

  8. Data Structures • Doubly-Linked Lists • Allow bidirectional scanning • Offer easier insertion and deletion • typedef struct { • // here the data for the element • elem *next; • elem *prev; • } elem; • class linkedlist { • elem *first; • elem *current; • };

  9. Data Structures • Queues (FIFO: First-In First-Out) • Insertion  the end of the list • extraction (deletion)  the begin of the list • Ex) supermarket waiting line • Circular Queues • Store the N most recent elements • New elements overwrite the oldest ones • Ex) rendering 3D elements • typedef struct { • // here the data for the element • elem *next; • } elem; • class queue { • elem *first; • elem *last; • void insertback(elem *); • elem *getfirst(); • };

  10. Data Structures • Stacks (LIFO: Last-In First-Out) • Priority to newer elements • Push (insertion)  the end of the structure • Pop (deletion)  the end of the structure • Ex) a pile of paper • typedef struct { • // here the data for the element • elem *next; • } elem; • class statck { • elem *first; • void push(elem *); • elem *pop(); • };

  11. Data Structures • Deques • Encapsulate both the queue and the stack • remove a waste of code • Allows both push and pop to be performed at both endpoints • typedef struct { • // here the data for the element • elem *next; • elem *prev; • } elem; • class deque { • elem *first; • elem *last; • void push_front(elem *); • elem *pop_front(); • void push_back(elem *); • elem *pop_back(); • };

  12. Data Structures • Tables • Sequential structures that associate data elements with an identifying key • Ex) database • Single key O(1) • Nonrepetitive: only appear once • Exhaustive: all key values are assigned • Static case • Sorting by key and storing data in ascending order  O(log2N) • Dynamic case • Insertion and deletion operation can be performed in O(n)

  13. Data Structures • Hash Tables • Handle both static and dynamic data as well as nonexhaustic keys • Offer nearly constant access time • Downside • More memory overhead • Slightly more complex coding • class hash_table { • dlinkedlist data[100]; • void create(); • elem *seek(int key); • void insert(elem *el); • void delete(int key); • dlinkedlist *hash_function(int key); • }; • dlinkedlist *hash_function(int key) { • int pos=(key%100); • return &data[pos]; • }

  14. Data Structures • How do hash functions provide us with a short cut to data? • Select the hash key • Ex) the last two digits of the passport the first two digits of the passport • Use mainstream application • Ex) government application people by their passport # : 10,000,000 each people’s data: 100 bytes • Static array 10,000,000 x 100 = 1,000,000,000 • Hash table (implemented doubly linked list) hash with passport modulo: 10,000 the average list length: approximately 1,000 Compare scanning a list of 1,000 unit with lists of 10,000,000 pointer: 4 bytes each list  1,000 x ( 8 + 100 ) whole structure  10,000 x (108,000 + 4) = 1,080,040,000 need an extra 8,004% storing space

  15. Data Structures • Multi-Key Tables • Need to access the table using both keys • Use two hash tables connected to doubly-linked lists that can be traversed horizontally or vertically • Downside • Coding can be a complex task • Insertion and deletion get a bit more complex • Additional memory O/H

  16. Data Structures • Trees • A data structure consisting of a series of nodes • Each node: information and pointers • Pointers to nodes that are “descendants” • Only have one direct father • Tree Types and Uses • Binary Trees • Each node has exactly two descendants • typedef struct { • tree *left; • tree *right; • // here goes the real data from the node • (…) • } tree;

  17. Data Structures • Binary Search Tree (BST) • The key value of all elements in the left subnode must be smaller than the right one • Access time  O(log2N) • Degrade when the tree is unbalanced • AVL-tree (Adelson-Velskii and Landis) • A BST with additional restriction • The depth of the left subtree must differ at most one unit from right one • Binary Space Partition (BSP) tree • Recursively split the level geometry in half by using splitting plane  closest to the player (Chap. 13) • Ex) Quake engine

  18. Data Structures • N-ary Trees • Quadtrees and Octrees (chap. 14) • The natural extension of BSP trees • Quadtree 2D, Octress 3D data sets • Powerful tools for visibility computation

  19. Data Structures • N-ary Trees • Tries • An N-ary Tree where each node can have a variable number of descendants • Offer very fast access time • Ex) a dictionary that stores the words

  20. 1 2 2 1 3 3 Data Structures • Tree Traversal Operations • Ordered traversal • Pre-order • Visit the node itself  the left subtrees  the right subtrees • In-order • Visit the left subtrees  the node itself  the right subtrees • Post-order • Visit the left subtrees  the right subtrees  the node itself 3 5 21 1 2

  21. Data Structures • Priority Queues • An extension of the queue model that incorporates a sense of priority • Implementation • Heap • Priority  node > left > right • Static array with some clever access routines • The ascendant is at position n/2 (truncating if needed) • The left descendant lies at the position 2*n • The right descendant lies at the position (2*n)+1

  22. Data Structures • Graph • Composed of a series of nodes and a series of connections between them • Representing relational data • Location on a map with roads and traveling distances • State machines with conditions to change from one state to another • People with relationships between them (friendship) • Board game configurations with possible moves • Powerful analysis algorithm • What is the shortest route from city A to B? • What is the chance of a state machine reaching a given state? • Given two people in a group, do they have any friends in common? • In a chess game, what is the best move to achieve victory?

  23. Data Structures • Graph • Graphs can be directed or nondirected • Cyclic or acyclic graph • Ex) AI programming (chap. 8) • shortest route, pathfinding

  24. STL • The Standard Template Library • Introduced in the 1990s as a collection of classes that made OOP easier • Include lists, queue, hash table, … • STL’s code • Error-free • Very efficient • Provide flexibility • Coherence • Most data structures are accessed in a similar way • Built into many standard complier • MS Visual C++, Borand’s C, …

More Related