• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Youbar

(C++) Printing Out 2d Array

17 posts in this topic

[CODE]

int map[5][5] =
{
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 2, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};

int column = 0;
int column2 = 0;

()void print_map()
void print_map()
{
for (column = 0; column < 6; column++)
{
cout << map[column][column2];
if (column == 5)
{
column2++;
column = 0;
cout << endl;
}
if (column && column2 == 5)
{
break;
}
}
}
[/CODE]
This is my code for printing out a 2d array.
What happens in the output though, is unexpected. It produces:

111110
00010
02010
00010
11110
1

Instead of:
11111
10001
10201
10001
11111

1. How can I fix this problem?
2. Can you call the an [b]if[/b] command an exception?
3. Please don't post the fixed code. Just give me hints, unless the code is beyond fixing.
1

Share this post


Link to post
Share on other sites
What's with the [CODE]column < 6[/CODE] ? There are only 5 columns, so it should be [CODE]column < 5[/CODE]. Remember, the index of the first element of an array is zero, and the index of the last element is number of elements - 1.
0

Share this post


Link to post
Share on other sites
That's to keep the loop going on. It's broken when Column and Column2 are both equal to 5.

[CODE]if (column && column2 == 5)
{
break;
}[/CODE]
0

Share this post


Link to post
Share on other sites
By the looks of it, the code would print the array diagonally.
Can you please explain how that loop would work? Edited by Youbar
0

Share this post


Link to post
Share on other sites
What I mean, is how does that print out all of the array? It looks as if it would just print out the array like this:
1
0
2
0
1
0

Share this post


Link to post
Share on other sites
To expand on what Alvaro said, nested loops do not run concurrently. The "column" for loop completes before control is given back to the "row" for loop. Another way of looking at that code snippet is:

[source]
for (row = 0; row < 5; ++row) {
cout << map[row][0] << ' '; // print cell data at [row][0], followed by a space.
cout << map[row][1] << ' '; // print cell data at [row][1], followed by a space.
cout << map[row][2] << ' '; // print cell data at [row][2], followed by a space.
cout << map[row][3] << ' '; // print cell data at [row][3], followed by a space.
cout << map[row][4] << ' '; // print cell data at [row][4], followed by a space.
cout << '\n'; // print a new line character
}
[/source]

The five columns for the first row (row 0) are printed in sequence, followed by a new line character and then "row" is incremented by one and the next five columns for that row are printed and so on. Edited by MarkS
2

Share this post


Link to post
Share on other sites
Oh, I understand now, thanks. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

[b]EDIT:[/b]
I'm getting an error in this code:

[CODE]
#include <iostream>
using namespace std;
int map[5][5] =
{
1, 1, 3, 1, 1,
1, 0, 0, 0, 1,
1, 0, 2, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};
int column = 0;
int row = 0;
int x = 2;
int y = 2;
char dir = 'x';
bool game_ended = false;
void stop()
{
cout << endl;
system("PAUSE");
}
void cls()
{
system("CLS");
}
void print_map()
{
for (row = 0; row < 5; ++row)
{
for (column = 0; column < 5; ++column)
{
cout << map[row][column] << ' ';
}
cout << endl;
}
}
void redraw()
{
cls();
print_map();
}
int main()
{
print_map();
cout << "What direction would you like to go in?" << endl;
cin >> dir;
while (game_ended != true)
{
if (dir == 'w')
{
map[x][y] = 0;
x--;
map[x][y] = 2;
redraw();
}
}
stop();
}
[/CODE]

I get an error that says:

[b]Unhandled exception at 0x00411ced in Turn-based RPG.exe: 0xC0000005: Access violation writing location 0x0041aff4.[/b]

I am guessing this is due to the array. How would I fix this? Edited by Youbar
0

Share this post


Link to post
Share on other sites
Glad I could help.

[edit]

The error you're getting is because you start "x" at 2 and deincrement it without any form of control to keep it from dropping bellow 0.

You need to do bounds checking. "x" and "y" are bounded to 0..4, inclusive.

[source]
while (game_ended != true)
{
if (dir == 'w')
{
map[x][y] = 0;
if(x > 0) // Only deincrement 'x' if x > 0. If x == 0, "x--;" is skipped and the array doesn't go out of bounds.
{
x--;
map[x][y] = 2;
}
redraw();
}
if (dir == 'e') // I'm making an assumption here to make a point....
{
map[x][y] = 0;
if(x < 4) // Only increment 'x' if x < 4. If x == 4, "x++;" is skipped and the array doesn't go out of bounds.
{
x++;
map[x][y] = 2;
}
redraw();
}
}
[/source] Edited by MarkS
2

Share this post


Link to post
Share on other sites
If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".[img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
1

Share this post


Link to post
Share on other sites
[quote name='MarkS' timestamp='1354902146' post='5008178']
If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".[img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
[/quote]
But that would require casting to int* first, otherwise it will access the row (y * 5) + x.
0

Share this post


Link to post
Share on other sites
How would those lines work?
map[(y * 5) + x] = ...
seems like y is pointing to 5, which would just slow things down by the looks of it.
And:
for (int i=0; i<25; ++i) std::cout << i[*map] << " \n"[i%5];
How does i[*map] work?
i points to map?
0

Share this post


Link to post
Share on other sites
Unless operator[] has been overridden, a[b] is really short-hand notation for *(a+b). Typically a is a pointer and b is an integer, but there is nothing preventing you from reversing the order. Now *map is a pointer to the first row of the matrix, and I am abusing it to access the whole matrix.

The " \n"[i%5] is a bit more straight forward: It evaluates to a space unless i%5==4, which is when you need to print a '\n'. So it separates elements in a row and prints new-line characters all in one little expression.
1

Share this post


Link to post
Share on other sites
[quote name='ultramailman' timestamp='1354913962' post='5008227']
[quote name='MarkS' timestamp='1354902146' post='5008178']
If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".[img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
[/quote]
But that would require casting to int* first, otherwise it will access the row (y * 5) + x.
[/quote]

DOH! And there I was trying to look smart...[img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img]
0

Share this post


Link to post
Share on other sites
[quote name='MarkS' timestamp='1354983238' post='5008514']
[quote name='ultramailman' timestamp='1354913962' post='5008227']
[quote name='MarkS' timestamp='1354902146' post='5008178']
If you really want to fry your noodle, map can also be referenced as "map[(y * 5) + x] = ...".[img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
[/quote]
But that would require casting to int* first, otherwise it will access the row (y * 5) + x.
[/quote]

DOH! And there I was trying to look smart...[img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img]
[/quote]
Heh, don't we all? My first post in this thread was also a failed attempt :o
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0