// File: Matlock42[P]Jeopardy/jgameClass.cpp
// Function: Game::loadQuestions()
int Game::loadQuestions(void)
{	
	/* initialize random seed: */
	srand (time(NULL));
	
	std::vector <std::string> files;
	// get list of xml files
	files = findXML(".");
	
	if( files.size() < mSize)
	{
		cout << "Unable load game: inadequate categories. ("<< files.size() <<")\n";
		return -1;
	}
	
	// shuffle up the list
	std::random_shuffle ( files.begin(), files.end() );
	
	// For each of the files found, go get some questions (up to the board size)
	for( int f=0; f<mSize; f++ )
	{
		// string lFileLocation = files.at(f);
		// rapidxml::file<> lXmlFile(lFileLocation.data());
		rapidxml::file<> lXmlFile(files.at(f).data());
		rapidxml::xml_document<> lDoc;

		// everything but the first 5 and last 4 charactors
		string lFileName = files.at(f).substr(5,(files.at(f).length()-5-4));
		
		int lFileCounter = f + 1;	// start at 1
		int lLevelCounter = 1;		// start at $100
		int lItemCounter;
		int lChosenItem;
		int lAns[4] = {0, 1, 2, 3};

		rapidxml::xml_node<> * lRoot_node;
		rapidxml::xml_node<> * lLevel;
		rapidxml::xml_node<> * lItem;
		rapidxml::xml_node<> * lQuestion;
		rapidxml::xml_node<> * lAnswerGroup;
		rapidxml::xml_node<> * lAnswerNode;
		rapidxml::xml_attribute<> * lAttr;
		
		// Parse the buffer using the xml file parsing library into lDoc
		lDoc.parse<0>(lXmlFile.data());
		//set the root node
		lRoot_node = lDoc.first_node("items");
		// make sure there are the right number of levels
		if( rapidxml::count_children(lRoot_node) < mSize )
		{
			// bad things are happening
			cout << "Unable load game: inadequate questions in Data/" << files.at(f) << ".\n";
			return -1;
		}
		// go through each Level of question (100,200,300,...)
		for( lLevel = lRoot_node->first_node("levels"); lLevel; lLevel = lLevel->next_sibling() )
		{
			lAttr = lLevel->first_attribute("id");
			// make sure that the level id matches
			if( lAttr )
			{
				char * lLevelNum = lAttr->value();

				if( * lLevelNum != (lLevelCounter * 100) )
				{
					// level doesn't match
					// complain with error/exception
					//cout << "Unable to load Game: Bad level values. (" << * lLevelNum << ")(" << lLevelCounter << ")\n";
				}
			}
			
			// count the # of items in the level
			//set default to 1
			lChosenItem = 1;
			
			if( rapidxml::count_children(lLevel) > 1 ) // if more than one item in the level
			{
				// pick one at random
				lChosenItem = rand() % rapidxml::count_children(lLevel) + 1;
			}
			
			lItemCounter = 1;
			lItem = lLevel->first_node("item");
			
			// now open the chosen item
			while(lChosenItem > lItemCounter)
			{
				lItem = lItem->next_sibling();
				lItemCounter ++;
			}
			
			//Have the right item, now extract the question and answers.
			lQuestion = lItem->first_node("question");
			char * lQuestionValue = lQuestion->value();
			mQuestionSet[lFileCounter - 1][lLevelCounter - 1] = new Question(lFileName,(lLevelCounter * 100), lQuestionValue);
			
			// mix up the answers
			random_shuffle(&lAns[0],&lAns[4]);
			lAnswerGroup = lItem->first_node("answers");
			lAnswerNode =  lAnswerGroup->first_node("ans");
			// loop through the answers
			for(int i = 0; i < 4; i++)
			{
				//Check if this is the correct answer
				lAttr = lAnswerNode->first_attribute("correct");
				if(lAttr)
				{
					char * lAnsCorrect = lAttr->value();
					// set the correct answer
					mQuestionSet[lFileCounter - 1][lLevelCounter - 1]->setCorrectAns(lAns[i]);
				}
				// add the answer to the stack
				mQuestionSet[lFileCounter - 1][lLevelCounter - 1]->addAnswer(lAns[i], lAnswerNode->value());
				// go to the next answer exept the last loop
				if( i < 3 ) lAnswerNode = lAnswerNode->next_sibling();
			}
			lLevelCounter ++;
		}
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10340, %rsp          # imm = 0x10340
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x10218(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3250
movl %eax, %edi
callq 0x3270
leaq -0x28(%rbp), %rdi
callq 0x5630
movq -0x10218(%rbp), %rsi
leaq 0x8868(%rip), %rdx      # 0xc289
leaq -0x40(%rbp), %rdi
callq 0x4c70
jmp 0x3a2c
leaq -0x28(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x5650
leaq -0x40(%rbp), %rdi
callq 0x5680
leaq -0x28(%rbp), %rdi
callq 0x56e0
movq -0x10218(%rbp), %rcx
movslq 0x8(%rcx), %rcx
cmpq %rcx, %rax
jae 0x3ad1
movq 0xe566(%rip), %rdi      # 0x11fc8
leaq 0x8767(%rip), %rsi      # 0xc1d0
callq 0x3280
movq %rax, -0x10220(%rbp)
jmp 0x3a77
leaq -0x28(%rbp), %rdi
callq 0x56e0
movq -0x10220(%rbp), %rdi
movq %rax, %rsi
callq 0x3070
movq %rax, -0x10228(%rbp)
jmp 0x3a98
movq -0x10228(%rbp), %rdi
leaq 0x8755(%rip), %rsi      # 0xc1fb
callq 0x3280
jmp 0x3aad
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x50(%rbp)
jmp 0x43a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x43bb
leaq -0x28(%rbp), %rdi
movq %rdi, -0x10230(%rbp)
callq 0x57d0
movq -0x10230(%rbp), %rdi
movq %rax, -0x58(%rbp)
callq 0x5800
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x5700
jmp 0x3b04
movl $0x0, -0x64(%rbp)
movq -0x10218(%rbp), %rcx
movl -0x64(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0x4398
movslq -0x64(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x5830
movq %rax, -0x10238(%rbp)
jmp 0x3b34
movq -0x10238(%rbp), %rdi
callq 0x3150
movq %rax, %rsi
leaq -0x80(%rbp), %rdi
callq 0x5870
jmp 0x3b4e
leaq -0x10108(%rbp), %rdi
callq 0x5b70
jmp 0x3b5c
movslq -0x64(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x5830
movq %rax, -0x10240(%rbp)
jmp 0x3b72
movslq -0x64(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x5830
movq %rax, -0x10248(%rbp)
jmp 0x3b88
movq -0x10248(%rbp), %rdi
callq 0x33c0
movq -0x10240(%rbp), %rsi
movq %rax, %rcx
addq $-0x9, %rcx
leaq -0x10128(%rbp), %rdi
movl $0x5, %edx
callq 0x32b0
jmp 0x3bb5
movl -0x64(%rbp), %eax
incl %eax
movl %eax, -0x1012c(%rbp)
movl $0x1, -0x10130(%rbp)
movaps 0x8a3f(%rip), %xmm0     # 0xc610
movaps %xmm0, -0x10150(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x5cc0
movq %rax, -0x10250(%rbp)
jmp 0x3bea
movq -0x10250(%rbp), %rsi
leaq -0x10108(%rbp), %rdi
callq 0x5ba0
jmp 0x3bff
leaq 0x85f8(%rip), %rsi      # 0xc1fe
xorl %eax, %eax
movl %eax, %edx
leaq -0x10108(%rbp), %rdi
movl $0x1, %ecx
callq 0x5ce0
movq %rax, -0x10258(%rbp)
jmp 0x3c24
movq -0x10258(%rbp), %rax
movq %rax, -0x10158(%rbp)
movq -0x10158(%rbp), %rdi
callq 0x5dc0
movq %rax, -0x10260(%rbp)
jmp 0x3c47
movq -0x10260(%rbp), %rax
movq -0x10218(%rbp), %rcx
movslq 0x8(%rcx), %rcx
cmpq %rcx, %rax
jae 0x3d0b
movq 0xe35f(%rip), %rdi      # 0x11fc8
leaq 0x8594(%rip), %rsi      # 0xc204
callq 0x3280
movq %rax, -0x10268(%rbp)
jmp 0x3c7e
movslq -0x64(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x5830
movq %rax, -0x10270(%rbp)
jmp 0x3c94
movq -0x10270(%rbp), %rsi
movq -0x10268(%rbp), %rdi
callq 0x3230
movq %rax, -0x10278(%rbp)
jmp 0x3cb0
movq -0x10278(%rbp), %rdi
leaq 0x847a(%rip), %rsi      # 0xc138
callq 0x3280
jmp 0x3cc5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x50(%rbp)
jmp 0x433b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x438d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4381
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4375
movq -0x10158(%rbp), %rdi
leaq 0x851b(%rip), %rsi      # 0xc234
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
callq 0x5ce0
movq %rax, -0x10280(%rbp)
jmp 0x3d30
movq -0x10280(%rbp), %rax
movq %rax, -0x10160(%rbp)
cmpq $0x0, -0x10160(%rbp)
je 0x4334
movq -0x10160(%rbp), %rdi
leaq 0x8569(%rip), %rsi      # 0xc2c3
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
callq 0x5e30
movq %rax, -0x10288(%rbp)
jmp 0x3d71
movq -0x10288(%rbp), %rax
movq %rax, -0x10188(%rbp)
cmpq $0x0, -0x10188(%rbp)
je 0x3dc5
movq -0x10188(%rbp), %rdi
callq 0x5f00
movq %rax, -0x10290(%rbp)
jmp 0x3d9e
movq -0x10290(%rbp), %rax
movq %rax, -0x10190(%rbp)
movq -0x10190(%rbp), %rax
movsbl (%rax), %eax
imull $0x64, -0x10130(%rbp), %ecx
cmpl %ecx, %eax
je 0x3dc3
jmp 0x3dc3
jmp 0x3dc5
movl $0x1, -0x10138(%rbp)
movq -0x10160(%rbp), %rdi
callq 0x5dc0
movq %rax, -0x10298(%rbp)
jmp 0x3de4
movq -0x10298(%rbp), %rax
cmpq $0x1, %rax
jbe 0x3e33
callq 0x30a0
cltq
movq %rax, -0x102a8(%rbp)
movq -0x10160(%rbp), %rdi
callq 0x5dc0
movq %rax, -0x102a0(%rbp)
jmp 0x3e14
movq -0x102a0(%rbp), %rcx
movq -0x102a8(%rbp), %rax
xorl %edx, %edx
divq %rcx
addq $0x1, %rdx
movl %edx, %eax
movl %eax, -0x10138(%rbp)
movl $0x1, -0x10134(%rbp)
movq -0x10160(%rbp), %rdi
leaq 0x83f0(%rip), %rsi      # 0xc23b
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
callq 0x5ce0
movq %rax, -0x102b0(%rbp)
jmp 0x3e62
movq -0x102b0(%rbp), %rax
movq %rax, -0x10168(%rbp)
movl -0x10138(%rbp), %eax
cmpl -0x10134(%rbp), %eax
jle 0x3ebe
movq -0x10168(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
movq %rdx, %rsi
callq 0x5f40
movq %rax, -0x102b8(%rbp)
jmp 0x3e9f
movq -0x102b8(%rbp), %rax
movq %rax, -0x10168(%rbp)
movl -0x10134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10134(%rbp)
jmp 0x3e70
movq -0x10168(%rbp), %rdi
leaq 0x8374(%rip), %rsi      # 0xc240
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
callq 0x5ce0
movq %rax, -0x102c0(%rbp)
jmp 0x3ee3
movq -0x102c0(%rbp), %rax
movq %rax, -0x10170(%rbp)
movq -0x10170(%rbp), %rdi
callq 0x5f00
movq %rax, -0x102c8(%rbp)
jmp 0x3f06
movq -0x102c8(%rbp), %rax
movq %rax, -0x10198(%rbp)
movl $0xd0, %edi
callq 0x3290
movq %rax, -0x102d0(%rbp)
jmp 0x3f27
movb $0x1, -0x101da(%rbp)
leaq -0x101b8(%rbp), %rdi
leaq -0x10128(%rbp), %rsi
callq 0x30f0
jmp 0x3f43
movl -0x10130(%rbp), %eax
imull $0x64, %eax, %eax
movl %eax, -0x102e4(%rbp)
movq -0x10198(%rbp), %rax
movq %rax, -0x102e0(%rbp)
leaq -0x101d9(%rbp), %rdi
movq %rdi, -0x102d8(%rbp)
callq 0x34b0
movq -0x102e0(%rbp), %rsi
movq -0x102d8(%rbp), %rdx
leaq -0x101d8(%rbp), %rdi
callq 0x5570
jmp 0x3f8f
movl -0x102e4(%rbp), %edx
movq -0x102d0(%rbp), %rdi
leaq -0x101b8(%rbp), %rsi
leaq -0x101d8(%rbp), %rcx
callq 0xafa0
jmp 0x3fb1
movq -0x102d0(%rbp), %rdx
movq -0x10218(%rbp), %rax
movb $0x0, -0x101da(%rbp)
movq 0x10(%rax), %rax
movl -0x1012c(%rbp), %ecx
decl %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl -0x10130(%rbp), %ecx
decl %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
leaq -0x101d8(%rbp), %rdi
callq 0x3180
leaq -0x101d9(%rbp), %rdi
callq 0x32d0
leaq -0x101b8(%rbp), %rdi
callq 0x3180
leaq -0x10140(%rbp), %rsi
leaq -0x10150(%rbp), %rdi
callq 0x6030
jmp 0x4021
movq -0x10168(%rbp), %rdi
leaq 0x821a(%rip), %rsi      # 0xc249
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
callq 0x5ce0
movq %rax, -0x102f0(%rbp)
jmp 0x4046
movq -0x102f0(%rbp), %rax
movq %rax, -0x10178(%rbp)
movq -0x10178(%rbp), %rdi
leaq 0x81ef(%rip), %rsi      # 0xc251
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
callq 0x5ce0
movq %rax, -0x102f8(%rbp)
jmp 0x4079
movq -0x102f8(%rbp), %rax
movq %rax, -0x10180(%rbp)
movl $0x0, -0x101e0(%rbp)
cmpl $0x4, -0x101e0(%rbp)
jge 0x42f1
movq -0x10180(%rbp), %rdi
leaq 0x81a9(%rip), %rsi      # 0xc255
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
callq 0x5e30
movq %rax, -0x10300(%rbp)
jmp 0x40c3
movq -0x10300(%rbp), %rax
movq %rax, -0x10188(%rbp)
cmpq $0x0, -0x10188(%rbp)
je 0x41af
movq -0x10188(%rbp), %rdi
callq 0x5f00
movq %rax, -0x10308(%rbp)
jmp 0x40f4
movq -0x10218(%rbp), %rax
movq -0x10308(%rbp), %rcx
movq %rcx, -0x101e8(%rbp)
movq 0x10(%rax), %rax
movl -0x1012c(%rbp), %ecx
decl %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl -0x10130(%rbp), %ecx
decl %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x101e0(%rbp), %rax
movl -0x10150(%rbp,%rax,4), %esi
callq 0xb340
jmp 0x4140
jmp 0x41af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x418e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4176
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x101d8(%rbp), %rdi
callq 0x3180
leaq -0x101d9(%rbp), %rdi
callq 0x32d0
leaq -0x101b8(%rbp), %rdi
callq 0x3180
testb $0x1, -0x101da(%rbp)
jne 0x4199
jmp 0x41aa
movq -0x102d0(%rbp), %rdi
movl $0xd0, %esi
callq 0x32a0
jmp 0x4375
movq -0x10218(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1012c(%rbp), %ecx
decl %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl -0x10130(%rbp), %ecx
decl %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x10320(%rbp)
movslq -0x101e0(%rbp), %rax
movl -0x10150(%rbp,%rax,4), %eax
movl %eax, -0x10314(%rbp)
movq -0x10180(%rbp), %rdi
callq 0x5f00
movq %rax, -0x10310(%rbp)
jmp 0x4208
leaq -0x10209(%rbp), %rdi
movq %rdi, -0x10328(%rbp)
callq 0x34b0
movq -0x10310(%rbp), %rsi
movq -0x10328(%rbp), %rdx
leaq -0x10208(%rbp), %rdi
callq 0x5570
jmp 0x4237
movl -0x10314(%rbp), %esi
movq -0x10320(%rbp), %rdi
leaq -0x10208(%rbp), %rdx
callq 0xb160
jmp 0x4252
leaq -0x10208(%rbp), %rdi
callq 0x3180
leaq -0x10209(%rbp), %rdi
callq 0x32d0
cmpl $0x3, -0x101e0(%rbp)
jge 0x42db
movq -0x10180(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
movq %rdx, %rsi
callq 0x5f40
movq %rax, -0x10330(%rbp)
jmp 0x4294
movq -0x10330(%rbp), %rax
movq %rax, -0x10180(%rbp)
jmp 0x42db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x42ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x10208(%rbp), %rdi
callq 0x3180
leaq -0x10209(%rbp), %rdi
callq 0x32d0
jmp 0x4375
jmp 0x42dd
movl -0x101e0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x101e0(%rbp)
jmp 0x4091
movl -0x10130(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10130(%rbp)
movq -0x10160(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
movq %rdx, %rsi
callq 0x5f40
movq %rax, -0x10338(%rbp)
jmp 0x4321
movq -0x10338(%rbp), %rax
movq %rax, -0x10160(%rbp)
jmp 0x3d3e
movl $0x0, -0x50(%rbp)
leaq -0x10128(%rbp), %rdi
callq 0x3180
leaq -0x10108(%rbp), %rdi
callq 0x60d0
leaq -0x80(%rbp), %rdi
callq 0x60f0
movl -0x50(%rbp), %eax
testl %eax, %eax
jne 0x43a6
jmp 0x4365
jmp 0x4367
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x3b0b
leaq -0x10128(%rbp), %rdi
callq 0x3180
leaq -0x10108(%rbp), %rdi
callq 0x60d0
leaq -0x80(%rbp), %rdi
callq 0x60f0
jmp 0x43bb
movl $0x0, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x5680
movl -0x4(%rbp), %eax
addq $0x10340, %rsp          # imm = 0x10340
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0x5680
movq -0x48(%rbp), %rdi
callq 0x34a0
nopl (%rax)