So in class we're making a pong game using the curses.h library. So far I've managed to make the ball move around and bounce, hit the paddle, determine the number of blocks/misses, and so forth.

The only trouble I've had is having it dynamically update the number of blocks and misses because mvaddch(y, x, ch) and mvaddstr(y, x, str) don't play well with integers... I've searched the webpage link that our professor supplied us, as well as a quick good search for "ncurses integer" and have yet to find a function in the curses library that might do the same things as mvaddch but with ints.

Is there such a function? If not what alternatives might I have?

Thank you for your time.

EDIT: This is in C++

#include<iostream>
#include<curses.h>
#include<fstream>
#include<cstring>

using namespace std;

void init_curses(); 
void finish_curses();
void movePaddle(int &y, char dir, bool &pad);
void moveBallx(int x, bool &minus, int &miss);
void moveBally(int y, bool &minus);
void sortScore(int score[], string names[]);
void printScore(int score[], string names[]);
int overallScore(int block, int miss);
int exp(int a, int b);

int main() {
	int ballx=40, bally=12;
	int paddlex=4, paddley=9, paddle;
	int miss=0, block=0;
	int score[11];
	bool xMinus = false;
	bool yMinus = false;
	bool pad = false;
	char ch, name[3];
	string names[11];
	ifstream fin;
	ofstream fout;
	
	cout <<"pppppppppppp"<<"        "<<"ooooooooooo"<<"        "<<"nnnnnn      nnn"<<"        "<<"ggggggggggg"<<endl;
	cout <<"pppppppppppp"<<"        "<<"ooooooooooo"<<"        "<<"nnn nnn     nnn"<<"        "<<"ggggggggggg"<<endl;
	cout <<"ppp      ppp"<<"        "<<"ooo     ooo"<<"        "<<"nnn  nnn    nnn"<<"        "<<"ggg        "<<endl;
	cout <<"pppppppppppp"<<"        "<<"ooo     ooo"<<"        "<<"nnn   nnn   nnn"<<"        "<<"ggggggggggg"<<endl;
	cout <<"pppppppppppp"<<"        "<<"ooo     ooo"<<"        "<<"nnn    nnn  nnn"<<"        "<<"ggg     ggg"<<endl;
	cout <<"ppp"<<"                 "<<"ooooooooooo"<<"        "<<"nnn     nnn nnn"<<"        "<<"ggggggggggg"<<endl;
	cout <<"ppp"<<"                 "<<"ooooooooooo"<<"        "<<"nnn      nnnnnn"<<"        "<<"ggggggggggg"<<endl<<endl;
	cout << "Enter your initials (3 letters): ";
	cin >> name;
	
	init_curses();
	for (paddle = 0; paddle < 5; paddle++) {
		mvaddch(paddley+paddle, paddlex, '|');
	}
	while (1) {
		ch = getch();		
		movePaddle(paddley, ch, pad);
		if (ch == 'q' || ch == 'Q') {
			break;
		}
		
		mvaddch(bally,ballx,' ');
		if (ballx == 79 || ballx == 0) {
			moveBallx(ballx, xMinus, miss);
		}		
		else if (bally == 24 || bally == 0) {
			moveBally(bally, yMinus);
		}
		else if (ballx == 5) {
			if (bally >= paddley && bally <= (paddley+4)) {
				moveBallx(ballx, xMinus, miss);
				block++;
			}
		}
		
		if (xMinus && !yMinus) {
			ballx--;
			bally++;
		}
		else if (yMinus && !xMinus) {
			ballx++;
			bally--;
		}
		else if (xMinus && yMinus) {
			ballx--;
			bally--;
		}
		else {
			ballx++;
			bally++;
		}
		mvaddstr(2, 21, "Blocks: ");
//		mvaddint
		mvaddch(bally,ballx,'O');
	}
	finish_curses();
	
	score[0] = overallScore(block, miss);
	names[0] = name;
	fin.open("top10.txt");
	for (int i = 1; i<11; i++) {
		fin >> names[i];
		fin >> score[i];
	}
	fin.close();
	cout << "Blocks: " << block << endl;
	cout << "Misses: " << miss << endl;
	sortScore(score, names);
	printScore(score, names);
	cin >> name;
	
	return 0;
	
}

void init_curses() {
	initscr();     
    keypad(stdscr, TRUE);  
    nonl();         
    cbreak();       
    noecho();  
    halfdelay(1);
}

void finish_curses() {
	endwin();
    system("clear");
    cout << "All Done!" << endl;
}

void movePaddle(int &y, char dir, bool &pad) {
	switch(dir) {
	case 'w':
	case 'W':
		if (y == 0) {
			break;
		}
		else {
			mvaddch(y+4, 4, ' ');
			y--;
			mvaddch(y, 4, '|');
			break;
		}
	case 's':
	case 'S':
		if (y+4 == 24) {
			break;
		}
		else {
			mvaddch(y, 4, ' ');
			y++;
			mvaddch(y+4, 4, '|');
			break;
		}
	}
}

void moveBallx(int x, bool &minus, int &miss) {
	if (x == 0) {
		minus = false;
		miss++;
	}
	else if (x == 5) {
		minus = false;
	}
	else if (x == 79) {
		minus = true;
	}
}

void moveBally(int y, bool &minus) {
	if (y == 0) {
		minus = false;
	}
	else if (y == 24) {
		minus = true;
	}	
}

void sortScore(int score[], string names[]) {
	int i, j;
	int check;
	string temp;
	
	for(i=1; i<11; i++) {
		check = score[i];
		temp = names[i];
		for(j=1; j>=1 && check > score[j-1]; j--) {
			score[j] = score[j-1];
			names[j] = names[j-1];
			score[j-1] = check;
			names[j-1] = temp; 
		}
	}
}

void printScore(int score[], string names[]) {
	ofstream fout;
	fout.open("top10.txt");
	
	for(int i=0; i<10; i++) {
		cout << names[i] << ": " << score[i] << endl;
		fout << names[i] << " " << score[i] << endl;
	}
	fout.close();
}

int overallScore(int block, int miss) {
	int bonus, r;
	if (miss >= block) {
		bonus = 1000;
	}
	else {
		bonus = 100;
	}
	r = (((exp(block,2) - miss) * 10) + bonus);
	return r;
}

int exp(int a, int b) {
	int r=1;
	for(int i=0; i<b; i++) {
		r *= a;
	}	
	return r;
}

The commented part is where I would like to output an integer, I've gotten everything else to work :) Don't mind the many many couts at the top......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.