tetris: factor out LockPiece()

pull/426/head
nick black 4 years ago
parent d120e74580
commit fff9118e17
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -0,0 +1,14 @@
// returns true iff the specified row of the board is clear (full with no gaps)
bool LineClear(int y){
int dimx = board_->get_dim_x();
for(int x = 1 ; x < dimx - 1 ; ++x){
ncpp::Cell c;
if(board_->get_at(y, x, &c) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(!c.get().gcluster || c.get().gcluster == ' '){
return false;
}
}
return true;
}

@ -0,0 +1,35 @@
void LockPiece(){
curpiece_->mergedown(*board_);
if(!board_->cursor_move(0, 1)){
throw TetrisNotcursesErr("cursor_move()");
}
int bdimy, bdimx;
board_->get_dim(&bdimy, &bdimx);
int cleared; // how many contiguous lines were cleared
do{
uint64_t tl, tr, bl, br;
tl = tr = bl = br = 0;
channels_set_bg_alpha(&tl, CELL_ALPHA_TRANSPARENT);
channels_set_bg_alpha(&tr, CELL_ALPHA_TRANSPARENT);
channels_set_bg_alpha(&bl, CELL_ALPHA_TRANSPARENT);
channels_set_bg_alpha(&br, CELL_ALPHA_TRANSPARENT);
channels_set_fg(&tl, 0xff0000);
channels_set_fg(&tr, 0x00ff00);
channels_set_fg(&bl, 0x0000ff);
channels_set_fg(&br, 0x00ffff);
if(!board_->stain(bdimy - 2, bdimx - 2, tl, tr, bl, br)){
throw TetrisNotcursesErr("stain()");
}
cleared = 0;
for(int y = bdimy - 2 ; y > 0 ; --y){
if(LineClear(y)){
++cleared;
}else if(cleared){
break;
}
}
if(cleared){
// FIXME purge them, augment score
}
}while(cleared);
}

@ -46,6 +46,8 @@ public:
#include "gravity.h"
#include "ticker.h"
#include "clear.h"
#include "lock.h"
#include "movedown.h"
#include "moveleft.h"
#include "moveright.h"

@ -7,25 +7,7 @@ bool MoveDown() {
if(y <= board_top_y_ - 1){
return true;
}
curpiece_->mergedown(*board_);
if(!board_->cursor_move(0, 1)){
throw TetrisNotcursesErr("cursor_move()");
}
int bdimy, bdimx;
board_->get_dim(&bdimy, &bdimx);
uint64_t tl, tr, bl, br;
tl = tr = bl = br = 0;
channels_set_bg_alpha(&tl, CELL_ALPHA_TRANSPARENT);
channels_set_bg_alpha(&tr, CELL_ALPHA_TRANSPARENT);
channels_set_bg_alpha(&bl, CELL_ALPHA_TRANSPARENT);
channels_set_bg_alpha(&br, CELL_ALPHA_TRANSPARENT);
channels_set_fg(&tl, 0xff0000);
channels_set_fg(&tr, 0x00ff00);
channels_set_fg(&bl, 0x0000ff);
channels_set_fg(&br, 0x00ffff);
if(!board_->stain(bdimy - 2, bdimx - 2, tl, tr, bl, br)){
throw TetrisNotcursesErr("stain()");
}
LockPiece();
curpiece_ = NewPiece();
}else{
++y;

Loading…
Cancel
Save