// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rdi, %r13
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1861(%rip), %rsi      # 0x50f8
leaq 0x185f(%rip), %rdx      # 0x50fd
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x37cc
movq (%r13), %rsi
movq %r13, 0x30(%rsp)
movq 0x8(%r13), %rdx
movq %rbx, %rdi
callq 0x2050
leaq 0x1837(%rip), %rsi      # 0x50fe
leaq 0x10(%rsp), %rdi
callq 0x2200
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1826(%rip), %rsi      # 0x5107
leaq 0x184b(%rip), %rdx      # 0x5133
leaq 0x38(%rsp), %rdi
callq 0x37cc
leaq 0x183b(%rip), %rsi      # 0x5134
leaq 0x38(%rsp), %rdi
callq 0x2200
movq 0x46c6(%rip), %rdi      # 0x7fd0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x46ab(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq 0x10(%rsp), %rsi
movq 0x18(%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 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x4651(%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 %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2b64
xorl %ebp, %ebp
xorl %ebx, %ebx
testb $0x1, %bl
jne 0x3a4c
movq (%r12), %rax
cmpl $0x0, 0x174(%rax)
je 0x39e4
xorl %eax, %eax
testl %r13d, %r13d
setg %al
subl %eax, %r13d
jmp 0x3a22
cmpl $0x0, 0x178(%rax)
je 0x39fb
leal 0x1(%r13), %eax
cmpl 0xc(%rsp), %eax
cmovll %eax, %r13d
jmp 0x3a22
cmpl $0x0, 0x16c(%rax)
je 0x3a0f
xorl %eax, %eax
testl %ebp, %ebp
setg %al
subl %eax, %ebp
jmp 0x3a22
cmpl $0x0, 0x170(%rax)
je 0x3a3d
leal 0x1(%rbp), %eax
cmpl 0x8(%rsp), %eax
cmovll %eax, %ebp
xorl %ebx, %ebx
movq %r14, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x2b64
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2170
jmp 0x39c1
movb $0x1, %bl
cmpl $0x0, 0x17c(%rax)
jne 0x3a24
xorl %ebx, %ebx
jmp 0x3a31
movq 0x457d(%rip), %rbx      # 0x7fd0
leaq 0x16ff(%rip), %rsi      # 0x5159
movl $0x2c, %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 0x454b(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq 0x4537(%rip), %rbx      # 0x7fd0
leaq 0x1698(%rip), %rsi      # 0x5138
movl $0x20, %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 0x4505(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x20b0
xorl %r15d, %r15d
xorl %ebx, %ebx
testb $0x1, %r15b
jne 0x3b59
movq (%r12), %rax
cmpl $0x0, 0x174(%rax)
je 0x3af7
movl $0x2, %ebx
jmp 0x3b25
cmpl $0x0, 0x178(%rax)
je 0x3b07
movl $0x4, %ebx
jmp 0x3b25
cmpl $0x0, 0x16c(%rax)
je 0x3b17
movl $0x1, %ebx
jmp 0x3b25
cmpl $0x0, 0x170(%rax)
je 0x3b43
movl $0x3, %ebx
xorl %r15d, %r15d
movq %r14, %rdi
movl %r13d, %esi
movl %ebp, %edx
movl %ebx, %ecx
callq 0x2b6c
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2170
jmp 0x3add
cmpl $0x0, 0x17c(%rax)
je 0x3b54
testl %ebx, %ebx
setne %r15b
jmp 0x3b28
xorl %r15d, %r15d
jmp 0x3b37
leaq 0x88(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x30(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
callq 0x25cc
leaq 0x78(%rsp), %r8
movq %r14, %rdi
movl %r13d, %esi
movl %ebp, %edx
movl %ebx, %ecx
callq 0x2a90
movl %eax, %ebp
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x3bae
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2140
testl %ebp, %ebp
jns 0x3c50
movq 0x4413(%rip), %rbx      # 0x7fd0
leaq 0x15c2(%rip), %rsi      # 0x5186
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 0x43e1(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x20b0
leaq 0x68(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x30(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x25cc
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r8d
callq 0x3864
movl %eax, %ebp
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x3c50
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2140
testl %ebp, %ebp
je 0x3cb6
movq %r14, %rdi
callq 0x2b48
testb %al, %al
jne 0x3cb6
movq 0x30(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x435d(%rip), %rdi      # 0x7fd0
callq 0x2150
movq %rax, %rbx
leaq 0x1540(%rip), %rsi      # 0x51c2
movl $0x2e, %edx
movq %rax, %rdi
callq 0x2150
movq (%rbx), %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 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x3cd2
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x3cee
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2140
movl %ebp, %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 %r15, %rdi
je 0x3d4c
movq 0x68(%rsp), %rsi
jmp 0x3d2b
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x3d4c
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2140
jmp 0x3d4c
jmp 0x3d37
movq %rax, %rbx
jmp 0x3d6d
jmp 0x3d49
jmp 0x3d49
jmp 0x3d49
movq %rax, %rbx
jmp 0x3d51
jmp 0x3d49
movq %rax, %rbx
leaq 0x20(%rsp), %r15
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x3d6d
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x3d84
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x21d0