[QUOTE=thisBrad;33309490]My Professor said you should [B]never[/B] use the break statement, and you should always set the loop condition to false to break out of the loop.
I thought this was kind of bizarre because I've never found a problem with break. And setting the conditional to false would finish the current loop and could cause some errors or logic errors.
Is my professor correct?[/QUOTE]
Check out the A* algorithm. There's a point where you need to break out of a while loop without fulfilling the condition or else the algorithm will keep checking for something that's already been solved.
[QUOTE=thisBrad;33309490]My Professor said you should [B]never[/B] use the break statement[/QUOTE]
This is like those professors who say you should never use do-while, there's always a "better way". Usually, the reasoning behind these suggestions are purely for code-readability and there's nothing actually [i]wrong[/i] with using them.
Though I have met professors who grow to hate certain things that students don't understand. The professor starts to think the technique is bad, rather than the students. This is like one professor at my university who believes the best way to generate random numbers is to call srand(time(0)) before every rand() (obviously this won't work, but don't worry, he has a [del]clever[/del] solution). Before calling srand() he inserts a for loop that is suppose to iterate one billion times (or something), so that the program "stops" for a moment, allowing time(0) to give a different result.
And he teaches this shit to people. Disaster.
[QUOTE=jalb;33311597]This is like those professors who say you should never use do-while, there's always a "better way". Usually, the reasoning behind these suggestions are purely for code-readability and there's nothing actually [i]wrong[/i] with using them.
Though I have met professors who grow to hate certain things that students don't understand. The professor starts to think the technique is bad, rather than the students. This is like one professor at my university who believes the best way to generate random numbers is to call srand(time(0)) before every rand() (obviously this won't work, but don't worry, he has a [del]clever[/del] solution). Before calling srand() he inserts a for loop that is suppose to iterate one billion times (or something), so that the program "stops" for a moment, allowing time(0) to give a different result.
And he teaches this shit to people. Disaster.[/QUOTE]
That professor is either lazy or not creative. I can think of a couple better (and faster) ways of getting a better seed.
[QUOTE=jalb;33311597]This is like those professors who say you should never use do-while, there's always a "better way".[/QUOTE]
I would not agree with that there's always a "better way", for example the FindFirstFile/FindNextFile functions in WinAPI are pretty much made to be used in do-while loops, and I don't believe there is a cleaner way to use them without a do-while.
Does python 3 have an equivalent to lua's ... operator?
I assume you mean variadic functions/varargs.
[cpp]def f(*args, **kwargs):
print args
print kwargs
>>> f(1, 2, "cow", "kitty")
(1, 2, "cow", "kitty")
{}
>>> f(arg1=1, sample=2, name="cow", hero="kitty")
()
{"arg1": 1, "sample": 2, "name": "cow", "hero": "kitty"}
>>> f(1, 2, name="cow", hero="kitty")
(1, 2)
{"name": "cow", "hero": "kitty"}[/cpp]
([url=http://en.wikipedia.org/wiki/Variadic_function#Variadic_functions_in_Python]Wikipedia[/url] or [url=http://docs.python.org/release/3.1.3/tutorial/controlflow.html#arbitrary-argument-lists]official docs[/url].)
In the example args is a tuple containing all the (unnamed) extra parameters, kwargs is a dictionary of all named parameters.
[QUOTE=raBBish;33313087]I assume you mean variadic functions/varargs.
[cpp]def f(*args, **kwargs):
print args
print kwargs
>>> f(1, 2, "cow", "kitty")
(1, 2, "cow", "kitty")
{}
>>> f(arg1=1, sample=2, name="cow", hero="kitty")
()
{"arg1": 1, "sample": 2, "name": "cow", "hero": "kitty"}
>>> f(1, 2, name="cow", hero="kitty")
(1, 2)
{"name": "cow", "hero": "kitty"}[/cpp]
([url=http://en.wikipedia.org/wiki/Variadic_function#Variadic_functions_in_Python]Wikipedia[/url] or [url=http://docs.python.org/release/3.1.3/tutorial/controlflow.html#arbitrary-argument-lists]official docs[/url].)
In the example args is a tuple containing all the (unnamed) extra parameters, kwargs is a dictionary of all named parameters.[/QUOTE]
That's exactly what I was looking for, thanks.
[QUOTE=Niteshifter;33311205]Check out the A* algorithm. There's a point where you need to break out of a while loop without fulfilling the condition or else the algorithm will keep checking for something that's already been solved.[/QUOTE]
Well, you [I]could[/I] do something like this (and some people would just to avoid break):
[code]// ...
boolean someflag = true;
while (loopcondition && someflag) {
// Some code ...
if (solvedcondition) {
someflag = false;
}
if (!someflag) {
// Some more code ...
}
}
// ...
[/code]
But it's pretty fucking redundant.
Just use break or goto when they increase readability and avoid them when they don't. Silly professor.
Could someone help me with my A* (in C) please? I struggle to make it work. [URL="http://pastebin.com/XN2UA7sM"]Here's[/URL] my code, please don't lecture me about how efficient/inefficient it would be, I would just be glad if it finally worked. :<
EDIT:
Or it's okay if someone just gives me a proper pseudo code.
EDIT2:
[URL="http://pastebin.com/Q3zJuxGF"]Here's[/URL] an updated one, it has problems if I uncomment line 133, but otherwise it works "okay" without obstacles. Damn.
trying to use SFML to load images and to use with OpenGL textures, but when I draw the quad its just black.
I know the UV is right so im pretty sure its the texture its self.
[cpp]
#include "./3dSprite.hpp"
#include "./Engine.hpp"
d3Sprite::d3Sprite(Engine *mEngine)
{
engine = mEngine;
shader = new Shader("./Data/vert.vert" ,"./Data/frag.frag");
UVList[0] = 1.0f;
UVList[1] = 1.0f;
UVList[2] = 0.0f;
UVList[3] = 1.0f;
UVList[4] = 0.0f;
UVList[5] = 0.0f;
UVList[6] = 1.0f;
UVList[7] = 1.0f;
UVList[8] = 0.0f;
UVList[9] = 0.0f;
UVList[10] = 1.0f;
UVList[11] = 0.0f;
vertexList[0] = 0.5f;
vertexList[1] = 1.0f;
vertexList[2] = 0.0f;
vertexList[3] = -0.5f;
vertexList[4] = 1.0f;
vertexList[5] = 0.0f;
vertexList[6] = -0.5f;
vertexList[7] = 0.0f;
vertexList[8] = 0.0f;
vertexList[9] = 0.5f;
vertexList[10] = 1.0f;
vertexList[11] = 0.0f;
vertexList[12] = -0.5f;
vertexList[13] = 0.0f;
vertexList[14] = 0.0f;
vertexList[15] = 0.5f;
vertexList[16] = 0.0f;
vertexList[17] = 0.0f;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexList), vertexList, GL_STATIC_DRAW);
MatrixID = glGetUniformLocation(shader->shaderID, "MVP");
positionID = glGetAttribLocation(shader->shaderID, "Position");
TextureID = glGetUniformLocation(shader->shaderID, "myTexture");
UVID = glGetAttribLocation(shader->shaderID, "UV");
std::cout << MatrixID << ":" << positionID << ":" << TextureID << ":" << UVID << std::endl;
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(UVList), UVList, GL_STATIC_DRAW);
glGenTextures(1, &texturebuffer);
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, texturebuffer);
// Give the image to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, TextureResourceManager::textureResourceManager.getFile("./Data/Pedro.png").GetWidth(), TextureResourceManager::textureResourceManager.getFile("./Data/Pedro.png").GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,TextureResourceManager::textureResourceManager.getFile("./Data/Pedro.png").CopyToImage().GetPixelsPtr() );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
d3Sprite::~d3Sprite()
{
// Cleanup VBO and shader
glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &uvbuffer);
glDeleteTextures(1, &texturebuffer);
delete shader;
}
void d3Sprite::Initiate(glm::mat4 *persMat, glm::mat4 *camMat)
{
*camMat = glm::lookAt(
glm::vec3( 0, 0, 5 ), // Camera is here glm::vec3( 0, 0, 5 );
glm::vec3( 0, 0, 0 ), // and looks here : at the same position, plus "direction"
glm::vec3( 0, 1, 0 ) // Head is up (set to 0,-1,0 to look upside-down)
);
Drawable::Initiate(persMat, camMat);
}
void d3Sprite::Update(float deltaTime)
{
Drawable::Update();
}
void d3Sprite::Draw()
{
glUseProgram(shader->shaderID);
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texturebuffer);
glUniform1i(TextureID, 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(positionID);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
positionID, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(UVID);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(
UVID, // attribute. No particular reason for 1, but must match the layout in the shader.
2, // size : U+V => 2
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
// For each model you render, since the MVP will be different (at least the M part)
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 6); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(UVID);
glDisableVertexAttribArray(positionID);
}[/cpp]
[QUOTE=SFArial;33317354]Could someone help me with my A* (in C) please? I struggle to make it work. [URL="http://pastebin.com/XN2UA7sM"]Here's[/URL] my code, please don't lecture me about how efficient/inefficient it would be, I would just be glad if it finally worked. :<
EDIT:
Or it's okay if someone just gives me a proper pseudo code.
EDIT2:
[URL="http://pastebin.com/Q3zJuxGF"]Here's[/URL] an updated one, it has problems if I uncomment line 133, but otherwise it works "okay" without obstacles. Damn.[/QUOTE]
Set the G score before you check if it's in the open list (line 122). When checking if you found a better path (line 133), the g score is still 0.
Would someone from the US that has or is pursuing a degree in Computer Science kindly message me or add me on steam? I have... quite a few questions regarding requirements and suggestions, and considering I'll be going to college in a little over a year and a half, I thought I'd get a head start.
Why not ask here? (I have a CS degree in the US, btw.)
[QUOTE=Niteshifter;33323048]Set the G score before you check if it's in the open list (line 122). When checking if you found a better path (line 133), the g score is still 0.[/QUOTE]
I managed to fix it, the problem was the way I compared two G scores and I was also setting the new score to the wrong node.
[URL="http://pastebin.com/nydKHHxG"]Here's[/URL] the new code, now my problem is tracing the path back. I know I'm supposed to just follow the parents back but for some reason at one point the parent doesn't get set to what it should be, the debug thing I wrote clearly shows this if you run the code:
[code]
PATH!
y:8 x:8, py:8 px:7
y:8 x:7, py:8 px:6
y:8 x:6, py:8 px:5
y:8 x:5, py:8 px:4
y:8 x:4, py:8 px:3
y:7 x:3, py:-1 px:-1
[/code]
It's like, what.. why is the current node set to (7,3) when it clearly should be (8,3)?
EDIT:
Fuck, found the problem, check this out and see if you can see it:
[code]
cur.x = nodes[cur.y][cur.x].px;
cur.y = nodes[cur.y][cur.x].py;
[/code]
I'm not entirely sure if this was the right forum/thread to post this, but I thought it was the best choice.
I was trying to install the latest Visual Studio C# Express (since I'm a cheap student who can't afford the real version), but when it's "...Loading installation components" it have been stuck there for like 20 minutes without any progress. I tried google it but nothing really related came up, maybe I fail(ed) at googling but, at least I tried. So do any of you experienced folk know how to solve this?
OS: Windows 7 x64.
Long story short, I have a function called tick() for the Player class in my game, but it uses methods from a SFML RenderWindow that I create in the main.cpp file. Here's the tick() function in player.cpp:
[cpp]void Player::tick()
{
//////////////////////
// Player Movement //
////////////////////
// Get elapsed time
float ElapsedTime = App.GetFrameTime();
if (App.GetInput().IsKeyDown(sf::Key::Left)) spr_pship.Move(-100 * ElapsedTime * p_speed, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) spr_pship.Move( 100 * ElapsedTime * p_speed, 0);
if (App.GetInput().IsKeyDown(sf::Key::Up)) spr_pship.Move(0, -100 * ElapsedTime * p_speed);
if (App.GetInput().IsKeyDown(sf::Key::Down)) spr_pship.Move(0, 100 * ElapsedTime * p_speed);
/////////////////////
// Player Drawing //
///////////////////
App.Draw(spr_pship);
}[/cpp]
And I run this to create my RenderWindow inside of main.cpp:
[cpp]sf::RenderWindow App(sf::VideoMode(1280, 720, 32), "Bullet Fusion");
App.SetFramerateLimit(60);[/cpp]
The problem is that when I try to compile the code, it shouts at me that App is not declared in this scope. I knew that was gonna happen in the first place, but I didn't know what to do about it, so my question is: how can I use the App from main.cpp in my classes over at player.cpp?
[QUOTE=ZeekyHBomb;33327724][cpp]void tick(sf::RenderWindow &App);
Player.tick(App);[/cpp]
[url]http://en.wikipedia.org/wiki/Reference_(C%2B%2B)[/url][/QUOTE]
Oh god we just keep running into eachother don't we. Well that fixes [i]that[/i] problem, but now I have another question: how can I access Player's sprites and images within the tick function? All the sprites and images I loaded and declared in Player::Player() basically vanish, and I need them to update the player sprite's depending on if it's turning left or right, going forward or backwards and etc.
[QUOTE=Samuka97;33328190]Oh god we just keep running into eachother don't we. Well that fixes [I]that[/I] problem, but now I have another question: how can I access Player's sprites and images within the tick function? All the sprites and images I loaded and declared in Player::Player() basically vanish, and I need them to update the player sprite's depending on if it's turning left or right, going forward or backwards and etc.[/QUOTE]
Are you declaring it as
[cpp]
void Player::tick(sf::RenderWindow& App);
[/cpp]
?
Okay nevermind, I still need help with my A*. Code is over [URL="http://pastebin.com/YyQaZsXs"]here[/URL], line 142 where I'm calculating if we have a better path (using G cost only).
It no longer gets stuck in an infinite loop, here's a fine working example (# = wall, 1 = start, 2 = goal, + = path):
[code]
####################
#1++++++ ## 2#
# +## + #
# +## + #
# +## + #
# +## + #
# +## + #
# +## + #
# +++ #
####################
[/code]
But if I give it something more tricky then it's just bad:
[code]
####################
#1++ # #
# +############ #
# + # #
# + # #
# + # #
# +# # #
# +############ #
# ++++++++++++2 #
####################
[/code]
[QUOTE=SFArial;33329455]Okay nevermind, I still need help with my A*. Code is over [URL="http://pastebin.com/YyQaZsXs"]here[/URL], line 142 where I'm calculating if we have a better path (using G cost only).
It no longer gets stuck in an infinite loop, here's a fine working example (# = wall, 1 = start, 2 = goal, + = path):
[code]
####################
#1++++++ ## 2#
# +## + #
# +## + #
# +## + #
# +## + #
# +## + #
# +## + #
# +++ #
####################
[/code]
But if I give it something more tricky then it's just bad:
[code]
####################
#1++ # #
# +############ #
# + # #
# + # #
# + # #
# +# # #
# +############ #
# ++++++++++++2 #
####################
[/code][/QUOTE]
That's pretty much expected and has to do with the g cost. Once you introduce the heuristic, there's pretty much a trade-off between speed and accuracy depending on the heuristic used, weights, etc. [url=http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html]This page[/url] is a great source that explains the problem more in-depth.
[QUOTE=Samuka97;33328190]Oh god we just keep running into eachother don't we. Well that fixes [i]that[/i] problem, but now I have another question: how can I access Player's sprites and images within the tick function? All the sprites and images I loaded and declared in Player::Player() basically vanish, and I need them to update the player sprite's depending on if it's turning left or right, going forward or backwards and etc.[/QUOTE]
Vanish? You're not declaring them as locals, are you?
I need help with my C/C++ file i/o code. [url]http://pastebin.com/PpZL3bhe[/url]
It just freezes at while(currentChar != '\n');
Never reaches the first line of the loop, nor does it ever exit it. It just hangs there.
Input file: [url]http://pastebin.com/jx4ZxNUj[/url]
Note: there is no third line in the file, pastebin just adds it for some reason.
[editline]18th November 2011[/editline]
The current char is '4', which is not '\n', so it should run, right?
[QUOTE=DeadKiller987;33330362]I need help with my C/C++ file i/o code. [url]http://pastebin.com/PpZL3bhe[/url]
It just freezes at while(currentChar != '\n');
Never reaches the first line of the loop, nor does it ever exit it. It just hangs there.
Input file: [url]http://pastebin.com/jx4ZxNUj[/url]
Note: there is no third line in the file, pastebin just adds it for some reason.
[editline]18th November 2011[/editline]
The current char is '4', which is not '\n', so it should run, right?[/QUOTE]
According to [url]http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/[/url], fgetc(FILE *stream) returns int. Not sure if this is related to your problem but it's worth a shot.
[QUOTE=Niteshifter;33329672]That's pretty much expected and has to do with the g cost. Once you introduce the heuristic, there's pretty much a trade-off between speed and accuracy depending on the heuristic used, weights, etc. [url=http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html]This page[/url] is a great source that explains the problem more in-depth.[/QUOTE]
Oh okay great, I set my heuristic to 0 to see how it works and it does seem to give a best possible path! So it's essentially a Best-First Search now or whatever it was called, right? I'm good with this, I wanted 100% accuracy as I'm not running this per frame.
One more thing, do you have any ideas how to stop cutting across corners? I find that rather ugly.
[QUOTE=thisBrad;33309490]My Professor said you should [B]never[/B] use the break statement, and you should always set the loop condition to false to break out of the loop.
I thought this was kind of bizarre because I've never found a problem with break. And setting the conditional to false would finish the current loop and could cause some errors or logic errors.
Is my professor correct?[/QUOTE]
Either you've mis-interpreted his statement or your professor is a retard.
[QUOTE=DeadKiller987;33330362]I need help with my C/C++ file i/o code. [url]http://pastebin.com/PpZL3bhe[/url]
It just freezes at while(currentChar != '\n');
Never reaches the first line of the loop, nor does it ever exit it. It just hangs there.
Input file: [url]http://pastebin.com/jx4ZxNUj[/url]
Note: there is no third line in the file, pastebin just adds it for some reason.
[editline]18th November 2011[/editline]
The current char is '4', which is not '\n', so it should run, right?[/QUOTE]
while(currentChar != '\n')[b];[/b]
There's an easy to miss error :P
Like it was already suggested, the int -> char conversion could be an issue. It is not in this case, but I think it is when checking for EOF since iirc EOF is an int, so it could easily be unrepresentable as a char; but like I said, I'm not completely certain of this.
Also, any reason to use stdio over the C++ equivalents?
[QUOTE=vexx21322;33312985]Does python 3 have an equivalent to lua's ... operator?[/QUOTE]
[url]http://en.wikipedia.org/wiki/Variadic_function#Variadic_functions_in_Python[/url]
[QUOTE=SFArial;33330686]
One more thing, do you have any ideas how to stop cutting across corners? I find that rather ugly.[/QUOTE]
You'll need to have it check if the sides that are adjacent to the corners are free and if they are, then you can move to that location ie: if the top square is free and the left square is free, then the top-left square can be moved to (providing it's also free).
[QUOTE=ZeekyHBomb;33330753]while(currentChar != '\n')[b];[/b]
There's an easy to miss error :P
Like it was already suggested, the int -> char conversion could be an issue. It is not in this case, but I think it is when checking for EOF since iirc EOF is an int, so it could easily be unrepresentable as a char; but like I said, I'm not completely certain of this.
Also, any reason to use stdio over the C++ equivalents?[/QUOTE]
Fuck. I'm retarded. Spent hours since last night trying to debug this :C
I'm using stdio because it seems easier and for now I just need simple i/o.
I might just switch over to plain C. Does VS work well as a C IDE? If not, any recommendations?
Sorry, you need to Log In to post a reply to this thread.