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 2206
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.
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'...
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 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...
|
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...
|
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!
|
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>
|
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
| |
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 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...
|
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) &&...
|
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,
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |