Last active
March 11, 2016 12:07
-
-
Save vainveins/602c6b720b2b3bf77060 to your computer and use it in GitHub Desktop.
yoyo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========================================================= | |
//File: game.cpp | |
//Author: Davis Giang | |
//Description: Implementation of game class | |
//Date: 2/17/2016 | |
//========================================================= | |
#include <iostream> | |
#include <iomanip> | |
#include <sstream> | |
#include <string> | |
#include "Well.h" | |
#include "Tetrimino.h" | |
#include "game.h" | |
//=============================================================== | |
// Function: Game | |
// Description: Game constructor for Game class | |
//================================================================ | |
Game::Game() { | |
if(!myFont.loadFromFile("RobotoSlab-Regular.ttf")) { | |
std::cout << "Error, font did not load."; | |
} | |
window.create( | |
sf::VideoMode(LAYOUT_WINDOW_WIDTH, LAYOUT_WINDOW_HEIGHT), | |
"Tetris 2016", | |
sf::Style::Titlebar | sf::Style::Close | |
); | |
window.setFramerateLimit(30); | |
tetriminoInPlay=0; | |
tetriminoOnDeck=0; | |
gameWell=0; | |
score=0; | |
for(int i=0; i<HIGHSCORE_ARRAY_SIZE; i++) { | |
topScores[i]=0; | |
} | |
}; | |
//================================================================ | |
// Function: convertToSfmlColor | |
// Description: changes the color passed from char to RGB values | |
// Arguments: color (I) - character of color | |
// Return value: | |
// returns the RGB values of color | |
//================================================================ | |
sf::Color Game::convertToSfmlColor(char color) { | |
switch(color) { | |
case 't': | |
return sf::Color(0, 255, 255); | |
break; | |
case 'b': | |
return sf::Color::Blue; | |
break; | |
case 'o': //black | |
return sf::Color(255, 165, 0); | |
break; | |
case 'y': | |
return sf::Color::Yellow; | |
break; | |
case 'g': | |
return sf::Color::Green; | |
break; | |
case 'p': //purple/magenta | |
return sf::Color::Magenta; | |
break; | |
case 'r': | |
return sf::Color::Red; | |
break; | |
default: | |
return sf::Color(0, 0, 0); | |
break; | |
} | |
}; | |
//================================================================ | |
// Function: drawWell | |
// Description: draws game Well | |
// Arguments: | |
// well (I) - well to draw | |
// topOfWell(I) - points to top of Well to draw | |
// leftOfWell(I) - points to left of Well to draw | |
// BLOCK_SIZE_PIXELS (I) - size of blocks | |
//================================================================ | |
void Game::drawWell(Well well, int topOfWell, int leftOfWell, int BLOCK_SIZE_PIXELS) { | |
char boardCheck[BOARD_ROW][BOARD_COL]; | |
well.getBoard(boardCheck); | |
//draw rectangle | |
sf::RectangleShape wellDraw[BOARD_ROW][BOARD_COL]; | |
//draw well block by block | |
for(int row=0; row<BOARD_ROW; row++) | |
for(int col=0; col<BOARD_COL; col++) { | |
wellDraw[row][col].setSize(sf::Vector2f(BLOCK_SIZE_PIXELS, BLOCK_SIZE_PIXELS)); | |
wellDraw[row][col].setOutlineThickness(1); | |
wellDraw[row][col].setOutlineColor(sf::Color::Black); | |
int y=topOfWell+BLOCK_SIZE_PIXELS*row; | |
int x=leftOfWell+BLOCK_SIZE_PIXELS*col; | |
wellDraw[row][col].setPosition(x, y); | |
if(boardCheck[row][col]==' ') | |
wellDraw[row][col].setFillColor(sf::Color::White); | |
else | |
wellDraw[row][col].setFillColor(Game::convertToSfmlColor(boardCheck[row][col])); | |
window.draw(wellDraw[row][col]); | |
} | |
} | |
//================================================================ | |
// Function: drawTetrimino | |
// Description: draws tetrimino piece on well | |
// Arguments: | |
// object (I) - piece to draw | |
// topOfWell(I) - points to top of Well to draw | |
// leftOfWell(I) - points to left of Well to draw | |
// BLOCK_SIZE_PIXELS (I) - size of blocks | |
//================================================================ | |
void Game::drawTetrimino(Tetrimino* object, int topOfWell, int leftOfWell, int BLOCK_SIZE_PIXELS) { | |
int gridCheck[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE]; | |
object->getGrid(gridCheck); | |
Location tempLocation; | |
tempLocation = object->getLocation(); | |
//draw rectangle | |
sf::RectangleShape myTetrimino; | |
myTetrimino.setSize(sf::Vector2f(BLOCK_SIZE_PIXELS, BLOCK_SIZE_PIXELS)); | |
myTetrimino.setOutlineThickness(1); | |
myTetrimino.setOutlineColor(sf::Color::Black); | |
myTetrimino.setFillColor(convertToSfmlColor(object->getColor())); | |
//draw well block by block | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) { | |
if(gridCheck[row][col] == 1) { | |
int y=topOfWell+(BLOCK_SIZE_PIXELS*(tempLocation.row+row)); | |
int x=leftOfWell+(BLOCK_SIZE_PIXELS*(tempLocation.col+col)); | |
myTetrimino.setPosition(x, y); | |
window.draw(myTetrimino); | |
} | |
} | |
} | |
//================================================================ | |
// Function: drawScore | |
// Description: draws the current score of game | |
// Arguments: | |
// score (I) - score to draw | |
// locationTop (I)- X position to draw | |
// locationLeft (I) - Y position to draw | |
//================================================================ | |
void Game::drawScore(int score, int locationTop, int locationLeft) { | |
std::string scoreString; | |
sf::Text currentScore; | |
sf::Text scoreHeader; | |
std::ostringstream convert; | |
convert << score; //change to score | |
scoreString = convert.str(); | |
scoreHeader.setFont(myFont); | |
scoreHeader.setColor(sf::Color::Black); | |
scoreHeader.setCharacterSize(50); | |
scoreHeader.setString("Score:"); | |
scoreHeader.setPosition(locationLeft, locationTop); | |
currentScore.setFont(myFont); | |
currentScore.setColor(sf::Color::Black); | |
currentScore.setCharacterSize(50); | |
currentScore.setString(scoreString); | |
currentScore.setPosition(locationLeft+40, locationTop+50); | |
window.draw(scoreHeader); | |
window.draw(currentScore); | |
} | |
//================================================================ | |
// Function: drawHoldPieceText | |
// Description: Displays text for next piece: | |
//================================================================ | |
void Game::drawHoldPieceText(int locationLeft, int locationTop) { | |
sf::Text holdPiece; | |
holdPiece.setFont(myFont); | |
holdPiece.setColor(sf::Color::Black); | |
holdPiece.setCharacterSize(25); | |
holdPiece.setString("Next piece:"); | |
holdPiece.setPosition(locationLeft, locationTop); | |
window.draw(holdPiece); | |
} | |
//================================================================ | |
// Function: playGame | |
// Description: starts the game | |
//================================================================ | |
void Game::playGame() { | |
GameState windowScreen; | |
windowScreen = TETRIS_SPLASH; | |
while (window.isOpen() && windowScreen!= TETRIS_GAME_OVER) { | |
if (windowScreen == TETRIS_SPLASH) { | |
processSplashScreen(); | |
windowScreen = TETRIS_PLAY; | |
} | |
else if (windowScreen == TETRIS_PLAY) { | |
processGameScreen(); | |
windowScreen = TETRIS_GAME_OVER; | |
} | |
if (windowScreen == TETRIS_GAME_OVER) { | |
if (processGameOverScreen()==true) | |
windowScreen = TETRIS_SPLASH; | |
else | |
window.close(); | |
} | |
} | |
}; | |
//================================================================ | |
// Function: processSplashScreen | |
// Description: displays the splash screen | |
//================================================================ | |
void Game::processSplashScreen() { | |
int left = 60; | |
int top = 50; | |
sf::Text splash; | |
splash.setFont(myFont); | |
splash.setColor(sf::Color::Black); | |
splash.setString("Tetris 2016"); | |
splash.setCharacterSize(55); | |
splash.setPosition(left, top-50); | |
sf::Text play; | |
play.setFont(myFont); | |
play.setColor(sf::Color::Black); | |
play.setString("Press enter to start playing!"); | |
play.setCharacterSize(22); | |
play.setPosition(left, top+400); | |
sf::Text instructions1; | |
instructions1.setFont(myFont); | |
instructions1.setColor(sf::Color::Black); | |
instructions1.setString("Use the arrow keys to move,"); | |
instructions1.setCharacterSize(12); | |
instructions1.setPosition(left+10, top+20); | |
sf::Text instructions2; | |
instructions2.setFont(myFont); | |
instructions2.setColor(sf::Color::Black); | |
instructions2.setString("The up arrow rotates right, Z rotates left"); | |
instructions2.setCharacterSize(12); | |
instructions2.setPosition(left+10, top+40); | |
sf::Text instructions3; | |
instructions3.setFont(myFont); | |
instructions3.setColor(sf::Color::Black); | |
instructions3.setString("Each line cleared will reward 1 point"); | |
instructions3.setCharacterSize(12); | |
instructions3.setPosition(left+10, top+60); | |
sf::Text highScores; | |
highScores.setFont(myFont); | |
highScores.setColor(sf::Color::Black); | |
highScores.setString("High Scores:"); | |
highScores.setCharacterSize(12); | |
highScores.setPosition(left+20, top+120); | |
while (window.isOpen()) { | |
sf::Event event; | |
while (window.pollEvent(event)) { | |
if (event.type == sf::Event::Closed) //if close button pressed, window will close | |
window.close(); | |
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Return) { | |
return; | |
} | |
} | |
// draw new frame | |
window.clear(sf::Color::White); | |
window.draw(splash); | |
window.draw(play); | |
window.draw(instructions1); | |
window.draw(instructions2); | |
window.draw(instructions3); | |
window.draw(highScores); | |
drawHighScores(score, left+60, top+130); | |
window.display(); | |
} | |
}; | |
//================================================================ | |
// Function: processGameScreen | |
// Description: plays the game | |
//================================================================ | |
void Game::processGameScreen() { | |
tetriminoInPlay = new Tetrimino; | |
tetriminoOnDeck = new Tetrimino; | |
gameWell = new Well; | |
int dropCounter=0; | |
int score=0; | |
while (window.isOpen()) | |
{ | |
sf::Event event; | |
while (window.pollEvent(event)) { | |
if (event.type == sf::Event::Closed) | |
window.close(); | |
//left arrow | |
if(event.type==sf::Event::KeyPressed | |
&& event.key.code == sf::Keyboard::Left) { | |
tetriminoInPlay->moveLeft(); | |
if(gameWell->tetriminoFit(*tetriminoInPlay)==false) | |
tetriminoInPlay->moveRight(); | |
} | |
//right arrow | |
if(event.type==sf::Event::KeyPressed | |
&& event.key.code == sf::Keyboard::Right) { | |
tetriminoInPlay->moveRight(); | |
if(gameWell->tetriminoFit(*tetriminoInPlay)==false) | |
tetriminoInPlay->moveLeft(); | |
} | |
//down arrow | |
if(event.type==sf::Event::KeyPressed | |
&& event.key.code == sf::Keyboard::Down) { | |
tetriminoInPlay->moveDown(); | |
if(gameWell->tetriminoFit(*tetriminoInPlay)==false) | |
tetriminoInPlay->moveUp(); | |
} | |
//up arrow | |
if(event.type==sf::Event::KeyPressed | |
&& event.key.code == sf::Keyboard::Up) { | |
tetriminoInPlay->rotateRight(); | |
if(gameWell->tetriminoFit(*tetriminoInPlay)==false) | |
tetriminoInPlay->rotateLeft(); | |
} | |
//Z rotate left | |
if(event.type==sf::Event::KeyPressed | |
&& event.key.code == sf::Keyboard::Z) { | |
tetriminoInPlay->rotateLeft(); | |
if(gameWell->tetriminoFit(*tetriminoInPlay)==false) | |
tetriminoInPlay->rotateRight(); | |
} | |
} | |
dropCounter++; | |
if(dropCounter>5) { | |
dropCounter=0; | |
tetriminoInPlay->moveDown(); | |
if(gameWell->tetriminoFit(*tetriminoInPlay)==false) { | |
tetriminoInPlay->moveUp(); | |
gameWell->addTetriminoToWell(*tetriminoInPlay); | |
score = score + gameWell->clearFullRows(); | |
if(gameWell->topReached()) { | |
highScoreCheck(score); | |
return; | |
} | |
else { | |
tetriminoInPlay=tetriminoOnDeck; | |
tetriminoInPlay->setLocation(0, 2); | |
tetriminoOnDeck = new Tetrimino; | |
} | |
} | |
} | |
// draw new frame | |
window.clear(sf::Color::White); | |
drawWell(*gameWell, LAYOUT_BOARD_TOP, LAYOUT_BOARD_LEFT, BLOCK_SIZE_PIXELS); | |
drawTetrimino(tetriminoInPlay, LAYOUT_BOARD_TOP, LAYOUT_BOARD_LEFT, BLOCK_SIZE_PIXELS); | |
drawHoldPieceText(25, 197); | |
tetriminoOnDeck->setLocation (10, -7); | |
drawTetrimino(tetriminoOnDeck, LAYOUT_BOARD_TOP,LAYOUT_BOARD_LEFT, BLOCK_SIZE_PIXELS); | |
drawScore(score, 20, 20); | |
window.display(); | |
} | |
saveHighScores(); | |
delete tetriminoInPlay; | |
delete tetriminoOnDeck; | |
tetriminoInPlay=NULL; | |
tetriminoOnDeck=NULL; | |
}; | |
//================================================================ | |
// Function: processGameOverScreen | |
// Description: displays the game over screen | |
//================================================================ | |
bool Game::processGameOverScreen() { | |
int top=50; | |
int left=50; | |
sf::Text gameOver; | |
gameOver.setFont(myFont); | |
gameOver.setColor(sf::Color::Black); | |
gameOver.setCharacterSize(50); | |
gameOver.setString("GAME OVER"); | |
gameOver.setPosition(left, top); | |
sf::Text playAgain; | |
playAgain.setFont(myFont); | |
playAgain.setColor(sf::Color::Black); | |
playAgain.setCharacterSize(20); | |
playAgain.setString("If you want to play again, press enter"); | |
playAgain.setPosition(left-30, top+340); | |
while(window.isOpen()) { | |
sf::Event event; | |
while(window.pollEvent(event)) { | |
if(event.type == sf::Event::Closed) | |
window.close(); | |
if(event.type==sf::Event::KeyPressed | |
&& event.key.code == sf::Keyboard::Return) | |
return true; | |
} | |
window.clear(sf::Color::White); | |
window.draw(gameOver); | |
window.draw(playAgain); | |
window.display(); | |
} | |
return false; | |
}; | |
//================================================================ | |
// Function: drawHighScores | |
// Description: Draws the top 10 high scores of player | |
// Arguments: | |
// locationTop (I)- X position to draw | |
// locationLeft (I) - Y position to draw | |
//================================================================ | |
void Game::drawHighScores(int score, int locationLeft, int locationTop) { | |
sf::Text scoreText; | |
scoreText.setFont(myFont); | |
scoreText.setCharacterSize(15); | |
scoreText.setColor(sf::Color::Black); | |
std::stringstream string; | |
string << score; | |
std::string scoreString; | |
scoreString = string.str(); | |
for(int i=0; i<10; i++) { | |
scoreText.setString(scoreString); | |
scoreText.setPosition(locationLeft, locationTop+i*20); | |
window.draw(scoreText); | |
} | |
}; | |
//================================================================ | |
// Function: highScoreCheck | |
// Description: checks if current score is a high score | |
// Arguments: | |
// score (I) - score of player | |
//================================================================= | |
void Game::highScoreCheck (int score) { | |
for (int i=HIGHSCORE_ARRAY_SIZE-1; i>=0; i--) { | |
if (score > topScores[i] ) { | |
for(int j=HIGHSCORE_ARRAY_SIZE-1; j>i; j--) { | |
topScores[j]=topScores[i-1]; | |
topScores[i]=score; | |
} | |
} | |
} | |
}; | |
void Game::saveHighScores() { | |
highScoreFile.open("HighScores.txt"); | |
for(int i=0; i<HIGHSCORE_ARRAY_SIZE; i++) { | |
highScoreFile << topScores[i] << std::endl; | |
} | |
highScoreFile.close(); | |
return; | |
} | |
void main() { | |
Game game; | |
game.playGame(); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========================================================= | |
//File: game.h | |
//Author: Davis Giang | |
//Description: Definition of game class | |
//Date: 2/17/2016 | |
//========================================================= | |
#include <SFML/Graphics.hpp> | |
#include <fstream> | |
#ifndef TETRIS_GAME | |
#define TETRIS_GAME | |
#include "Tetrimino.h" | |
#include "Well.h" | |
const int LAYOUT_WINDOW_WIDTH = 400; | |
const int LAYOUT_WINDOW_HEIGHT = 500; | |
const int LAYOUT_BOARD_TOP = 10; | |
const int LAYOUT_BOARD_LEFT = 200; | |
const int BLOCK_SIZE_PIXELS = 20; | |
const int HIGHSCORE_ARRAY_SIZE=10; | |
enum GameState {TETRIS_SPLASH, TETRIS_PLAY, TETRIS_GAME_OVER}; | |
class Game { | |
public: | |
Game(); | |
void playGame(); | |
void drawScore(int score, int locationTop, int locationLeft); | |
void drawHoldPieceText(int locationLeft, int locationTop); | |
void drawHighScores(int score, int locationLeft, int locationTop); | |
void highScoreCheck (int score); | |
void saveHighScores(); | |
private: | |
sf::RenderWindow window; | |
sf::Color convertToSfmlColor(char color); | |
void drawWell(Well well, int topOfWell, int leftOfWell, int BLOCK_SIZE_PIXELS); | |
void drawTetrimino(Tetrimino *object, int topOfWell, int leftOfWell, int BLOCK_SIZE_PIXELS); | |
void processGameScreen(); | |
void processSplashScreen(); | |
bool processGameOverScreen(); | |
Tetrimino * tetriminoInPlay; | |
Tetrimino * tetriminoOnDeck; | |
Well *gameWell; | |
int score; | |
sf::Font myFont; | |
int topScores[HIGHSCORE_ARRAY_SIZE]; | |
std::fstream highScoreFile; | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========================================================= | |
//File: tetrimino.cpp | |
//Author: Davis Giang | |
//Description: Implementation of tetris piece class | |
//Date: 2/3/2016 | |
//========================================================= | |
#include <cstdlib> | |
#include <ctime> | |
#include <iostream> | |
#include <iomanip> | |
#include "tetrimino.h" | |
#include "Well.h" | |
//========================================================= | |
//Function:Tetrimino | |
//Description: Initializes Location, grid, and color of piece | |
//Arguments: | |
// type (I) - value of tetris piece | |
//========================================================= | |
Tetrimino::Tetrimino(int type) { | |
location.row=0; | |
location.col=0; | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) | |
grid[row][col]=0; | |
if(type < 0 || type > 6) | |
type = getRandomNumber(0,6); | |
switch(type) { | |
case 0: //i shape | |
grid[1][0] = 1; | |
grid[1][1] = 1; | |
grid[1][2] = 1; | |
grid[1][3] = 1; | |
color='c'; | |
break; | |
case 1: //j Shape | |
grid[1][1] = 1; | |
grid[2][1] = 1; | |
grid[2][2] = 1; | |
grid[2][3] = 1; | |
color='b'; | |
break; | |
case 2: // l shape | |
grid[1][3] = 1; | |
grid[2][1] = 1; | |
grid[2][2] = 1; | |
grid[2][3] = 1; | |
color='o'; | |
break; | |
case 3: //o shape | |
grid[1][1] = 1; | |
grid[1][2] = 1; | |
grid[2][1] = 1; | |
grid[2][2] = 1; | |
color='y'; | |
break; | |
case 4: //s shape | |
grid[1][3] = 1; | |
grid[1][2] = 1; | |
grid[2][1] = 1; | |
grid[2][2] = 1; | |
color='g'; | |
break; | |
case 5: //t shape | |
grid[1][1] = 1; | |
grid[2][0] = 1; | |
grid[2][1] = 1; | |
grid[2][2] = 1; | |
color='p'; | |
break; | |
case 6: //z shape | |
grid[1][0] = 1; | |
grid[1][1] = 1; | |
grid[2][1] = 1; | |
grid[2][2] = 1; | |
color='r'; | |
break; | |
default: | |
break; | |
} | |
} | |
//mutators | |
//================================================================ | |
//Function: getColor | |
//description: returns the color of the tetrimino object | |
//================================================================ | |
char Tetrimino::getColor() const { | |
return color; | |
}; | |
//================================================================ | |
//Function: getLocation | |
//Description: returns the location of the Tetrimino | |
//================================================================ | |
Location Tetrimino::getLocation() const { | |
return location; | |
}; | |
//================================================================ | |
//Function: getGrid | |
//Description: returns a copy of the grid | |
//Argument: | |
//gridOut (O) - grid | |
//================================================================= | |
void Tetrimino::getGrid(int gridOut[][TETRIMINO_GRID_SIZE]) const { | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) | |
gridOut[row][col]=grid[row][col]; | |
}; | |
//================================================================ | |
//Function: setLocation | |
//Description: modify location.row and location.col | |
//Arguments: | |
// int row (I) - location.row to modify to | |
// int col (I) - location.col to modify to | |
//================================================================= | |
bool Tetrimino::setLocation(Location newLocation) { | |
location=newLocation; | |
return true; | |
}; | |
//================================================================ | |
//Function: setLocation | |
//Description: modify location.row and location.col | |
//Arguments: | |
// int row (I) - location.row to modify to | |
// int col (I) - location.col to modify to | |
//================================================================= | |
bool Tetrimino::setLocation(int row, int col) { | |
Location newLocation; | |
newLocation.row=row; | |
newLocation.col=col; | |
location=newLocation; | |
return true; | |
}; | |
//================================================================ | |
//Function: rotateLeft | |
//Description: rotates the array to the left (counter clockwise) | |
//================================================================= | |
void Tetrimino::rotateLeft() { | |
int tempArray[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] = {}; | |
//Switches the columns and rows in the array, | |
//then reverses the columns elements | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) { | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) { | |
tempArray[TETRIMINO_GRID_SIZE-col-1][row] = grid[row][col]; | |
} | |
} | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) { | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) { | |
grid[row][col] = tempArray[row][col]; | |
} | |
} | |
}; | |
//================================================================ | |
//Function: rotateRight | |
//Description: rotates the array to the right (clockwise) | |
//=============================================================== | |
void Tetrimino::rotateRight() { | |
int tempArray[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE] = {}; | |
//Switches the columns and rows in the array, | |
//then reverses the rows elements | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) { | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) { | |
tempArray[col][TETRIMINO_GRID_SIZE-row-1] = grid[row][col]; | |
} | |
} | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) { | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) | |
grid[row][col] = tempArray[row][col]; | |
} | |
}; | |
//===================================== | |
//Function: moveLeft | |
//Description: Move location left 1 | |
//==================================== | |
void Tetrimino::moveLeft() { | |
Location newLocation; | |
newLocation = location; | |
newLocation.col = newLocation.col-1; | |
location = newLocation; | |
}; | |
//===================================== | |
//Function: moveRight | |
//Description: Move location right 1 | |
//==================================== | |
void Tetrimino::moveRight() { | |
Location newLocation; | |
newLocation = location; | |
newLocation.col = newLocation.col+1; | |
location = newLocation; | |
}; | |
//===================================== | |
//Function: moveDown | |
//Description: Move location down 1 | |
//==================================== | |
void Tetrimino::moveDown() { | |
Location newLocation; | |
newLocation = location; | |
newLocation.row = newLocation.row+1; | |
location = newLocation; | |
}; | |
//===================================== | |
//Function: moveUp | |
//Description: Move location up 1 | |
//==================================== | |
void Tetrimino::moveUp() { | |
Location newLocation; | |
newLocation = location; | |
newLocation.row = newLocation.row-1; | |
location = newLocation; | |
}; | |
//=============================================================== | |
//Function:dataDump | |
//Description: Prints out the grid value, color, and location of | |
// Tetris piece | |
//============================================================== | |
void Tetrimino::dataDump() { | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) { | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) { | |
if(grid[row][col] == 1) | |
std::cout << std::setw(1) << "1"; | |
else | |
std::cout << std::setw(1) << "0"; | |
} | |
std::cout << std::endl; | |
} | |
switch(color) { | |
case 't': | |
std::cout << "cyan" << std::endl; | |
break; | |
case 'b': | |
std::cout << "blue" << std::endl; | |
break; | |
case 'o': | |
std::cout << "orange" << std::endl; | |
break; | |
case 'y': | |
std::cout << "yellow" << std::endl; | |
break; | |
case 'g': | |
std::cout << "green" << std::endl; | |
break; | |
case 'p': | |
std::cout << "purple" << std::endl; | |
break; | |
case 'r': | |
std::cout << "red" << std::endl; | |
break; | |
} | |
std::cout << "Location = " << "(" << location.row << "," << location.col << ")" << std::endl; | |
}; | |
//================================================================ | |
// Function: getRandomNumber | |
// Description: returns a random number between given low and high | |
// values, inclusive. | |
// Note: include cstdlib (for rand) and ctime (for time). | |
// Arguments: | |
// low (I) - The lowest number to be generated | |
// high (I) - The highest number to be generated (must be > low) | |
// Return value: | |
// A random number between low and high (inclusive) | |
//================================================================ | |
int Tetrimino::getRandomNumber(int low, int high) { | |
static bool firstTime=true; | |
int randNum; | |
//if first time called, seed random number generator | |
if (firstTime) { | |
srand( static_cast<unsigned int>(time(NULL)) ); | |
firstTime=false; | |
} | |
//generate random number between given low and high values (inclusive) | |
randNum = rand() % (high-low+1) + low; | |
return randNum; | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========================================================= | |
//File: Tetrimino.h | |
//Author: Davis Giang | |
//Description:Definition of tetris piece class | |
//Date: 2/3/2016 | |
//========================================================= | |
#ifndef TETRIS_TETRIMINO | |
#define TETRIS_TETRIMINO | |
const int TETRIMINO_GRID_SIZE = 4; | |
struct Location { | |
int row; | |
int col; | |
}; | |
class Tetrimino { | |
public: | |
Tetrimino(int type = 7); | |
char getColor() const; | |
Location getLocation() const; | |
void getGrid(int gridOut[][TETRIMINO_GRID_SIZE]) const; | |
bool setLocation(Location newLocation); | |
bool setLocation(int row, int col); | |
void rotateLeft(); | |
void rotateRight(); | |
void moveLeft(); | |
void moveRight(); | |
void moveDown(); | |
void moveUp(); | |
void dataDump(); | |
private: | |
int grid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE]; | |
char color; | |
Location location; | |
int getRandomNumber(int low, int high); | |
}; | |
#endif | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========================================================= | |
//File: Well.cpp | |
//Author: Davis Giang | |
//Description: Implementation of tetris well class | |
//Date: 2/10/2016 | |
//========================================================= | |
#include <iostream> | |
#include <iomanip> | |
#include "Well.h" | |
#include "Tetrimino.h" | |
//================================================================ | |
// Function: Well | |
// Description: Well constructor for Well class | |
//================================================================ | |
Well::Well() { | |
height = BOARD_ROW; | |
width = BOARD_COL; | |
for(int row=0; row<height; row++) | |
for(int col=0; col<width; col++) | |
board[row][col]=' '; | |
} | |
//================================================================ | |
// Function: tetriminoFit | |
// Description: Checks if the tetrimino that is passsed fits in the | |
// board | |
// Arguments: | |
// Tetrimino& piece (I) - piece to check | |
// Return value: | |
// true if it can fit | |
// false if it cannot fit | |
//================================================================ | |
bool Well::tetriminoFit(Tetrimino& piece) { | |
Location locationCheck; | |
locationCheck = piece.getLocation(); | |
int localGrid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE]; | |
piece.getGrid(localGrid); | |
//check left, right, under | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) { | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) | |
if(localGrid[row][col]==1) { | |
if(locationCheck.col+col<0) | |
return false; | |
if(locationCheck.col+col>BOARD_COL-1) | |
return false; | |
if(locationCheck.row+row>BOARD_ROW-1) | |
return false; | |
if(board[locationCheck.row+row][locationCheck.col+col]!=' ') | |
return false; | |
} | |
} | |
return true; | |
}; | |
//================================================================ | |
// Function: topReached | |
// Description: Checks if the top row is reached | |
// Return value: | |
// true if the top has been reached | |
// false if the top has not been reached | |
//================================================================ | |
bool Well::topReached() { | |
for(int i=0; i<BOARD_COL; i++) { | |
if(board[0][i]!=' ') | |
return true; | |
} | |
return false; | |
}; | |
//================================================================ | |
// Function: getBoard | |
// Description: returns a copy of the board | |
// Arguments: | |
// boardOut (O) - board | |
//================================================================ | |
void Well::getBoard(char boardOut[BOARD_ROW][BOARD_COL]) const { | |
for(int row=0; row<BOARD_ROW; row++) | |
for(int col=0; col<BOARD_COL; col++) | |
boardOut[row][col]=board[row][col]; | |
}; | |
//================================================================ | |
// Function: addTetriminoToTell | |
// Description: adds the tetrimino piece to the well and changes its color | |
// Arguments: | |
// piece (I) - tetrimino piece being added to the well | |
//================================================================ | |
void Well::addTetriminoToWell(Tetrimino piece) { | |
int localGrid[TETRIMINO_GRID_SIZE][TETRIMINO_GRID_SIZE]; | |
piece.getGrid(localGrid); | |
Location locationToAdd; | |
locationToAdd = piece.getLocation(); | |
char localColor; | |
localColor=piece.getColor(); | |
if(tetriminoFit(piece)==true) { | |
for(int row=0; row<TETRIMINO_GRID_SIZE; row++) | |
for(int col=0; col<TETRIMINO_GRID_SIZE; col++) | |
if(localGrid[row][col]==1) | |
board[locationToAdd.row+row][locationToAdd.col+col]=localColor; | |
} | |
}; | |
//================================================================ | |
// Function: clearFullRows | |
// Description: Clears the full rows | |
// Return value: | |
// the total number of rows cleared | |
//================================================================ | |
int Well::clearFullRows() { | |
int totalRows=0; | |
for(int row=0; row<BOARD_ROW; row++) { | |
if(rowIsFull(row)) | |
moveRowsDown(row); | |
totalRows++; | |
} | |
return totalRows; | |
}; | |
//================================================================ | |
// Function: boardDump | |
// Description: prints the board out | |
//================================================================ | |
void Well::boardDump() { | |
for(int row=0; row<BOARD_ROW; row++) { | |
for(int col=0; col<BOARD_COL; col++) { | |
if(board[row][col]!=' ') | |
std::cout << std::setw(1) << "1"; | |
else | |
std::cout << std::setw(1) << "0"; | |
} | |
std::cout << std::endl; | |
} | |
}; | |
//================================================================ | |
// Function: rowIsFull | |
// Description: Checks if the row is full | |
// Arguments: | |
// row (I) - row to be checked | |
// Return value: | |
// true if the row is full | |
// false if the row is not full | |
//================================================================ | |
bool Well::rowIsFull(int row) { | |
int counter = BOARD_COL; | |
for(int col=0; col<BOARD_COL; col++) { | |
if(board[row][col]!=' ') | |
counter--; | |
} | |
if(counter==0) | |
return true; | |
else | |
return false; | |
}; | |
//================================================================ | |
// Function: moveRowsDown | |
// Description: moves rows down | |
// Arguments: | |
// firstRowToMove (I) - first row to start moving down | |
//================================================================ | |
void Well::moveRowsDown(int firstRowToMove) { | |
for(int row=firstRowToMove; row>=0; row--) | |
for(int col=0; col<BOARD_COL; col++) | |
board[row][col]=board[row-1][col]; | |
for(int col=0; col<BOARD_COL; col++) | |
board[0][col]=' '; | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========================================================= | |
//File: Well.h | |
//Author: Davis Giang | |
//Description: Definition of tetris well class | |
//Date: 2/10/2016 | |
//========================================================= | |
#ifndef TETRIS_WELL | |
#define TETRIS_WELL | |
#include "Tetrimino.h" | |
const int BOARD_ROW = 24; | |
const int BOARD_COL = 8; | |
class Well { | |
public: | |
Well(); | |
bool tetriminoFit(Tetrimino& piece); | |
bool topReached(); | |
void getBoard(char boardOut[BOARD_ROW][BOARD_COL]) const; | |
void addTetriminoToWell(Tetrimino piece); | |
int clearFullRows(); | |
void boardDump(); | |
bool rowIsFull(int row); | |
void moveRowsDown(int firstRowToMove); | |
private: | |
char board[BOARD_ROW][BOARD_COL]; | |
int height; | |
int width; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment