473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

my display so weird...

254 100+
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:
Expand|Select|Wrap|Line Numbers
  1. // screen.h
  2. #ifndef _SCREEN_H_
  3. #define _SCREEN_H_
  4. #include <string>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <cstdlib>
  8. #include "array2d.h"
  9.  
  10. namespace Game{
  11.     using namespace std;
  12.  
  13.     class Screen{
  14.         public:
  15.  
  16.         Screen(unsigned xsize = 80, unsigned ysize = 23): Xsize(xsize), Ysize(ysize), myArray(Xsize, Ysize){}
  17.  
  18.         ~Screen()
  19.         {
  20.         }
  21.  
  22.         //Insert a character into the screen image at a specified row and column.
  23.         void Insert(unsigned x, unsigned y , char c){
  24.                     myArray.Put(x,y,c);
  25.         }
  26.  
  27.         void setStatus(string str){
  28.             status = str;
  29.         }
  30.  
  31.         /* clear the previous screen and display new screen image with Xsize Ysize */
  32.         void Paint(){
  33.  
  34.             /* clear the screen */
  35.             system("clear");
  36.  
  37.             // and then display the screen with width and height
  38.             for(unsigned y = 0; y < Ysize; y++){
  39.                 for(unsigned x = 0; x < Xsize; x++){
  40.                     cout << myArray.Get(x , y);
  41.                 }
  42.             }
  43.         }
  44.  
  45.         void Beep(){
  46.             cout << '\a';
  47.         }
  48.         private:
  49.         unsigned Xsize;
  50.         unsigned Ysize;
  51.         Array2D<char> myArray;
  52.         string status;
  53.     };
  54. }
  55. #endif
  56.  
Expand|Select|Wrap|Line Numbers
  1. // terrain.h
  2. #ifndef _TERRAIN_H_
  3. #define _TERRAIN_H_
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include "array2d.h"
  8. #include "screen.h"
  9.  
  10. namespace Game{
  11. using namespace std;
  12.  
  13.     class Terrain{
  14.  
  15.         public:
  16.  
  17.             // constructor that will input the terrain from a specified file and store it
  18.             // in dynamically allocated storage of an appropriate size.
  19.  
  20.                 Terrain(char* filename =""): width(25) , height(11), ter_array(width, height){
  21.  
  22.  
  23.                 ifstream in(filename);
  24.  
  25.                 if(!in){
  26.                     cout << "Cannot open terrain file." << endl;
  27.                 }
  28.  
  29.                 for(unsigned y = 0; y < height; y++){
  30.                     for(unsigned x = 0; x < width; x++){
  31.                         in >> piece;
  32.                         ter_array.Put(x,y,piece);
  33.                     }
  34.                 }
  35.                 in.close();
  36.             };
  37.  
  38.             ~Terrain(){  }
  39.  
  40.             // draw the terrain onto a specified Screen object.
  41.             void Draw(Screen& scr){
  42.                 for(unsigned y = 0; y < height; y++){
  43.                     for(unsigned x = 0; x < width; x++){
  44.                         scr.Insert(x, y, ter_array.Get(x,y));
  45.                     }
  46.                 }
  47.             }
  48.  
  49.         private:
  50.  
  51.             unsigned width;
  52.             unsigned height;
  53.  
  54.             Array2D<char> ter_array;
  55.             char piece;
  56.  
  57.     };
  58. }
  59. #endif
  60.  
Expand|Select|Wrap|Line Numbers
  1. // array2d.h - Simple template class for a sparse 2D array
  2. #ifndef _ARRAY2D_H_
  3. #define _ARRAY2D_H_
  4. #include "exception.h"
  5.  
  6. namespace Game {
  7.  
  8.     /*** Simple template class for a 2D Array ***/
  9.     template<class T>
  10.     class Array2D {
  11.     public:
  12.         Array2D(unsigned width, unsigned height) : xsize(width), ysize(height) {
  13.             array = new T[xsize * ysize];
  14.         }
  15.         ~Array2D() { delete array; }
  16.         void Put(unsigned x, unsigned y, T item) {
  17.             if (x >= xsize || y >= ysize) {
  18.                 throw GameException("Array out of bounds");
  19.             }
  20.             array[y * xsize + x] = item;
  21.         }
  22.         T Get(unsigned x, unsigned y) const {
  23.             if (x >= xsize || y >= ysize) {
  24.                 throw GameException("Array out of bounds");
  25.             }
  26.             return array[y * xsize + x];
  27.         }
  28.         void SetAll(T item) {
  29.             for (unsigned i = 0; i < xsize * ysize; i++) {
  30.                 array[i] = item;
  31.             }
  32.         }
  33.     private:
  34.         const unsigned xsize;
  35.         const unsigned ysize;
  36.         T* array;
  37.     };
  38. }
  39. #endif
  40.  
here is the main.cpp i used:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include "screen.h"
  4. #include "array2d.h"
  5. #include "terrain.h"
  6.  
  7. using namespace std;
  8. using namespace Game;
  9.  
  10. // get terrain.txt from command line
  11. int main(int argc, char* argv[]){
  12.         Screen s;
  13.         Terrain t(argv[1]);
  14.  
  15.         t.Draw(s);
  16.         s.Paint();
  17.  
  18.     return 0;
  19. }
  20.  
Dec 14 '06 #1
8 2184
nickyeng
254 100+
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.
Dec 14 '06 #2
nickyeng
254 100+
here is the code i been changing it for past 15 minutes.

Expand|Select|Wrap|Line Numbers
  1. // terrain.h
  2. #ifndef _TERRAIN_H_
  3. #define _TERRAIN_H_
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include "array2d.h"
  8. #include "screen.h"
  9.  
  10. namespace Game{
  11. using namespace std;
  12.  
  13.     class Terrain{
  14.  
  15.         public:
  16.  
  17.             // constructor that will input the terrain from a specified file and store it
  18.             // in dynamically allocated storage of an appropriate size.
  19.  
  20.                 Terrain(char* filename =""): width(25) , height(11), ter_array(width, height){
  21.  
  22.  
  23.                 ifstream in(filename);
  24.  
  25.                 if(!in){
  26.                     cout << "Cannot open terrain file." << endl;
  27.                 }
  28.  
  29.                 for(unsigned y = 0; y < height; y++){
  30.                     for(unsigned x = 0; x < width; x++){
  31.                                 piece = in.get();               // i get whitespace useing get()
  32.                         ter_array.Put(x,y,piece);
  33.                     }
  34.                 }
  35.                 in.close();
  36.             };
  37.  
  38.             ~Terrain(){  }
  39.  
  40.             // draw the terrain onto a specified Screen object.
  41.             void Draw(Screen& scr){
  42.                 for(unsigned y = 0; y < height; y++){
  43.                     for(unsigned x = 0; x < width; x++){
  44.                         scr.Insert(x, y, ter_array.Get(x,y));
  45.                     }
  46.                 }
  47.             }
  48.  
  49.         private:
  50.  
  51.             unsigned width;
  52.             unsigned height;
  53.  
  54.             Array2D<char> ter_array;
  55.             char piece;
  56.  
  57.     };
  58. }
  59. #endif
  60.  
Expand|Select|Wrap|Line Numbers
  1. // screen.h
  2. #ifndef _SCREEN_H_
  3. #define _SCREEN_H_
  4. #include <string>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <cstdlib>
  8. #include "array2d.h"
  9.  
  10. namespace Game{
  11.     using namespace std;
  12.  
  13.     class Screen{
  14.         public:
  15.  
  16.         Screen(unsigned xsize = 79, unsigned ysize = 23): Xsize(xsize), Ysize(ysize), myArray(Xsize, Ysize){}
  17.  
  18.         ~Screen()
  19.         {
  20.         }
  21.  
  22.         //Insert a character into the screen image at a specified row and column.
  23.         void Insert(unsigned x, unsigned y , char c){
  24.                     myArray.Put(x,y,c);
  25.         }
  26.  
  27.         void setStatus(string str){
  28.             status = str;
  29.         }
  30.  
  31.         /* clear the previous screen and display new screen image with Xsize Ysize */
  32.         void Paint(){
  33.  
  34.             /* clear the screen */
  35.             system("clear");
  36.  
  37.             // and then display the screen with width and height
  38.             for(unsigned y = 0; y < Ysize; y++){
  39.                 for(unsigned x = 0; x < Xsize; x++){
  40.                     cout << myArray.Get(x , y);
  41.                 }
  42.             }
  43.         }
  44.  
  45.         void Beep(){
  46.             cout << '\a';
  47.         }
  48.         private:
  49.         unsigned Xsize;
  50.         unsigned Ysize;
  51.         Array2D<char> myArray;
  52.         string status;
  53.     };
  54. }
  55. #endif
  56.  
terrain.txt file:
Expand|Select|Wrap|Line Numbers
  1. +-----------------------+
  2. |                       |
  3. |              +-+--+ +-+
  4. |                |      |
  5. | +----+ +-------+      |
  6. | |      |              |
  7. | |  +---+    +         |
  8. | |           |         |
  9. | +-----+     +--+ +    |
  10. |                  |    |
  11. +------------------+----+
  12.  
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.
Dec 14 '06 #3
Ganon11
3,652 Expert 2GB
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'...
Dec 14 '06 #4
nickyeng
254 100+
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.
Dec 14 '06 #5
nickyeng
254 100+
if the terrain.txt file is like this:
Expand|Select|Wrap|Line Numbers
  1. +++++++++++++++++++++++++
  2. +++++++++++++++++++++++++
  3. +++++++++++++++++++++++++
  4. +++++++++++++++++++++++++
  5. +++++++++++++++++++++++++
  6. +++++++++++++++++++++++++
  7. +++++++++++++++++++++++++
  8. +++++++++++++++++++++++++
  9. +++++++++++++++++++++++++
  10. +++++++++++++++++++++++++
  11. +++++++++++++++++++++++++
  12.  
it will display to screen like this:

Dec 14 '06 #6
if the terrain.txt file is like this:
Expand|Select|Wrap|Line Numbers
  1. +++++++++++++++++++++++++
  2. +++++++++++++++++++++++++
  3. +++++++++++++++++++++++++
  4. +++++++++++++++++++++++++
  5. +++++++++++++++++++++++++
  6. +++++++++++++++++++++++++
  7. +++++++++++++++++++++++++
  8. +++++++++++++++++++++++++
  9. +++++++++++++++++++++++++
  10. +++++++++++++++++++++++++
  11. +++++++++++++++++++++++++
  12.  
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.
Dec 15 '06 #7
nickyeng
254 100+
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.
Expand|Select|Wrap|Line Numbers
  1.     for(unsigned y = 0; y < height; y++){
  2.                     for(unsigned x = 0; x < width; x++){
  3.  
  4.                         in.get(piece);
  5.                         ter_array.Put(x,y,piece);
  6.                     }
  7.                 }
  8.  
Dec 15 '06 #8
nickyeng
254 100+
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.

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char* argv[]){
  2.  
  3.         Terrain t(argv[1]);
  4.  
  5.         unsigned width = t.getWidth();
  6.         unsigned height = t.getHeight();
  7.  
  8.         Screen s(width, height);
  9.  
  10.         //t.Draw(s);
  11.         s.Paint();
  12.  
  13.     return 0;
  14. }
  15.  
Dec 15 '06 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

13
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...
0
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....
2
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...
11
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,...
11
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...
6
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);
1
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...
3
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...
0
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...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.