// File: 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
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0xe8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x2270
movq -0xe0(%rbp), %rdx
leaq 0xf79(%rip), %rsi       # 0x710c
leaq -0x40(%rbp), %rdi
callq 0x3bb0
jmp 0x619e
leaq -0x41(%rbp), %rdi
callq 0x21a0
movq -0xe8(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x22b0
jmp 0x61b9
leaq 0xf52(%rip), %rsi       # 0x7112
leaq -0x40(%rbp), %rdi
callq 0x22a0
jmp 0x61cb
leaq -0x79(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x2270
movq -0xf0(%rbp), %rdx
leaq 0xf32(%rip), %rsi       # 0x711b
leaq -0x78(%rbp), %rdi
callq 0x3bb0
jmp 0x61f4
leaq -0x79(%rbp), %rdi
callq 0x21a0
leaq 0xf44(%rip), %rsi       # 0x7148
leaq -0x78(%rbp), %rdi
callq 0x22a0
jmp 0x620f
movq 0x4dba(%rip), %rdi      # 0xafd0
movq 0x4da3(%rip), %rsi      # 0xafc0
callq 0x2180
movq %rax, -0xf8(%rbp)
jmp 0x622b
movq -0xf8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x2140
movq %rax, -0x100(%rbp)
jmp 0x6244
movq -0x100(%rbp), %rdi
movq 0x4d6e(%rip), %rsi      # 0xafc0
callq 0x2180
jmp 0x6259
movl $0x0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movb $0x0, -0x85(%rbp)
movl $0x0, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movq 0x4d44(%rip), %rdi      # 0xafd0
leaq -0x78(%rbp), %rsi
callq 0x2140
movq %rax, -0x108(%rbp)
jmp 0x629e
movq -0x108(%rbp), %rdi
movq 0x4d14(%rip), %rsi      # 0xafc0
callq 0x2180
jmp 0x62b3
movq -0x10(%rbp), %rdi
movl -0x80(%rbp), %esi
movl -0x84(%rbp), %edx
callq 0x2fd0
jmp 0x62c7
jmp 0x62c9
movb -0x85(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x62da
jmp 0x642e
movq -0x18(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0x6cf0
movl %eax, -0x10c(%rbp)
jmp 0x62f1
movl -0x10c(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x8c(%rbp), %eax
je 0x641d
movl -0x90(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x118(%rbp)
subl $0x4, %eax
ja 0x6407
movq -0x118(%rbp), %rax
leaq 0xdc1(%rip), %rcx       # 0x70f8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x21a0
jmp 0x679d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x6794
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x79(%rbp), %rdi
callq 0x21a0
jmp 0x6794
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x678b
movl -0x80(%rbp), %eax
subl $0x1, %eax
cmpl $0x0, %eax
jl 0x63aa
movl -0x80(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x6407
movl -0x80(%rbp), %eax
addl $0x1, %eax
cmpl -0x1c(%rbp), %eax
jge 0x63c0
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x6407
movl -0x84(%rbp), %eax
subl $0x1, %eax
cmpl $0x0, %eax
jl 0x63df
movl -0x84(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x6407
movl -0x84(%rbp), %eax
addl $0x1, %eax
cmpl -0x20(%rbp), %eax
jge 0x63fe
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x6407
movb $0x1, -0x85(%rbp)
movq -0x10(%rbp), %rdi
movl -0x80(%rbp), %esi
movl -0x84(%rbp), %edx
callq 0x2fd0
jmp 0x641b
jmp 0x641d
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2210
jmp 0x6429
jmp 0x62c9
movb $0x0, -0x85(%rbp)
movl $0x0, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movq 0x4b76(%rip), %rdi      # 0xafd0
leaq 0xd0c(%rip), %rsi       # 0x716d
callq 0x2150
movq %rax, -0x120(%rbp)
jmp 0x646f
movq -0x120(%rbp), %rdi
movq 0x4b43(%rip), %rsi      # 0xafc0
callq 0x2180
jmp 0x6484
movq 0x4b45(%rip), %rdi      # 0xafd0
leaq 0xcba(%rip), %rsi       # 0x714c
callq 0x2150
movq %rax, -0x128(%rbp)
jmp 0x64a0
movq -0x128(%rbp), %rdi
movq 0x4b12(%rip), %rsi      # 0xafc0
callq 0x2180
jmp 0x64b5
jmp 0x64b7
movb -0x85(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x64c8
jmp 0x6599
movq -0x18(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0x6cf0
movl %eax, -0x12c(%rbp)
jmp 0x64df
movl -0x12c(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x8c(%rbp), %eax
je 0x6588
movl -0x90(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x138(%rbp)
subl $0x4, %eax
ja 0x656c
movq -0x138(%rbp), %rax
leaq 0xbc3(%rip), %rcx       # 0x70e4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x2, -0x94(%rbp)
jmp 0x656c
movl $0x4, -0x94(%rbp)
jmp 0x656c
movl $0x1, -0x94(%rbp)
jmp 0x656c
movl $0x3, -0x94(%rbp)
jmp 0x656c
cmpl $0x0, -0x94(%rbp)
setne %al
andb $0x1, %al
movb %al, -0x85(%rbp)
movq -0x10(%rbp), %rdi
movl -0x80(%rbp), %esi
movl -0x84(%rbp), %edx
movl -0x94(%rbp), %ecx
callq 0x3000
jmp 0x6586
jmp 0x6588
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2210
jmp 0x6594
jmp 0x64b7
movq -0xe8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x144(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x140(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x13c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x2080
jmp 0x65da
movl -0x13c(%rbp), %ecx
movl -0x140(%rbp), %edx
movl -0x144(%rbp), %esi
movq -0x150(%rbp), %rdi
leaq -0xb8(%rbp), %r8
callq 0x2e90
movl %eax, -0x154(%rbp)
jmp 0x6607
leaq -0xb8(%rbp), %rdi
callq 0x20c0
movl -0x154(%rbp), %eax
movl %eax, -0x98(%rbp)
cmpl $0x0, -0x98(%rbp)
jge 0x66e9
movq 0x499d(%rip), %rdi      # 0xafd0
leaq 0xb60(%rip), %rsi       # 0x719a
callq 0x2150
movq %rax, -0x160(%rbp)
jmp 0x6648
movq -0x160(%rbp), %rdi
movq 0x496a(%rip), %rsi      # 0xafc0
callq 0x2180
jmp 0x665d
movq -0xe8(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x2080
jmp 0x6672
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
leaq -0xd8(%rbp), %rdi
callq 0x6150
movl %eax, -0x164(%rbp)
jmp 0x6695
movl -0x164(%rbp), %eax
movl %eax, -0x98(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x20c0
jmp 0x66e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x20c0
jmp 0x678b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x20c0
jmp 0x678b
cmpl $0x0, -0x98(%rbp)
jle 0x675e
movq -0x10(%rbp), %rdi
callq 0x2f80
movb %al, -0x165(%rbp)
jmp 0x6703
movb -0x165(%rbp), %al
testb $0x1, %al
jne 0x675e
jmp 0x670f
movq -0xe8(%rbp), %rsi
movq 0x48b3(%rip), %rdi      # 0xafd0
callq 0x2140
movq %rax, -0x170(%rbp)
jmp 0x672b
movq -0x170(%rbp), %rdi
leaq 0xa9d(%rip), %rsi       # 0x71d6
callq 0x2150
movq %rax, -0x178(%rbp)
jmp 0x6747
movq -0x178(%rbp), %rdi
movq 0x486b(%rip), %rsi      # 0xafc0
callq 0x2180
jmp 0x675c
jmp 0x675e
movl -0x98(%rbp), %eax
movl %eax, -0x17c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x20c0
leaq -0x40(%rbp), %rdi
callq 0x20c0
movl -0x17c(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
leaq -0x78(%rbp), %rdi
callq 0x20c0
leaq -0x40(%rbp), %rdi
callq 0x20c0
movq -0x50(%rbp), %rdi
callq 0x2260
nopw %cs:(%rax,%rax)