From cb85e120e0fc55e39f4bee0abb3ef91bcd4b6041 Mon Sep 17 00:00:00 2001 From: nick black Date: Thu, 26 Mar 2020 23:59:13 -0400 Subject: [PATCH] tetris: replace PieceStuck() with InvalidMove() #421 --- src/tetris/movedown.h | 11 +++++--- src/tetris/rotate.h | 31 ----------------------- src/tetris/stuck.h | 59 ++++++++++++++++++------------------------- 3 files changed, 32 insertions(+), 69 deletions(-) diff --git a/src/tetris/movedown.h b/src/tetris/movedown.h index 390cc71c1..6c510f099 100644 --- a/src/tetris/movedown.h +++ b/src/tetris/movedown.h @@ -3,7 +3,13 @@ bool MoveDown() { const std::lock_guard 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()"); diff --git a/src/tetris/rotate.h b/src/tetris/rotate.h index f090f3aa8..bba19b708 100644 --- a/src/tetris/rotate.h +++ b/src/tetris/rotate.h @@ -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 lock(mtx_); int y, x; diff --git a/src/tetris/stuck.h b/src/tetris/stuck.h index d32ba07e7..e11d92eac 100644 --- a/src/tetris/stuck.h +++ b/src/tetris/stuck.h @@ -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 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;