|
Some are negative and some are positive. It depends on the direction the snake is moving.
If you can help me redesign the code I would be much apreciated. Maybe it would help if I post the program.
|
|
|
|
|
After looking at the full code you posted in a different branch, there are a few pieces of advice I'd like to give out:
(I) General coding advice
1. Don't put more than one statement on one line. It just makes the code harder to read. This does include control statements such as if or else , but not neccessarily tokens such as curly braces.
2. Don't use magic numbers. As others already pointed out, the constants -2, -1, 1, or 2 may be shorter to write, but they're also harder to read. Also, it's much easier to accidentally omit (or add) a '-' and thus swap an UP with DOWN or LEFT with RIGHT, than accidentally swapping symbols that actually say what they mean, rather than imply.
3. Don't write long functions. If your code (after reformatting according to item 1 above) is longer than ~50 lines it's about time to consider if you shouldn't split this function into multiple parts, or if there are repetitive parts in this function that you could extract into a separate function.
(II) 2D graphical coding advice
4. Don't encode a direction as a single number in a two-dimensional world. If you're traversing a linear array, then it makes sense to encode the direction you're moving in as -1 or 1. In 2D however it is much more practical to denote the X- and Y-components of your movement separately. It is quite easy to take the current position of an object, add to it the movement vector, and then compare it to the positions of objects that it might bump into. It's a lot more complicated to make if statements for each direction that you could move to, and it requires you to duplicate your code for every possible direction! (that is what you obviously did)
5. Provide a simple function or set of functions for collision checks between different types of shapes in your 2D-world. Apparently, snake segments, and food patches all occupy a rectangular box of a fixed size. Just define a struct called box that contains their position (x,y) and size (width, height); then define a function that checks two boxes for overlaps (or collision).
6. A somewhat more sophisticated system with 2D elements and collision checks can be found here. This goes beyond your requirements, but gives a very good insight into coding techniques for the kind of problems you're struggling with.
|
|
|
|
|
Thanks for the advice. I am a beginer coder and have lots of learning to do.
|
|
|
|
|
dear madam,
will u plz tell me how to convert the MFC C++ application into .net application
Shrikant
|
|
|
|
|
don't know if there's an easy answer for this... but easiest way would be to convert to C++/CLI... either way you go, there will be a lot of work involved.
|
|
|
|
|
that application itself in c++ thats why
|
|
|
|
|
yeah but the infrastructure is completely different... that's why its a lot of work... almost like converting MFC application to Qt, a lot of work... there's always advantages/disadvantages of particular programming methodologies, this is a negative of using an existing infrastructure, converting it to another may be quite a bit of work.
|
|
|
|
|
if you HAVE to do it, your best bet will probably be to start a .net application from scratch and transfer over functionality a bit at a time, you'll essentially end up rewriting massive sections of code.
|
|
|
|
|
shiks wrote: will u plz tell me how to convert the MFC C++ application into .net application
An MFC application runs in a completely different environment, has access to things like a printer and your webcam. Next problem is that browsers generally use HTML to display a UI, so this often hints towards a rewrite. Now, it would be hard to answer this question without any further information. Is it a large project? Used any COM-objects? Multithreading?
If it were a WinForm app, I'd say that the best option would be an interface to the most complex routines in the MFC-application and to P/Invoke the things you need from the .NET side of life. I guess ASP.NET would allow for something similar.
I are Troll
|
|
|
|
|
its not as big as project. its a genetic algorithm implemented on class scheduling and i want to implement it on practical time tabling system. i got that source code but making modification in it is tedius job. any one having genetic algorithm in c# so i could implement it in my work
|
|
|
|
|
I have to detect whether two given expressions are equivalent (both compute the same value for all inputs). The inputs will all be of simple types, such as int32, so it is theoretically possible to try all inputs and see whether the result are always the same.
That's too slow of course, so it will have to be done differently.
False positives are not acceptable at all.
False negatives are acceptable, but I need a useful amount of equivalences, so merely testing whether two expressions are equal does not solve the problem.
I have tried applying many transformations to one of the expressions and see whether they are ever equal, but that doesn't work very well. The False Negatives rate is still far too high to be useful.
|
|
|
|
|
Theoretically, if false positive are not acceptable then you have either to:
- Analitically proof the equivalence.
or
My common sense suggests no escapes.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
But surely there are some shortcuts?
After all, false negatives are allowed, so the disjunction of a bunch of heuristic guestimates is OK as long as they are all pessimistic.
|
|
|
|
|
what is the input? maybe provide some examples.
two strings representing integer expressions?
if so, define a canonical form, bring both of them to their canonical form, and those should then be identical.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
The expressions are represented by their AST's
That sounds like a good idea, but how would one go about that? I know about some canonical forms for boolean expressions, but not for integer expressions.
|
|
|
|
|
it would take a number of steps, possibly iteratively until no more changes, such as:
- eliminating parentheses
- replacing constant expressions by their value
- setting a canonical order (e.g. alphabetical)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Ok I'll test how well it works
|
|
|
|
|
Depending on how these expressions are (they do not make different computations depending on the entry value), and if the inputs are always natural numbers you can take advantage of the induction method for mathematical demonstrations, so you would only have to check equality for: one base case (input = 0 for example); one random case (say input = n, being n different form the base case); and the next case to the random case (input = n + 1). If these three cases macth, the expressons are equivalent. For example:
We want to check if these two expressions are equivalent for every natural numbers bigger than 1:
- Expression 1: 1+2+3+4+...+n
- Expression 2: (1+n)*n/2
So our base case would be n=1:
-Expression 1: result = 1;
-Expression 2: (1+1)*1/2 = 1;
Our base cases match.
Lets take a random n... lets say 7:
-Expression 1: 1+2+3+4+5+6+7=28;
-Expression 2: (1+7)*7/2 = 28;
Our random cases match.
Lets check now for random+1, I mean, n = 8:
-Expression 1: 1+2+3+4+5+6+7+8=36;
-Expression 2: (1+8)*8/2 = 36;
Random + 1 matches as well, so both expressions are equivalent for every natural number greater or equal to 1.
I think making a general expression comparer with delegates to the methods implementing each expression would not be too difficult.
|
|
|
|
|
I'm confused, that's not quite how I learned induction works..
Also, it doesn't seem to work for integers:
-Expression 1: = x/y
-Expression 2: = x/z
If the random sample is less than Min(y, z), they would be detected as equivalent (the result would be zero) regardless of whether y and z are equal.
|
|
|
|
|
David1987 wrote: I'm confused, that's not quite how I learned induction works..
Sure, I am not making demonstrations, but just using the mathematical induction method to check if two expressions are equivalent... It's a heuristic method.
David1987 wrote: Also, it doesn't seem to work for integers: -Expression 1: =
x/y -Expression 2: = x/z
If the random sample is less than Min(y, z),
they would be detected as equivalent (the result would be zero) regardless of
whether y and z are equal.
Errrr, they would not. If you want to represent a division, say for example "n / m", using only integer numbers you just cannot forget the rest of the division, I mean:
n / m = c * m + r, where r is n mod m.
In the case you show, "r" would be equal to "y" in the first case and equal to "z" in the second one, so if y != z then expression 1 is not equivalent to expression 2.
|
|
|
|
|
So then I'd have to convert the expressions first, ok, it's still looking quite weird though because surely "expressions are equivalent for 3 sampled inputs" does not imply "expressions are equivalent for all inputs"?
|
|
|
|
|
Errr, of course not. I am sorry if I am not explaining it well... We do not check if they are equivalent for any three random values, we check them for the base case, a random case and the succesor of the random case. It is just a straight application of the axiom of induction[^] for natural numbers (remember, works only with natural numbers).
|
|
|
|
|
Well then, what if the expressions are both polynomials that both happen to intersect the x-axis at 3 points, namely the base case, the random case, and its successor?
|
|
|
|
|
Induction can only be applied with Natural numbers. If your expression will only evaluate Natural numbers then this is a good, quick and effective approach.
David1987 wrote: Well then, what if the expressions are both polynomials that both happen to
intersect the x-axis at 3 points, namely the base case, the random case, and its
successor?
This means Real numbers, so you cannot apply induction here. That's why I said in my first reply: "if the inputs are always natural numbers...". If you don't know what mathematical induction is then I recommend you to follow the link I gave you before but, please, stop replying as if I was stupid and did not know what I am talking about... I can't belive it...
|
|
|
|
|
_Erik_ wrote: This means Real numbers
Why does it mean that? Why can't two polynomials intersect at 3 places that are all natural integers?
Also I know perfectly well what induction is and this isn't it. You have to prove that you can go from one case to the next - you're just proving that some random case and the next are both accidentally correct.
There is no random in induction. You have to analytically show that if something is true for n, that implies that it is also true for n+1.
|
|
|
|
|