tetris: rewrite moveleft()/moveright() with invalidmove() #421
parent
cb85e120e0
commit
4a02d7ed80
@ -1,39 +1,19 @@
|
|||||||
void MoveLeft() {
|
void MoveLeft() {
|
||||||
const std::lock_guard<std::mutex> lock(mtx_);
|
const std::lock_guard<std::mutex> lock(mtx_);
|
||||||
int y, x;
|
int y, x;
|
||||||
if(!PrepForMove(&y, &x)){
|
if(PrepForMove(&y, &x)){
|
||||||
return;
|
if(!curpiece_->move(y, x - 2)){
|
||||||
}
|
throw TetrisNotcursesErr("move()");
|
||||||
// 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(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() {
|
void MoveRight() {
|
||||||
const std::lock_guard<std::mutex> lock(mtx_);
|
const std::lock_guard<std::mutex> lock(mtx_);
|
||||||
int y, x;
|
int y, x;
|
||||||
if(!PrepForMove(&y, &x)){
|
if(PrepForMove(&y, &x)){
|
||||||
return;
|
if(!curpiece_->move(y, x + 2)){
|
||||||
}
|
throw TetrisNotcursesErr("move()");
|
||||||
// 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(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;
|
return;
|
||||||
if(!curpiece_->move(y, x) || !nc_.render()){ // FIXME needs y?
|
|
||||||
throw TetrisNotcursesErr("move() or render()");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue