• 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

Program that prints out array is not working

7 posts in this topic

[code]
#include <iostream>
using namespace std;
int map[5][5] =
{
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};
//other stuff
int mapn = 0;
int mapn2 = 0;
int number = 0;
int number2 = 0;
int x, y = map[x][y];
int main()
{
x = map[2][y];
y = map[x][2];
while (mapn < 5, mapn2 < 5)
{
cout << map[number][number2];
mapn++;
number++;
if (mapn == 5)
{
mapn2++;
number2++;
mapn = 0;
cout << endl;
}
}
cout << endl;
system("PAUSE");
}
[/code]

This program is supposed to print out an array. To do so, it prints out a number, adds onto that number and once that number equals five, number2 has 1 added onto it, and number is reset. Why isn't it working?
It compiles fine, but the output is a lot of random numbers. :(
0

Share this post


Link to post
Share on other sites
number isn't reset. Is there any particular reason you declared number and number2 instead of using mapn and mapn2? Also, you should probably be using nested for loops for going through a 2D array, and "mapn < 5, mapn2 < 5" probably doesn't do what you think it does (the comma operator is simply equal to the second expression, so that code is equivalent to mapn2 < 5)
2

Share this post


Link to post
Share on other sites
[spoiler][s]Ah thanks, number isn't reset! That solved the problem! Thanks for pointing that out.

Well, now I'm trying to create a movement script for an ingame character:
[code]
prev = map[x][y];
cout << "Where would you like to go?" << endl;
cin >> input;
if (input == 8)
{
y--;
x, y == 2;
prev == 0;
goto map;
mapn = 0;
mapn2 = 0;
number = 0;
number2 = 0;
}
[/code]

But it seems to just automatically skip the loop again, despite it being reset...
Is there a way to fix this?

Sorry, but I'm just really new to C++ so my code probably looks extremely bad.[/spoiler][/s]

Nvm Edited by Youbar
0

Share this post


Link to post
Share on other sites
Just an FYI "goto" is typically frowned upon by programmers and usually considered bad practice and harmful, just search "Edsger Dijkstra on goto".

Example for loop for cycling this kind of 2d array:
[code]
for (int x = 0; x < 5; x++) {
// In here you can do something with just x in the map
// int somethingElse = map[x][0];
for (int y = 0; y < 5; y++) {
int something = map[x][y];
}
}
[/code] Edited by wicked357
1

Share this post


Link to post
Share on other sites
Seriously... where are new developers even being exposed to goto anymore?!?!?

I mean, it should have been relegated to complete niche status within the language by this point. I dont think it is even featured in most C++ books, so how the heck people even learn about it.
0

Share this post


Link to post
Share on other sites
[quote name='Youbar' timestamp='1350359411' post='4990604']
int x, y = map[x][y];
[/quote]
I'm really curious what happens here. x is declared as an int without being defined, and y is declared as an int defined as map[x][y]...that's a recursive definition on top of an undefined index? It's been a while since I mucked with C++.
I guess it doesn't matter since x and y are unused after their first assignment in main. But I don't see how that's being resolved by the compiler.
0

Share this post


Link to post
Share on other sites
[quote name='BCullis' timestamp='1350405737' post='4990791']
[quote name='Youbar' timestamp='1350359411' post='4990604']
int x, y = map[x][y];
[/quote]
I'm really curious what happens here. x is declared as an int without being defined, and y is declared as an int defined as map[x][y]...that's a recursive definition on top of an undefined index? It's been a while since I mucked with C++.
I guess it doesn't matter since x and y are unused after their first assignment in main. But I don't see how that's being resolved by the compiler.
[/quote]
Unless I am missing something, the array is improperly populated.
Both X and Y are declared as NULL
After MAIN they are both declared as map[2][2], which are both NULL
Also as it's written the values of X,Y will not update when the map is updated.

Corrected 2D array declaration.
[CODE]
int map[5][5] =
{
{1, 1, 1, 1, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 1, 1, 1, 1}
};
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='Shippou' timestamp='1350416954' post='4990858']
[quote name='BCullis' timestamp='1350405737' post='4990791']
[quote name='Youbar' timestamp='1350359411' post='4990604']
int x, y = map[x][y];
[/quote]
I'm really curious what happens here. x is declared as an int without being defined, and y is declared as an int defined as map[x][y]...that's a recursive definition on top of an undefined index? It's been a while since I mucked with C++.
I guess it doesn't matter since x and y are unused after their first assignment in main. But I don't see how that's being resolved by the compiler.
[/quote]
Unless I am missing something, the array is improperly populated.
Both X and Y are declared as NULL
After MAIN they are both declared as map[2][2], which are both NULL
Also as it's written the values of X,Y will not update when the map is updated.

Corrected 2D array declaration.
[CODE]
int map[5][5] =
{
{1, 1, 1, 1, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 1, 1, 1, 1}
};
[/CODE]
[/quote]

Actually, you can populate a 2D array that way. Both are functionally equivalent.

I am rather surprised that the code does compile though... there is a lot going on there that would require good knowledge of the spec to figure out what would happen.

Interesting lines are the aforementioned:
[code]
int x, y = map[x][y];
[/code]

Which my brain is strugging to grok. I am kinda surprised that compiles. I believed C++ evaluated right to left, so it would encounter the y and x indexes that are undeclared.

The other fun line is:
while (mapn < 5, mapn2 < 5)

Which if my understand of C++ is correct, actually evaluates to
while(mapn2 < 5)

In that it would evaluate the first expression, throw it away, then evaluate the second expression as the condition of the loop. Edited by Serapth
0

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