From 5b7e2f574cc9b8f3805cd9e08383eff59173c19c Mon Sep 17 00:00:00 2001 From: nick black Date: Thu, 26 Mar 2020 20:10:25 -0400 Subject: [PATCH] tetris: fix up PieceStuck() #421 --- src/tetris/main.cpp | 1 + src/tetris/rotate.h | 1 + src/tetris/stuck.h | 57 +++++++++++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/tetris/main.cpp b/src/tetris/main.cpp index d46755925..b929948c4 100644 --- a/src/tetris/main.cpp +++ b/src/tetris/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include diff --git a/src/tetris/rotate.h b/src/tetris/rotate.h index 868bfbb11..67d86b40b 100644 --- a/src/tetris/rotate.h +++ b/src/tetris/rotate.h @@ -4,6 +4,7 @@ void RotateCcw() { if(!PrepForMove(&y, &x)){ return; } + // FIXME need to check game board for validity of rotation in both of these if(!curpiece_->rotate_ccw() || !nc_.render()){ throw TetrisNotcursesErr("rotate_ccw() or render()"); } diff --git a/src/tetris/stuck.h b/src/tetris/stuck.h index 6e3d4df33..fdde31623 100644 --- a/src/tetris/stuck.h +++ b/src/tetris/stuck.h @@ -3,29 +3,40 @@ bool PieceStuck() { // 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, x; - curpiece_->get_dim(&y, &x); - while(x--){ - ncpp::Cell piecec; - if(curpiece_->get_at(y - 1, x, &piecec) < 0){ - throw TetrisNotcursesErr("get_at()"); - } - if(!piecec.get().gcluster || piecec.get().gcluster == ' '){ - continue; - } - const char* egc = curpiece_->get_extended_gcluster(piecec); - if(strcmp(egc, "█")){ - continue; - } - int cmpy = y, 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.get().gcluster){ - if(c.get().gcluster != ' '){ - return true; + 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; + } + ///* + const char* egc = curpiece_->get_extended_gcluster(piecec); + if(strcmp(egc, "█")){ + 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; + } } } }