Skip to content

Commit

Permalink
next piece window works correctly now
Browse files Browse the repository at this point in the history
  • Loading branch information
acgreek committed May 28, 2016
1 parent 59b695e commit 4f98f4d
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 54 deletions.
28 changes: 24 additions & 4 deletions block.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
#ifndef BLOCK_HPP
#define BLOCK_HPP
class Block : public Movable {

#include "vectorable.hpp"

class Block : public Movable, public Vectorable {
public:
Block() :x_(0), y_(0){dy_ = 1;dx_ = 0;}
Block(int x, int y) :x_(x), y_(y){dy_ = 1;dx_ =0;}
int getX() const {
return x_;
}
int getY() const {
return y_;
}
int getZ() const {
return 0;
}
int setX(int x){
return x_ = x;
}
int setY(int y){
return y_ = y;
}
int setZ(__attribute__((unused))int z){
return 0;
}
void set(int x, int y) {
unmark();
x_ = x;
y_ = y;
mark();
}
void draw(WINDOW * win) {
void draw(WINDOW * win, const char *symbol="o", int drawoffsetx =0,int drawoffsety= 0 ) {
if (x_ >=0)
mvwprintw(win, y_, x_, "o");
mvwprintw(win, y_ + drawoffsety, x_+drawoffsetx, symbol);
}
void unmark() {
board[x_][y_] = CLEAR_BLOCK;
Expand Down Expand Up @@ -43,7 +64,6 @@ class Block : public Movable {
return (!done_moving() && x_ > 0 && board[x_-1][y_] == CLEAR_BLOCK);
}
void stopMoving() {

dx_= dy_ = 0;
}
void uncheckedMoveLeft() {
Expand Down
38 changes: 38 additions & 0 deletions gameboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef GAMEBOARD_HPP
#define GAMEBOARD_HPP
#include <vector>
#include <algorithm>

#define MAXBOARDH 1000
#define MAXBOARDW 1000

#define CLEAR_BLOCK ' '
static char board[MAXBOARDW][MAXBOARDH];
static int max_y = 0, max_x = 0;

class GameBoard {
public:
GameBoard(int max_x, int max_y) {
board_.resize(max_y);
std::for_each(board_.begin(), board_.end(), [&max_x] (std::vector<char> & row) {row.resize(max_x,CLEAR_BLOCK);});
}
bool isPopulated(int x, int y) const{
return (CLEAR_BLOCK != board_[y][x] );
}
void populated(int x, int y, char what) {
board_[y][x]= what;
}
void empty(int x, int y) {
board_[y][x]= CLEAR_BLOCK;
}
size_t maxY() const {
return board_.size();
}
size_t maxX() const {
return board_[0].size();
}
private:
std::vector<std::vector <char>> board_;
};

#endif
2 changes: 1 addition & 1 deletion logpiece.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class LogPiece :public Piece {
public:
LogPiece() { }
virtual void construct(int offset=0) {
virtual void construct(blist & blocks,int offset=0) {
dir_=HORIZONAL;
blocks.push_back(Block(offset + 0,0));
sitr_ = blocks.end();
Expand Down
2 changes: 1 addition & 1 deletion piece.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Piece {
public:
Piece():dir_(HORIZONAL) {}
virtual ~Piece() {}
virtual void construct(int offset=0) = 0 ;
virtual void construct(blist & blocks,int offset=0) = 0 ;

virtual void move() {
blist::iterator eitr = litr_;
Expand Down
33 changes: 15 additions & 18 deletions piece_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,38 @@
class PieceSelector {
public:
PieceSelector(): currentPiece_(SQUARE) {}
void drawNextPiece(WINDOW * win) {
mvwprintw(win, 1,1, "next piece") ;
}
Piece * getNextPiece() {
Piece * getNextPiece(blist & blocks) {
piece_type nextPiece = ( piece_type) (rand() % (LEL + 1));
switch(currentPiece_) {
case ZED:
currentPiece_ = nextPiece;
z_.construct();
return &z_;
z_.construct(blocks);
return new ZPiece(z_);
case LZED:
currentPiece_ = nextPiece;
lz_.construct();
return &lz_;
lz_.construct(blocks);
return new LZPiece(lz_);
case LOG:
currentPiece_ = nextPiece;
log_.construct();
return &log_;
log_.construct(blocks);
return new LogPiece(log_);
case PYRAMID:
currentPiece_ = nextPiece;
pyramid_.construct();
return &pyramid_;
pyramid_.construct(blocks);
return new Pyramid(pyramid_);
case EL:
currentPiece_ = nextPiece;
el_.construct();
return &el_;
el_.construct(blocks);
return new El(el_);
case LEL:
currentPiece_ = nextPiece;
lel_.construct();
return &lel_;
lel_.construct(blocks);
return new LEl(lel_);
default:
case SQUARE:
currentPiece_ = nextPiece;
square_.construct();
return &square_;
square_.construct(blocks);
return new SquarePiece(square_);
}
}
private:
Expand Down
12 changes: 6 additions & 6 deletions pieces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class SquarePiece :public Piece {
public:
SquarePiece() {
}
virtual void construct(int offset = 0) {
virtual void construct(blist & blocks,int offset = 0) {
dir_=HORIZONAL;
blocks.push_back(Block(offset + 0,0));
sitr_ = blocks.end();
Expand All @@ -23,7 +23,7 @@ class LZPiece :public Piece {
LZPiece() {
offset_ = 0;
}
virtual void construct(int offset=0) {
virtual void construct(blist & blocks,int offset=0) {
dir_=HORIZONAL;
blocks.push_back(Block(offset + 0,1));
sitr_ = blocks.end();
Expand All @@ -42,7 +42,7 @@ class LEl:public Piece {
LEl() {
offset_ = 0;
}
virtual void construct(int offset = 0) {
virtual void construct(blist & blocks,int offset = 0) {
dir_=HORIZONAL;
blocks.push_back(Block(offset + 0,0));
sitr_ = blocks.end();
Expand All @@ -61,7 +61,7 @@ class El:public Piece {
El() {
offset_ = 0;
}
virtual void construct(int offset = 0) {
virtual void construct(blist & blocks,int offset = 0) {
dir_=HORIZONAL;
blocks.push_back(Block(offset + 0,1));
sitr_ = blocks.end();
Expand All @@ -78,7 +78,7 @@ class El:public Piece {
class Pyramid :public Piece {
public:
Pyramid() { }
virtual void construct(int offset = 0 ) {
virtual void construct(blist & blocks,int offset = 0 ) {
dir_=HORIZONAL;
blocks.push_back(Block(offset + 0,1));
sitr_ = blocks.end();
Expand All @@ -93,7 +93,7 @@ class Pyramid :public Piece {
class ZPiece :public Piece {
public:
ZPiece() { }
virtual void construct(int offset=0) {
virtual void construct(blist & blocks,int offset=0) {
dir_=HORIZONAL;
blocks.push_back(Block(offset + 0,0));
sitr_ = blocks.end();
Expand Down
58 changes: 34 additions & 24 deletions tetris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,30 @@
#include <string.h>
#include <math.h>
#include <iostream>
#include <memory>
#include "keyboard.h"
int delay = 3000;

#define MAXBOARDH 1000
#define MAXBOARDW 1000

#define CLEAR_BLOCK ' '
#include "gameboard.h"

static char board[MAXBOARDW][MAXBOARDH];
int delay = 3000;

static int max_y = 0, max_x = 0;
#include "movable.hpp"
#include "block.hpp"

typedef std::list<Block> blist;
static blist blocks;

#include "piece.hpp"
#include "logpiece.hpp"
#include "pieces.hpp"
#include "piece_selector.hpp"

static void shiftRowsDown(int row) {
static void shiftRowsDown(blist & blocks, int row) {
std::for_each(blocks.begin(), blocks.end(), [&row ](Block & b) {if (b.y_ == row ){ b.unmark();};});
blocks.erase(std::remove_if(blocks.begin(), blocks.end(), [&row](Block & b)->bool { return b.y_ == row;}), blocks.end() );
std::for_each(blocks.begin(), blocks.end(), [&row ](Block & b) {if (b.y_ < row ){ b.unmark(); b.y_++; b.mark();};});
std::for_each(blocks.begin(), blocks.end(), [&row ](Block & b) {b.mark();});
}
#define MIN(A,B) (A >B ? B :A)
static int checkCompleteRows() {
static int checkCompleteRows(blist & blocks) {
int rowsRemoved=0;
int i=max_y;
while (i > 0) {
Expand All @@ -45,45 +39,59 @@ static int checkCompleteRows() {
}
if (max_x +1 ==j) {
rowsRemoved++;
shiftRowsDown(i);
shiftRowsDown(blocks, i);
} else {
i--;
}
}
return rowsRemoved;
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
WINDOW *create_newwin(int height, int width, int starty, int startx) {
auto *local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0);/* 0, 0 gives default characters
* for the vertical and horizontal
* * lines*/
wrefresh(local_win);/* Show that box */
return local_win;
}

class CursesSetup {
public:
CursesSetup() {
initscr();
noecho();

}
~CursesSetup (){
endwin();
}

};

int main() {
blist blocks;
blist nextblocks;
int score = 0;
/* initialize random seed: */
srand (time(NULL));
initscr();
noecho();
curs_set(FALSE);
memset(board, CLEAR_BLOCK, sizeof(board[0][0]) * MAXBOARDW* MAXBOARDH);
CursesSetup cursesSetup;

getmaxyx(stdscr, max_y, max_x);
max_x = MIN(max_x,9);
WINDOW * score_win= create_newwin(3, 11, max_y-4, 0);
WINDOW * next_piece_win= create_newwin(5, 5, 2, 11);
WINDOW * next_piece_win= create_newwin(5, 6, 2, 11);
max_y = max_y-4;
GameBoard tetrisGameBoard(max_x, max_y);

bool done= false;
int moveDownCount = 100;
int currentCount = 1;
PieceSelector pieceSelector;
Piece * curPiecep =pieceSelector.getNextPiece();
std::shared_ptr<Piece> curPiecep(pieceSelector.getNextPiece(blocks));
std::shared_ptr<Piece> nextPiecep(pieceSelector.getNextPiece(nextblocks));
std::for_each(nextblocks.begin(), nextblocks.end(), [&](Block & b) {b.draw(next_piece_win, "o", 1, 1);});
while(!done) {
bool needRedraw =false;
clear();
Expand All @@ -107,15 +115,19 @@ int main() {
usleep(delay);
if (needRedraw) {
if (curPiecep->done_moving()) {
switch (checkCompleteRows()) {
switch (checkCompleteRows(blocks)) {
case 4: score +=100; break;
case 3: score +=50; break;
case 2: score +=25; break;
case 1: score +=10; break;
default:
case 0: score +=1; break;
}
curPiecep =pieceSelector.getNextPiece();
std::for_each(nextblocks.begin(), nextblocks.end(), [&](Block & b) {b.draw(next_piece_win," ", 1,1 );});
blocks.splice(blocks.begin(), nextblocks);
curPiecep = std::move(nextPiecep);
nextPiecep.reset( pieceSelector.getNextPiece(nextblocks));
std::for_each(nextblocks.begin(), nextblocks.end(), [&](Block & b) {b.draw(next_piece_win, "o", 1, 1);});
curPiecep->markAll();
}
std::for_each(blocks.begin(), blocks.end(), [](Block & b) {b.draw(stdscr);});
Expand All @@ -124,12 +136,10 @@ int main() {
mvwprintw(score_win, 1, 1, "score: %d",score);
wnoutrefresh(score_win);/* Show that box */
box(next_piece_win, 0 , 0);
pieceSelector.drawNextPiece(next_piece_win);
wnoutrefresh(next_piece_win);/* Show that box */
doupdate();
}
}
endwin();
std::cout << "final score:" << score << std::endl;
};

0 comments on commit 4f98f4d

Please sign in to comment.