<commit_before><?php

class Game {
	private $rolls = array();

	/**
	 * @return int
	 * @throws Exception
	 */
	public function score(): int {
		$frames = $this->calculateFrames();

		if ( $this->isInvalid( $frames ) ) {
			throw new Exception();
		}

		return array_sum( array_slice($frames,0,10) );
	}

	/**
	 * @return array
	 * @throws Exception
	 */
	public function calculateFrames(): array {
		$frames          = [];
		$is_in_new_Frame = true;
		$go_on           = true;
		for ( $i = 0; $i < sizeof( $this->rolls ) && $go_on; $i ++ ) {
			if ( $is_in_new_Frame ) {
				list( $frames, $is_in_new_Frame, $go_on ) = $this->newFrame( $frames, $i );
			} else {
				$frames = $this->updateFrame( $frames, $i );
			}
			$is_in_new_Frame = ! $is_in_new_Frame;
		}

		return $frames;
	}

	/**
	 * @param $frames
	 * @param $i
	 *
	 * @return array
	 * @throws Exception
	 */
	public function newFrame( $frames, $i ): array {
		$roll  = $this->rolls[ $i ];
		$go_on = true;
		$next  = true;

		array_push( $frames, $roll );
		if ( $this->areAllPinsHit( $frames ) ) {
			list( $frames, $next ) = $this->calculateStrike( $frames, $i );
			if ( sizeof( $frames ) == 10 ) {
				$go_on = false;
			}
		}

		return array( $frames, $next, $go_on );
	}

	/**
	 * @param $frames
	 * @param $i
	 *
	 * @return array
	 * @throws Exception
	 */
	public function calculateStrike( $frames, $i ): array {
		$next = true;
		if ( sizeof( $frames ) <= 10 ) {
			$frames = $this->addNextRoll( $frames, $i, 1 );
			$frames = $this->addNextRoll( $frames, $i, 2 );

			$next                            = false;

			if ( $this->rolls[ $i + 1 ] != 10
			     && $this->rolls[ $i + 1 ] + $this->rolls[ $i + 2 ] > 10 ) {
				throw new Exception();
			}
		}

		return array( $frames, $next );
	}

	/**
	 * @param $frames
	 * @param $i
	 *
	 * @return array
	 * @throws Exception
	 */
	public function updateFrame( $frames, $i ): array {
		$roll = $this->rolls[ $i ];
		if ( $frames[ sizeof( $frames ) - 1 ] < 10
		     && $frames[ sizeof( $frames ) - 1 ] + $roll > 10 ) {
			throw new Exception();
		}
		$frames[ sizeof( $frames ) - 1 ] += $roll;

		if ( $this->areAllPinsHit( $frames ) ) {
			$frames = $this->calculateSpare( $frames, $i );
		}

		return $frames;
	}

	/**
	 * @param $int<commit_msg>Problem 19: refactor<commit_after><?php

class Game {
	private $rolls = array();

	/**
	 * @return int
	 * @throws Exception
	 */
	public function score(): int {
		$frames = $this->calculateFrames();

		if ( $this->isInvalid( $frames ) ) {
			throw new Exception();
		}

		return array_sum( array_slice($frames,0,10) );
	}

	/**
	 * @return array
	 * @throws Exception
	 */
	public function calculateFrames(): array {
		$frames          = [];
		$is_in_new_Frame = true;
		$go_on           = true;
		for ( $i = 0; $i < sizeof( $this->rolls ) && $go_on; $i ++ ) {
			if ( $is_in_new_Frame ) {
				list( $frames, $is_in_new_Frame, $go_on ) = $this->newFrame( $frames, $i );
			} else {
				$frames = $this->updateFrame( $frames, $i );
			}
			$is_in_new_Frame = ! $is_in_new_Frame;
		}

		return $frames;
	}

	/**
	 * @param $frames
	 * @param $i
	 *
	 * @return array
	 * @throws Exception
	 */
	public function newFrame( $frames, $i ): array {
		$roll  = $this->rolls[ $i ];
		$go_on = true;
		$next  = true;

		array_push( $frames, $roll );
		if ( $this->areAllPinsHit( $frames ) ) {
			list( $frames, $next ) = $this->calculateStrike( $frames, $i );
			if ( sizeof( $frames ) == 10 ) {
				$go_on = false;
			}
		}

		return array( $frames, $next, $go_on );
	}

	/**
	 * @param $frames
	 * @param $i
	 *
	 * @return array
	 * @throws Exception
	 */
	public function calculateStrike( $frames, $i ): array {
		$next = true;
		if ( sizeof( $frames ) <= 10 ) {
			$frames = $this->addNextRoll( $frames, $i, 1 );
			$frames = $this->addNextRoll( $frames, $i, 2 );

			$next                            = false;

		}

		if ( sizeof( $this->rolls ) >= $i + 2 && $this->rolls[ $i + 1 ] != 10
		     && $this->rolls[ $i + 1 ] + $this->rolls[ $i + 2 ] > 10 ) {
			throw new Exception();
		}

		return array( $frames, $next );
	}

	/**
	 * @param $frames
	 * @param $i
	 *
	 * @return array
	 * @throws Exception
	 */
	public function updateFrame( $frames, $i ): array {
		$roll = $this->rolls[ $i ];
		if ( $frames[ sizeof( $frames ) - 1 ] < 10
		     && $frames[ sizeof( $frames ) - 1 ] + $roll > 10 ) {
			throw new Exception();
		}
		$frames[ sizeof( $frames ) - 1 ] += $roll;

		if ( $this->areAllPinsHit( $frames ) ) {
			$frames = $this->calculateSpare( $frames, $i );
		}

		return $frames;
	}

	/**
	 * @param $int
{'fraction_non_alphanumeric': 0.19240837696335078, 'fraction_numerical': 0.01112565445026178, 'mean_word_length': 2.4813971146545177, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, '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': '104180', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1698, 'n_words': 419}