// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10198, %rsp          # imm = 0x10198
movq %rdi, %r15
xorl %edi, %edi
callq 0x21e0
movl %eax, %edi
callq 0x2200
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
leaq 0x3b1d(%rip), %rdx      # 0x62ad
leaq -0x101c0(%rbp), %rdi
movq %r15, %rsi
callq 0x32aa
leaq -0x70(%rbp), %rbx
leaq -0x101c0(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3f0e
movq %r14, %rdi
callq 0x37aa
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movq %rsi, %rax
subq %rdi, %rax
sarq $0x5, %rax
movslq 0x8(%r15), %rcx
cmpq %rcx, %rax
jae 0x2815
movq 0x77e2(%rip), %rdi      # 0x9fc0
leaq 0x3a0f(%rip), %rsi      # 0x61f4
callq 0x2210
movq -0x68(%rbp), %rsi
subq -0x70(%rbp), %rsi
sarq $0x5, %rsi
movq %rax, %rdi
callq 0x2150
leaq 0x3a1a(%rip), %rsi      # 0x621f
movq %rax, %rdi
callq 0x2210
pushq $-0x1
popq %rbx
jmp 0x2c2c
movq %r15, -0x40(%rbp)
callq 0x37d5
xorl %ebx, %ebx
movq %rbx, %r12
movq -0x40(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
cmpq %rax, %rbx
jge 0x2c1e
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x3834
movq (%rax), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x3856
andq $0x0, -0x101a0(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x101c0(%rbp)
andl $0x0, -0x10198(%rbp)
andq $0x0, -0x10190(%rbp)
andq $0x0, -0x10180(%rbp)
leaq -0x148(%rbp), %rax
movups %xmm0, (%rax)
leaq -0x10148(%rbp), %rcx
movq %rcx, -0x10160(%rbp)
movq %rcx, -0x10158(%rbp)
movq %rax, -0x10150(%rbp)
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x3834
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x3834
movq 0x8(%rax), %rcx
addq $-0x9, %rcx
leaq -0xf8(%rbp), %rdi
movq %rbx, %rsi
pushq $0x5
popq %rdx
callq 0x2250
movaps 0x377f(%rip), %xmm0     # 0x6060
movaps %xmm0, -0x90(%rbp)
movq -0xb8(%rbp), %rsi
leaq -0x101c0(%rbp), %rdi
callq 0x39c8
leaq -0x101c0(%rbp), %rdi
leaq 0x3919(%rip), %rsi      # 0x6222
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3a9e
movq %rax, %rbx
movq %rax, %rdi
callq 0x3b37
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0xa0(%rbp)
cmpq %rax, -0x78(%rbp)
jae 0x29b2
movq 0x7682(%rip), %rdi      # 0x9fc0
leaq 0x38e3(%rip), %rsi      # 0x6228
callq 0x2210
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x3834
movq %rbx, %rdi
movq %rax, %rsi
callq 0x21c0
movq %rax, %rdi
leaq 0x37ee(%rip), %rsi      # 0x615c
callq 0x2210
leaq 0x1(%r12), %rbx
leaq -0xf8(%rbp), %rdi
callq 0x20d0
leaq -0x10160(%rbp), %rdi
callq 0x3d56
leaq -0xb8(%rbp), %rdi
callq 0x3dd6
movq -0xa0(%rbp), %rax
cmpq %rax, -0x78(%rbp)
jae 0x2820
jmp 0x2c1e
movq %rbx, %rdi
leaq 0x389c(%rip), %rsi      # 0x6258
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3a9e
movq %rax, -0x38(%rbp)
movq %r12, -0x48(%rbp)
pushq $0x1
popq %r12
movq %r12, -0x50(%rbp)
movq -0x48(%rbp), %r12
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x2973
leaq 0x38fd(%rip), %rsi      # 0x62e7
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3b6a
movq -0x38(%rbp), %rdi
callq 0x3b37
pushq $0x1
popq %rcx
movl %ecx, %r13d
cmpq $0x2, %rax
jb 0x2a2a
callq 0x2070
movl %eax, %ebx
movq -0x38(%rbp), %rdi
callq 0x3b37
movq %rax, %rcx
movslq %ebx, %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %r13
incl %r13d
movq -0x38(%rbp), %rdi
leaq 0x382a(%rip), %rsi      # 0x625f
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3a9e
movq %rax, %rbx
pushq $0x1
popq %r14
cmpl %r14d, %r13d
jle 0x2a62
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3bf6
movq %rax, %rbx
incl %r14d
jmp 0x2a46
movq %rbx, %rdi
leaq 0x37f8(%rip), %rsi      # 0x6264
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3a9e
movq 0x8(%rax), %r13
testq %r13, %r13
leaq 0x77a7(%rip), %rax      # 0xa22b
cmoveq %rax, %r13
movl $0xd0, %edi
callq 0x2220
movq %rax, %r14
leaq -0x118(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x22d0
leaq -0xd8(%rbp), %rdi
movq %r13, %rsi
leaq -0x29(%rbp), %rdx
callq 0x376e
movq -0x50(%rbp), %r13
imull $0x64, %r13d, %edx
movq %r14, %rdi
movq %r14, %r15
leaq -0x118(%rbp), %r14
movq %r14, %rsi
leaq -0xd8(%rbp), %rcx
callq 0x5768
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r12,8), %rax
movq %r15, -0x8(%rax,%r13,8)
leaq -0xd8(%rbp), %rdi
callq 0x20d0
movq %r14, %rdi
callq 0x20d0
leaq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x3ca1
movq %rbx, %rdi
leaq 0x374f(%rip), %rsi      # 0x626d
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3a9e
movq %rax, %rdi
leaq 0x3743(%rip), %rsi      # 0x6275
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3a9e
movq %rax, %rbx
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0x2c01
movq %rbx, %rdi
leaq 0x3723(%rip), %rsi      # 0x6279
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3b6a
testq %rax, %rax
movq -0x40(%rbp), %r14
movq -0x50(%rbp), %r13
je 0x2b87
movq 0x10(%r14), %rax
movq (%rax,%r12,8), %rax
movq -0x8(%rax,%r13,8), %rdi
movl -0x90(%rbp,%r15,4), %esi
callq 0x59ac
movq 0x10(%r14), %rax
movq (%rax,%r12,8), %rax
movq -0x8(%rax,%r13,8), %r13
movl -0x90(%rbp,%r15,4), %r14d
movq 0x8(%rbx), %rsi
testq %rsi, %rsi
leaq 0x7681(%rip), %rax      # 0xa22b
cmoveq %rax, %rsi
leaq -0x138(%rbp), %r12
movq %r12, %rdi
leaq -0x29(%rbp), %rdx
callq 0x376e
movq %r13, %rdi
movl %r14d, %esi
movq %r12, %rdx
callq 0x588a
movq %r12, %rdi
callq 0x20d0
cmpq $0x3, %r15
jne 0x2be3
movq -0x48(%rbp), %r12
jmp 0x2bf9
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3bf6
movq -0x48(%rbp), %r12
movq %rax, %rbx
incq %r15
jmp 0x2b42
movq -0x38(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x3bf6
movq %rax, -0x38(%rbp)
incq -0x50(%rbp)
jmp 0x29da
xorl %ebx, %ebx
cmpq -0x98(%rbp), %r12
setl %bl
negl %ebx
leaq -0x70(%rbp), %rdi
callq 0x37aa
movl %ebx, %eax
addq $0x10198, %rsp          # imm = 0x10198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c4d
jmp 0x2c4d
movq %rax, %rbx
jmp 0x2cd5
movq %rax, %rbx
jmp 0x2cbd
jmp 0x2cae
movq %rax, %rbx
jmp 0x2c84
movq %rax, %rbx
jmp 0x2c78
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x20d0
movq %r15, %r14
leaq -0x118(%rbp), %rdi
callq 0x20d0
movl $0xd0, %esi
movq %r14, %rdi
callq 0x2230
jmp 0x2cb1
jmp 0x2cae
jmp 0x2cae
jmp 0x2cae
jmp 0x2cae
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
callq 0x20d0
jmp 0x2cb1
jmp 0x2cae
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x20d0
leaq -0x10160(%rbp), %rdi
callq 0x3d56
leaq -0xb8(%rbp), %rdi
callq 0x3dd6
leaq -0x70(%rbp), %rdi
callq 0x37aa
movq %rbx, %rdi
callq 0x2390