// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movl %r8d, 0x8(%rsp)
movl %ecx, 0xc(%rsp)
movq %rdx, %r12
movq %rsi, (%rsp)
movq %rdi, %r14
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1898(%rip), %rsi      # 0x5128
leaq 0x1896(%rip), %rdx      # 0x512d
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x37cc
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0x2050
leaq 0x1874(%rip), %rsi      # 0x512e
leaq 0x30(%rsp), %rdi
callq 0x2200
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1863(%rip), %rsi      # 0x5137
leaq 0x1888(%rip), %rdx      # 0x5163
leaq 0x10(%rsp), %rdi
callq 0x37cc
leaq 0x1878(%rip), %rsi      # 0x5164
leaq 0x10(%rsp), %rdi
callq 0x2200
movq 0x46d3(%rip), %rdi      # 0x7fd0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x46b8(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rax, %rdi
callq 0x2150
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq %r14, 0x50(%rsp)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x4659(%rip), %rdi      # 0x7fd0
callq 0x2150
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
xorl %r13d, %r13d
movq (%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2b4c
leaq 0x173b(%rip), %rbp      # 0x50f8
xorl %ebx, %ebx
xorl %r15d, %r15d
movl 0x8(%rsp), %r14d
testb $0x1, %r15b
jne 0x3a79
movq (%r12), %rax
movl $0x1, %ecx
cmpl $0x0, 0x174(%rax)
jne 0x3a1c
movl $0x2, %ecx
cmpl $0x0, 0x178(%rax)
jne 0x3a1c
movl $0x3, %ecx
cmpl $0x0, 0x16c(%rax)
jne 0x3a1c
movl $0x4, %ecx
cmpl $0x0, 0x170(%rax)
jne 0x3a1c
xorl %ecx, %ecx
cmpl $0x0, 0x17c(%rax)
setne %cl
leal (%rcx,%rcx,4), %ecx
movl %ecx, %eax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
xorl %eax, %eax
testl %r13d, %r13d
setg %al
subl %eax, %r13d
jmp 0x3a5c
leal 0x1(%rbx), %eax
cmpl %r14d, %eax
cmovll %eax, %ebx
jmp 0x3a5c
leal 0x1(%r13), %eax
cmpl 0xc(%rsp), %eax
cmovll %eax, %r13d
jmp 0x3a5c
xorl %eax, %eax
testl %ebx, %ebx
setg %al
subl %eax, %ebx
jmp 0x3a5c
movb $0x1, %r15b
movq (%rsp), %rdi
movl %r13d, %esi
movl %ebx, %edx
callq 0x2b4c
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2170
jmp 0x39c7
movq 0x4550(%rip), %rbp      # 0x7fd0
leaq 0x1702(%rip), %rsi      # 0x5189
movl $0x2c, %edx
movq %rbp, %rdi
callq 0x2150
movq (%rbp), %rax
addq -0x18(%rax), %rbp
movq %rbp, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x451d(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq 0x4509(%rip), %rbp      # 0x7fd0
leaq 0x169a(%rip), %rsi      # 0x5168
movl $0x20, %edx
movq %rbp, %rdi
callq 0x2150
movq (%rbp), %rax
addq -0x18(%rax), %rbp
movq %rbp, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x44d6(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x20b0
xorl %r14d, %r14d
leaq 0x15ff(%rip), %r15      # 0x5110
xorl %ebp, %ebp
testb $0x1, %r14b
jne 0x3bb4
movq (%r12), %rax
movl $0x1, %ecx
cmpl $0x0, 0x174(%rax)
jne 0x3b68
movl $0x2, %ecx
cmpl $0x0, 0x178(%rax)
jne 0x3b68
movl $0x3, %ecx
cmpl $0x0, 0x16c(%rax)
jne 0x3b68
movl $0x4, %ecx
cmpl $0x0, 0x170(%rax)
jne 0x3b68
xorl %ecx, %ecx
cmpl $0x0, 0x17c(%rax)
setne %cl
leal (%rcx,%rcx,4), %ecx
movl %ecx, %eax
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
movl $0x2, %ebp
jmp 0x3b95
movl $0x3, %ebp
jmp 0x3b95
movl $0x4, %ebp
jmp 0x3b95
movl $0x1, %ebp
jmp 0x3b95
testl %ebp, %ebp
setne %r14b
movq (%rsp), %rdi
movl %r13d, %esi
movl %ebx, %edx
movl %ebp, %ecx
callq 0x2b54
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2170
jmp 0x3b13
leaq 0x88(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x50(%rsp), %r15
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
callq 0x25bc
leaq 0x78(%rsp), %r8
movq (%rsp), %rdi
movl %r13d, %esi
movl %ebx, %edx
movl %ebp, %ecx
callq 0x2a78
movl %eax, %ebx
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x3c0a
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2140
testl %ebx, %ebx
jns 0x3ca8
movq 0x43b7(%rip), %rbx      # 0x7fd0
leaq 0x1596(%rip), %rsi      # 0x51b6
movl $0x3b, %edx
movq %rbx, %rdi
callq 0x2150
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x4385(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x20b0
leaq 0x68(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x25bc
leaq 0x58(%rsp), %rdi
movq (%rsp), %rsi
movq %r12, %rdx
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r8d
callq 0x385c
movl %eax, %ebx
movq 0x58(%rsp), %rdi
cmpq %r14, %rdi
je 0x3ca8
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2140
testl %ebx, %ebx
je 0x3d0a
movq (%rsp), %rdi
callq 0x2b30
testb %al, %al
jne 0x3d0a
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0x4309(%rip), %rdi      # 0x7fd0
callq 0x2150
movq %rax, %r14
leaq 0x151c(%rip), %rsi      # 0x51f2
movl $0x2e, %edx
movq %rax, %rdi
callq 0x2150
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x3d26
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3d42
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2140
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d9d
movq 0x68(%rsp), %rsi
jmp 0x3d7f
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d9d
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2140
jmp 0x3d9d
jmp 0x3d8b
movq %rax, %rbx
jmp 0x3db9
jmp 0x3d9a
jmp 0x3d9a
jmp 0x3d9a
jmp 0x3d9a
jmp 0x3d9a
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x3db9
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3dd5
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x21d0