tetris: replace PieceStuck() with InvalidMove() #421

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

@ -3,7 +3,13 @@ bool MoveDown() {
const std::lock_guard<std::mutex> lock(mtx_);
int y, x;
if(PrepForMove(&y, &x)){
if(PieceStuck()){
if(!curpiece_->move(y + 1, x)){
throw TetrisNotcursesErr("move()");
}
if(InvalidMove()){
if(!curpiece_->move(y, x)){
throw TetrisNotcursesErr("move()");
}
if(y <= board_top_y_ - 1){
return true;
}
@ -11,9 +17,6 @@ bool MoveDown() {
curpiece_ = NewPiece();
}else{
++y;
if(!curpiece_->move(y, x)){
throw TetrisNotcursesErr("move()");
}
}
if(!nc_.render()){
throw TetrisNotcursesErr("render()");

@ -1,34 +1,3 @@
bool InvalidMove() { // a bit wasteful, but piece are tiny
int dy, dx;
curpiece_->get_dim(&dy, &dx);
while(dy--){
int x = dx;
while(x--){
ncpp::Cell c, b;
if(curpiece_->get_at(dy, x, &c) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(c.is_simple()){
continue;
}
curpiece_->release(c);
int transy = dy, transx = x; // need game area coordinates via translation
curpiece_->translate(*board_, &transy, &transx);
if(transy < 0 || transy >= board_->get_dim_y() || transx < 0 || transx >= board_->get_dim_x()){
return true;
}
if(board_->get_at(transy, transx, &b) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(!b.is_simple()){
return true;
}
board_->release(b);
}
}
return false;
}
void RotateCcw() {
const std::lock_guard<std::mutex> lock(mtx_);
int y, x;

@ -1,38 +1,29 @@
bool PieceStuck() {
if(curpiece_){
// check for impact. iterate over bottom row of piece's plane, checking for
// presence of glyph. if there, check row below. if row below is occupied,
// we're stuck.
int y, dimx, x;
curpiece_->get_dim(&y, &dimx);
std::vector<bool> columns(dimx, false); // bitmap for column verification
int checked = 0;
while(y--){
x = dimx;
while(x--){
if(!columns[x]){
ncpp::Cell piecec;
if(curpiece_->get_at(y, x, &piecec) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(piecec.is_simple()){
continue;
}
int cmpy = y + 1, cmpx = x; // need game area coordinates via translation
curpiece_->translate(*board_, &cmpy, &cmpx);
ncpp::Cell c;
if(board_->get_at(cmpy, cmpx, &c) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(!c.is_simple()){
return true;
}
columns[x] = true;
if(++checked == dimx){
return false;
}
}
bool InvalidMove() { // a bit wasteful, but piece are tiny
int dy, dx;
curpiece_->get_dim(&dy, &dx);
while(dy--){
int x = dx;
while(x--){
ncpp::Cell c, b;
if(curpiece_->get_at(dy, x, &c) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(c.is_simple()){
continue;
}
curpiece_->release(c);
int transy = dy, transx = x; // need game area coordinates via translation
curpiece_->translate(*board_, &transy, &transx);
if(transy < 0 || transy >= board_->get_dim_y() || transx < 0 || transx >= board_->get_dim_x()){
return true;
}
if(board_->get_at(transy, transx, &b) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(!b.is_simple()){
return true;
}
board_->release(b);
}
}
return false;

Loading…
Cancel
Save