From 4a02d7ed80614e14240eb0972f04dfccad2f8ebb Mon Sep 17 00:00:00 2001 From: nick black Date: Fri, 27 Mar 2020 00:04:12 -0400 Subject: [PATCH] tetris: rewrite moveleft()/moveright() with invalidmove() #421 --- src/tetris/movedown.h | 6 +++--- src/tetris/moveleft.h | 42 +++++++++++------------------------------- src/tetris/moveright.h | 42 ++++++++++++------------------------------ 3 files changed, 26 insertions(+), 64 deletions(-) diff --git a/src/tetris/movedown.h b/src/tetris/movedown.h index 6c510f099..f0a5982c9 100644 --- a/src/tetris/movedown.h +++ b/src/tetris/movedown.h @@ -17,9 +17,9 @@ bool MoveDown() { curpiece_ = NewPiece(); }else{ ++y; - } - if(!nc_.render()){ - throw TetrisNotcursesErr("render()"); + if(!nc_.render()){ + throw TetrisNotcursesErr("render()"); + } } } return false; diff --git a/src/tetris/moveleft.h b/src/tetris/moveleft.h index 6b046cc76..dccacd505 100644 --- a/src/tetris/moveleft.h +++ b/src/tetris/moveleft.h @@ -1,39 +1,19 @@ void MoveLeft() { const std::lock_guard lock(mtx_); int y, x; - if(!PrepForMove(&y, &x)){ - return; - } - // For each line of the current piece, find the leftmost populated column. - // Check the game area to the immediate left. If something's there, we - // can't make this move. - ncpp::Cell c; - for(int ly = 0 ; ly < curpiece_->get_dim_y() ; ++ly){ - int lx = 0; - while(lx < curpiece_->get_dim_x()){ - if(curpiece_->get_at(ly, lx, &c)){ - if(c.get().gcluster && c.get().gcluster != ' '){ - break; - } - } - ++lx; + if(PrepForMove(&y, &x)){ + if(!curpiece_->move(y, x - 2)){ + throw TetrisNotcursesErr("move()"); } - if(lx < curpiece_->get_dim_x()){ // otherwise, nothing on this row - ncpp::Cell b; - for(int xdelt = 1 ; xdelt < 3 ; ++xdelt){ - int cmpy = ly, cmpx = lx - xdelt; - curpiece_->translate(*board_, &cmpy, &cmpx); - if(board_->get_at(cmpy, cmpx, &b)){ - // FIXME deal with e.g. lower half sliding under upper half - if(b.get().gcluster && b.get().gcluster != ' '){ - return; // move is blocked - } - } + if(InvalidMove()){ + if(!curpiece_->move(y, x)){ + throw TetrisNotcursesErr("move()"); + } + }else{ + x -= 2; + if(!nc_.render()){ + throw TetrisNotcursesErr("render()"); } } } - x -= 2; - if(!curpiece_->move(y, x) || !nc_.render()){ // FIXME needs y? - throw TetrisNotcursesErr("move() or render()"); - } } diff --git a/src/tetris/moveright.h b/src/tetris/moveright.h index d16f0656d..30b66a641 100644 --- a/src/tetris/moveright.h +++ b/src/tetris/moveright.h @@ -1,38 +1,20 @@ void MoveRight() { const std::lock_guard lock(mtx_); int y, x; - if(!PrepForMove(&y, &x)){ - return; - } - // For each line of the current piece, find the rightmost populated column. - // Check the game area to the immediate right. If something's there, we - // can't make this move. - ncpp::Cell c; - for(int ly = 0 ; ly < curpiece_->get_dim_y() ; ++ly){ - int lx = curpiece_->get_dim_x() - 1; - while(lx >= 0){ - if(curpiece_->get_at(ly, lx, &c)){ - if(c.get().gcluster && c.get().gcluster != ' '){ - break; - } - } - --lx; + if(PrepForMove(&y, &x)){ + if(!curpiece_->move(y, x + 2)){ + throw TetrisNotcursesErr("move()"); } - if(lx >= 0){ // otherwise, nothing on this row - ncpp::Cell b; - for(int xdelt = 1 ; xdelt < 3 ; ++xdelt){ - int cmpy = ly, cmpx = lx + xdelt; - curpiece_->translate(*board_, &cmpy, &cmpx); - if(board_->get_at(cmpy, cmpx, &b)){ - if(b.get().gcluster && b.get().gcluster != ' '){ - return; // move is blocked - } - } + if(InvalidMove()){ + if(!curpiece_->move(y, x)){ + throw TetrisNotcursesErr("move()"); + } + }else{ + x += 2; + if(!nc_.render()){ + throw TetrisNotcursesErr("render()"); } } - } - x += 2; - if(!curpiece_->move(y, x) || !nc_.render()){ // FIXME needs y? - throw TetrisNotcursesErr("move() or render()"); + return; } }