tetris: rewrite moveleft()/moveright() with invalidmove() #421

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

@ -17,9 +17,9 @@ bool MoveDown() {
curpiece_ = NewPiece();
}else{
++y;
}
if(!nc_.render()){
throw TetrisNotcursesErr("render()");
if(!nc_.render()){
throw TetrisNotcursesErr("render()");
}
}
}
return false;

@ -1,39 +1,19 @@
void MoveLeft() {
const std::lock_guard<std::mutex> 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()");
}
}

@ -1,38 +1,20 @@
void MoveRight() {
const std::lock_guard<std::mutex> 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;
}
}

Loading…
Cancel
Save