Created
May 5, 2023 19:18
-
-
Save lupinitylabs/90c8e93df3c6d4194aeac5195d1deb6b to your computer and use it in GitHub Desktop.
This is an attempt of the `StarCoder` AI to generate a PHP class simulating a game of bowling. Looks like the request broke it... π
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Models; | |
/** | |
* Class GameOfBowling | |
*/ | |
class GameOfBowling { | |
private $_rolls = []; | |
protected static $_scores = ['' => '']; | |
const STRIKES = '_strikes'; | |
const SPARES = '_spares'; | |
const PINS = '_pins'; | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
self::$_scores = [ | |
'' => '', | |
1 => 0, | |
2 => 0, | |
self::STRIKES => [], | |
self::SPARES => [], | |
self::PINS => [] | |
]; | |
foreach ([self::STRIKES, self::SPARES, self::PINS] as $key) { | |
self::$_scores[$key][self::_scoreKey()] += count(self::$_scores[$key]); | |
} | |
} | |
/** | |
* Get score key based on total number of frames played | |
* | |
* @return mixed | |
*/ | |
protected function _scoreKey(){ | |
switch ($this->framesPlayed()){ | |
case 0: | |
break; | |
default: | |
return min((array)$this->_rolls)[0]; | |
} | |
return ''; | |
} | |
/** | |
* Roll the ball | |
* | |
* @param string|int $pins Number of pins knocked down per roll | |
*/ | |
public function roll($pins){ | |
array_push($_rolls,$pins); | |
} | |
/** | |
* Calculate frame scores | |
* | |
* @throws Exception | |
*/ | |
public function calculateScores(){ | |
reset(self::$_scores['_strikes']); | |
reset(self::$_scores['_spares']); | |
reset(self::$_scores['_pins']); | |
$frameIndex = null; | |
$strikeBonus = true; | |
$previousFrameScore = 0; | |
$currentRoll = next(self::$_scores['_pins']); | |
$nextRoll = next(self::$_scores['_pins']); | |
$bonusRollsCount = 0; | |
while (!empty($currentRoll)){ | |
++$frameIndex; | |
if (!$strikeBonus && !isset($nextRoll)){ | |
throw new Exception("Invalid Frame"); | |
} | |
if (($currentRoll === 10 || isset($nextRoll) && $currentRoll+$nextRoll===10) | |
&& !$strikeBonus ){ | |
$bonusRollsCount++; | |
if ((count($this->_rolls)-$frameIndex-$bonusRollsCount)%2 !== 0 | |
|| empty($nextRoll)){ | |
unset($nextRoll); | |
continue; | |
} | |
$nextRoll = next(self::$_scores['_pins']); | |
} | |
if ((!$strikeBonus && $currentRoll+$nextRoll >= 10) | |
|| ($strikeBonus | |
&& $currentRoll+$nextRoll>=10 | |
&& $currentRoll!==10)){ | |
$strikeBonus = true; | |
if(!isset($nextRoll)){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
continue; | |
}else{ | |
$currentRoll=$currentRoll+$nextRoll; | |
$nextRoll = next(self::$_scores['_pins']); | |
} | |
}elseif(!$strikeBonus&&$currentRoll+$nextRoll<10){ | |
$currentRoll=$currentRoll+$nextRoll; | |
} | |
if ($strikeBonus){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll==10||$currentRoll>10){ | |
$currentRoll-=10; | |
$strikeBonus = false; | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll>$nextRoll){ | |
$currentRoll -= $nextRoll; | |
$nextRoll = next(self::$_scores['_pins']); | |
} elseif ($currentRoll<$nextRoll) { | |
$nextRoll -= $currentRoll; | |
$currentRoll = next(self::$_scores['_pins']); | |
} else { | |
$currentRoll = next(self::$_scores['_pins']); | |
$nextRoll = next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10){ | |
$currentRoll+=next(self::$_scores['_pins']); | |
} | |
if ($currentRoll+$nextRoll==10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment