// File: Doug0855[P]TigerZone/Board.cpp
// Function: Board::getOptimalPlacement(Tile&, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>)
std::pair<int,int> Board::getOptimalPlacement(Tile &tile, std::vector< std::pair<int, int> > availableMoves)
{
	// Check that there's more than 1 possible place to put the tile
	if (availableMoves.size() > 1)
	{
		// Keep track of the orientation of the tile for when we find the best location
		int bestRotation = 0;
		std::pair<int, int> bestSpot;
		int mostPoints = -1; // set this to -1 otherwise if points is never > mostPoints bestSpot won't be selected
		for (size_t z = 0; z < availableMoves.size(); z++)
		{
			// set the row and col (i and j)
			int i = availableMoves[z].first;
			int j = availableMoves[z].second;
			// Check the available location with all possible tile rotations
			for (int q = 0; q < 4; q++)
			{
				int points = 0;
				// double check that the tile can connect to all neighbors and run the positionPoints method if it can
				if ((m_board[i + 1][j] == NULL || m_board[i + 1][j]->getUpFace()->faceEquals(*tile.getDownFace())) &&
				(m_board[i - 1][j] == NULL || m_board[i - 1][j]->getDownFace()->faceEquals(*tile.getUpFace())) &&
				(m_board[i][j + 1] == NULL || m_board[i][j + 1]->getLeftFace()->faceEquals(*tile.getRightFace())) &&
				(m_board[i][j - 1] == NULL || m_board[i][j - 1]->getRightFace()->faceEquals(*tile.getLeftFace())))
				{
					points = positionPoints(i, j);
				}
				else
				{
					points = -1;
				}
				if (points > mostPoints)
				{
					mostPoints = points;
					bestSpot = availableMoves[z];
					bestRotation = q;
				}
				tile.rotate();
			}
		}
		for (int i = 0; i < bestRotation; i++)
		{
			tile.rotate();
		}
		return bestSpot;
	}
	// If there was only 1 available location to place the tile
	else
	{
		int i = availableMoves[0].first;
		int j = availableMoves[0].second;
		for (int q = 0; q < 4; q++)
		{
			if ((m_board[i + 1][j] == NULL || m_board[i + 1][j]->getUpFace()->faceEquals(*tile.getDownFace())) &&
			(m_board[i - 1][j] == NULL || m_board[i - 1][j]->getDownFace()->faceEquals(*tile.getUpFace())) &&
			(m_board[i][j + 1] == NULL || m_board[i][j + 1]->getLeftFace()->faceEquals(*tile.getRightFace())) &&
			(m_board[i][j - 1] == NULL || m_board[i][j - 1]->getRightFace()->faceEquals(*tile.getLeftFace())))
			{
				//debugging std::cout << "Location " << i << ' ' << j << " passed optimalPlacement checks of connecting to all surrounding tiles" << std::endl;
				break;
			}
			else
			{
				tile.rotate();
			}
		}
		return availableMoves[0];
	}
	return availableMoves[0];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movq %rdx, -0x1f8(%rbp)
movq %rdi, %rax
movq -0x1f8(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
movq %rax, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdi, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1e8(%rbp)
callq 0x15160
cmpq $0x1, %rax
jbe 0x1a3f9
movl $0x0, -0x24(%rbp)
leaq -0x8(%rbp), %rdi
callq 0xa050
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x30(%rbp)
movq -0x1f0(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x200(%rbp)
callq 0x15160
movq %rax, %rcx
movq -0x200(%rbp), %rax
cmpq %rcx, %rax
jae 0x1a3c5
movq -0x1f0(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x15180
movq -0x1f0(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rsi
callq 0x15180
movl 0x4(%rax), %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jge 0x1a3b2
movq -0x1e8(%rbp), %rdi
movl $0x0, -0x40(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x81d0
movq %rax, %rdi
movslq -0x38(%rbp), %rsi
callq 0x8230
movq (%rax), %rax
movb $0x0, -0x69(%rbp)
movb $0x0, -0xa9(%rbp)
movb $0x0, -0xd9(%rbp)
movb $0x0, -0x109(%rbp)
cmpq $0x0, %rax
je 0x19ffe
movq -0x1e8(%rbp), %rdi
movl -0x34(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x81d0
movq %rax, %rdi
movslq -0x38(%rbp), %rsi
callq 0x8230
movq (%rax), %rdi
callq 0x203a0
movq %rax, -0x218(%rbp)
movq -0x18(%rbp), %rdi
callq 0x203c0
movq %rax, %rsi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x8e70
movq -0x218(%rbp), %rdi
movq -0x210(%rbp), %rsi
movb $0x1, -0x69(%rbp)
callq 0x281c0
movb %al, -0x201(%rbp)
jmp 0x19fe6
movb -0x201(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x219(%rbp)
jne 0x19ffe
jmp 0x1a242
movq -0x1e8(%rbp), %rdi
movl -0x34(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x81d0
movq %rax, %rdi
movslq -0x38(%rbp), %rsi
callq 0x8230
cmpq $0x0, (%rax)
je 0x1a0bb
movq -0x1e8(%rbp), %rdi
movl -0x34(%rbp), %eax
decl %eax
movslq %eax, %rsi
callq 0x81d0
movq %rax, %rdi
movslq -0x38(%rbp), %rsi
callq 0x8230
movq (%rax), %rdi
callq 0x203c0
movq %rax, -0x228(%rbp)
jmp 0x1a05a
movq -0x18(%rbp), %rdi
callq 0x203a0
movq %rax, -0x230(%rbp)
jmp 0x1a06c
movq -0x230(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x8e70
jmp 0x1a081
movq -0x228(%rbp), %rdi
movb $0x1, -0xa9(%rbp)
leaq -0xa8(%rbp), %rsi
callq 0x281c0
movb %al, -0x231(%rbp)
jmp 0x1a0a3
movb -0x231(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x219(%rbp)
jne 0x1a0bb
jmp 0x1a242
movq -0x1e8(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x81d0
movq %rax, %rdi
movl -0x38(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x8230
cmpq $0x0, (%rax)
je 0x1a178
movq -0x1e8(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x81d0
movq %rax, %rdi
movl -0x38(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x8230
movq (%rax), %rdi
callq 0x20400
movq %rax, -0x240(%rbp)
jmp 0x1a117
movq -0x18(%rbp), %rdi
callq 0x203e0
movq %rax, -0x248(%rbp)
jmp 0x1a129
movq -0x248(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x8e70
jmp 0x1a13e
movq -0x240(%rbp), %rdi
movb $0x1, -0xd9(%rbp)
leaq -0xd8(%rbp), %rsi
callq 0x281c0
movb %al, -0x249(%rbp)
jmp 0x1a160
movb -0x249(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x219(%rbp)
jne 0x1a178
jmp 0x1a242
movq -0x1e8(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x81d0
movq %rax, %rdi
movl -0x38(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8230
movq %rax, %rcx
movb $0x1, %al
cmpq $0x0, (%rcx)
movb %al, -0x24a(%rbp)
je 0x1a236
movq -0x1e8(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x81d0
movq %rax, %rdi
movl -0x38(%rbp), %eax
decl %eax
movslq %eax, %rsi
callq 0x8230
movq (%rax), %rdi
callq 0x203e0
movq %rax, -0x258(%rbp)
jmp 0x1a1df
movq -0x18(%rbp), %rdi
callq 0x20400
movq %rax, -0x260(%rbp)
jmp 0x1a1f1
movq -0x260(%rbp), %rsi
leaq -0x108(%rbp), %rdi
callq 0x8e70
jmp 0x1a206
movq -0x258(%rbp), %rdi
movb $0x1, -0x109(%rbp)
leaq -0x108(%rbp), %rsi
callq 0x281c0
movb %al, -0x261(%rbp)
jmp 0x1a228
movb -0x261(%rbp), %al
movb %al, -0x24a(%rbp)
jmp 0x1a236
movb -0x24a(%rbp), %al
movb %al, -0x219(%rbp)
movb -0x219(%rbp), %al
movb %al, -0x262(%rbp)
testb $0x1, -0x109(%rbp)
jne 0x1a259
jmp 0x1a265
leaq -0x108(%rbp), %rdi
callq 0x28160
testb $0x1, -0xd9(%rbp)
jne 0x1a270
jmp 0x1a27c
leaq -0xd8(%rbp), %rdi
callq 0x28160
testb $0x1, -0xa9(%rbp)
jne 0x1a287
jmp 0x1a293
leaq -0xa8(%rbp), %rdi
callq 0x28160
testb $0x1, -0x69(%rbp)
jne 0x1a29b
jmp 0x1a2a4
leaq -0x68(%rbp), %rdi
callq 0x28160
movb -0x262(%rbp), %al
testb $0x1, %al
jne 0x1a2b3
jmp 0x1a364
movq -0x1e8(%rbp), %rdi
movl -0x34(%rbp), %esi
movl -0x38(%rbp), %edx
callq 0x1a8f0
movl %eax, -0x40(%rbp)
jmp 0x1a36b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x1a34e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x1a335
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x1a31c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
testb $0x1, -0x109(%rbp)
jne 0x1a30e
jmp 0x1a31a
leaq -0x108(%rbp), %rdi
callq 0x28160
jmp 0x1a31c
testb $0x1, -0xd9(%rbp)
jne 0x1a327
jmp 0x1a333
leaq -0xd8(%rbp), %rdi
callq 0x28160
jmp 0x1a335
testb $0x1, -0xa9(%rbp)
jne 0x1a340
jmp 0x1a34c
leaq -0xa8(%rbp), %rdi
callq 0x28160
jmp 0x1a34e
testb $0x1, -0x69(%rbp)
jne 0x1a356
jmp 0x1a35f
leaq -0x68(%rbp), %rdi
callq 0x28160
jmp 0x1a8e0
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
movl -0x40(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x1a39b
movq -0x1f0(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x30(%rbp), %rsi
callq 0x15180
movq %rax, %rsi
leaq -0x8(%rbp), %rdi
callq 0x15ef0
movl -0x3c(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rdi
callq 0x268d0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x19f24
jmp 0x1a3b4
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x19ec8
movl $0x0, -0x110(%rbp)
movl -0x110(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1a3f4
movq -0x18(%rbp), %rdi
callq 0x268d0
movl -0x110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x110(%rbp)
jmp 0x1a3cf
jmp 0x1a8d3
movq -0x1f0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x15180
movq -0x1f0(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x114(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x15180
movl 0x4(%rax), %eax
movl %eax, -0x118(%rbp)
movl $0x0, -0x11c(%rbp)
cmpl $0x4, -0x11c(%rbp)
jge 0x1a8bc
movq -0x1e8(%rbp), %rdi
movl -0x114(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x81d0
movq %rax, %rdi
movslq -0x118(%rbp), %rsi
callq 0x8230
movq (%rax), %rax
movb $0x0, -0x149(%rbp)
movb $0x0, -0x179(%rbp)
movb $0x0, -0x1a9(%rbp)
movb $0x0, -0x1d9(%rbp)
cmpq $0x0, %rax
je 0x1a51f
movq -0x1e8(%rbp), %rdi
movl -0x114(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x81d0
movq %rax, %rdi
movslq -0x118(%rbp), %rsi
callq 0x8230
movq (%rax), %rdi
callq 0x203a0
movq %rax, -0x278(%rbp)
movq -0x18(%rbp), %rdi
callq 0x203c0
movq %rax, %rsi
leaq -0x148(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x8e70
movq -0x278(%rbp), %rdi
movq -0x270(%rbp), %rsi
movb $0x1, -0x149(%rbp)
callq 0x281c0
movb %al, -0x263(%rbp)
jmp 0x1a507
movb -0x263(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x279(%rbp)
jne 0x1a51f
jmp 0x1a787
movq -0x1e8(%rbp), %rdi
movl -0x114(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x81d0
movq %rax, %rdi
movslq -0x118(%rbp), %rsi
callq 0x8230
cmpq $0x0, (%rax)
je 0x1a5e8
movq -0x1e8(%rbp), %rdi
movl -0x114(%rbp), %eax
decl %eax
movslq %eax, %rsi
callq 0x81d0
movq %rax, %rdi
movslq -0x118(%rbp), %rsi
callq 0x8230
movq (%rax), %rdi
callq 0x203c0
movq %rax, -0x288(%rbp)
jmp 0x1a587
movq -0x18(%rbp), %rdi
callq 0x203a0
movq %rax, -0x290(%rbp)
jmp 0x1a599
movq -0x290(%rbp), %rsi
leaq -0x178(%rbp), %rdi
callq 0x8e70
jmp 0x1a5ae
movq -0x288(%rbp), %rdi
movb $0x1, -0x179(%rbp)
leaq -0x178(%rbp), %rsi
callq 0x281c0
movb %al, -0x291(%rbp)
jmp 0x1a5d0
movb -0x291(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x279(%rbp)
jne 0x1a5e8
jmp 0x1a787
movq -0x1e8(%rbp), %rdi
movslq -0x114(%rbp), %rsi
callq 0x81d0
movq %rax, %rdi
movl -0x118(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x8230
cmpq $0x0, (%rax)
je 0x1a6b1
movq -0x1e8(%rbp), %rdi
movslq -0x114(%rbp), %rsi
callq 0x81d0
movq %rax, %rdi
movl -0x118(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x8230
movq (%rax), %rdi
callq 0x20400
movq %rax, -0x2a0(%rbp)
jmp 0x1a650
movq -0x18(%rbp), %rdi
callq 0x203e0
movq %rax, -0x2a8(%rbp)
jmp 0x1a662
movq -0x2a8(%rbp), %rsi
leaq -0x1a8(%rbp), %rdi
callq 0x8e70
jmp 0x1a677
movq -0x2a0(%rbp), %rdi
movb $0x1, -0x1a9(%rbp)
leaq -0x1a8(%rbp), %rsi
callq 0x281c0
movb %al, -0x2a9(%rbp)
jmp 0x1a699
movb -0x2a9(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x279(%rbp)
jne 0x1a6b1
jmp 0x1a787
movq -0x1e8(%rbp), %rdi
movslq -0x114(%rbp), %rsi
callq 0x81d0
movq %rax, %rdi
movl -0x118(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8230
movq %rax, %rcx
movb $0x1, %al
cmpq $0x0, (%rcx)
movb %al, -0x2aa(%rbp)
je 0x1a77b
movq -0x1e8(%rbp), %rdi
movslq -0x114(%rbp), %rsi
callq 0x81d0
movq %rax, %rdi
movl -0x118(%rbp), %eax
decl %eax
movslq %eax, %rsi
callq 0x8230
movq (%rax), %rdi
callq 0x203e0
movq %rax, -0x2b8(%rbp)
jmp 0x1a724
movq -0x18(%rbp), %rdi
callq 0x20400
movq %rax, -0x2c0(%rbp)
jmp 0x1a736
movq -0x2c0(%rbp), %rsi
leaq -0x1d8(%rbp), %rdi
callq 0x8e70
jmp 0x1a74b
movq -0x2b8(%rbp), %rdi
movb $0x1, -0x1d9(%rbp)
leaq -0x1d8(%rbp), %rsi
callq 0x281c0
movb %al, -0x2c1(%rbp)
jmp 0x1a76d
movb -0x2c1(%rbp), %al
movb %al, -0x2aa(%rbp)
jmp 0x1a77b
movb -0x2aa(%rbp), %al
movb %al, -0x279(%rbp)
movb -0x279(%rbp), %al
movb %al, -0x2c2(%rbp)
testb $0x1, -0x1d9(%rbp)
jne 0x1a79e
jmp 0x1a7aa
leaq -0x1d8(%rbp), %rdi
callq 0x28160
testb $0x1, -0x1a9(%rbp)
jne 0x1a7b5
jmp 0x1a7c1
leaq -0x1a8(%rbp), %rdi
callq 0x28160
testb $0x1, -0x179(%rbp)
jne 0x1a7cc
jmp 0x1a7d8
leaq -0x178(%rbp), %rdi
callq 0x28160
testb $0x1, -0x149(%rbp)
jne 0x1a7e3
jmp 0x1a7ef
leaq -0x148(%rbp), %rdi
callq 0x28160
movb -0x2c2(%rbp), %al
testb $0x1, %al
jne 0x1a7fe
jmp 0x1a89d
jmp 0x1a8bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x1a884
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x1a86b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x1a852
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
testb $0x1, -0x1d9(%rbp)
jne 0x1a844
jmp 0x1a850
leaq -0x1d8(%rbp), %rdi
callq 0x28160
jmp 0x1a852
testb $0x1, -0x1a9(%rbp)
jne 0x1a85d
jmp 0x1a869
leaq -0x1a8(%rbp), %rdi
callq 0x28160
jmp 0x1a86b
testb $0x1, -0x179(%rbp)
jne 0x1a876
jmp 0x1a882
leaq -0x178(%rbp), %rdi
callq 0x28160
jmp 0x1a884
testb $0x1, -0x149(%rbp)
jne 0x1a88f
jmp 0x1a89b
leaq -0x148(%rbp), %rdi
callq 0x28160
jmp 0x1a8e0
movq -0x18(%rbp), %rdi
callq 0x268d0
jmp 0x1a8a8
movl -0x11c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x11c(%rbp)
jmp 0x1a434
movq -0x1f0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x15180
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x34f0
nopl (%rax)