Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
steelpipe75 committed May 7, 2024
2 parents 05ef36b + cf573bb commit 792f441
Show file tree
Hide file tree
Showing 14 changed files with 2,131 additions and 0 deletions.
1 change: 1 addition & 0 deletions java/bowling_game/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/**
29 changes: 29 additions & 0 deletions java/bowling_game/src/BowlingGame.java
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;
}
}
35 changes: 35 additions & 0 deletions java/bowling_game/src/Main.java
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());
}
}
}
18 changes: 18 additions & 0 deletions php/README.md
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
```
1 change: 1 addition & 0 deletions php/bowling_game/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/**
53 changes: 53 additions & 0 deletions php/bowling_game/bowling_game.php
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];
}
}

?>
55 changes: 55 additions & 0 deletions php/bowling_game/bowling_game_test.php
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());
}
}

?>
5 changes: 5 additions & 0 deletions php/bowling_game/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require-dev": {
"phpunit/phpunit": "^9.6"
}
}
Loading

0 comments on commit 792f441

Please sign in to comment.