• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

# superman3275

Member Since 08 Sep 2012
Offline Last Active Nov 24 2013 08:33 PM

### #5072552Want to build a game

Posted by on 24 June 2013 - 01:15 PM

Well, it's just text and a GUI. I'm sure its possible, just remember to build for networking from the ground up.

Cheers !

### #5051683Function Recursion

Posted by on 09 April 2013 - 07:11 PM

...snip...

The analogy you presented, the child can have more children, kind of confuses me. If I am using recursion, I am making another call to the method I am in, so does that not mean that I am simply going back and forth between 2 children?

The recursive function operates on each node, which calls the recursive function on all it's children, i.e:
if we
```  void Func(Node N){
for(int i=0;i<N.Children.Count;i++) Func(N.Children[i]);
return;
}
```
which allows you to operate on an indeterminable number of children.

>>>

```package mainPackage;

public class RecursionCoin {

final static int toonie = 6;

static int[] coin = {0,0,0,0};
static int turns = 0;

public static void main(String[] args) {
for(int i = 0; i < 4; i++){
coin[i] = 0 + (int)(Math.random() * ((6 - 0) + 1));
}
checkCoins(coin, turns);
System.out.println(turns);
}

private static void checkCoins(int[] coin, int turns2) {
turns = turns2 + 1;

for(int i = 0; i < 4; i++){
if(coin[i] != toonie){
coin[i] = 0 + (int)(Math.random() * ((6 - 0) + 1));
return checkCoins(coin, turns); //Once this function returns succesfully, you know that you are done.
//returning here allows all your recursive functions to escape, instead of each doing potentially more work.
}
}
}
}

```
Made a small change for doing unnecessary work.

That code has a side effect. You're returning a void method? What is the point of that. What you should do is say that once it returns successfully, you break out of the loop:

```checkCoins:
for(int i = 0; i < 4; i++) {
if(coin[i] != toonie) {
coin[i] = (int) (Math.random() * ((6-0) + 1));
return checkCoins(coin, turns);
break checkCoins;
}
}
```

This is a labeled break statment.

Once it returns successfully, you break out and avoid a side-effect of the method.

(Yes, I know it's an unimportant side effect that doesn't actually affect anything, however consistency !)

(I just want to point out that there are some non-elegant pieces of your code, such as using an int assigned to a random number to toonie. You could create a class with an state called isToonie

```class Coin {
bool isToonie = false;
}
```

and then have your code make more sense (instead of: if (coin[i] != toonie), we get: if (coins[index].isToonie))).

Cheers !

### #5051673Function Recursion

Posted by on 09 April 2013 - 06:49 PM

Considering that Juleian has described recursion, I'll describe loops.

Let's look at a Project Euler practice problem:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Okay, so we could use some recursion:

(Code is in JavaScript because JavaScript is basically universally easy to read for people who have a background in almost any standard programming language. That doesn't make it good, though ).

```function findSmallestNumber(currentNumber, currentFunctionCall, amountOfFactors) {
if(currentFunctionCall > amountOfFactors) {
return currentNumber;
} else if (currentNumber % currentFunctionCall === 0) {
++currentFunctionCall;
return findSmallestNumber(currentNumber, currentFunctionCall, amountOfFactors);
} else if (currentNumber % currentFunctionCall !== 0) {
++currentNumber;
currentFunctionCall = 1;
return findSmallestNumber(currentNumber, currentFunctionCall, amountOfFactors);
}
}

var amountOfFactors;
do {
amountOfFactors = prompt("How many factors are there (0 - ?)");
} while (amountOfFactors.isNaN);

console.log(findSmallestNumber(1, 1, amountOfFactors));
```

(What's awesome about learning recursion is that this makes sense )!

So, I tested this with 1-10 and it worked. However, try it with 1-20 (This link will bring you to a page with my code. Click the "run" button to see it in action!) You should get the error "Too much recursion". The compiler can't handle 25,000,000 function calls (I solved the code euler problem, obviously, so yeah, there's that many). So, we have to translate it into loops:

```function findSmallestNumber(amountOfFactors) {
var currentNumber = 1;
var currentFactor = 1;

while(currentFactor <= amountOfFactors) {
if(currentNumber % currentFactor === 0) {
++currentFactor;
continue;
} else if(currentNumber % currentFactor !== 0) {
++currentNumber;
currentFactor = 1;
continue;
}
}

return currentNumber;
}

var amountOfFactors = 0;
do {
amountOfFactors = prompt("How many factors (1 - ?)", "");
} while (amountOfFactors.isNaN);

console.log(findSmallestNumber(amountOfFactors));
```

This is the same function, using loops. We see that it works the same, however it will work for an (almost) infinite amount of number.

(One more thing, don't try the script using a while() loop with the value "20". It will cause your browser to freeze because Javascript really isn't meant for this Because it's an interpreted language and was created for changing HTML / CSS with minor logic ).

Cheers !

### #5048770triangle question

Posted by on 31 March 2013 - 07:57 PM

I actually added user input too ideone, however I removed it for simplicity ;).

Cheers !

### #5048757triangle question

Posted by on 31 March 2013 - 07:14 PM

I've updated my comment to include the full solution, with flowcharts, diagrams, equations, code, the full nine yards !

### #5048748triangle question

Posted by on 31 March 2013 - 06:56 PM

Phil67rpg, I have a question: Why not just solve the problem?

I mean this very seriously: In almost all of your posts, you ask a question which you could probably solve if you actually tried. Rather than trying, however, you always just try to Google the answer and then ask us for a "hint". You're never going to learn to code properly unless you bang your head against the wall enough to where you can actually solve problems. The whole idea of computer science is solving problems using a computer.

Also, if you really want the answer: Here is is ! (I highly recommend you solve the problem yourself. My code is uncommented about how the solution works for your benefit.

Cheers !

(Read below for a full solution.)

Okay, so look at this:

(- is a space)

---x

--xxx

-xxxxx

xxxxxxx

If we look at the above code, we notice that for every row of X's we decrease the amount of spaces on the left by one. If we know that, it's trivial to write a for loop which will print the necessary amount of spaces depending on what the first amount of spaces we printed was.

So how do we figure out the first amount of spaces (by first amount of spaces I mean the spaces before the first x printed)?

Well, if we look we notice that the first amount of spaces is the same amount on each row of the first x. So, all we need to do is figure out the "middle" of the amount of x's we print on the last line:

If we look at the above diagram, we notice that X1 and X2 are the same. Which means that the first amount of spaces is equal to one less than the maximum amount of x's we print divided by two:

So, we just need to find X3 (Which is equal to the amountOfLoops variable I created in the ideone code above), then apply the equation above, and plug the equation inside a for loop before we print x.

### #5043836Is it time to move on to c++?

Posted by on 16 March 2013 - 05:51 PM

EddieV, have you ever actually programmed in Python? How long?

### #5043490Pointers

Posted by on 15 March 2013 - 02:06 PM

A pointer is simply a variable which holds a memory address. Because of this, all pointers are the same size on your system. If you have a pointer to an object which is eight bytes and a pointer to a type of object which is four bytes, they both are the same size. Pointers are useful because they allow us to operate on the same data throughout our program. Here's an example:

```void add(int op1, int op2, int result) {
result = op1 + op2;
}
```

In this, we don't actually change the value of result. The function creates a copy of result whose value is changed to the sum of op1 and op2. What if we wanted to change the actual value of result? If we have the memory address of our original result variable, then we can change the value at the memory address (thus changing the value of result):

```void add(int op1, int op2, int * const result) {
*result = op1 + op2;
}
```

This changes the actual value of result for us. However, we now have new syntax. Some noticeable changes in syntax here are how we declare a pointer. To declare a pointer, we use the syntax:

```type * name;
```

We specify the type of variable name points to by changing type. You may be wondering why we have the dereference operator (*) in-between the type and name. This tells the compiler that we are creating a pointer. Later on, we will discuss the dereference operator. When we create a pointer and use (*), it is completely unrelated to the actual dereference operator.

To assign a pointer a memory address, we use the reference operator (&). This returns the memory address or the variable following it. Examine:

```int normalVariable = 5;
int * pointerVariable = &normalVariable;
```

Here, we create a normal integer variable (normalVariable). We then create a pointer to an integer (pointerVariable) and assign it to the memory address of normalVariable.

If we look back at the first pointer function example, we have this line of code:

```*result = op1 + op2;
```

This syntax may seem confusing at first. When we created a pointer, we used the dereference operator (*) to specify that a variable was a pointer. In reality, when we use (*) to create a pointer, it is completely unrelated to the real use of the dereference operator (*): to dereference. We use the reference operator (&) to return the address of a variable, and thus we use the dereference operator (*) to get and set the value of a variable. When we put the dereference operator (*) before a pointer, it means that we're returning the value of the pointer. This can also be used to set the value of the pointer, as seen above.

This means that in the code above, we're setting the value of result to the sum of op1 and op2.

Pointers also allow us to create memory on the heap (a large area of memory set aside for dynamic allocation). To do this, we use the new and delete operators. This part of pointers is very complicated, so I recommend you read about The Stack and the Heap along with Dynamic Allocation With Pointers.

You might now be wondering: When would I ever use pointers? This is a valid question, and many tutorials don't explain it well. So, I'll use an example which I deal with right now: Windows.

I use an API called SFML for game development. SFML is very object oriented, so you create a RenderWindow object. Many components of my game have to draw to the window. I can't pass the window by value, because then the components are drawing to a RenderWindow copy, located in separate memory. I use pointers so that they all have access to the same RenderWindow.

(You can PM me for the source code of my State Machine, which uses pointers so that every state can draw to the same window.)

Cheers !

### #5043477C++ String to a number

Posted by on 15 March 2013 - 01:04 PM

You can also use String-Streams!

Cheers !

(Sorry, OP was already using them!)

Posted by on 15 March 2013 - 11:25 AM

If you want to learn how to program or can program, look for game-development libraries in your language of choice. If you don't want to program, however you still want great (production-quality) games, you can either use Construct Two or Game-maker (There are many more, however those are some of the most popular). Whatever you do, just don't use Dark-Basic, however!

Some tips (for when you're developing):

• Make a good state machine. It will be worth it. There is a mound of articles on the subject (lazy foo is commonly linked to).
• Learn and utilize multiple paradigms. Don't stick to Object-Oriented Programming or Procedural programming for your whole career.
• A little object-oriented programming can go a long way.
• In games where you load maps, you're probably going to get an Access Violation error (if you use arrays or vectors to store the maps).
• Don't give up.
• Know when to give up (know when to refactor, start over, and leave a project).
• Have fun!

Cheers !

### #5043410Am I a bad programmer?

Posted by on 15 March 2013 - 09:36 AM

Thanks for responding. I'm just sticking to replies in hack forums right now, thanks.

### #5043282Am I a bad programmer?

Posted by on 15 March 2013 - 01:53 AM

I recently went on hackforums.net and started writing tutorials. Immediately, a user called Gamma started calling me a terrible programmer, pointing out many things in my tutorials (all in the first actually. Most of them were because he hated system("pause"); and visual studio's pre-compiled headers). Do you think I'm a bad programmer? Do you know of any ways I can improve my programming.

### #5041347Is my Finite-State Machine good or bug-prone?

Posted by on 09 March 2013 - 07:45 PM

I took it upon myself to program a finite state machine. After trying (and failing) with no help, I looked at the lazy-foo class setup (Not the actual code) and changed my design. What do you think? Is this error-prone or good?

State.h:

```#pragma once

namespace th {

class State
{
public:
State(void);
virtual ~State(void) = 0;

virtual void logic() = 0;
virtual void render() = 0;

virtual void setNextState(th::gameState nextState) = 0;
virtual th::gameState getNextState() = 0;

virtual sf::RenderWindow* getWindow() = 0;
protected:
sf::RenderWindow* window;
th::gameState currentState;
th::gameState nextState;
};

}

```

State.cpp:

```#include "StdAfx.h"
#include "State.h"

th::State::State(void)
{
}

th::State::~State(void)
{
}

```

The general idea is that for each state, I make a class which inherits from th::State, however the class has it's own variables and logic / rendering. I also have an enumeration for the states:

Global.h:

```#pragma once

namespace th
{

//GameState Enumeration
enum gameState {
null,
inlevelOne,
ingameOver,
ingameEnd
};

}
```

and the change_state function:

```void changeState(th::State* state) {
sf::RenderWindow* window;
th::gameState nextState;

window = state->getWindow();
nextState = state->getNextState();

//If the nextState is null, it's not time to switch the state yet
if(nextState != th::null) {

//If the game is ending, there's no point in switching the state. The game will end right after the call to render,
//which is placed after changeState() in the main loop. If I delete the state, the call to render will return an
//error (and there is no state to switch too from ingameEnd.
if(nextState != th::ingameEnd) {
delete state;

switch(nextState) {
break;
case th::inlevelOne:
state = new th::levelOne(*window);
break;
default:
state = new th::levelOne(*window);
break;

}
}
}
}
```

What do you think? Does it make sense? Do you see any problems?

So far, in one of the states I've implemented, I kept on getting an "access-violation" error in visual studio which occurred in changeState(), and at the end of changeState when I would change into the forementioned State, all of the values in the new State were corrupted. I believe this is simply from bad-initialization (Although it's taken me a few hours of debugging to figure it out).

It's

Cheers !

### #5040996How to aquire relevant data without iterating through vector containing all t...

Posted by on 08 March 2013 - 05:41 PM

I'm just going to say: One of the reasons you're not getting posts is your attitude. No one wants to help someone who starts out with:

No F**k you read my post

Or someone who says this (to a largely english speaking crowd):

The English language is inferior and nothing could be explained properly.

Because they couldn't grasp design patterns. You're essentially saying: "I read an advanced book I wasn't ready for, and now I'll blame my misunderstanding on how terrible English is."

### #5036585Any point Learning XNA?

Posted by on 25 February 2013 - 09:54 PM

Many people have been asking this exact question here on gamedev, and it's normally beginning developers. Here's what I am going to say:

XNA is not gone. It's a great framework that has very few bugs and can still be very useful. Companies (and people) "drop support" of API's, programming languages, and projects all the time. What it really means is: "We're developing new API's right now, and are currently not working on XNA". XNA still works for Windows, you can still download it, and it's still a great framework. They're just working on other API's (Probably HTML / JavaScript in Windows Eight).

Go for it. XNA is great, however I recommend another API which is still under development: MonoGame!

Have fun! Cheers !

PARTNERS