I have written 3 files, i dont know whether i do it correctly or wrongly but somehow it compiled well and can run.
My simple aim is to display the terrain.txt file into the terrain array, and then display terrain into screen.
I can't display the whitespace characters from terrain.txt file to screen.
and
It display "weird"( the red color part).
I tried searching one line by line, and i can't figure out which line has the problem.
here is my 3 file: -
// screen.h
-
#ifndef _SCREEN_H_
-
#define _SCREEN_H_
-
#include <string>
-
#include <iostream>
-
#include <fstream>
-
#include <cstdlib>
-
#include "array2d.h"
-
-
namespace Game{
-
using namespace std;
-
-
class Screen{
-
public:
-
-
Screen(unsigned xsize = 80, unsigned ysize = 23): Xsize(xsize), Ysize(ysize), myArray(Xsize, Ysize){}
-
-
~Screen()
-
{
-
}
-
-
//Insert a character into the screen image at a specified row and column.
-
void Insert(unsigned x, unsigned y , char c){
-
myArray.Put(x,y,c);
-
}
-
-
void setStatus(string str){
-
status = str;
-
}
-
-
/* clear the previous screen and display new screen image with Xsize Ysize */
-
void Paint(){
-
-
/* clear the screen */
-
system("clear");
-
-
// and then display the screen with width and height
-
for(unsigned y = 0; y < Ysize; y++){
-
for(unsigned x = 0; x < Xsize; x++){
-
cout << myArray.Get(x , y);
-
}
-
}
-
}
-
-
void Beep(){
-
cout << '\a';
-
}
-
private:
-
unsigned Xsize;
-
unsigned Ysize;
-
Array2D<char> myArray;
-
string status;
-
};
-
}
-
#endif
-
-
// terrain.h
-
#ifndef _TERRAIN_H_
-
#define _TERRAIN_H_
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
#include "array2d.h"
-
#include "screen.h"
-
-
namespace Game{
-
using namespace std;
-
-
class Terrain{
-
-
public:
-
-
// constructor that will input the terrain from a specified file and store it
-
// in dynamically allocated storage of an appropriate size.
-
-
Terrain(char* filename =""): width(25) , height(11), ter_array(width, height){
-
-
-
ifstream in(filename);
-
-
if(!in){
-
cout << "Cannot open terrain file." << endl;
-
}
-
-
for(unsigned y = 0; y < height; y++){
-
for(unsigned x = 0; x < width; x++){
-
in >> piece;
-
ter_array.Put(x,y,piece);
-
}
-
}
-
in.close();
-
};
-
-
~Terrain(){ }
-
-
// draw the terrain onto a specified Screen object.
-
void Draw(Screen& scr){
-
for(unsigned y = 0; y < height; y++){
-
for(unsigned x = 0; x < width; x++){
-
scr.Insert(x, y, ter_array.Get(x,y));
-
}
-
}
-
}
-
-
private:
-
-
unsigned width;
-
unsigned height;
-
-
Array2D<char> ter_array;
-
char piece;
-
-
};
-
}
-
#endif
-
-
// array2d.h - Simple template class for a sparse 2D array
-
#ifndef _ARRAY2D_H_
-
#define _ARRAY2D_H_
-
#include "exception.h"
-
-
namespace Game {
-
-
/*** Simple template class for a 2D Array ***/
-
template<class T>
-
class Array2D {
-
public:
-
Array2D(unsigned width, unsigned height) : xsize(width), ysize(height) {
-
array = new T[xsize * ysize];
-
}
-
~Array2D() { delete array; }
-
void Put(unsigned x, unsigned y, T item) {
-
if (x >= xsize || y >= ysize) {
-
throw GameException("Array out of bounds");
-
}
-
array[y * xsize + x] = item;
-
}
-
T Get(unsigned x, unsigned y) const {
-
if (x >= xsize || y >= ysize) {
-
throw GameException("Array out of bounds");
-
}
-
return array[y * xsize + x];
-
}
-
void SetAll(T item) {
-
for (unsigned i = 0; i < xsize * ysize; i++) {
-
array[i] = item;
-
}
-
}
-
private:
-
const unsigned xsize;
-
const unsigned ysize;
-
T* array;
-
};
-
}
-
#endif
-
here is the main.cpp i used: -
#include <iostream>
-
#include <string>
-
#include "screen.h"
-
#include "array2d.h"
-
#include "terrain.h"
-
-
using namespace std;
-
using namespace Game;
-
-
// get terrain.txt from command line
-
int main(int argc, char* argv[]){
-
Screen s;
-
Terrain t(argv[1]);
-
-
t.Draw(s);
-
s.Paint();
-
-
return 0;
-
}
-
8 2184
Hi all,
i fix the whitespace problem, DeMan helped me out with that already.
get the whitespace using get().
So now leave the problem of that red color spot.
I think somehow the problem issued when i draw terrain to screen.
but still no idea about where's wrong. I'm looking at it now. Hope you guys can help me out too.
here is the code i been changing it for past 15 minutes. - // terrain.h
-
#ifndef _TERRAIN_H_
-
#define _TERRAIN_H_
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
#include "array2d.h"
-
#include "screen.h"
-
-
namespace Game{
-
using namespace std;
-
-
class Terrain{
-
-
public:
-
-
// constructor that will input the terrain from a specified file and store it
-
// in dynamically allocated storage of an appropriate size.
-
-
Terrain(char* filename =""): width(25) , height(11), ter_array(width, height){
-
-
-
ifstream in(filename);
-
-
if(!in){
-
cout << "Cannot open terrain file." << endl;
-
}
-
-
for(unsigned y = 0; y < height; y++){
-
for(unsigned x = 0; x < width; x++){
-
piece = in.get(); // i get whitespace useing get()
-
ter_array.Put(x,y,piece);
-
}
-
}
-
in.close();
-
};
-
-
~Terrain(){ }
-
-
// draw the terrain onto a specified Screen object.
-
void Draw(Screen& scr){
-
for(unsigned y = 0; y < height; y++){
-
for(unsigned x = 0; x < width; x++){
-
scr.Insert(x, y, ter_array.Get(x,y));
-
}
-
}
-
}
-
-
private:
-
-
unsigned width;
-
unsigned height;
-
-
Array2D<char> ter_array;
-
char piece;
-
-
};
-
}
-
#endif
-
-
// screen.h
-
#ifndef _SCREEN_H_
-
#define _SCREEN_H_
-
#include <string>
-
#include <iostream>
-
#include <fstream>
-
#include <cstdlib>
-
#include "array2d.h"
-
-
namespace Game{
-
using namespace std;
-
-
class Screen{
-
public:
-
-
Screen(unsigned xsize = 79, unsigned ysize = 23): Xsize(xsize), Ysize(ysize), myArray(Xsize, Ysize){}
-
-
~Screen()
-
{
-
}
-
-
//Insert a character into the screen image at a specified row and column.
-
void Insert(unsigned x, unsigned y , char c){
-
myArray.Put(x,y,c);
-
}
-
-
void setStatus(string str){
-
status = str;
-
}
-
-
/* clear the previous screen and display new screen image with Xsize Ysize */
-
void Paint(){
-
-
/* clear the screen */
-
system("clear");
-
-
// and then display the screen with width and height
-
for(unsigned y = 0; y < Ysize; y++){
-
for(unsigned x = 0; x < Xsize; x++){
-
cout << myArray.Get(x , y);
-
}
-
}
-
}
-
-
void Beep(){
-
cout << '\a';
-
}
-
private:
-
unsigned Xsize;
-
unsigned Ysize;
-
Array2D<char> myArray;
-
string status;
-
};
-
}
-
#endif
-
terrain.txt file: -
+-----------------------+
-
| |
-
| +-+--+ +-+
-
| | |
-
| +----+ +-------+ |
-
| | | |
-
| | +---+ + |
-
| | | |
-
| +-----+ +--+ + |
-
| | |
-
+------------------+----+
-
here is the output:
it seems that the last line of terrain.txt file is missing some part.
where's the wrong code anyway...?
thanks in advance.
Nick.
DId you make terrain.txt in a simple editor such as Notepad? There may be extra characters used for formatting if you used something 'fancier'...
DId you make terrain.txt in a simple editor such as Notepad? There may be extra characters used for formatting if you used something 'fancier'...
i used TextPad 4.7.3: 32-bit Edition to create the terrain.txt file.
I make sure there is no extra space in each end of line. The terrain should be 25 in columns and 11 in rows.
if the terrain.txt file is like this: -
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
it will display to screen like this:
if the terrain.txt file is like this: -
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
+++++++++++++++++++++++++
-
it will display to screen like this:
Hi,
I believe this is a problem with the X and Y coordinates with your screen which doesn't match with the widtch and height of what you are drawing. try lowering down the value of Xsize to 23 then Ysize to 11 in screen.h. Or try organizing your array well to match with the how big your screen is.
That's all thanks. Hope this helps.
Hi,
I believe this is a problem with the X and Y coordinates with your screen which doesn't match with the widtch and height of what you are drawing. try lowering down the value of Xsize to 23 then Ysize to 11 in screen.h. Or try organizing your array well to match with the how big your screen is.
That's all thanks. Hope this helps.
since you mentioned me, i change the code in game.cpp(the main function located), where i obtain the width and height of terrain array, and use them to initialise screen array with screen constructor.
it display well, but seems that last line of the terrain.txt file cannot be display.
i guess this code is the problem where it can't get all the characters from terrain text file. -
for(unsigned y = 0; y < height; y++){
-
for(unsigned x = 0; x < width; x++){
-
-
in.get(piece);
-
ter_array.Put(x,y,piece);
-
}
-
}
-
i fixed the reading all character from file problem by setting the terrain array with 27 in width and 11 in height. With this setting, i can display all characters from terrain text file.
now my problem is, i dont know why i can draw the terrain array into screen object without calling t.Draw(s) as following. i set 80x23 of the screen and Paint() is to display the screen into cout. So if i didnt' call t.Draw(s), it should display 80x23 screen into cout.
Anyone please explain to me. -
int main(int argc, char* argv[]){
-
-
Terrain t(argv[1]);
-
-
unsigned width = t.getWidth();
-
unsigned height = t.getHeight();
-
-
Screen s(width, height);
-
-
//t.Draw(s);
-
s.Paint();
-
-
return 0;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Wolfgang Kaml |
last post by:
Hello All,
I have been researching newsgroups and knowledgebase all morning and not
found a solution that would solve the problem I have. I am having an ASP or
ASPX web page that implement a...
|
by: LRW |
last post by:
I manage our mySQL database through putty (SSH terminal client).
And whenever I do a select * from the table that contains ENCODEd
passwords, the funky characters do funky things with the display....
|
by: Martin Doyle |
last post by:
Ok, I'm building a JS-based limitless-sublevel dynamic menu and am
making it cross browser as well - 3 packs of aspirin so far and
counting ;)
I'm having a weird rendering problem using Opera...
|
by: Les Paul |
last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's
just like a Wiki page, but my own very simple version. It's a page full
of plain old HTML content, and then at the bottom,...
|
by: bala |
last post by:
hi!!!
i need to display a disclaimer which is two page in length in a word
document. i also need to format the text. the idea is something as
follows
on opening the application, a form which...
|
by: Michel Rouzic |
last post by:
With the following code :
int main()
{
int i, j, n;
n=12;
for (i=0; i<n; i++)
{
printf("Iteration %i out of %i\n", i+1, n);
|
by: rbinington |
last post by:
Hi,
I am trying to write a DNN module that has the ability to insert articles into an article repository. I want the users to be able to move pages around and enter text into the FCKEditor. I...
|
by: Dameon99 |
last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the...
|
by: kai97 |
last post by:
Dear bro and sis,
I am trying to convert a web application to arabic gcc version.
Everything work fine. But the GD::Graph display weird character when i use the arabic words.
Please see...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |