An Introduction to Programming Through C++ Lecture 24 Revision PDF

Document Details

TantalizingAllegory3793

Uploaded by TantalizingAllegory3793

Manoj Prabhakaran

Tags

C++ programming programming design tetromino game computer science

Summary

This document provides lecture notes on programming concepts, particularly relevant to designing a large-scale program. The content encompasses a high-level design for a Tetris game, low-level types (like block and point), and data structures for representing a game board in C++. The material is geared towards understanding object-oriented design for larger projects.

Full Transcript

AN INTRODUCTION TO PROGRAMMING THROUGH C++ with Manoj Prabhakaran Lecture 24 Revision An Example: De...

AN INTRODUCTION TO PROGRAMMING THROUGH C++ with Manoj Prabhakaran Lecture 24 Revision An Example: Designing a Large-ish Program Based on material developed by Prof. Abhiram G. Ranade Designing a Large Program ¥ Today a game of Tetris (see demo) ¥ Plan Ð A high-level design Ð Glimpses of various classes Our High-Level Design get display updates input driver display move actions: status move, and next display block updates block board high-scores for move empty cells needed Geometry and make a move Maintains information about the position of a block filled and blank cells Some Low-Level Types ¥ For the different objects to communicate amongst themselves, need appropriate types enum block_t {nil, I, O, L, J, T, S, Z }; // the 7 types of blocks, and empty enum move_t {none, drp, dwn, rotCW, rotACW, lft, rgt}; // 6 types of moves + none // the 4 orientations class direction { char d=0; // direction is 0,1,2,3 public: direction& operator++() { (++d) %= 4; return *this;} direction& operator--() { (d+=3) %= 4; return *this;} // prevents going -ve bool operator==(const int& i) { return d==i;} }; Some Low-Level Types class point; // forward declaration // a tetromino has 4 squares. stored tersely as a 2D array struct tetromino { char pts; // the position of the 4 squares operator vector () const; // to cast into a vector of 4 points }; class point { // stores x, y coordinates of a cell in the board's grid char x, y; public: point(char a=0, char b=0) : x(a), y(b) {} point(move_t mv); // calculate shift of origin due to a move point& operator+=(const point& p) { x += p.x; y += p.y; return *this; } tetromino operator+(const tetromino& t) const; // point+tetromino => tetromino bool operator==(const point& pt) const { return x==pt.x && y==pt.y; } int X() const { return x; } int Y() const { return y; } }; Some Low-Level Types // a class used to store a list of cells to be updated // this will be used by the board class internally class updateList { vector Lpt; // for each point in this list vector Lblk; // set it to be from this block type public: void clear() { Lpt.clear(); Lblk.clear(); } size_t size() { return Lpt.size(); } int X(int i) { return Lpt[i].X(); } int Y(int i) { return Lpt[i].Y(); } block_t blk(int i) { return Lblk[i]; } // add updates corresponding to moving a block from start to finish void move(const tetromino& start, const tetromino& finish, block_t blk); // after extracting points, the list gets reset void extractPoints(vector& up); }; Our High-Level Design get display updates input driver display move actions: status move, and next display block updates block board high-scores for move empty cells needed Geometry and make a move Maintains information about the position of a block filled and blank cells Block class block { // static functions for computing geometry and room for manoeuvring static tetromino pattern(block_t,direction); static vector relRoom(block_t, direction d, move_t); block_t blk; // the type of the block point origin; // left-bottom corner of block's grid on the board (can be -ve) direction dir; // orientation of the block within its grid tetromino occupied; // cells occupied, relative to origin public: block() = default; // needed to create an uninitialised block block(block_t b, point o) : blk(b), origin(o), occupied(origin + pattern(blk,dir)) {} block& operator= (const block& B); block_t blocktype() const { return blk; } tetromino where() const { return occupied; } vector room(move_t mv); // list of squares needed to make a move Shape of a Block tetromino block::pattern(block_t b, direction d) { if(b==L) { if (d==0) return tetromino {{ {0,1}, {1,1}, {2,1}, {2,2} }}; if (d==1) return tetromino {{ {1,0}, {1,1}, {1,2}, {2,0} }}; if (d==2) return tetromino {{ {0,0}, {0,1}, {1,1}, {2,1} }}; //if (d==3) return tetromino {{ {0,2}, {1,0}, {1,1}, {1,2} }}; }... if(b==O) { return tetromino {{ {0,0}, {1,0}, {0,1}, {1,1} }}; } throw std::invalid_argument("Invalid block"); // for nil block type }; Room for a Block // for each block type, for each move, room needed to make the move mv // relative to its origin // This may include the room currently occupied as well vector block::relFullRoom(block_t b, direction d, move_t mv); See code // remove the points occupied by the block from the room vector block::relRoom(block_t b, direction d, move_t mv) { auto room = relFullRoom(b,d,mv); auto self = pattern(b,d); for(int i=0; i

Use Quizgecko on...
Browser
Browser