473,668 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

my display so weird...

254 Contributor
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 2206
nickyeng
254 Contributor
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 Contributor
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 Recognized Expert Specialist
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 Contributor
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 Contributor
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
montzter
15 New Member
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 Contributor
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 Contributor
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
3794
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 counter functionality and read/insert some data in a MS Access database on that Windows 2003 server. The weird part is, as long as the web page is very short in size, I can hit the refresh button and Internet Explorer will reload the page and display...
0
1429
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. From doing weird alignment things, to making it so that so long as those characters are visible, the display is always scrolled to a line where one of those screwy ASCII characters are. I can scroll up or down, but as soon as I type something on...
2
2575
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 7.51, even though it displays fine in Mozilla 1.6, Firefox 0.9, Netscape 7.1 and Internet Explorer 6.0 Hope someone can help!
11
13687
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, there's an "Edit" link. So the page itself looks something like this: <HTML><HEAD><TITLE>blah</TITLE></HEAD><BODY> <!-- TEXT STARTS HERE --> <H1>Hello World!</H1> <P>More stuff here...</P>
11
4625
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 serves as splash screen with the disclaimer of the application is shown. now the problem i am running into is in the display of the disclaimer which is really two page long and it should be formatted too. using a textbox seems not
6
1987
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
4524
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 want only one instance of the FCKEditor on the screen at one time so I make tabs that the user can click and I store the values in variables behind the scenes. I change the CSS class on the link that is the currently selected one. I have a really...
3
2393
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 class: (one I added all the asteriss to. // If we are at the end of input then NoSuchElement; // If there is still input left then InputMismatch private void throwFor() { skipped = false; if ((sourceClosed) &&...
0
1402
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 example codes below: my $graph = new GD::Graph::hbars( 400, 300 ); $graph->set( y_max_value => 100,
0
8378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.