Friday 1 February 2013

one-line display function solution

  Thank to Gary's solution giving me idea.
  I have tested it. If there is any bug, just tell me.

This is Gary's link: http://garydeng.tumblr.com/



  void Console::display(const char* str, int row, int col, int fieldLen){

      for(setPos(row, col),row=0;fieldLen==0?(str[row] != 0):(row<fieldLen);row<strlen(str)?(putChar(str[row])):(putChar(' ')),row++);
  
  }

Xmover solution

#include "console.h"

using namespace cio;

int main(){
  bool done = false;
  int row = console.getRows()/2;
  int col = console.getCols()/2;
  int key ;
  while(!done){
    console.setPos(row, col);
    console.putChar('X');
    console.setPos(row, col);
    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()-2)||((col != console.getCols()-1)&&(row = console.getRows()-2))){
        row++;
      }
      else{
        console.alarm();
      }
      break;
    case LEFT:
      if(col > 0){
        col--;
      }
      else{
        console.alarm();
      }
      break;
    case RIGHT:
      if((col < console.getCols()-2)||((row != console.getRows()-1)&&(col = console.getCols()-2))){
        col++;
      }
      else{
        console.alarm();
      }
      break;
    case ESCAPE:
      done = true;
      break;
    }
  }

  return 0;
}