Saturday 26 January 2013

Week 3 To Do's

For the Xmover program, the solution I wrote is a total kludge. If I come up with something a little more elegant I'll post that code later.


case DOWN:
 if(row < console.getRows()-1){
 if(row == console.getRows()-2 && col == console.getCols()-1){
 console.alarm();
 }
 else{
 row++;
 }
      }
      else{
        console.alarm();
      }
      break;


case RIGHT:
      if(col < console.getCols()-1){
 if(col == console.getCols()-2 && row == console.getRows()-1){
 console.alarm();
 }
 else{
col++;
 }
      }
      else{
        console.alarm();
      }
      break;


For removing the 'if' from the ::display() function and reducing it to one line I once again wrote a total kludge, it is completely confusing to look at and it's ugly but dang it it works.


  void Console::display(const char* str, int row, int col, int fieldLen){
        setPos(row, col);
int i;
for(i=0,fieldLen==0?fieldLen=strlen(str):i=0;i<fieldLen;str[i]!=0?putChar(str[i]):putChar(' '),i++);
  }

This code also makes it possible to display any characters that you may insert past the first NULL char in the array when you are scrolling through str.


2 comments:

  1. i did it in same way......removing if statement is great

    ReplyDelete
  2. Have you tested the display function? I try it but it doesn't work.

    str[i]!=0?putChar(str[i]):putChar(' ');

    At this part, when strlen(str)<i<fieldLen, the str appears again.

    U can fix it by

    i<strlen(str)?(putChar(str[i])):(putChar(' '));

    ReplyDelete