Week 3 challenges: Xmover
#include "console.h"
using namespace cio;
int main(){
bool done = false;
int row = console.getRows()/2;
int col = console.getCols()/2;
int key;
int end_flag=0;
while(!done){
if ((row == console.getRows()-1 && col == console.getCols()-2) || (row == console.getRows()-2 && col == console.getCols()-1)) {
end_flag=1;
} else {
end_flag=0;
}
console.setPos(row, col);
console.putChar('A');
key = console.getKey();
console.setPos(row, col);
console.putChar('.');
switch(key){
case UP:
if(row > 0 ){
row--;
}
else{
console.alarm();
}
break;
case DOWN:
if(row < console.getRows()-1 && end_flag==0){
row++;
}
else{
console.alarm();
}
break;
case LEFT:
if(col > 0){
col--;
}
else{
console.alarm();
}
break;
case RIGHT:
if(col < console.getCols()-1 && end_flag==0){
col++;
}
else{
console.alarm();
}
break;
case ESCAPE:
done = true;
break;
}
}
return 0;
}
No comments:
Post a Comment