I am currently working on a mini project for my class. the project is designing a game called etch n sketch. you first etch out a map then quit the game after youre done. then sketch the etch you just created. the problem im having is the scoring system.

score is calculated by the formula ((t+h-m)/t) * 100. t = the number of x,y positions that were etched. h = the number of hits when you trace over the etch correctly. m = is the number of positions that the player has moved the cursor to and that do not belong to the pattern.

I also had to use the curses library. the code is a little but lengthy, but the scoring functions are located at lines 322 and 356.

my problem right now is i cant get to calculate m correctly. heres the code i wrote so far. i know my code is cluttered and could be cleaned up but right now i just want the program to work.

thank you for any help.

#include <iostream>
#include <curses.h>
#include <fstream>
#include <cmath>
using namespace std;
    // Color codes
    const short WHITE_ON_BLACK = 0;  // must always be zero (0)
    const short CYAN_ON_BLACK = 1;
    const short RED_ON_WHITE = 2;
    const short YELLOW_ON_BLACK = 3;
    const short CYAN_ON_WHITE = 4;
    const short GREEN_ON_WHITE = 5;
    const short BLUE_ON_WHITE = 6;
    const short BLACK_ON_BLACK = 7;
    const int BRIGHT = 101;
    const int NORMAL = 102;
	
    // Program/Utility constants
    const int FAILED_ROUTINE = -1;
    const int NOVICE_WAIT = 4;  // tenths of a second
    const int STANDARD_WAIT = 3;
    const int EXPERT_WAIT = 2;
    const int LONG_WAIT = 100;
    const int TIMED_WAIT = 1;
    const int FIXED_WAIT = 2;
    const int TMP_SIZE = 25; 
    const int MAX_SCORES = 10;
    const char MAP_MAKING_CHAR = '*';
    const short MAP_MAKING_CHAR_COLOR = GREEN_ON_WHITE;
    const char GAME_DISP_CHAR = '#';
    const short GAME_DISP_CHAR_COLOR = GREEN_ON_WHITE;
    const char MAP_DISP_CHAR = '+';
    const short MAP_DISP_CHAR_COLOR = BLUE_ON_WHITE;
    const char SPACE = ' ';
    
    //Screen clear
    const int SCRN_MIN_Y = 0;
    const int SCRN_MIN_X = 0;
    const int SCRN_MAX_Y = 24;
    const int SCRN_MAX_X = 79;
    
    void setKeyBoardWait( int secTenths );

bool initializeColors();
void printTextAt( int yPos, int xPos, 
                     const string &outString, 
                                    short colorCode, int brightness );
void clearScreen( int yLo, int xLo, int yHi, 
                                           int xHi, short colorCode );
void printCharAt( int yPos, int xPos, char outChar, 
                                    short colorCode, int brightness );
void printIntAt( int yPos, int xPos, const string &prefix, 
                                           int val, short colorCode );
int waitForInput( int timedWait );
void setKeyBoardWait( int secTenths );
int calculateScore(int t, int h, int m);
int getMiss( int x[],int y[],int xCoord, int yCoord, int size, int misses);
void printEndLine();
void finish();
void initCurses();
int getCheckVelocity(int xy, int v, int max);

int maxy = 25, maxx = 80;
int t = 0;
int h = 0;
int m = 0;

int xstore[255];
int ystore[255];
void startColor();
void endCurses();
int main(){     
  initCurses();
  
  
  
  
  int c;
  char op;
  bool flag = false;
  int x = maxx/2, y = maxy/2;
  int vx = 0, vy = 0;
  printTextAt(2,8, "S", 1, 101);
  printTextAt(2,7, "S", 1, 101);
  printTextAt(3,7, "S", 1, 101);
  printTextAt(3,6, "S", 1, 101);
  printTextAt(4,6, "S", 1, 101);
  printTextAt(4,5, "S", 1, 101);
  printTextAt(5,5, "S", 1, 101);
  printTextAt(5,4, "S", 1, 101);
  printTextAt(6,5, "S", 1, 101);
  printTextAt(6,6, "S", 1, 101);
  printTextAt(7,6, "S", 1, 101);
  printTextAt(7,7, "S", 1, 101);
  printTextAt(8,7, "S", 1, 101);
  printTextAt(8,8, "S", 1, 101);
  printTextAt(9,7, "S", 1, 101);
  printTextAt(9,6, "S", 1, 101);
  printTextAt(10,5, "S", 1, 101);
  printTextAt(10,6, "S", 1, 101);
  printTextAt(11,4, "S", 1, 101);
  printTextAt(11,5, "S", 1, 101);
  
  printTextAt(2,11, "PPPPPPP", 1, 101);
  printTextAt(3,11, "P", 1, 101);
  printTextAt(4,11, "P", 1, 101);
  printTextAt(5,11, "P", 1, 101);
  printTextAt(6,11, "P", 1, 101);
  printTextAt(7,11, "P", 1, 101);
  printTextAt(8,11, "P", 1, 101);
  printTextAt(9,11, "P", 1, 101);
  printTextAt(10,11, "P", 1, 101);
  printTextAt(11,11, "P", 1, 101);
  printTextAt(3,17, "P", 1, 101);
  printTextAt(4,17, "P", 1, 101);
  printTextAt(5,17, "P", 1, 101);
  printTextAt(6,12, "PPPPPP", 1, 101);
  
  printTextAt(2,20, "EEEEEEE", 1, 101);
  printTextAt(3,20, "E", 1, 101);
  printTextAt(4,20, "E", 1, 101);
  printTextAt(5,20, "E", 1, 101);
  printTextAt(6,20, "E", 1, 101);
  printTextAt(7,20, "EEEEE", 1, 101);
  printTextAt(8,20, "E", 1, 101);
  printTextAt(9,20, "E", 1, 101);
  printTextAt(10,20, "E", 1, 101);
  printTextAt(11,20, "E", 1, 101);
  printTextAt(12,20, "E", 1, 101);
  printTextAt(12,72, "E", 1, 101);
  printTextAt(11,71, "E", 1, 101);
  printTextAt(10,70, "E", 1, 101);  
  printTextAt(13,20, "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE", 1, 101);
  printTextAt(14,72, "E", 1, 101);
  printTextAt(15,71, "E", 1, 101);
  printTextAt(16,70, "E", 1, 101);
  
  printTextAt(2,29, "EEEEEEE", 1, 101);
  printTextAt(3,29, "E", 1, 101);
  printTextAt(4,29, "E", 1, 101);
  printTextAt(5,29, "E", 1, 101);
  printTextAt(6,29, "E", 1, 101);
  printTextAt(7,29, "EEEEE", 1, 101);
  printTextAt(8,29, "E", 1, 101);
  printTextAt(9,29, "E", 1, 101);
  printTextAt(10,29, "E", 1, 101);
  printTextAt(11,29, "EEEEEE", 1, 101);
  
  printTextAt(2,38, "DDDDD", 1, 101);
  printTextAt(3,38, "D", 1, 101);
  printTextAt(4,38, "D", 1, 101);
  printTextAt(5,38, "D", 1, 101);
  printTextAt(6,38, "D", 1, 101);
  printTextAt(7,38, "D", 1, 101);
  printTextAt(8,38, "D", 1, 101);
  printTextAt(9,38, "D", 1, 101);
  printTextAt(10,38, "D", 1, 101);
  printTextAt(11,38, "DDDDD", 1, 101);
  printTextAt(3,43, "D", 1, 101);
  printTextAt(4,44, "D", 1, 101);
  printTextAt(5,45, "D", 1, 101);
  printTextAt(6,46, "D", 1, 101);
  printTextAt(7,47, "D", 1, 101);
  printTextAt(8,46, "D", 1, 101);
  printTextAt(9,45, "D", 1, 101);
  printTextAt(10,44, "D", 1, 101);
  printTextAt(11,43, "D", 1, 101);
  
  printTextAt(2,50, "OOOOOOOOO", 1, 101);
  printTextAt(3,50, "O", 1, 101);
  printTextAt(4,50, "O", 1, 101);
  printTextAt(5,50, "O", 1, 101);
  printTextAt(6,50, "O", 1, 101);
  printTextAt(7,50, "O", 1, 101);
  printTextAt(8,50, "O", 1, 101);
  printTextAt(9,50, "O", 1, 101);
  printTextAt(10,50, "O", 1, 101);
  printTextAt(11,50, "O", 1, 101);
  printTextAt(11,50, "OOOOOOOOO", 1, 101);
  printTextAt(3,58, "O", 1, 101);
  printTextAt(4,58, "O", 1, 101);
  printTextAt(5,58, "O", 1, 101);
  printTextAt(6,58, "O", 1, 101);
  printTextAt(7,58, "O", 1, 101);
  printTextAt(8,58, "O", 1, 101);
  printTextAt(9,58, "O", 1, 101);
  printTextAt(10,58, "O", 1, 101);
  printTextAt(11,58, "O", 1, 101);
  
  printTextAt(14,29, "                                   ", 1, 101);
  printTextAt(15,35, "By", 1, 101);
  printTextAt(16,29, "                                   ", 1, 101);
  printTextAt(17,28, "                              ", 1, 101);
  printTextAt(18,29, "                                   ", 1, 101);
  printTextAt(19,28, "Would you like to create a map? M/m", 1, 101);
  printTextAt(20,28, "Would you like to play the game? P/p", 1, 101);
  mvaddch(y, x, '0');

clearScreen( SCRN_MIN_Y, SCRN_MIN_X, SCRN_MAX_Y, SCRN_MAX_X, 6 );
      
cin >> op;
//initCurses();

 switch(op){
 case 'm':
 case 'M':
 ofstream fout;
 fout.open("trace.txt");
 fout.close();

  while(true){
  ofstream fout;
  fout.open("trace.txt",ios::out | ios::app);
  fout << x << " " << y << endl; 
    c = getch();     

    printTextAt(y, x, "#", 3 , 101); 
    switch(c){
    case KEY_LEFT:   
      vx = -1;
      vy = 0;
      break;

    case KEY_RIGHT:
      vx = 1;
      vy = 0;
      break;

    case KEY_UP:
      vy = -1;
      vx = 0;
      break;

    case KEY_DOWN:
      vy = 1;
      vx = 0;
      break;

    case 'q':
    case 'Q':
      flag = true;
      break;
    }
    if(flag) break;

    vy = getCheckVelocity(y, vy, maxy);
    vx = getCheckVelocity(x, vx, maxx);

    y = y + vy;
    x = x + vx;
    
    
    mvaddch(y, x, '0'); 
    fout.close();
  }
  break;
}
switch(op){
case 'p':
case 'P':    

ifstream fin;
fin.open("trace.txt");
int xCoord, yCoord, ;        
while(fin >> xCoord >> yCoord){            
         printTextAt(yCoord, xCoord, "-", 2 , 101);
         xstore[t] = xCoord;
         ystore[t] = yCoord;
                   t++;
}
fin.close();
	do {
		c = getch();
	} while (c==ERR);

  while(true){
    
 //           initCurses();      

    printTextAt(y, x, "#", 3 , 101);
    c = getch();
    
    switch(c){
    case KEY_LEFT:   
      vx = -1;
      vy = 0;
      break;

    case KEY_RIGHT:
      vx = 1;
      vy = 0;
      break;

    case KEY_UP:
      vy = -1;
      vx = 0;
      break;

    case KEY_DOWN:
      vy = 1;
      vx = 0;
      break;



    case 'q':
    case 'Q':
      flag = true;
      break;
    }
    if(flag) break;

    vy = getCheckVelocity(y, vy, maxy);
    vx = getCheckVelocity(x, vx, maxx);

    y = y + vy;
    x = x + vx;
    


    
    for(int i =0; i < t ; i++){
            if(x == xstore[i] && y == ystore[i]){
                 h++;
                 xstore[i] = 'x';
                 ystore[i] = 'x';
              }
              else if(x != xstore[i] && y != ystore[i]){
                   m++;
                   }
 
}
                 
                 

				 
}


mvaddch(y, x, '0'); 
printIntAt( 1, 25, "Score: ", m, 1);
                                          
  }

  
  break;
}



  finish();
                 
  return 0;

}


	
int calculateScore(int t, int h, int m){
    int score;
    score = ((t + h - m)/t) * 100;
  return score;
}

int getCheckVelocity(int xy, int v, int max){
  int vel = v;
  if(xy + v <= 0 || xy + v >= max){
      vel = -v;
  }
  return vel;
}

void printEndLine()
{
  cout << endl;
}


void finish()
{
    endwin();
    cout << "All Done!" << endl;
    system("pause");

}



void setKeyBoardWait( int secTenths )
   {
   secTenths = 1;
    halfdelay( secTenths );
   }

void initCurses(){
    (void) initscr();      
    keypad(stdscr, TRUE);  
    start_color();
    init_pair(1, COLOR_CYAN,COLOR_BLACK );        
    init_pair(2, COLOR_YELLOW,COLOR_BLACK);
    init_pair(3, COLOR_BLUE, COLOR_BLACK);        
    (void) nonl();         
    (void) cbreak();       
    (void) noecho();       
    halfdelay(1);
}




//UTILITY FUNCTIONS


void printTextAt( int yPos, int xPos, 
                     const string &outString, 
                                    short colorCode, int brightness )
   {
    if( brightness == BRIGHT )
       {
        // set the color attribute
        attron( A_BOLD | COLOR_PAIR( colorCode ) );

        // output the string
        mvaddstr( yPos, xPos, outString.c_str() );

        // unset the color attribute
        attroff( A_BOLD | COLOR_PAIR( colorCode ) );
       }
    else
       {
        // set the color attribute
        attron( COLOR_PAIR( colorCode ) );

        // output the string
        mvaddstr( yPos, xPos, outString.c_str() );

        // unset the color attribute
        attroff( COLOR_PAIR( colorCode ) );
       }

    refresh();
   }

bool initializeColors()
   {
    // initialize variables
    short itemCode, foreGround, backGround;

    // test for color problems with console
    if( ( !has_colors() ) || ( start_color() == ERR ) )
       {
        return false;
       }

    // set color here - CYAN ON BLACK
    foreGround = COLOR_CYAN;  // this is defined in curses
    backGround = COLOR_BLACK; // this is defined in curses
    itemCode = CYAN_ON_BLACK; // you have to create this constant
                                 // starting with the next number
    init_pair( itemCode, foreGround, backGround );

    // set color here - RED ON WHITE
    foreGround = COLOR_RED;  // this is defined in curses
    backGround = COLOR_WHITE; // this is defined in curses
    itemCode = RED_ON_WHITE; // you have to create this constant
                                 // starting with the next number
    init_pair( itemCode, foreGround, backGround );

    // set color here - YELLOW ON BLACK
    foreGround = COLOR_YELLOW;  // this is defined in curses
    backGround = COLOR_BLACK; // this is defined in curses
    itemCode = YELLOW_ON_BLACK; // you have to create this constant
                                 // starting with the next number
    init_pair( itemCode, foreGround, backGround );

    // set color here - CYAN ON WHITE
    foreGround = COLOR_CYAN;  // this is defined in curses
    backGround = COLOR_WHITE; // this is defined in curses
    itemCode = CYAN_ON_WHITE; // you have to create this constant
                                 // starting with the next number
    init_pair( itemCode, foreGround, backGround );

    // set color here - GREEN ON WHITE
    foreGround = COLOR_GREEN;  // this is defined in curses
    backGround = COLOR_WHITE; // this is defined in curses
    itemCode = GREEN_ON_WHITE; // you have to create this constant
                                 // starting with the next number
    init_pair( itemCode, foreGround, backGround );

    // set color here - BLUE ON WHITE
    foreGround = COLOR_BLUE;  // this is defined in curses
    backGround = COLOR_WHITE; // this is defined in curses
    itemCode = BLUE_ON_WHITE; // you have to create this constant
                                 // starting with the next number
    init_pair( itemCode, foreGround, backGround );


    return true;
   }
   
void clearScreen( int yLo, int xLo, int yHi, 
                                           int xHi, short colorCode )
   {
    // initialize function
    int yCtr, xCtr;

    attron( COLOR_PAIR( colorCode ) );

    // loop across rows of the screen
    for( yCtr = yLo; yCtr <= yHi; yCtr++ )
       {
        // loop across the columns of the screen
        for( xCtr = xLo; xCtr <= xHi; xCtr++ )
           {
            // output a space
            mvaddch( yCtr, xCtr, SPACE );
           }
       }

    attroff( COLOR_PAIR( colorCode ) );
   }

void printCharAt( int yPos, int xPos, char outChar, 
                                    short colorCode, int brightness )
   {
    if( brightness == BRIGHT )
       {
        // set the color attribute
        attron( A_BOLD | COLOR_PAIR( colorCode ) );

        // output the character
        mvaddch( yPos, xPos, outChar );

        // unset the color attribute
        attroff( A_BOLD | COLOR_PAIR( colorCode ) );
       }
    else
       {
        // set the color attribute
        attron( COLOR_PAIR( colorCode ) );

        // output the character
        mvaddch( yPos, xPos, outChar );

        // unset the color attribute
        attroff( COLOR_PAIR( colorCode ) );
       }

    refresh();
   }
   
void printIntAt( int yPos, int xPos, const string &prefix, 
                                           int val, short colorCode )
   {
    string localString = prefix;
    char tempStr[ TMP_SIZE ];

    sprintf( tempStr, "%i", val );

    localString += tempStr;

    printTextAt( yPos, xPos, localString, colorCode, NORMAL );
   }

int waitForInput( int timedWait )
   {
    int testVal;

    // uses time constant presently set in halfdelay
    if( timedWait == TIMED_WAIT )
       {
        return getch();
       }

    do
       {
        testVal = getch();
       }
    while(  testVal == ERR );

    return testVal;
   }

At least learn to read the forum rules and how to post code before puking a 1000 lines of unformatted code on the board.

my problem right now is i cant get to calculate m correctly. heres the code i wrote so far. i know my code is cluttered and could be cleaned up but right now i just want the program to work.

Please explain how m is to be computed. It looks like what's happening is that if the player get an x or a y coordinate correct, the m++ does not execute. If you mean that either he gets both x and y correct for a point, and anything else is a miss, you don't need a second condition, just the else.

thats what i tried, just have an else. but the thing that happens is that m starts calculating at really fast rate. by like thousands

What is the point of the for loop at 322? x and y do not change inside it, so either the xstore and ystore arrays will get filled up with t elements, or m will be incremented t times.

To what purpose?

This article has been dead for over six months. Start a new discussion instead.