Doing the O'Reilly Day at the Races lab has got me stumped. I'm making a race and I want to check if any of the four dogs passed the finish line yet, and if not, then run some more.
The Psuedocode
[code]
dog = new dog[3]
...
while (theres no winner)
{
for (loop through each dog, making sure they havent passed the line and became winner)
{
have dog run some more
}
}
[/code]
So what I would have to do is check all the dogs in the array through the loop and see if they've passed some variable. How would I write the syntax for this?
[code]
while(theres no winner)
{
foreach(Dog d in dogs){
d.doShit();
}
}
[/code]
This is assuming your language has ForEach.
[cpp]for(int i = 0; i < 3; i++){
// look at me ma i'm running aka:
dog[i] = 3; // i have no idea how you want to set this up so here you go
}[/cpp]
This is the first time I've actually ever helped someone with programming, so sorry if I did a bad job. But I'm pretty sure this is what you're looking for..
[editline]11:40 PM[/editline]
fuc
[QUOTE=The Inzuki;27768417][cpp]for(int i = 0; i < 3; i++){
// look at me ma i'm running aka:
dog[i] = 3; // i have no idea how you want to set this up so here you go
}[/cpp]
This is the first time I've actually ever helped someone with programming, so sorry if I did a bad job. But I'm pretty sure this is what you're looking for..
[editline]11:40 PM[/editline]
fuc[/QUOTE]
Something like this would work, a for loop, then inside that you could have something like
[cpp]
for(int i = 0; i < 3; i++)
{
bool ADogHasFinished = false;
if(dog[i].Finished == true)
{
ADogHasFinished = true;
break;
}
else
{
dog[i].Run();
}
}
[/cpp]
Alright so everything is functioning from CriticalImpact's code but now I have another problem.
[cpp] public bool Run()
{
// move forward either 1 - 4 spaces at random
//update picturebox location
//return true if won race
Random Randomizer = new Random();
//moves either 1-4 spaces
int[] movementSpaces;
movementSpaces = new int[3];
movementSpaces[0] = 1;
movementSpaces[1] = 2;
movementSpaces[2] = 3;
movementSpaces[3] = 4;
if (Location < RacetrackLength)
{
Location += movementSpaces[Randomizer.Next(0, 3)];
}
else
return true;
[/cpp]
An error pops up saying "Not all code paths return a value". When I add in "return false" to the brackets of "if (Location < RacetrackLength)", it seems to fix the error. But when I go and compile it and execute the method, all of my dog arrays are null. Is it because of the return false statement?
[url]http://headfirstlabs.com/books/hfcsharp/lab01.php[/url] This?
I'll try it out, doesn't seem hard at all.
Assuming Run is a instance method of the Dog class, so like
public class Dog()
{
public bool Run();
}
What you could do is, change Run to return a void, and then in
if (Location < RacetrackLength)
{
Location += movementSpaces[Randomizer.Next(0, 3)];
}
else
{
IsFinished = true;
}
Unless you are actually using the return value from Run, it doesnt need to be a bool, it can be a void(as in return nothing)
If I get some time, maybe I'll join in and program something and share the code.
I'll check it tomorrow, i'm about to go to sleep. Thanks y'all for the help though.
[QUOTE=CriticalImpact;27769593]Assuming Run is a instance method of the Dog class, so like
public class Dog()
{
public bool Run();
}
What you could do is, change Run to return a void, and then in
if (Location < RacetrackLength)
{
Location += movementSpaces[Randomizer.Next(0, 3)];
}
else
{
IsFinished = true;
}
Unless you are actually using the return value from Run, it doesnt need to be a bool, it can be a void(as in return nothing)
If I get some time, maybe I'll join in and program something and share the code.[/QUOTE]
From what I can remember you call each dogs Run method and if some of them returns true (dog passed finishing line) you end the game.
Oh and OP, code to find the dog who is farthest along.
[cpp]dog.Aggregate((farthest, dog) => dog.Position > farthest.Position ? dog : farthest)[/cpp]
[editline]31st January 2011[/editline]
Needs .net 3.5 because of the LINQ goodness.
[QUOTE=Venice Queen;27788250][csharp]
dogs.Max(dog => dog.Position);
[/csharp][/QUOTE]
That's a sort operation, mine just returns the dog in the lead. And it's probably a bit faster because it doesn't have to sort.
[QUOTE=ShaRose;27788409]That's a sort operation, mine just returns the dog in the lead. And it's probably a bit faster because it doesn't have to sort.[/QUOTE]
No, it's just an O(n) scan:
[img]http://ahb.me/1FQX[/img]
Gah, you are correct. I was thinking of the actual sort method.
[QUOTE=Cookieeater;27768958]
[cpp]
//moves either 1-4 spaces
int[] movementSpaces;
[B]movementSpaces = new int[3];[/B]
movementSpaces[0] = 1;
movementSpaces[1] = 2;
movementSpaces[2] = 3;
movementSpaces[3] = 4;
[/cpp]
An error pops up saying "Not all code paths return a value". When I add in "return false" to the brackets of "if (Location < RacetrackLength)", it seems to fix the error. But when I go and compile it and execute the method, all of my dog arrays are null. Is it because of the return false statement?[/QUOTE]
You need to make movementSpaces an array of 4 items, not 3.
movementSpaces = new int[4];
It may be helpful to abstract it a little bit and create a variable called numberOfDogs.
Sorry, you need to Log In to post a reply to this thread.