Jump to content

  • Log In with Google      Sign In   
  • Create Account

Álvaro

Member Since 07 Mar 2002
Online Last Active Today, 03:07 PM

#5138283 Function with specific characteristics

Posted by on 11 March 2014 - 08:10 PM

Take some 1D noise function with a range that includes 0, then take the maximum of that function and 0. Now compute the integral of this function. I believe that should fit the bill.


#5138182 Difference between multiprocessing and multiprocessor

Posted by on 11 March 2014 - 01:37 PM

The definition from britannica.com seems bogus to me. In particular, it requires the parts of the code to be different. What if all the CPUs run the same part of the program?

 

In any case, unless you are writing an article for publication or something like that, I wouldn't worry about the precise definition: In context the meaning is usually clear.




#5138090 Classes and Objects

Posted by on 11 March 2014 - 07:08 AM

I am not going to read a whole bunch of beginner's code just for fun, but I did look at your Main.cpp. The obvious thing that can be improved is readability. You have a bunch of comments that would not be needed if your variables had descriptive names. So instead of
        //Temporary Name Holder
        string x;
        //Choice
        int c;
        //Random Monster Selection
        int v;
        //Temporary damage
        int attack;
you could have
  string tmp_name_holder;
  int choice;
  int random_monster_selection;
  int tmp_damage_from_attack;
More things:
* It's best to declare variables at the first point where they are needed. That will provide the reader the context to understand what the descriptive name means. And yes, you should think of your code as something that people read.
* Don't repeat yourself. There are 4 nearly-identical blocks in the code that handles attacking a monster. Those should only be one block that can work with any of the monsters. This would be easier to do if the monsters were contained in an array instead of being 4 separate variables.
* Including a file called "Includes.h" is a generally a bad idea. It won't matter for a program of this size, but the point of this exercise is learning good practices. So think of what dependencies you really have and include those. Otherwise, all of your code will depend on all of your other code and you will have to recompile all of your code whenever anything changes. In my project at work, full recompilation takes 15 minutes, and that's with a parallel compilation that devotes 10 processes to it.

OK, I did look through some of the other files. Your classes are mostly just holders for data, which probably means you are not thinking in terms of what operations you want to support on them, kind of missing the point of objects and classes. Also, some of your methods have surprising semantics:
void playerClass::setGold(int gold)
{
        playerClass::gold += gold;
}
Why does `setGold' add to the amount of gold instead of setting it to the value I just passed? How about calling it `addGold' instead?


#5137432 How does your Pseudocode look?

Posted by on 08 March 2014 - 04:45 PM

I sometimes write down the name of a function and a breakdown of the task into steps, described in English. If I do it on the computer, these descriptions might end up being comments. But I often do this on paper, as a tool to put my thoughts in order, or to convince myself that a certain approach will work. When things are clear in my head, I throw the piece of paper away and get on with coding.


#5137253 Reversing a procedural generation

Posted by on 07 March 2014 - 09:11 PM

You can also use a more straight-forward compression library, without the hack of going through an image. Start with something like zlib.


#5137040 Reversing a procedural generation

Posted by on 06 March 2014 - 07:26 PM

You can design a PRNG that is easily reversible, by using just about any private-key cryptographic method (any block cipher would do).

Or you can just perform a sequence of reversible operations on sets of 64 bits, like
* multiply by an odd constant
* add a constant
* XOR with a constant
* rotate the bits (e.g., `seed = (seed << 24) ^ (seed >> 40);')

A long enough sequence of these operations will hash the seed into being completely unrecognizable, and that can be used as a PRNG. The inverses of these operations are relatively straight-forward, and that allows you to reverse the process and compute the seed from a result.

However, I think we all understood your situation perfectly well and you should re-read what we posted.


#5136945 Reversing a procedural generation

Posted by on 06 March 2014 - 02:40 PM

I don't see what your last paragraph has to do with the rest of the post. If the player collects a certain amount of mineral deposits, you better write that down somewhere so the next time you generate this planet you can remove the appropriate amount of mineral resources. The information of whatever the player has done to the environment can't be magically folded into a new seed for the PRNG, in particular because the seed is probably not long enough to encode all the possible things the player might have done to the planet.




#5136429 Different fibonacci function

Posted by on 04 March 2014 - 07:34 PM

Since someone resurrected the thread, here's yet another implementation, using the GMP library:
#include <iostream>
#include <gmpxx.h>

typedef mpq_class Q;

// Q5 represents elements of Q[sqrt(5)], which are of the form a+b*sqrt(5), where a and b are rationals
struct Q5 {
  Q a, b;
  
  Q5(Q a, Q b = 0) : a(a), b(b) {
  }
};

std::ostream &operator<<(std::ostream &os, Q5 x) {
  if (x.a == 0 && x.b != 0) {
    os << x.b << "*sqrt(5)";
  }
  else {
    os << x.a;
    if (x.b > 0)
      os << '+' << x.b << "*sqrt(5)";
    else if (x.b < 0)
      os << '-' << -x.b << "*sqrt(5)";
  }
  
  return os;
}

Q5 operator+(Q5 x, Q5 y) {
  return Q5(x.a + y.a, x.b + y.b);
}

Q5 operator-(Q5 x, Q5 y) {
  return Q5(x.a - y.a, x.b - y.b);
}

Q5 operator*(Q5 x, Q5 y) {
  return Q5(x.a * y.a + 5 * x.b * y.b, x.a * y.b + x.b * y.a);
}

template <typename T>
T pow(T t, unsigned n) {
  return n == 0 ? T(1) : n % 2 ? t * pow(t, n - 1) : pow(t * t, n / 2);
}

int main() {
  Q5 Phi1(Q(1)/2, Q(1)/2);
  Q5 Phi2(Q(1)/2, -Q(1)/2);
  
  for (int n=0; n<100; ++n)
    std::cout << (Q5(0, Q(1)/5) * (pow(Phi1, n) - pow(Phi2, n))) << '\n';
}



#5135792 Logic (That's what programming is centered around right?)

Posted by on 01 March 2014 - 11:25 PM

Premise 1: All reasoning requires logic.
 
Premise 2: Programming a computer requires reasoning.
 
Conclusion: Computer programming requires logic.


The problem is that premise 1 is false.

Logic is to reason as grammar is to language. And I wouldn't say studying grammar is a prerequisite for speaking.


#5135673 pathfinding - making turning cost (A-star algorithm)

Posted by on 01 March 2014 - 11:12 AM

The straight-forward way to do this is to consider a node in the graph to mean a position on the map together with a heading. A node is connected only to the nodes corresponding to turning left or right (same position on the map, change the heading) and moving forward (different position on the map, same heading). However, this means multiplying the number of nodes by 8...


#5135556 Strange behavior of operator type

Posted by on 28 February 2014 - 08:48 PM

And before you start to explain what i should and what i shouldn't do in code, i know this, i have more than 13 years professional c++ experience, and more than 7 on lead position. Yes i do know which problems this code can create. And yes i know how to fix them.


Maybe all that lead has gotten to you. [/bad_joke]

I thought you posted here because you wanted our opinions. We've worked with C++ for a similar amount of time, but I guess I have had worse experiences with types that can hold pretty much anything (we have them at work, and they are a headache) and unexpected implicit conversions.


#5135498 Strange behavior of operator type

Posted by on 28 February 2014 - 04:50 PM

I don't know off hand whether that piece of code is correct or not, which is enough for me not to like it. A container of anything doesn't fit the C++ style of programming. If you feel like you need it, you might be better off using some interpreted language that allows this kind of thing.


#5135466 binary difference between working on singed and unsigned

Posted by on 28 February 2014 - 03:01 PM

Another way to look at why unsigned and two's-complement integers behave the same under addition, subtraction and multiplication is that they are both different ways of thinking of the commutative ring Z/(2^32). The definition of that ring says that its elements are the classes of equivalence in Z (the integers) when two numbers are considered equivalent if their difference is a multiple of 2^32. This is a ring with 2^32 elements, and you can use {0, 1, 2, ..., 2^32-1} as representatives or {-2^31, -2^31+1, ..., 2^31-1} as representatives. Two's complement is the way of looking at this ring by picking the latter representatives. But you are performing the exact same operations.

 

Things like overflow and order are concepts that are not part of the Z/(2^32) structure, so you have differences in what those mean between signed and unsigned.




#5135393 Logic (That's what programming is centered around right?)

Posted by on 28 February 2014 - 09:33 AM

This is a bad question because "Logic" can mean many things. Primarily, it could be a branch of Philosophy or a branch of Mathematics. However, the answer is still "no". A smart kid can learn programming without any prerequisites whatsoever.

 

I started to program when I was 7. My dad taught me a few basic things about how his programmable calculator worked, and I played around with it until I figured it out. If instead of teaching me what "if" and "goto" do he had started by introducing Logic and whatever other things people think should be prerequisites for programming, I would have been overwhelmed and I wouldn't have learned anything.




#5135179 code -> .data & data -> .code

Posted by on 27 February 2014 - 03:23 PM

Other than hearing this, have you also read it somewhere on the interwebs? Perhaps you can post a link to such a claim, so we can know exactly what you are talking about?






PARTNERS