-
-
- (0)
-
premium membership
-
Posts
38
|
|
Hi everybody, I'm trying to write a Risk-like game, just for fun right now, but if I think it has any potential, I may try to sell it. However, I'm having trouble with one of my logic methods, where I determine whether two territories are neighbors.
I set up a method to return a bool, and then I loop through the List I'm using to store the neighbors for each territory, but when I go to compile my code and test it, it tells me that not all code paths return a value...I'm not sure why it says that, but I thought maybe someone here would be able to help me figure it out...here's my code:
static public bool IsNeighbor(Territory attack, Territory defense) { foreach (string neighbor in defense.neighbors) { if (neighbor.CompareTo(defense.tName)) return true; else return false; } }
Each territory has a List<String> object to store the string representation of each neighbor's name.
Any help would be greatly appreciated, as until I can figure out how to get this to work, I'm at a loss as to how to continue because my code checking for whether a territory is owned, or is owned by the current player use the same basic code as this does.
In Development: Strategery
|
|
-
-
- (4670)
-
premium membership
MVP
-
Posts
2.729
|
|
Well, you have a for..each loop with two return conditions, but imagine that you don't have any neighbors in defense.neighbors, that means that whole for...each loop would be skipped, but what would be returned? true? false? that's what the compiler is telling you. It recognizes there's a condition that can be met where you haven't defined what should be returned.
But looking at your code, it appears you have an error any way. Your loop would only cycle through one item. It would look through the first item, compare it and if it matches defense.tName it would return true otherwise it's going to return false and not loop through any more items.
I believe what you want to do is get rid of that "else" case altogether in your for...each loop and instead have that be the other exit path outside of your loop. So you cycle through all of the neighbors, if any compare to the defense, return true otherwise, your loop exits and your return false.
Hope that helps!
|
|
-
-
- (1152)
-
premium membership
-
Posts
687
|
|
You've got numerous problems here.
First, you're comparing the neighbors of defense with itself, which should always return true in every case. You should be comparing the neighbors with attack.tName.
Second, CompareTo doesn't do what you think it does. Just compare the strings directly, like this: "neighbor == attack.tName".
Third, if C# allowed you to run this, the loop wouldn't make it past the first iteration. Regardless of the result of your comparison, you will return from the function. This is bad. Instead, you should only be returning true in the true case. Don't do anything in the else case, because if the current territory does not match, then you want to continue checking the other neighbors.
Fourth, if "neighbors" is a List<string>, just use the Contains method. If it's a string[], make it a List<string> and just use the Contains method. In fact, don't bother storing the string names of the neighbors; store the neighbor lists directly as List<Territory> and pass the Territory in question into the Contains method. It's faster and less error-prone than a bunch of string comparisons.
Edit: Sniped by George!
Previously known as "Rainault". Twitter - me, Jade Vault GamesAnnouncing ASCII Quest, a Roguelike under development for Xbox LIVE Indie Games
|
|
-
-
- (0)
-
premium membership
-
Posts
38
|
|
*smacks self*
oy, I really should have noticed that. Thanks a lot. It works now...I never thought to check for no neighbors, since it will never happen in the game.
Also...just so I don't find out the hard way(since the msdn website is sorely lacking in helping with this type of stuff...) when you do a String.CompareTo, is it -1 if there isn't a match and 1 if there is a match?
also, is there a website somewhere I can find an API for either XNA or C#? I haven't found one yet, and I'm really missing it...using the official API for Java really helped me learn the language much faster than I would have otherwise.
In Development: Strategery
|
|
-
-
- (1152)
-
premium membership
-
Posts
687
|
|
The MSDN is actually a pretty good resource, once you get used to it. It's cluttered with every one of Microsoft's APIs, but it has all the information you'll need. You might be able to find an independent resource elsewhere using your favorite search engine.
For XNA GS-specific APIs, use the XNA Game Studio Documentation that comes with the install.
Previously known as "Rainault". Twitter - me, Jade Vault GamesAnnouncing ASCII Quest, a Roguelike under development for Xbox LIVE Indie Games
|
|
-
-
- (1152)
-
premium membership
-
Posts
687
|
|
Oh, and for str1.CompareTo(str2), it will return a negative int if str1 comes first lexicographically, a postiive int if str2 comes first, and 0 if they are equal.
Previously known as "Rainault". Twitter - me, Jade Vault GamesAnnouncing ASCII Quest, a Roguelike under development for Xbox LIVE Indie Games
|
|
-
-
- (0)
-
premium membership
-
Posts
38
|
|
Rainault:You've got numerous problems here.
First, you're comparing the neighbors of defense with itself, which should always return true in every case. You should be comparing the neighbors with attack.tName.
Second, CompareTo doesn't do what you think it does. Just compare the strings directly, like this: "neighbor == attack.tName".
Third, if C# allowed you to run this, the loop wouldn't make it past the first iteration. Regardless of the result of your comparison, you will return from the function. This is bad. Instead, you should only be returning true in the true case. Don't do anything in the else case, because if the current territory does not match, then you want to continue checking the other neighbors.
Fourth, if "neighbors" is a List<string>, just use the Contains method. If it's a string[], make it a List<string> and just use the Contains method. In fact, don't bother storing the string names of the neighbors; store the neighbor lists directly as List<Territory> and pass the Territory in question into the Contains method. It's faster and less error-prone than a bunch of string comparisons.
Edit: Sniped by George!
1) I noticed that as well, I had been messing around with different string comparison methods and didn't notice I c/p the wrong version until you mentioned it 2) same as the first...I had been trying compareTo(x) >0, but forgot to put it on there in that version 3) Thanks for the heads-up! I really should have noticed that, I've been programming for years and can't believe I made a mistake like that! 4) I had no idea List had a contains function! that makes things so much easier! Any idea where I can find an API for XNA or C#? The MSDN website is rather...unsatisfactory when I'm trying to look up info on aspects of C# that I'm not familiar with(List and Dictionary among them...)
In Development: Strategery
|
|
-
|
|
|
There's the Object Explorer if you just want to look at the members of the classes.
For a more immediate approach, you can select the type you're interested in and press F12 to seek its definition. This gives you the same kind of information as the Object Explorer.
The MSDN's articles on Generic Collections are some of the most well documented pages in the service. Press F1 and search on the name of the class you want and it'll typically be the first option (excluding cases where there's a type by the same name in another API). All of the Generic Collections pages tend to have some good information about the performance and memory implications of each collection.
|
|
|