kbjradmin
New Member
- Messages
- 512
- Reaction score
- 2
- Points
- 0
i am teaching myself C++ and as a project to get used to the language i am making a simple dice game using ncurses. i got everything to work up to the point where i tried to put the individual dice in their own windows. there is no error occurring, but the window isn't showing up. please help.
Code:
/*****************************************************************************
*
* dice.cpp
* Author: James Brumond
* Date Created: 24 October, 2009
*
* A simple dice game.
*
*****************************************************************************/
#include <string>
#include <string.h>
#include <iostream>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
using namespace std;
// function prototypes
void endProg(int);
void startCurses();
WINDOW *createNewWin(int, int, int, int);
// initialize windows
WINDOW *wins [5] = { createNewWin(5, 9, 0, 10), createNewWin(5, 9, 0, 20), createNewWin(5, 9, 0, 30), createNewWin(5, 9, 0, 40),
createNewWin(5, 9, 0, 50) };
// the die class
class Die {
private:
int side;
int value;
int values [6];
const char* display;
const char* displays [6];
public:
Die(unsigned int);
void roll();
void set(int);
int getValue();
const char* getDisplay();
int win;
void draw();
};
// initialize the die
// set all of the die's values, display texts, and the window where the die
// outputs data to
Die::Die(unsigned int window) {
// when die is a one
values[0] = 1;
displays[0] = (char*)"\n \n o \n \n\n Die %d", (window + 1);
// when die is two
values[1] = 2;
displays[1] = (char*)"\n o \n \n o \n\n Die %d", (window + 1);
// when die is three
values[2] = 3;
displays[2] = (char*)"\n o \n o \n o \n\n Die %d", (window + 1);
// when die is four
values[3] = 4;
displays[3] = (char*)"\n o o \n \n o o \n\n Die %d", (window + 1);
// when die is five
values[4] = 5;
displays[4] = (char*)"\n o o \n o \n o o \n\n Die %d", (window + 1);
// when die is six
values[5] = 6;
displays[5] = (char*)"\n o o \n o o \n o o \n\n Die %d", (window + 1);
// set values
side = 0;
value = values[side];
display = displays[side];
win = window;
}
// takes a number between 0 and 5
// sets the die's value and display according to
void Die::set(int newSide) {
if (newSide > 5 || newSide < 0) {
cout << "void Die::set(int) must recieve a value between 0 and 5." << endl;
cout << "fatal error: aborting..." << endl;
endProg(1);
}
else {
side = newSide;
value = values[side];
display = displays[side];
}
}
// roll the die
// uses srand with the current time for seed and rand % 6 to get
// a random value between 0 and 5
void Die::roll() {
srand(time(0));
side = rand() % 6;
set(side);
}
// get the numerical value of the die
int Die::getValue() {
return value;
}
// get the text to be displayed for the die
const char* Die::getDisplay() {
return display;
}
void Die::draw() {
Die::roll();
wprintw(wins[win], Die::getDisplay());
wborder(wins[win], '|', '|', '-', '-', '+', '+', '+', '+');
}
// end curses and exit the program
void endProg(int status) {
try {
endwin();
}
catch(...) { }
exit(status);
}
// start the curses library
void startCurses() {
initscr();
cbreak();
keypad(stdscr, true);
}
// create a new window with a border
WINDOW *createNewWin(int height, int width, int y, int x) {
WINDOW *localWin;
localWin = newwin(height, width, y, x);
wborder(localWin, '|', '|', '-', '-', '+', '+', '+', '+');
wrefresh(localWin);
return localWin;
}
// main entry point of the program
int main() {
startCurses();
Die dice [5] = { Die(0), Die(1), Die(2), Die(3), Die(4) };
dice[0].roll();
dice[0].draw();
while (0 == 0) {
char ch = getch();
if (ch == 'q') {
break;
}
dice[0].roll();
dice[0].draw();
}
endProg(0);
}
Last edited: