/* Game Class -- implementation * This is a container which holds the grid and the current piece * and conrols game play. It relay commands to and from those members. */ #include #include "game.h" #include "lshape.h" // constructor -- generates a new piece Game::Game(GP142Display *display, Grid *grid) { this->display = display; this->grid = grid; // this should be changed to make a random piece // once you've created the other pieces. piece = new LShape(display, grid); } // clear the display and redraw the grid and piece void Game::Draw() { display->clear(); grid->Draw(); if (piece != NULL) piece->Draw(); } // move the current piece in direction d (usually down) void Game::MovePiece(Direction d) { if (piece != NULL) piece->Move(d); } // rotate the piece clockwise void Game::RotatePiece() { if (piece != NULL) piece->Rotate(); } // if the piece can no longer move down, // we make it part of the grid and remove it. // generate a new piece if needed void Game::UpdatePiece() { Square temp; if (piece == NULL) { piece = NewPiece(); return; } // set grid positions corresponding to frozen piece's location // and remove piece if (!(piece->canMove(DOWN))) { for (int i=0; igetSquare(i); grid->Set(temp.getX(),temp.getY(),piece->getColor()); } delete piece; piece = NULL; } } // check for and remove completely filled in rows void Game::CheckRows() { grid->CheckRows(); } // returns true if a piece occupies the same // space as some part of the grid // (usually happens when a new piece is made, signifies game is over) bool Game::IsGameOver() const { Square temp; if (piece == NULL) return false; for (int i=0; igetSquare(i); if (grid->IsSet(temp.getX(),temp.getY())) return true; // game over } return false; } // returns an int, 0<= int <= 6 int Game::RandomNumber() const { return rand() % 7; } // Generate a random new piece and return a pointer to it. Piece * Game::NewPiece() const { Piece *new_piece = NULL; // you will need to modify this once you've created // more pieces switch(RandomNumber()) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: // change is needed here break; default: break; } return new_piece; }