• What Do You Need Help With? V6
    7,544 replies, posted
I was comparing the wrong boolean so the count fails :v: Here's my actual results. [IMG]http://puu.sh/2KOQT/5e93166f04.png[/IMG] It does seem like you aren't dipping into the child nodes.
[QUOTE=pyschomc;40490545]True As you can see in my draft [code] base case is if(root == null) {return 0;} else if (this.root.children.size() %2 == 1) {counter ++); return counter;[/code] As this just checks the basic 1 and 0. Haha. I heard stacks might be useful ,but I'm not too sure.[/QUOTE] You simply call the function for each child node and add it the result counter. [editline]1st May 2013[/editline] Just a side node: If you do it this way, you are implicitly using a stack: the callstack, which is a stack of each function-call. It exists so the computer knows where to go when a function returns.
Well psychomc, no idea if it's actually correct but the values output now reflect the expected values for your cases. [IMG]http://puu.sh/2KPTE/78cf06e62a.png[/IMG] This code had been converted to C++, but the logic is all there. [code]#include <vector> using namespace std; /** * * @author J * Draft */ template <typename T> class Tree { template <typename T> class Node { public: T data; vector<Tree<T> >* children; int nodesWithOddNumberChildren() { return nodesWithOddNumberChildren(); } }; private: Node<T>* root; /** Constructs a tree with one node and no children. @param rootData the data for the root */ public: Tree(T rootData) { root = new Node<T>; (*root).data = rootData; (*root).children = new vector<Tree<T> >(); } /** Constructs an empty tree, with null for a root. */ Tree() { root = NULL; } /** Adds a subtree as the last child of the root. Doesn't work on an empty tree. */ void addSubtree(Tree subtree) { (*(*root).children).push_back(subtree); } int nodesWithOddNumberChildren() { int oddNumbers = 0; if (root == NULL) return 0; else if ((*(*root).children).size() % 2 == 0) { oddNumbers = 0; if ( !(*(*root).children).empty() ) { for (int i = 0; i < (*(*root).children).size(); i++) { oddNumbers += (*(*root).children)[i].nodesWithOddNumberChildren(); } } } else if ((*(*root).children).size() % 2 == 1) { oddNumbers = 1; if ( !(*(*root).children).empty() ) { for (int i = 0; i < (*(*root).children).size(); i++) { oddNumbers += (*(*root).children)[i].nodesWithOddNumberChildren(); } } } return oddNumbers; } };[/code] Notice the recursive call on nodesWithOddNumberChildren() - this makes sure the function iterates through all child nodes, provided it has any. It might not be perfect but it should be close to the logic you're looking for. [editline]1st May 2013[/editline] Here's the main code if it helps. Again, C++, not Java. [code]#include "tree.h" #include <string> #include <iostream> using namespace std; int main() { //Case 1 --------------------------------------------------------- cout << "Case 1" << endl; Tree<string> * t1 = new Tree<string>("Tom"); Tree<string> * s11 = new Tree<string>("Jerry"); Tree<string> * s12 = new Tree<string>("Kevin"); cout << (*t1).nodesWithOddNumberChildren() << endl; cout << "Expected: 0" << endl; (*t1).addSubtree( *s11 ); cout << (*t1).nodesWithOddNumberChildren() << endl; cout << "Expected: 1" << endl; (*t1).addSubtree( *s12 ); cout << (*t1).nodesWithOddNumberChildren() << endl; cout << "Expected: 0" << endl; Tree<string> * temp = new Tree<string>(); cout << (*temp).nodesWithOddNumberChildren() << endl; cout << "Expected: 0" << endl; //Case 2 --------------------------------------------------------- cout << "\nCase 2" << endl; t1 = new Tree<string>("Tom"); s11 = new Tree<string>("Jerry"); s12 = new Tree<string>("Kevin"); Tree<string> * s121 = new Tree<string>("Mary"); Tree<string> * s111 = new Tree<string>("Sally"); Tree<string> * s112 = new Tree<string>("Peggy"); Tree<string> * s113 = new Tree<string>("Sue"); (*s11).addSubtree(*s111); (*s11).addSubtree(*s112); (*s11).addSubtree(*s113); (*s12).addSubtree(*s121); cout << (*t1).nodesWithOddNumberChildren() << endl; cout << "Expected: 0" << endl; (*t1).addSubtree(*s11); cout << (*t1).nodesWithOddNumberChildren() << endl; cout << "Expected: 2" << endl; (*t1).addSubtree(*s12); cout << (*t1).nodesWithOddNumberChildren() << endl; cout << "Expected: 2" << endl; system("pause"); }[/code]
[code]static void Main(string[] args) { string gameState = "start"; object[] allCards; object[] playersHand = { }; object[] compsHand = { }; while (true) { if (gameState == "start") { Console.Write("Welcome to Top Trumps!\n\nPlease enter a player name: "); string playerName = Console.ReadLine(); Console.Clear(); gameState = "create"; } if (gameState == "create") { Card card1 = new Card("Black Knight", 75, 75); Card card2 = new Card("Darkwraith Knight", 60, 60); Card card3 = new Card("Undead Mage", 45, 50); Card card4 = new Card("Tree Lizard", 30, 40); Card card5 = new Card("Serpent Soldier", 55, 55); Card card6 = new Card("Anor Londo Guard", 65, 70); Card card7 = new Card("Drake", 85, 95); Card card8 = new Card("Demonite Demon", 80, 85); Card card9 = new Card("Hollow Soldier", 10, 25); Card card10 = new Card("Giant Skeleton", 40, 65); //allCards = { card1, card2, card3, card4, card5, card6, card7, card8, card9, card10 }; Console.WriteLine(allCards.Length + " cards created"); gameState = "deal"; } }[/code] On the line that I commented, does anyone know why I get an error for each card saying ; expected? Thanks for any help!
The compiler can't seem to correctly deal with the syntax error. I'll refer you to the solution though: [QUOTE=ZeekyHBomb;40487100]First you want the type for the arrays to be more precise than object. And then the problem is, that you can only assign an initialize list (I don't know if this is the proper term for that { ... }-style list in C#) to initialize an array, but not to assign to it. You can initialize it though like you initialized the categories-array: [code]Card[] allCards = { new Card("Black Knight", 75, 75), new Card("Darkwraith Knight", 60, 60), ... }[/code] If the creation depends on a function-parameter though, then you can do this: [code]allCards = new[] { new Card("Black Knight", 75, 75), new Card("Darkwraith Knight", 60, 60), ... }[/code][/QUOTE] I guess you missed it due to the cluster-post.
Alright , thanks! So basically.. Call the function? Haha, I tried calling up the odd function, but i'm getting 0 , 1 , 0 still.
[QUOTE=pyschomc;40490899]Alright , thanks! So basically.. Call the function? Haha, I tried calling up the odd function, but i'm getting 0 , 1 , 0 still.[/QUOTE] Not quite. Your original logic only counts through the children of the root node. You'll have to recursively call the function on each child node as well.
Wait I think I got it, but I will keep modifying til lit gives me the expected cases.
[QUOTE=ZeekyHBomb;40490898]The compiler can't seem to correctly deal with the syntax error. I'll refer you to the solution though: I guess you missed it due to the cluster-post.[/QUOTE] Sorry, I missed that. Thanks a lot though!
[QUOTE=JakeAM;40484881]Ok thanks that helped, but now I have bigger problems, for some reason the objects won't go in the array they just get error highlights. -code-[/QUOTE] [del] Not quite sure what you mean, but I can spot a couple things: In your Cards.Deal() method, you have a foreach statement that's trying to iterate through "cards," which doesn't look like it exists. Perhaps you meant to use "allCards?" It's been a while since I've used C#, so I may be wrong, but try replacing your use of "object" with "Card" public Card[] allCards; public Card[] playersHand; public Card[] compsHand; and... foreach (Card card in allCards) You could use "object" to store a Card, but the computer will not recognize it as a Card until you cast it. I'm assuming that you're only going to have Cards in those arrays, so I believe that you should be using "Card" instead of "object." [/del] wow I missed a whole page, nevermind
It was all going fine and dandy, then suddenly I get the error, index was outside the bounds of the array. It's referring to my deal gamestate. [code]using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TopTrumps { class Program { static void Main(string[] args) { string gameState = "start"; Card[] playersHand = { }; Card[] compsHand = { }; Card[] allCards = { new Card("Black Knight", 75, 75), new Card("Darkwraith Knight", 60, 60), new Card("Undead Mage", 45, 50), new Card("Tree Lizard", 30, 40), new Card("Serpent Soldier", 55, 55), new Card("Anor Londo Guard", 65, 70), new Card("Drake", 85, 95), new Card("Demonite Demon", 80, 85), new Card("Hollow Soldier", 10, 25), new Card("Giant Skeleton", 40, 65) }; while (true) { if (gameState == "start") { Console.Write("Welcome to Top Trumps!\n\nPlease enter a player name: "); string playerName = Console.ReadLine(); Console.Clear(); gameState = "create"; } if (gameState == "create") { Console.WriteLine(allCards.Length + " cards created"); gameState = "deal"; } if (gameState == "deal") { int i = 0; foreach (Card card in allCards) { if (playersHand.Length <= 4) { playersHand[i] = card; Console.WriteLine("Card " + i + " assigned."); } else { compsHand[i] = card; } i++; } } } } } class Card { public string name; public int level; public int vitality; public Card(string nm, int lev, int vit) { name = nm; level = lev; vitality = vit; } } }[/code] Again, thanks so much for the help :)
In C# how would I dynamically use + - / * %? As in [code]result = 5 operand 3;[/code] Where operand is a string containing one of these characters? Or anything similiar that doesn't require me to make a switch to switch on each operand.
Can't you just say x = 5 % 3? I can't imagine it'd be much more difficult than that. Then again, I don't know C#. [editline]1st May 2013[/editline] nevermind read it wrong no idea, i don't think it works like that
[QUOTE=eternalflamez;40491971]In C# how would I dynamically use + - / * %? As in [code]result = 5 operand 3;[/code] Where operand is a string containing one of these characters? Or anything similiar that doesn't require me to make a switch to switch on each operand.[/QUOTE] You can just write a function that does this and pass both numbers as params. Not syntactically identical, but functionally. Make sure the string is accessible in the scope of the function.
[QUOTE=eternalflamez;40491971]In C# how would I dynamically use + - / * %? As in [code]result = 5 operand 3;[/code] Where operand is a string containing one of these characters? Or anything similiar that doesn't require me to make a switch to switch on each operand.[/QUOTE] You can do it like this: [code] public double Eval(string expression) { DataTable table = new DataTable(); table.Columns.Add("expression", typeof(string), expression); DataRow row = table.NewRow(); table.Rows.Add(row); return double.Parse((string)row["expression"]); } Console.WriteLine(Eval("5*5")); [/code]
I think he meant like [cpp]sum = num1 operVar num2[/cpp] and pass in both numbers and the operation of choice. which i'm pretty sure you can't do because they're basically functions. (C# works like C++, right?)
[QUOTE=JakeAM;40491896]It was all going fine and dandy, then suddenly I get the error, index was outside the bounds of the array. It's referring to my deal gamestate. Again, thanks so much for the help :)[/QUOTE] Arrays are fixed size and you're creating an array with 0 elements. Create the array at the desired size or use something like a List.
So when I create the array how do I designate it to be 5 in size?
[code]Card[5] cardArray;[/code] I'm used to other container types but I think that's what the bracket is for
That doesn't seem to work :/
yes that's how it works ideally though you probably shouldn't hard code it [editline]1st May 2013[/editline] what are you in again?
C# [editline]1st May 2013[/editline] I don't really know how lists work though...
Yeah I was doing it wrong, it's more like this [code]Card[] cardArray; cardArray = new Card[10]; [/code] Not sure. Or hell you can do it inline [code]Card[] cardArray = new Card[10]; [/code]
oh. well assuming C# is not that much different, i think i can help. if not it should at least point out odd things. I see your problem. [cpp]Card card1 = x[/cpp] won't do anything because the way it's written there you're not treating it like an array at all (that's treating Card as a data type, so unless you're using enum you're doing it wrong (and if you are doing enum you're definitely doing it wrong) try like [cpp]//max card array size AR_SIZE = 5; string CardAr[AR_SIZE]; //Reference each element like this CardAr[1] = card1 CardAr[2] = card2 . . . [/cpp] that may or not actually be how c# works but i'm reasonably sure it's more correct [editline]1st May 2013[/editline] or just listen to protocol he's smarter than i am also c# and c++ apparently are not similar
Lists would be [code] List<Card> cardList = new List<Card>( { card1, card2, card3, card4, card5, card6, card7, card8, card9, card10 }; ); [/code] Something like that. I forget the exact syntax for initializing an IEnumerable inline
[QUOTE=Protocol7;40492461]Lists would be [code] List<Card> cardList = new List<Card>( { card1, card2, card3, card4, card5, card6, card7, card8, card9, card10 }; ); [/code] Something like that. I forget the exact syntax for initializing an IEnumerable inline[/QUOTE] [url]http://msdn.microsoft.com/en-us/library/vstudio/bb384062.aspx[/url]
Hmmm... this is odd, it now gives the error after 0, 1, 2, 3 then it doesn't add the 5th. [code] using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TopTrumps { class Program { static void Main(string[] args) { string gameState = "start"; Card[] playersHand = new Card[4]; Card[] compsHand = new Card[4]; Card[] allCards = { new Card("Black Knight", 75, 75), new Card("Darkwraith Knight", 60, 60), new Card("Undead Mage", 45, 50), new Card("Tree Lizard", 30, 40), new Card("Serpent Soldier", 55, 55), new Card("Anor Londo Guard", 65, 70), new Card("Drake", 85, 95), new Card("Demonite Demon", 80, 85), new Card("Hollow Soldier", 10, 25), new Card("Giant Skeleton", 40, 65) }; while (true) { if (gameState == "start") { Console.Write("Welcome to Top Trumps!\n\nPlease enter a player name: "); string playerName = Console.ReadLine(); Console.Clear(); gameState = "create"; } if (gameState == "create") { Console.WriteLine(allCards.Length + " cards created"); gameState = "deal"; } if (gameState == "deal") { int i = 0; foreach (Card card in allCards) { if (playersHand.Length <= 4) { playersHand[i] = card; Console.WriteLine("Card " + i + " assigned."); } else { compsHand[i] = card; Console.WriteLine("Card " + i + " assigned."); } i++; } } } } } class Card { public string name; public int level; public int vitality; public Card(string nm, int lev, int vit) { name = nm; level = lev; vitality = vit; } } }[/code]
Yeah I wrote up a test program that works [code]using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test { class Card { public Card(String _Name, int _A, int _B) { cName = _Name; A = _A; B = _B; } public string cName { get; set; } public int A { get; set; } public int B { get; set; } } class Program { static void Main(string[] args) { Card card1 = new Card("Black Knight", 75, 75); Card card2 = new Card("Darkwraith Knight", 60, 60); Card card3 = new Card("Undead Mage", 45, 50); Card card4 = new Card("Tree Lizard", 30, 40); Card card5 = new Card("Serpent Soldier", 55, 55); Card card6 = new Card("Anor Londo Guard", 65, 70); Card card7 = new Card("Drake", 85, 95); Card card8 = new Card("Demonite Demon", 80, 85); Card card9 = new Card("Hollow Soldier", 10, 25); Card card10 = new Card("Giant Skeleton", 40, 65); List<Card> cardList = new List<Card>{ card1, card2, card3, card4, card5, card6, card7, card8, card9, card10 }; cardList.ForEach(xx => Console.WriteLine(xx.cName)); } } } [/code]
Ahh... I see, the only thing I don't understand is the alteration of the card class. What is with the get; and set;?
[QUOTE=JakeAM;40492595]Hmmm... this is odd, it now gives the error after 0, 1, 2, 3 then it doesn't add the 5th.[/QUOTE] Well yeah, your playersHand array only has a size of 4. When it goes to access playersHand[4] it throws an exception. [editline]1st May 2013[/editline] [QUOTE=JakeAM;40492659]Ahh... I see, the only thing I don't understand is the alteration of the card class. What is with the get; and set;?[/QUOTE] That's a C# property. [URL]http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx[/URL] It's a brain leftover from all the WPF shit I do. Your way is fine.
Sorry, you need to Log In to post a reply to this thread.