<commit_before><?php
include 'config.inc.php';

$MYSQLI = new mysqli(
	$CONFIG['host'], $CONFIG['username'], $CONFIG['password'], $CONFIG['db']
);
$GRID = array();

for ($x = 0; $x < 4; $x++)
{
	$column = array();

	for ($y = 0; $y < 4; $y++)
	{
		$column[] = -1;
	}

	$GRID[] = $column;
}

function clear()
{
	global $MYSQLI;
	$query = 'TRUNCATE TABLE `moves`;';
	$stmt = $MYSQLI->query($query);
	$stmt->close();
}

function clear_grid()
{
	global $GRID;

	for ($x = 0; $x < 4; $x++)
	{
		for ($y = 0; $y < 4; $y++)
		{
			$GRID[$x][$y] = -1;
		}
	}
}

function initialize()
{
	global $GRID;

	clear_grid();

	for ($tile = 0; $tile < 2; $tile++)
	{
		move('', -1);
	}
}

function move($from, $move)
{
	global $GRID, $MYSQLI;
	move_grid($move);
	$random = tile_random();
	$tile = $random['tile'];
	$x = $random['x'];
	$y = $random['y'];
	// Insert move into database.
	$query = 'INSERT INTO `moves` (`from`, `move`, `tile`, `x`, `y`)
		VALUES (?, ?, ?, ?, ?);';
	$stmt = $MYSQLI->prepare($query);
	$stmt->bind_param('siiii', $from, $move, $tile, $x, $y);
	$stmt->execute() or die(
		'MySQL Error: ' . $MYSQLI->error.__LINE__
	);
	$stmt->close();
	$GRID[$x][$y] = $tile;
}

function move_grid($move)
{
	global $GRID;
	$merged = array();

	for ($x = 0; $x < 4; $x++)
	{
		for ($y = 0; $y < 4; $y++)
		{
			$merged[$x][$y] = false;
		}
	}

	switch ($move)
	{
		// Up
		case 0:
			for ($y = 1; $y < 4; $y++)
			{
				for ($x = 0; $x < 4; $x++)
				{
					$y2 = $y;

					while ($y2 >= 0 && $GRID[$x][$y2 - 1] == -1)
					{
						$tile = $GRID[$x][$y2];
						$GRID[$x][$y2] = -1;
						$y2--;
						$GRID[$x][$y2] = $tile;
					}

					if (
						$y2 >= 0 && $GRID[$x][$y2] == $GRID[$x][$y2 - 1] &&
						!$merged[$x][$y2 - 1]
					)
					{
						$GRID[$x][$y2] = -1;
						$y2--;
						$GRID[$x][$y2] = $tile * 2;
						$merged[$x][$y2] = true;
					}
				}
			}
		break;

		// Right
		case 1:
			for ($x = 2; $x >= 0; $x--)
			{
				for ($y = 0; $y < 4; $y++)
				{
					$x2 = $x;

					while ($x2 < 4 && $GRID[$x2 + 1][$y] == -1)
					{
						$tile = $GRID[$x2][$y];
						$GRID[$x2][$y] = -1;
						$x2++;
						$GRID[$x2][$y] = $tile;
					}

					if (
						$x2 < 4 && $GRID[$x2][$y] == $GRID[$x2 + 1][$y] &&
						!$merged[$x2 + 1][$y]
					)
					{
						$GRID[$x2][$y] = -1;
						$x2++;
						$GRID[$x2][$y] = $tile * 2;
						$merged[$x2][$y] = true;
					}
				}
			}
		break;

		// Down
		case 2:
			for ($y = 2; $y >= 0; $y--)
			{
				for ($x = 0; $x < 4; $x++)
				{
					$y2 = $y;

					while ($y2 < 4 && $GRID[$x][$y2 + 1] == -1)
					{
						$tile = $GRID[$x][$y2];
						$GRID[$x][$y2] = -1;
						$y2++;
						$GRID[$x][$y2] = $tile;
					}

					if (
						$y2 < 4 && $GRID[$x][$y2] == $GRID[$x][$y2 + 1] &&
						!$merged[$x][$y2 + 1]
					)
					{
						$GRID[$x][$y2] = -1;
						$y2++;
						$GRID[$x][$y2] = $tile * 2;
						$merged[$x][$y2] = true;
					}
				}
			}
		break;

		// Left
		case 3:
			for ($x = 1; $x < 4; $x++)
			{
				for ($y = 0; $y < 4; $y++)
				{
					$x2 = $x;

					while ($x2 >= 0 && $GRID[$x2 - 1][$y] == -1)
					{
						$tile = $GRID[$x2][$y];
						$GRID[$x2][$y] = -1;
						$x2--;
						$GRID[$x2][$y] = $tile;
					}

					if (
						$x2 >= 0 && $GRID[$x2][$y] == $GRID[$x2 - 1][$y] &&
						!$merged[$x2 - 1][$y]
					)
					{
						$GRID[$x2][$y] = -1;
						$x2--;
						$GRID[$x2][$y] = $tile * 2;
						$merged[$x2][$y] = true;
					}
				}
			}
		break;
	}
}

function move_parse($text)
{
	$moves_valid = array('up', 'right', 'down', 'left');
	$text = trim(strtolower($text));

	foreach ($moves_valid as $move => $str)
	{
		if (strpos($text, $str) !== false)
		{
			return $move;
		}
	}

	return false;
}

function read()
{<commit_msg>Server-side grid fixes.<commit_after><?php
include 'config.inc.php';

$MYSQLI = new mysqli(
	$CONFIG['host'], $CONFIG['username'], $CONFIG['password'], $CONFIG['db']
);
$GRID = array();

for ($x = 0; $x < 4; $x++)
{
	$column = array();

	for ($y = 0; $y < 4; $y++)
	{
		$column[] = -1;
	}

	$GRID[] = $column;
}

function clear()
{
	global $MYSQLI;
	$query = 'TRUNCATE TABLE `moves`;';
	$stmt = $MYSQLI->query($query);
	$stmt->close();
}

function clear_grid()
{
	global $GRID;

	for ($x = 0; $x < 4; $x++)
	{
		for ($y = 0; $y < 4; $y++)
		{
			$GRID[$x][$y] = -1;
		}
	}
}

function initialize()
{
	global $GRID;

	clear_grid();

	for ($tile = 0; $tile < 2; $tile++)
	{
		move('', -1);
	}
}

function move($from, $move)
{
	global $GRID, $MYSQLI;
	move_grid($move);
	$random = tile_random();
	$tile = $random['tile'];
	$x = $random['x'];
	$y = $random['y'];
	// Insert move into database.
	$query = 'INSERT INTO `moves` (`from`, `move`, `tile`, `x`, `y`)
		VALUES (?, ?, ?, ?, ?);';
	$stmt = $MYSQLI->prepare($query);
	$stmt->bind_param('siiii', $from, $move, $tile, $x, $y);
	$stmt->execute() or die(
		'MySQL Error: ' . $MYSQLI->error.__LINE__
	);
	$stmt->close();
	$GRID[$x][$y] = $tile;
}

function move_grid($move)
{
	global $GRID;
	$merged = array();

	for ($x = 0; $x < 4; $x++)
	{
		for ($y = 0; $y < 4; $y++)
		{
			$merged[$x][$y] = false;
		}
	}

	switch ($move)
	{
		// Up
		case 0:
			for ($y = 1; $y < 4; $y++)
			{
				for ($x = 0; $x < 4; $x++)
				{
					$y2 = $y;

					while ($y2 >= 0 && $GRID[$x][$y2 - 1] == -1)
					{
						$tile = $GRID[$x][$y2];
						$GRID[$x][$y2] = -1;
						$y2--;
						$GRID[$x][$y2] = $tile;
					}

					if (
						$y2 >= 0 && $GRID[$x][$y2] != -1 &&
						$GRID[$x][$y2] == $GRID[$x][$y2 - 1] &&
						!$merged[$x][$y2 - 1]
					)
					{
						$tile = $GRID[$x][$y2];
						$GRID[$x][$y2] = -1;
						$y2--;
						$GRID[$x][$y2] = $tile * 2;
						$merged[$x][$y2] = true;
					}
				}
			}
		break;

		// Right
		case 1:
			for ($x = 2; $x >= 0; $x--)
			{
				for ($y = 0; $y < 4; $y++)
				{
					$x2 = $x;

					while ($x2 < 4 && $GRID[$x2 + 1][$y] == -1)
					{
						$tile = $GRID[$x2][$y];
						$GRID[$x2][$y] = -1;
						$x2++;
						$GRID[$x2][$y] = $tile;
					}

					if (
						$x2 < 4 && $GRID[$x2][$y] != -1 &&
						$GRID[$x2][$y] == $GRID[$x2 + 1][$y] &&
						!$merged[$x2 + 1][$y]
					)
					{
						$tile = $GRID[$x2][$y];
						$GRID[$x2][$y] = -1;
						$x2++;
						$GRID[$x2][$y] = $tile * 2;
						$merged[$x2][$y] = true;
					}
				}
			}
		break;

		// Down
		case 2:
			for ($y = 2; $y >= 0; $y--)
			{
				for ($x = 0; $x < 4; $x++)
				{
					$y2 = $y;

					while ($y2 < 4 && $GRID[$x][$y2 + 1] == -1)
					{
						$tile = $GRID[$x][$y2];
						$GRID[$x][$y2] = -1;
						$y2++;
						$GRID[$x][$y2] = $tile;
					}

					if (
						$y2 < 4 && $GRID[$x][$y2] != -1 &&
						$GRID[$x][$y2] == $GRID[$x][$y2 + 1] &&
						!$merged[$x][$y2 + 1]
					)
					{
						$tile = $GRID[$x][$y2];
						$GRID[$x][$y2] = -1;
						$y2++;
						$GRID[$x][$y2] = $tile * 2;
						$merged[$x][$y2] = true;
					}
				}
			}
		break;

		// Left
		case 3:
			for ($x = 1; $x < 4; $x++)
			{
				for ($y = 0; $y < 4; $y++)
				{
					$x2 = $x;

					while ($x2 >= 0 && $GRID[$x2 - 1][$y] == -1)
					{
						$tile = $GRID[$x2][$y];
						$GRID[$x2][$y] = -1;
						$x2--;
						$GRID[$x2][$y] = $tile;
					}

					if (
						$x2 >= 0 && $GRID[$x2][$y] != -1 &&
						$GRID[$x2][$y] == $GRID[$x2 - 1][$y] &&
						!$merged[$x2 - 1][$y]
					)
					{
						$tile = $GRID[$x2][$y];
						$GRID[$x2][$y] = -1;
						$x2--;
						$GRID[$x2][$y] = $tile * 2;
						$merged[$x2][$y] = true;
					}
				}
			}
		break;
	}
}

function move_parse($text)
{
	$moves_valid = array('up', 'right', 'down', 'left');
	$text = trim(strtolower($text));

	foreach ($moves_valid as $move => $str)
	{
		if (strpos($text, $str) !== false)
		{
			return $move;
		}
	}

	return false;
}

function read()
{
{'fraction_non_alphanumeric': 0.2914704343275772, 'fraction_numerical': 0.03636839351125065, 'mean_word_length': 1.9921722113502935, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2591883', 'n_tokens_mistral': 4772, 'n_tokens_neox': 3712, 'n_words': 722}