// File: /workspace/github_cpp_makefile_star2_O2/juancalaca[P]dotsboxes/main.cc
// Function: TakeTurn(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, DotsBoard*, ZedBoard*, int, int)
int TakeTurn(string name, DotsBoard *gameBoard, ZedBoard *z, int rows, int columns)
{
	//create a string indicating whose turn it is
	string turn_string = "It's ";
	turn_string.append(name);
	turn_string.append("'s turn!");
	
	// tell the player how to make a selection
	string box_directions = "Please use the push buttons to select a box\n";
	box_directions.append("and hit the center button to confirm");
	
	// print out turn indicator
	cout << endl << turn_string << endl;
	
	int selected_row = 0;
	int selected_column = 0;
	
	// indicates whether or not a final selection has been made
	bool confirmed = false;
	
	// Place to hold the value of the pushbutton we pressed - need two for comparison purposes
	int push_value = 0;
	int new_push = 0;
	
	//print directions
	cout << box_directions << endl;

	//print the board so they have some idea what's going on
	gameBoard->PrintSelect(selected_row, selected_column);

	//Infinite loop for selecting a box
	while(!confirmed)
	{
		new_push = z->PushButtonGet(push_value);
		
		if (new_push != push_value)
		{
			switch(new_push)
			{
				case 1: // Up - go up a row
					if ((selected_row - 1) >= 0)
					{
						selected_row -= 1;
					}
					break;
				case 2: // Down - go down a row
					if((selected_row + 1) < rows) {
						selected_row += 1;
					}
					break;
				case 3: // Left - go one column to the left
					if ((selected_column - 1) >= 0)
					{
						selected_column -= 1;
					}
					break;
				case 4: // Right - go one column to the right
					if((selected_column + 1) < columns) {
						selected_column += 1;
					}
					break;
				case 5: // Center - user has confirmed selection
					confirmed = true;
			}
			
			//only print the board on a button press
			gameBoard->PrintSelect(selected_row, selected_column);
		}
		//Sleep for half a second then repeat
		usleep(.5 * 1000000);
	}
	
	// reset the confirmed flag to enter the next loop
	confirmed = false;
	
	//reset the button values for the new loop
	push_value = 0;
	new_push = 0;
	
	// indicate which edge has been selected
	// 0 is nonsense, 1 = left, 2 = up, 3 = right, 4 = down
	int edge = 0;
	
	// print directions
	cout << "Please choose an edge using the push buttons" << endl;
	cout << "hit the center button to confirm" << endl;

	//Infinite loop for selecting an edge based on the box
	while(!confirmed)
	{
		
		new_push = z->PushButtonGet(push_value);
		
		if (new_push != push_value)
		{
			switch(new_push)
			{
				case 1: // Up - edge = 2
					edge = 2;
					break;
				case 2: // Down - edge = 4
					edge = 4;
					break;
				case 3: // Left - edge = 1
					edge = 1;
					break;
				case 4: // Right - edge = 3
					edge = 3;
					break;
				case 5: // Center - user has confirmed selection
					//double check that an edge has been selected
					confirmed = (edge != 0);
			}
			
			// only print on button press	
			gameBoard->PrintSelectEdge(selected_row, selected_column, edge);

		}
		//Sleep for half a second then repeat
		usleep(.5 * 1000000);
	}
	
	
	//now that we actually have all of the data, tell the board the edge is claimed
	int claimed_boxes = gameBoard->ClaimEdge(selected_row, selected_column, edge, name);
	
	if (claimed_boxes < 0)
	{
		// edge was taken, try again
		cout << "That edge has already been taken, please select another one" << endl;
		claimed_boxes = TakeTurn(name, gameBoard, z, rows, columns);
	}
	if (claimed_boxes > 0 && (!gameBoard->GameOver())) {
		cout << name << " gets another turn because they claimed a box!" << endl; 
	}
	
	return claimed_boxes;	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %r8d, 0x1c(%rsp)
movl %ecx, 0x18(%rsp)
movq %rdx, %r12
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
leaq 0xcf4(%rip), %rsi       # 0x4130
leaq 0x48(%rsp), %rbx
leaq 0x28(%rsp), %rdx
movq %rbx, %rdi
callq 0x2dd2
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2210
leaq 0xcd6(%rip), %rsi       # 0x4136
leaq 0x48(%rsp), %rdi
callq 0x21f0
leaq 0xcce(%rip), %rsi       # 0x413f
leaq 0x28(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x2dd2
leaq 0xce5(%rip), %rsi       # 0x416c
leaq 0x28(%rsp), %rdi
callq 0x21f0
movq 0x3b38(%rip), %rdi      # 0x6fd0
callq 0x2080
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2080
movq 0x3b17(%rip), %rdi      # 0x6fd0
leaq 0x28(%rsp), %rsi
callq 0x2110
movq %rax, %rdi
callq 0x2080
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2926
leaq 0xc1d(%rip), %rbp       # 0x4100
xorl %ebx, %ebx
xorl %r15d, %r15d
testb $0x1, %r15b
jne 0x355f
movq %r12, %rdi
xorl %esi, %esi
callq 0x3992
xorl %r15d, %r15d
cmpl $0x5, %eax
ja 0x353f
movl %eax, %eax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
xorl %eax, %eax
testl %r13d, %r13d
setg %al
subl %eax, %r13d
jmp 0x353c
leal 0x1(%rbx), %eax
cmpl 0x1c(%rsp), %eax
cmovll %eax, %ebx
jmp 0x353c
leal 0x1(%r13), %eax
cmpl 0x18(%rsp), %eax
cmovll %eax, %r13d
jmp 0x353c
xorl %eax, %eax
testl %ebx, %ebx
setg %al
subl %eax, %ebx
xorl %r15d, %r15d
movq 0x10(%rsp), %rdi
movl %r13d, %esi
movl %ebx, %edx
callq 0x2926
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2170
jmp 0x34e8
movb $0x1, %r15b
jmp 0x353f
movq %r14, 0x20(%rsp)
movq 0x3a65(%rip), %rdi      # 0x6fd0
leaq 0xc1f(%rip), %rsi       # 0x4191
callq 0x2120
movq %rax, %rdi
callq 0x2080
movq 0x3a4a(%rip), %rdi      # 0x6fd0
leaq 0xbe3(%rip), %rsi       # 0x4170
callq 0x2120
movq %rax, %rdi
callq 0x2080
xorl %r14d, %r14d
leaq 0xb74(%rip), %r15       # 0x4118
xorl %ebp, %ebp
testb $0x1, %r14b
jne 0x3609
movq %r12, %rdi
xorl %esi, %esi
callq 0x3992
xorl %r14d, %r14d
cmpl $0x5, %eax
ja 0x35e4
movl %eax, %eax
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
xorl %r14d, %r14d
pushq $0x2
jmp 0x35e3
xorl %r14d, %r14d
pushq $0x3
jmp 0x35e3
xorl %r14d, %r14d
pushq $0x4
jmp 0x35e3
xorl %r14d, %r14d
pushq $0x1
popq %rbp
movq 0x10(%rsp), %rdi
movl %r13d, %esi
movl %ebx, %edx
movl %ebp, %ecx
callq 0x292e
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2170
jmp 0x35a6
testl %ebp, %ebp
setne %r14b
jmp 0x35e4
leaq 0x88(%rsp), %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x2160
leaq 0x88(%rsp), %r8
movq 0x10(%rsp), %rdi
movl %r13d, %esi
movl %ebx, %edx
movl %ebp, %ecx
callq 0x2886
movl %eax, %ebx
leaq 0x88(%rsp), %rdi
callq 0x2070
testl %ebx, %ebx
jns 0x3699
movq 0x397f(%rip), %rdi      # 0x6fd0
leaq 0xb66(%rip), %rsi       # 0x41be
callq 0x2120
movq %rax, %rdi
callq 0x2080
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0x2160
leaq 0x68(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
movl 0x18(%rsp), %ecx
movl 0x1c(%rsp), %r8d
callq 0x3410
movl %eax, %ebx
leaq 0x68(%rsp), %rdi
callq 0x2070
testl %ebx, %ebx
je 0x36d1
movq 0x10(%rsp), %rdi
callq 0x2912
testb %al, %al
jne 0x36d1
movq 0x391e(%rip), %rdi      # 0x6fd0
movq %r14, %rsi
callq 0x2110
leaq 0xb39(%rip), %rsi       # 0x41fa
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x2080
leaq 0x28(%rsp), %rdi
callq 0x2070
leaq 0x48(%rsp), %rdi
callq 0x2070
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0x370e
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x2070
jmp 0x3729
jmp 0x3717
movq %rax, %rbx
jmp 0x3733
jmp 0x3726
jmp 0x3726
jmp 0x3726
jmp 0x3726
jmp 0x3726
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x2070
leaq 0x48(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x21c0