-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/steelpipe75/bowling_game_…
- Loading branch information
Showing
14 changed files
with
2,131 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bin/** |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
public class BowlingGame { | ||
|
||
private int[] rolls = new int[21]; | ||
private int currentRoll = 0; | ||
|
||
public void roll(int pins) { | ||
rolls[currentRoll++] = pins; | ||
} | ||
|
||
public int score() { | ||
int score = 0; | ||
int rollIndex = 0; | ||
|
||
for (int frame = 0; frame < 10; frame++) { | ||
if (rolls[rollIndex] == 10) { // ストライクの場合 | ||
score += 10 + rolls[rollIndex + 1] + rolls[rollIndex + 2]; | ||
rollIndex++; | ||
} else if (rolls[rollIndex] + rolls[rollIndex + 1] == 10) { // スペアの場合 | ||
score += 10 + rolls[rollIndex + 2]; | ||
rollIndex += 2; | ||
} else { // その他の場合 | ||
score += rolls[rollIndex] + rolls[rollIndex + 1]; | ||
rollIndex += 2; | ||
} | ||
} | ||
|
||
return score; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
// コマンドライン引数でファイルパスが指定されているか確認 | ||
if (args.length < 1) { | ||
System.out.println("ファイルパスを指定してください。"); | ||
return; | ||
} | ||
|
||
String filePath = args[0]; | ||
BowlingGame bg = new BowlingGame(); | ||
|
||
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
try { | ||
int num = Integer.parseInt(line); | ||
if (num < 0) { | ||
// System.out.println("負の値が見つかったため、プログラムを終了します。"); | ||
break; | ||
} | ||
bg.roll(num); | ||
} catch (NumberFormatException e) { | ||
// System.out.println("整数以外の行が見つかったため、スキップします: " + line); | ||
} | ||
} | ||
System.out.println(bg.score()); | ||
} catch (IOException e) { | ||
System.out.println("ファイルを読み取る際にエラーが発生しました: " + e.getMessage()); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# bowling_game_kata : php | ||
|
||
PHP で ボーリングのスコア計算 | ||
|
||
## 実行方法 | ||
|
||
``` | ||
cd .\bowling_game\ | ||
php main.php ../../testdata/all_ones.txt | ||
``` | ||
|
||
## テスト実行 | ||
|
||
``` | ||
cd .\bowling_game\ | ||
composer install | ||
vendor/phpunit/phpunit/phpunit bowling_game_test.php | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor/** |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
class BowlingGame { | ||
private $rolls = []; | ||
|
||
// 投球を登録するメソッド | ||
public function roll($pins) { | ||
$this->rolls[] = $pins; | ||
} | ||
|
||
// スコアを計算するメソッド | ||
public function score() { | ||
$score = 0; | ||
$frameIndex = 0; | ||
|
||
for ($frame = 0; $frame < 10; $frame++) { | ||
if ($this->isStrike($frameIndex)) { // ストライク | ||
$score += $this->strikeScore($frameIndex); | ||
$frameIndex++; | ||
} elseif ($this->isSpare($frameIndex)) { // スペア | ||
$score += $this->spareScore($frameIndex); | ||
$frameIndex += 2; | ||
} else { | ||
$score += $this->rolls[$frameIndex] + $this->rolls[$frameIndex + 1]; | ||
$frameIndex += 2; | ||
} | ||
} | ||
|
||
return $score; | ||
} | ||
|
||
// ストライクかどうかを判定するメソッド | ||
private function isStrike($frameIndex) { | ||
return $this->rolls[$frameIndex] == 10; | ||
} | ||
|
||
// スペアかどうかを判定するメソッド | ||
private function isSpare($frameIndex) { | ||
return $this->rolls[$frameIndex] + $this->rolls[$frameIndex + 1] == 10; | ||
} | ||
|
||
// ストライクのスコア計算 | ||
private function strikeScore($frameIndex) { | ||
return 10 + $this->rolls[$frameIndex + 1] + $this->rolls[$frameIndex + 2]; | ||
} | ||
|
||
// スペアのスコア計算 | ||
private function spareScore($frameIndex) { | ||
return 10 + $this->rolls[$frameIndex + 2]; | ||
} | ||
} | ||
|
||
?> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
require_once 'bowling_game.php'; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class bowling_game_test extends TestCase { | ||
public function testAllZerosGame() { | ||
$game = new BowlingGame(); | ||
for ($i = 0; $i < 20; $i++) { | ||
$game->roll(0); | ||
} | ||
$this->assertSame(0, $game->score()); | ||
} | ||
|
||
public function testAllOnesGame() { | ||
$game = new BowlingGame(); | ||
for ($i = 0; $i < 20; $i++) { | ||
$game->roll(1); | ||
} | ||
$this->assertSame(20, $game->score()); | ||
} | ||
|
||
public function testOneSpareGame() { | ||
$game = new BowlingGame(); | ||
$game->roll(5); | ||
$game->roll(5); | ||
$game->roll(3); | ||
for ($i = 0; $i < 17; $i++) { | ||
$game->roll(0); | ||
} | ||
$this->assertSame(16, $game->score()); | ||
} | ||
|
||
public function testOneStrikeGame() { | ||
$game = new BowlingGame(); | ||
$game->roll(10); | ||
$game->roll(3); | ||
$game->roll(4); | ||
for ($i = 0; $i < 16; $i++) { | ||
$game->roll(0); | ||
} | ||
$this->assertSame(24, $game->score()); | ||
} | ||
|
||
public function testPerfectGame() { | ||
$game = new BowlingGame(); | ||
for ($i = 0; $i < 12; $i++) { | ||
$game->roll(10); | ||
} | ||
$this->assertSame(300, $game->score()); | ||
} | ||
} | ||
|
||
?> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.6" | ||
} | ||
} |
Oops, something went wrong.