/*
Boggle Environment - Board and Tile Class
Miguel Aroca-Ouellette
05/14/2016
*/

#include <iostream>
#include <stdlib.h>
#include <string>
#include <cassert>
#include <time.h>
#include "boggle_env.cuh"

#define NUM_LETTERS 26

/*Constructor: Set dimensions of board.*/
CUDA_CALLABLE_MEMBER
Board::Board(int _width, int _height)
{
	width = _width;
	height = _height;
	grid = (Tile *)malloc(sizeof(Tile)*height*width);	
}

/* Destructor */
CUDA_CALLABLE_MEMBER
Board::~Board()
{
	free(grid);
}

/*Copy construtor*/
CUDA_CALLABLE_MEMBER
Board::Board(const Board &obj)
{
	//printf("Copy constructor allocating grid.\n");
	width = obj.width;
	height = obj.height;
	grid = (Tile *)malloc(sizeof(Tile)*height*width);
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			grid[x + y*width].letter = obj.grid[x + y*width].letter;
			grid[x + y*width].x = x;
			grid[x + y*width].y = y;
			grid[x + y*width].used = false;
			for (int i = 0; i < NUM_ADJ; i++)
			{
				grid[x + y*width].adj_available[i] = true;
				grid[x + y*width].adj_list[i] = NULL;
			}
			getAllAdj(&grid[x + y*width], grid[x + y*width].adj_list);
		}
	}
}

/*Populates the board with random letters.*/
void Board::genRandLetters()
{
	//random seed

	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			//get random number between 0 and 25 -> letters
			char rand_char = 'a' + rand() % NUM_LETTERS;
			grid[x + y*width].letter = rand_char;
			grid[x + y*width].x = x;
			grid[x + y*width].y = y;
			grid[x + y*width].used = false;
			for (int i = 0; i < NUM_ADJ; i++)
			{
				grid[x + y*width].adj_available[i] = true;
				grid[x + y*width].adj_list[i] = NULL;
			}
			getAllAdj(&grid[x + y*width], grid[x + y*width].adj_list);
		}
	}
}

/*Set letters. Ensure that it is the right size.*/
void Board::setLetters(string letters)
{
	assert(letters.length() == width*height);

	int x, y;
	for (int i = 0; i < (width*height); i++)
	{
		x = i % width;
		y = i / width;
		grid[x+y*width].x = x;
		grid[x+y*width].y = y;
		grid[x+y*width].letter = letters[i];
		grid[x+y*width].used = false;
		for (int i = 0; i < NUM_ADJ; i++)
		{
			grid[x + y*width].adj_available[i] = true;
			grid[x + y*width].adj_list[i] = NULL;
		}
		getAllAdj(&grid[x + y*width], grid[x + y*width].adj_list);
	}
}

/* Returns list of adjancent tiles filtered by adj_available list */
void Board::getAdjList(Tile *center, Tile **adj)
{
	
	for (int i = 0; i < NUM_ADJ; i++)
	{
		if ((center->adj_list[i] != NULL) && (center->adj_available[i]))
		{
			adj[i] = center->adj_list[i];
		}
		else
			adj[i] = NULL;
	}
}

/*Get adjacent tile letters which are not used. Origin is top left.
Inputs: (x,y) coordinate of center tile.
Pointer to adjacency list to fill. MUST BE ABLE TO ACCOMODATE 8 LETTERS.
Returns indicates size of filled adjacenct list.*/
int Board::getAdj(Tile* center, Tile **adj)
{
	int size = 0;
	int x = center->x;
	int y = center->y;
	if ((x > 0) && (y > 0)) //top left
		pushTile(adj, x - 1, y - 1, &size, false);
	if ((x > 0) && (y < (height - 1))) //bottom left
		pushTile(adj, x - 1, y + 1, &size, false);
	if ((x < (width - 1)) && (y > 0)) //top right
		pushTile(adj, x + 1, y - 1, &size, false);
	if ((x < (width - 1)) && (y < (height - 1))) //bottom right
		pushTile(adj, x + 1, y + 1, &size, false);
	if (y > 0) //top center
		pushTile(adj, x, y - 1, &size, false);
	if (y < (height - 1)) //bottom center
		pushTile(adj, x, y + 1, &size, false);
	if (x > 0) //left center
		pushTile(adj, x - 1, y, &size, false);
	if (x < (width - 1)) //right center
		pushTile(adj, x + 1, y, &size, false);

	return size;
}

/*Private: Helper function for get_adj. Only assigns the tile if it is unused. Increments counts.*/
void Board::pushTile(Tile **target, int x, int y, int *count, bool all)
{
	if ((!grid[x+y*width].used) || all)
		target[(*count)++] = &grid[x+y*width];
}


/*Get adjacent tile letters which are not used. Origin is top left.
Inputs: (x,y) coordinate of center tile.
Pointer to adjacency list to fill. MUST BE ABLE TO ACCOMODATE 8 LETTERS.
Returns indicates size of filled adjacenct list.*/
int Board::getAllAdj(Tile* center, Tile **adj)
{
	int size = 0;
	int x = center->x;
	int y = center->y;
	if ((x > 0) && (y > 0)) //top left
		pushTile(adj, x - 1, y - 1, &size, true);
	if ((x > 0) && (y < (height - 1))) //bottom left
		pushTile(adj, x - 1, y + 1, &size, true);
	if ((x < (width - 1)) && (y > 0)) //top right
		pushTile(adj, x + 1, y - 1, &size, true);
	if ((x < (width - 1)) && (y < (height - 1))) //bottom right
		pushTile(adj, x + 1, y + 1, &size, true);
	if (y > 0) //top center
		pushTile(adj, x, y - 1, &size, true);
	if (y < (height - 1)) //bottom center
		pushTile(adj, x, y + 1, &size, true);
	if (x > 0) //left center
		pushTile(adj, x - 1, y, &size, true);
	if (x < (width - 1)) //right center
		pushTile(adj, x + 1, y, &size, true);

	return size;
}

/* Prints the boggle board*/
void Board::printBoard()
{
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
			printf("%c ",grid[x+y*width].letter);
		printf("\n");
	}
}

/* Prints the used tiles as 1, otherwise 0*/
void Board::printUsed()
{
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
			printf("%c ", ((grid[x + y*width].used) ? grid[x + y*width].letter : ' '));
		printf("\n");
	}
}

/*Getter for tile.*/
Tile* Board::getTile(int x, int y)
{
	return &grid[x+y*width];
}

/*Returns all tiles in the board.*/
void Board::getAllTiles(Tile *all_tiles[])
{
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
			all_tiles[y*width + x] = getTile(x, y);
	}
}

/* Resets all tiles on board to unused*/
void Board::resetBoard()
{
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			grid[x + y*width].used = false;
			for (int i = 0; i < NUM_ADJ; i++)
				grid[x + y*width].adj_available[i] = true;
		}
	}
}

/*Checks if the Tile is in the list of Tiles */
bool checkTileList(Tile *check, Tile **list, int size)
{
	for (int i = 0; i < size; i++)
	{
		if (check == list[i])
			return true;
	}
	return false;
}

/* Returns the number of tiles with this letter. */
int Board::getLetterCount(char c)
{
	int count = 0;
	for (int y = 0; y < height; y++)
	{
		for (int x = 0; x < width; x++)
		{
			if (grid[x + y*width].letter == c)
				count++;
		}
	}
	return count;
}

/*Returns all tiles in the board that match a given letter. 
  Should call getLetterCount before hand to get size.*/
void Board::getTilesByLetter(Tile *all_tiles[], char c)
{
	int i = 0;
	for (int x = 0; x < height*width; x++)
	{
		if (grid[x].letter == c)
			all_tiles[i++] = &grid[x];
	}
}
	.file	"tmpxft_00235d50_00000000-6_boggle_env.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3663:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3663:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.globl	_ZN5BoardC2Eii
	.type	_ZN5BoardC2Eii, @function
_ZN5BoardC2Eii:
.LFB3639:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movl	%esi, (%rdi)
	movslq	%esi, %rsi
	movl	%edx, 4(%rdi)
	movslq	%edx, %rdi
	movq	%rdi, %rdx
	imulq	%rsi, %rdx
	imulq	$88, %rdx, %rdi
	call	malloc@PLT
	movq	%rax, 8(%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3639:
	.size	_ZN5BoardC2Eii, .-_ZN5BoardC2Eii
	.globl	_ZN5BoardC1Eii
	.set	_ZN5BoardC1Eii,_ZN5BoardC2Eii
	.align 2
	.globl	_ZN5BoardD2Ev
	.type	_ZN5BoardD2Ev, @function
_ZN5BoardD2Ev:
.LFB3642:
	.cfi_startproc
	endbr64
	movq	8(%rdi), %rdi
	jmp	free@PLT
	.cfi_endproc
.LFE3642:
	.size	_ZN5BoardD2Ev, .-_ZN5BoardD2Ev
	.globl	_ZN5BoardD1Ev
	.set	_ZN5BoardD1Ev,_ZN5BoardD2Ev
	.align 2
	.globl	_ZN5Board10getAdjListEP4TilePS1_
	.type	_ZN5Board10getAdjListEP4TilePS1_, @function
_ZN5Board10getAdjListEP4TilePS1_:
.LFB3649:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	xorl	%edi, %edi
.L7:
	movq	24(%rsi,%rax,8), %rcx
	testq	%rcx, %rcx
	je	.L6
	cmpb	$0, 10(%rsi,%rax)
	cmove	%rdi, %rcx
.L6:
	movq	%rcx, (%rdx,%rax,8)
	incq	%rax
	cmpq	$8, %rax
	jne	.L7
	ret
	.cfi_endproc
.LFE3649:
	.size	_ZN5Board10getAdjListEP4TilePS1_, .-_ZN5Board10getAdjListEP4TilePS1_
	.align 2
	.globl	_ZN5Board8pushTileEPP4TileiiPib
	.type	_ZN5Board8pushTileEPP4TileiiPib, @function
_ZN5Board8pushTileEPP4TileiiPib:
.LFB3651:
	.cfi_startproc
	endbr64
	imull	(%rdi), %ecx
	addl	%edx, %ecx
	movslq	%ecx, %rax
	imulq	$88, %rax, %rax
	addq	8(%rdi), %rax
	cmpb	$1, 9(%rax)
	jne	.L17
	testb	%r9b, %r9b
	je	.L14
.L17:
	movslq	(%r8), %rdx
	leal	1(%rdx), %ecx
	movl	%ecx, (%r8)
	movq	%rax, (%rsi,%rdx,8)
.L14:
	ret
	.cfi_endproc
.LFE3651:
	.size	_ZN5Board8pushTileEPP4TileiiPib, .-_ZN5Board8pushTileEPP4TileiiPib
	.align 2
	.globl	_ZN5Board6getAdjEP4TilePS1_
	.type	_ZN5Board6getAdjEP4TilePS1_, @function
_ZN5Board6getAdjEP4TilePS1_:
.LFB3650:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rsi, %rax
	xorl	%r9d, %r9d
	movq	%rdx, %rsi
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movl	(%rax), %r11d
	movl	4(%rax), %r10d
	movq	%fs:40, %rdx
	movq	%rdx, 8(%rsp)
	xorl	%edx, %edx
	movl	%r9d, 4(%rsp)
	testl	%r11d, %r11d
	jle	.L21
	testl	%r10d, %r10d
	jle	.L19
	leal	-1(%r10), %ecx
	leal	-1(%r11), %edx
	xorl	%r9d, %r9d
	leaq	4(%rsp), %r8
	call	_ZN5Board8pushTileEPP4TileiiPib
	jmp	.L20
.L19:
	testl	%r11d, %r11d
	jle	.L21
.L20:
	movl	4(%rdi), %eax
	decl	%eax
	cmpl	%r10d, %eax
	jle	.L21
	leal	1(%r10), %ecx
	leal	-1(%r11), %edx
	xorl	%r9d, %r9d
	leaq	4(%rsp), %r8
	call	_ZN5Board8pushTileEPP4TileiiPib
.L21:
	movl	(%rdi), %eax
	leal	-1(%rax), %ebx
	cmpl	%r11d, %ebx
	jle	.L23
	testl	%r10d, %r10d
	jle	.L24
	leal	-1(%r10), %ecx
	leal	1(%r11), %edx
	xorl	%r9d, %r9d
	leaq	4(%rsp), %r8
	call	_ZN5Board8pushTileEPP4TileiiPib
.L24:
	movl	4(%rdi), %eax
	decl	%eax
	cmpl	%r10d, %eax
	jle	.L25
	xorl	%r9d, %r9d
	leal	1(%r10), %ecx
	leal	1(%r11), %edx
	leaq	4(%rsp), %r8
	call	_ZN5Board8pushTileEPP4TileiiPib
	testl	%r10d, %r10d
	jg	.L26
.L29:
	leal	1(%r10), %ecx
	xorl	%r9d, %r9d
	leaq	4(%rsp), %r8
	movl	%r11d, %edx
	call	_ZN5Board8pushTileEPP4TileiiPib
	jmp	.L27
.L23:
	testl	%r10d, %r10d
	jle	.L28
.L26:
	leal	-1(%r10), %ecx
	xorl	%r9d, %r9d
	leaq	4(%rsp), %r8
	movl	%r11d, %edx
	call	_ZN5Board8pushTileEPP4TileiiPib
.L28:
	movl	4(%rdi), %eax
	decl	%eax
	cmpl	%r10d, %eax
	jg	.L29
.L27:
	testl	%r11d, %r11d
	jle	.L30
	leal	-1(%r11), %edx
	xorl	%r9d, %r9d
	leaq	4(%rsp), %r8
	movl	%r10d, %ecx
	call	_ZN5Board8pushTileEPP4TileiiPib
.L30:
	cmpl	%r11d, %ebx
	jle	.L31
	leal	1(%r11), %edx
	xorl	%r9d, %r9d
	leaq	4(%rsp), %r8
	movl	%r10d, %ecx
	call	_ZN5Board8pushTileEPP4TileiiPib
.L31:
	movl	4(%rsp), %eax
	movq	8(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L33
	call	__stack_chk_fail@PLT
.L25:
	testl	%r10d, %r10d
	jle	.L27
	jmp	.L26
.L33:
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3650:
	.size	_ZN5Board6getAdjEP4TilePS1_, .-_ZN5Board6getAdjEP4TilePS1_
	.align 2
	.globl	_ZN5Board9getAllAdjEP4TilePS1_
	.type	_ZN5Board9getAllAdjEP4TilePS1_, @function
_ZN5Board9getAllAdjEP4TilePS1_:
.LFB3652:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rsi, %rax
	xorl	%r9d, %r9d
	movq	%rdx, %rsi
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movl	(%rax), %r11d
	movl	4(%rax), %r10d
	movq	%fs:40, %rdx
	movq	%rdx, 8(%rsp)
	xorl	%edx, %edx
	movl	%r9d, 4(%rsp)
	testl	%r11d, %r11d
	jle	.L45
	testl	%r10d, %r10d
	jle	.L43
	leal	-1(%r10), %ecx
	leal	-1(%r11), %edx
	movl	$1, %r9d
	leaq	4(%rsp), %r8
	call	_ZN5Board8pushTileEPP4TileiiPib
	jmp	.L44
.L43:
	testl	%r11d, %r11d
	jle	.L45
.L44:
	movl	4(%rdi), %eax
	decl	%eax
	cmpl	%r10d, %eax
	jle	.L45
	leal	1(%r10), %ecx
	leal	-1(%r11), %edx
	movl	$1, %r9d
	leaq	4(%rsp), %r8
	call	_ZN5Board8pushTileEPP4TileiiPib
.L45:
	movl	(%rdi), %eax
	leal	-1(%rax), %ebx
	cmpl	%r11d, %ebx
	jle	.L47
	testl	%r10d, %r10d
	jle	.L48
	leal	-1(%r10), %ecx
	leal	1(%r11), %edx
	movl	$1, %r9d
	leaq	4(%rsp), %r8
	call	_ZN5Board8pushTileEPP4TileiiPib
.L48:
	movl	4(%rdi), %eax
	decl	%eax
	cmpl	%r10d, %eax
	jle	.L49
	leal	1(%r10), %ecx
	leal	1(%r11), %edx
	movl	$1, %r9d
	leaq	4(%rsp), %r8
	call	_ZN5Board8pushTileEPP4TileiiPib
	testl	%r10d, %r10d
	jg	.L50
.L53:
	leal	1(%r10), %ecx
	movl	$1, %r9d
	leaq	4(%rsp), %r8
	movl	%r11d, %edx
	call	_ZN5Board8pushTileEPP4TileiiPib
	jmp	.L51
.L47:
	testl	%r10d, %r10d
	jle	.L52
.L50:
	leal	-1(%r10), %ecx
	movl	$1, %r9d
	leaq	4(%rsp), %r8
	movl	%r11d, %edx
	call	_ZN5Board8pushTileEPP4TileiiPib
.L52:
	movl	4(%rdi), %eax
	decl	%eax
	cmpl	%r10d, %eax
	jg	.L53
.L51:
	testl	%r11d, %r11d
	jle	.L54
	leal	-1(%r11), %edx
	movl	$1, %r9d
	leaq	4(%rsp), %r8
	movl	%r10d, %ecx
	call	_ZN5Board8pushTileEPP4TileiiPib
.L54:
	cmpl	%r11d, %ebx
	jle	.L55
	leal	1(%r11), %edx
	movl	$1, %r9d
	leaq	4(%rsp), %r8
	movl	%r10d, %ecx
	call	_ZN5Board8pushTileEPP4TileiiPib
.L55:
	movl	4(%rsp), %eax
	movq	8(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L57
	call	__stack_chk_fail@PLT
.L49:
	testl	%r10d, %r10d
	jle	.L51
	jmp	.L50
.L57:
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3652:
	.size	_ZN5Board9getAllAdjEP4TilePS1_, .-_ZN5Board9getAllAdjEP4TilePS1_
	.align 2
	.globl	_ZN5BoardC2ERKS_
	.type	_ZN5BoardC2ERKS_, @function
_ZN5BoardC2ERKS_:
.LFB3645:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdi, %rbx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movslq	(%rsi), %rax
	movl	%eax, (%rdi)
	movslq	4(%rsi), %rdi
	movq	%rax, %r15
	movl	%edi, 4(%rbx)
	movq	%rdi, %r13
	imulq	%rax, %rdi
	imulq	$88, %rdi, %rdi
	call	malloc@PLT
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$72340172838076673, %r10
	movq	%rax, 8(%rbx)
.L67:
	cmpl	%r8d, %r13d
	jle	.L66
	movslq	%r9d, %rbp
	xorl	%r14d, %r14d
	imulq	$88, %rbp, %rbp
.L70:
	cmpl	%r14d, %r15d
	jle	.L72
	movq	8(%r12), %rax
	movq	8(%rbx), %rsi
	movl	$16, %ecx
	movl	%r9d, 12(%rsp)
	movl	%r8d, 8(%rsp)
	movb	8(%rax,%rbp), %al
	addq	%rbp, %rsi
	addq	$88, %rbp
	leaq	24(%rsi), %rdx
	movl	%r14d, (%rsi)
	incl	%r14d
	movb	%al, 8(%rsi)
	movq	%rdx, %rdi
	xorl	%eax, %eax
	movl	%r8d, 4(%rsi)
	movq	%r10, 10(%rsi)
	movb	$0, 9(%rsi)
	rep stosl
	movq	%rbx, %rdi
	call	_ZN5Board9getAllAdjEP4TilePS1_
	movl	12(%rsp), %r9d
	movl	8(%rsp), %r8d
	movabsq	$72340172838076673, %r10
	jmp	.L70
.L72:
	incl	%r8d
	addl	%r15d, %r9d
	jmp	.L67
.L66:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3645:
	.size	_ZN5BoardC2ERKS_, .-_ZN5BoardC2ERKS_
	.globl	_ZN5BoardC1ERKS_
	.set	_ZN5BoardC1ERKS_,_ZN5BoardC2ERKS_
	.align 2
	.globl	_ZN5Board14genRandLettersEv
	.type	_ZN5Board14genRandLettersEv, @function
_ZN5Board14genRandLettersEv:
.LFB3647:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movabsq	$72340172838076673, %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L74:
	cmpl	%r12d, 4(%rbp)
	jle	.L79
	xorl	%r13d, %r13d
.L76:
	cmpl	%r13d, 0(%rbp)
	jle	.L80
	call	rand@PLT
	movl	$26, %ecx
	movl	0(%rbp), %esi
	cltd
	idivl	%ecx
	imull	%r12d, %esi
	xorl	%eax, %eax
	movl	$16, %ecx
	addl	%r13d, %esi
	movslq	%esi, %rsi
	imulq	$88, %rsi, %rsi
	addq	8(%rbp), %rsi
	movl	%r13d, (%rsi)
	incl	%r13d
	movl	%r12d, 4(%rsi)
	movb	$0, 9(%rsi)
	movq	%rbx, 10(%rsi)
	addl	$97, %edx
	movb	%dl, 8(%rsi)
	leaq	24(%rsi), %rdx
	movq	%rdx, %rdi
	rep stosl
	movq	%rbp, %rdi
	call	_ZN5Board9getAllAdjEP4TilePS1_
	jmp	.L76
.L80:
	incl	%r12d
	jmp	.L74
.L79:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3647:
	.size	_ZN5Board14genRandLettersEv, .-_ZN5Board14genRandLettersEv
	.align 2
	.globl	_ZN5Board10setLettersENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
	.type	_ZN5Board10setLettersENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, @function
_ZN5Board10setLettersENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE:
.LFB3648:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movabsq	$72340172838076673, %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L82:
	movl	0(%rbp), %ecx
	movl	4(%rbp), %edx
	movl	%ebx, %eax
	imull	%ecx, %edx
	cmpl	%ebx, %edx
	jle	.L85
	cltd
	imulq	$88, %rbx, %rsi
	addq	8(%rbp), %rsi
	idivl	%ecx
	movl	$16, %ecx
	movl	%eax, 4(%rsi)
	movq	(%r12), %rax
	movl	%edx, (%rsi)
	leaq	24(%rsi), %rdx
	movb	(%rax,%rbx), %al
	movq	%rdx, %rdi
	movb	$0, 9(%rsi)
	incq	%rbx
	movq	%r13, 10(%rsi)
	movb	%al, 8(%rsi)
	xorl	%eax, %eax
	rep stosl
	movq	%rbp, %rdi
	call	_ZN5Board9getAllAdjEP4TilePS1_
	jmp	.L82
.L85:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3648:
	.size	_ZN5Board10setLettersENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, .-_ZN5Board10setLettersENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%c "
.LC1:
	.string	"\n"
	.text
	.align 2
	.globl	_ZN5Board10printBoardEv
	.type	_ZN5Board10printBoardEv, @function
_ZN5Board10printBoardEv:
.LFB3653:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	leaq	.LC0(%rip), %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L87:
	cmpl	%r12d, 4(%rbp)
	jle	.L92
	xorl	%r13d, %r13d
.L89:
	movl	0(%rbp), %eax
	cmpl	%r13d, %eax
	jle	.L93
	imull	%r12d, %eax
	movq	%rbx, %rsi
	movl	$2, %edi
	addl	%r13d, %eax
	incl	%r13d
	cltq
	imulq	$88, %rax, %rax
	addq	8(%rbp), %rax
	movsbl	8(%rax), %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L89
.L93:
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incl	%r12d
	call	__printf_chk@PLT
	jmp	.L87
.L92:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3653:
	.size	_ZN5Board10printBoardEv, .-_ZN5Board10printBoardEv
	.align 2
	.globl	_ZN5Board9printUsedEv
	.type	_ZN5Board9printUsedEv, @function
_ZN5Board9printUsedEv:
.LFB3654:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	leaq	.LC0(%rip), %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L95:
	cmpl	%r12d, 4(%rbp)
	jle	.L102
	xorl	%r13d, %r13d
.L98:
	movl	0(%rbp), %eax
	cmpl	%r13d, %eax
	jle	.L103
	imull	%r12d, %eax
	movl	$32, %edx
	addl	%r13d, %eax
	cltq
	imulq	$88, %rax, %rax
	addq	8(%rbp), %rax
	cmpb	$0, 9(%rax)
	je	.L96
	movsbl	8(%rax), %edx
.L96:
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incl	%r13d
	call	__printf_chk@PLT
	jmp	.L98
.L103:
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incl	%r12d
	call	__printf_chk@PLT
	jmp	.L95
.L102:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3654:
	.size	_ZN5Board9printUsedEv, .-_ZN5Board9printUsedEv
	.align 2
	.globl	_ZN5Board7getTileEii
	.type	_ZN5Board7getTileEii, @function
_ZN5Board7getTileEii:
.LFB3655:
	.cfi_startproc
	endbr64
	imull	(%rdi), %edx
	leal	(%rdx,%rsi), %eax
	cltq
	imulq	$88, %rax, %rax
	addq	8(%rdi), %rax
	ret
	.cfi_endproc
.LFE3655:
	.size	_ZN5Board7getTileEii, .-_ZN5Board7getTileEii
	.align 2
	.globl	_ZN5Board11getAllTilesEPP4Tile
	.type	_ZN5Board11getAllTilesEPP4Tile, @function
_ZN5Board11getAllTilesEPP4Tile:
.LFB3656:
	.cfi_startproc
	endbr64
	movl	4(%rdi), %r10d
	xorl	%edx, %edx
.L106:
	cmpl	%edx, %r10d
	jle	.L105
	movl	(%rdi), %r8d
	movl	%r8d, %eax
	imull	%edx, %eax
	cltq
	imulq	$88, %rax, %rcx
	leaq	(%rsi,%rax,8), %r9
	xorl	%eax, %eax
.L109:
	cmpl	%eax, %r8d
	jle	.L110
	movq	8(%rdi), %r11
	addq	%rcx, %r11
	addq	$88, %rcx
	movq	%r11, (%r9,%rax,8)
	incq	%rax
	jmp	.L109
.L110:
	incl	%edx
	jmp	.L106
.L105:
	ret
	.cfi_endproc
.LFE3656:
	.size	_ZN5Board11getAllTilesEPP4Tile, .-_ZN5Board11getAllTilesEPP4Tile
	.align 2
	.globl	_ZN5Board10resetBoardEv
	.type	_ZN5Board10resetBoardEv, @function
_ZN5Board10resetBoardEv:
.LFB3657:
	.cfi_startproc
	endbr64
	movabsq	$72340172838076673, %r10
	movl	4(%rdi), %r9d
	xorl	%edx, %edx
.L112:
	cmpl	%edx, %r9d
	jle	.L111
	movl	(%rdi), %esi
	movl	%edx, %eax
	xorl	%ecx, %ecx
	imull	%esi, %eax
	cltq
	imulq	$88, %rax, %rax
.L115:
	cmpl	%ecx, %esi
	jle	.L116
	movq	8(%rdi), %r8
	incl	%ecx
	addq	%rax, %r8
	addq	$88, %rax
	movb	$0, 9(%r8)
	movq	%r10, 10(%r8)
	jmp	.L115
.L116:
	incl	%edx
	jmp	.L112
.L111:
	ret
	.cfi_endproc
.LFE3657:
	.size	_ZN5Board10resetBoardEv, .-_ZN5Board10resetBoardEv
	.globl	_Z13checkTileListP4TilePS0_i
	.type	_Z13checkTileListP4TilePS0_i, @function
_Z13checkTileListP4TilePS0_i:
.LFB3658:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L118:
	cmpl	%eax, %edx
	jle	.L122
	incq	%rax
	cmpq	%rdi, -8(%rsi,%rax,8)
	jne	.L118
	movb	$1, %al
	ret
.L122:
	xorl	%eax, %eax
	ret
	.cfi_endproc
.LFE3658:
	.size	_Z13checkTileListP4TilePS0_i, .-_Z13checkTileListP4TilePS0_i
	.align 2
	.globl	_ZN5Board14getLetterCountEc
	.type	_ZN5Board14getLetterCountEc, @function
_ZN5Board14getLetterCountEc:
.LFB3659:
	.cfi_startproc
	endbr64
	movl	4(%rdi), %r10d
	xorl	%edx, %edx
	xorl	%ecx, %ecx
.L124:
	cmpl	%edx, %r10d
	jle	.L123
	movl	(%rdi), %r9d
	xorl	%r8d, %r8d
	movl	%r9d, %eax
	imull	%edx, %eax
	cltq
	imulq	$88, %rax, %rax
.L128:
	cmpl	%r8d, %r9d
	jle	.L129
	movq	8(%rdi), %r11
	cmpb	%sil, 8(%r11,%rax)
	jne	.L125
	incl	%ecx
.L125:
	incl	%r8d
	addq	$88, %rax
	jmp	.L128
.L129:
	incl	%edx
	jmp	.L124
.L123:
	movl	%ecx, %eax
	ret
	.cfi_endproc
.LFE3659:
	.size	_ZN5Board14getLetterCountEc, .-_ZN5Board14getLetterCountEc
	.align 2
	.globl	_ZN5Board16getTilesByLetterEPP4Tilec
	.type	_ZN5Board16getTilesByLetterEPP4Tilec, @function
_ZN5Board16getTilesByLetterEPP4Tilec:
.LFB3660:
	.cfi_startproc
	endbr64
	movl	4(%rdi), %r8d
	imull	(%rdi), %r8d
	xorl	%eax, %eax
	xorl	%r9d, %r9d
.L131:
	cmpl	%eax, %r8d
	jle	.L134
	imulq	$88, %rax, %rcx
	addq	8(%rdi), %rcx
	cmpb	%dl, 8(%rcx)
	jne	.L132
	movslq	%r9d, %r10
	incl	%r9d
	movq	%rcx, (%rsi,%r10,8)
.L132:
	incq	%rax
	jmp	.L131
.L134:
	ret
	.cfi_endproc
.LFE3660:
	.size	_ZN5Board16getTilesByLetterEPP4Tilec, .-_ZN5Board16getTilesByLetterEPP4Tilec
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3686:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3686:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#ifdef __HIPCC__
#define CUDA_CALLABLE_MEMBER __host__ __device__
#else
#define CUDA_CALLABLE_MEMBER
#endif 

#ifndef BOGGLE_ENV_H
#define BOGGLE_ENV_H

/* --- Boggle Environment header --- */

#include <string>
#include <hip/hip_runtime.h>
using namespace std;

#define NUM_ADJ 8 //tiles can have at most 8 adjacent tiles

/*Letter Tile struct*/
struct Tile
{
	int x;
	int y;
	char letter;
	bool used;
	bool adj_available[NUM_ADJ];	//list which denotes if adjacency available
	Tile* adj_list[NUM_ADJ];			//list which holds adjacencies
};

/*Boggle Board Class.*/
class Board {
	int width, height;
	CUDA_CALLABLE_MEMBER void pushTile(Tile **target, int x, int y, int *count, bool all);
public:
	CUDA_CALLABLE_MEMBER Board(int _width, int _height);
	CUDA_CALLABLE_MEMBER Board(const Board &obj);
	CUDA_CALLABLE_MEMBER ~Board();
	void genRandLetters();
	void setLetters(string letters);
	CUDA_CALLABLE_MEMBER int getAdj(Tile* center, Tile **adj);
	CUDA_CALLABLE_MEMBER void getAdjList(Tile *center, Tile **adj);
	CUDA_CALLABLE_MEMBER int getAllAdj(Tile* center, Tile **adj);
	CUDA_CALLABLE_MEMBER Tile* getTile(int x, int y);
	CUDA_CALLABLE_MEMBER void getAllTiles(Tile *all_tiles[]);
	CUDA_CALLABLE_MEMBER int getNumTiles() { return width*height; }
	int getHeight() { return height; }
	int getWidth() { return width; }
	CUDA_CALLABLE_MEMBER void resetBoard();
	CUDA_CALLABLE_MEMBER void printBoard();
	CUDA_CALLABLE_MEMBER void printUsed();
	CUDA_CALLABLE_MEMBER int getLetterCount(char c);
	CUDA_CALLABLE_MEMBER void getTilesByLetter(Tile *all_tiles[], char c);
	Tile *grid;					  //necessary for cuda memory allocation
};

CUDA_CALLABLE_MEMBER bool checkTileList(Tile *check, Tile **list, int size); //returns true if in list
#endif
	.text
	.file	"boggle_env.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
