Parcourir la source

Merge branch '13-scrabble-score'

Frédéric G. MARAND il y a 6 mois
Parent
commit
f19333b078

+ 26 - 0
scrabble-score/.exercism/config.json

@@ -0,0 +1,26 @@
+{
+  "authors": [
+    "MichaelBunker"
+  ],
+  "contributors": [
+    "arueckauer",
+    "kunicmarko20",
+    "kytrinyx",
+    "petemcfarlane",
+    "mk-mxp"
+  ],
+  "files": {
+    "solution": [
+      "ScrabbleScore.php"
+    ],
+    "test": [
+      "ScrabbleScoreTest.php"
+    ],
+    "example": [
+      ".meta/example.php"
+    ]
+  },
+  "blurb": "Given a word, compute the Scrabble score for that word.",
+  "source": "Inspired by the Extreme Startup game",
+  "source_url": "https://github.com/rchatley/extreme_startup"
+}

+ 1 - 0
scrabble-score/.exercism/metadata.json

@@ -0,0 +1 @@
+{"track":"php","exercise":"scrabble-score","id":"8f22038d8bfc43cfbdd1665150a90d23","url":"https://exercism.org/tracks/php/exercises/scrabble-score","handle":"Fairgame","is_requester":true,"auto_approve":false}

+ 52 - 0
scrabble-score/HELP.md

@@ -0,0 +1,52 @@
+# Help
+
+## Running the tests
+
+## Running the tests
+
+1. Go to the root of your PHP exercise directory, which is `<EXERCISM_WORKSPACE>/php`.
+   To find the Exercism workspace run
+
+       ➜ exercism debug | grep Workspace
+
+1. Get [PHPUnit] if you don't have it already.
+
+       ➜ wget -O phpunit https://phar.phpunit.de/phpunit-9.phar
+       ➜ chmod +x phpunit
+       ➜ ./phpunit --version
+
+2. Execute the tests:
+
+       ➜ ./phpunit file_to_test.php
+
+   For example, to run the tests for the Hello World exercise, you would run:
+
+       ➜ ./phpunit HelloWorldTest.php
+
+[PHPUnit]: https://phpunit.de
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit ScrabbleScore.php` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [PHP track's documentation](https://exercism.org/docs/tracks/php)
+- The [PHP track's programming category on the forum](https://forum.exercism.org/c/programming/php)
+- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+To get help if you're having trouble, you can use one of the following resources:
+
+ - [/r/php](https://www.reddit.com/r/php) is the PHP subreddit.
+ - [StackOverflow](https://stackoverflow.com/questions/tagged/php) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

+ 56 - 0
scrabble-score/README.md

@@ -0,0 +1,56 @@
+# Scrabble Score
+
+Welcome to Scrabble Score on Exercism's PHP Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Introduction
+
+[Scrabble][wikipedia] is a word game where players place letter tiles on a board to form words.
+Each letter has a value.
+A word's score is the sum of its letters' values.
+
+[wikipedia]: https://en.wikipedia.org/wiki/Scrabble
+
+## Instructions
+
+Your task is to compute a word's Scrabble score by summing the values of its letters.
+
+The letters are valued as follows:
+
+| Letter                       | Value |
+| ---------------------------- | ----- |
+| A, E, I, O, U, L, N, R, S, T | 1     |
+| D, G                         | 2     |
+| B, C, M, P                   | 3     |
+| F, H, V, W, Y                | 4     |
+| K                            | 5     |
+| J, X                         | 8     |
+| Q, Z                         | 10    |
+
+For example, the word "cabbage" is worth 14 points:
+
+- 3 points for C
+- 1 point for A
+- 3 points for B
+- 3 points for B
+- 1 point for A
+- 2 points for G
+- 1 point for E
+
+## Source
+
+### Created by
+
+- @MichaelBunker
+
+### Contributed to by
+
+- @arueckauer
+- @kunicmarko20
+- @kytrinyx
+- @petemcfarlane
+- @mk-mxp
+
+### Based on
+
+Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup

+ 42 - 0
scrabble-score/ScrabbleScore.php

@@ -0,0 +1,42 @@
+<?php
+
+/*
+ * By adding type hints and enabling strict type checking, code can become
+ * easier to read, self-documenting and reduce the number of potential bugs.
+ * By default, type declarations are non-strict, which means they will attempt
+ * to change the original type to match the type specified by the
+ * type-declaration.
+ *
+ * In other words, if you pass a string to a function requiring a float,
+ * it will attempt to convert the string value to a float.
+ *
+ * To enable strict mode, a single declare directive must be placed at the top
+ * of the file.
+ * This means that the strictness of typing is configured on a per-file basis.
+ * This directive not only affects the type declarations of parameters, but also
+ * a function's return type.
+ *
+ * For more info review the Concept on strict type checking in the PHP track
+ * <link>.
+ *
+ * To disable strict typing, comment out the directive below.
+ */
+
+declare(strict_types=1);
+
+function score(string $word): int {
+  $score = 0;
+  $chars = mb_str_split(mb_strtoupper($word));
+  foreach ($chars as $char) {
+    $score += match ($char) {
+      'D', 'G' => 2,
+      'B', 'C', 'M', 'P' => 3,
+      'F', 'H', 'V', 'W', 'Y' => 4,
+      'K' => 5,
+      'J', 'X' => 8,
+      'Q', 'Z' => 10,
+      default => 1,
+    };
+  }
+  return $score;
+}

+ 124 - 0
scrabble-score/ScrabbleScoreTest.php

@@ -0,0 +1,124 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * Calculate the value of scrabble score for a given word.
+ */
+class ScrabbleScoreTest extends PHPUnit\Framework\TestCase
+{
+    public static function setUpBeforeClass(): void
+    {
+        require_once 'ScrabbleScore.php';
+    }
+
+    /**
+     * uuid f46cda29-1ca5-4ef2-bd45-388a767e3db2
+     * @testdox Lowercase letter
+     */
+    public function testLowercaseSingleLetter(): void
+    {
+        $word = 'a';
+        $this->assertEquals(1, score($word));
+    }
+
+    /**
+     * uuid f7794b49-f13e-45d1-a933-4e48459b2201
+     * @testdox Uppercase letter
+     */
+    public function testUppercaseSingleLetter(): void
+    {
+        $word = 'A';
+        $this->assertEquals(1, score($word));
+    }
+
+    /**
+     * uuid eaba9c76-f9fa-49c9-a1b0-d1ba3a5b31fa
+     * @testdox Valuable letter
+     */
+    public function testValuableSingleLetter(): void
+    {
+        $word = 'f';
+        $this->assertEquals(4, score($word));
+    }
+
+    /**
+     * uuid f3c8c94e-bb48-4da2-b09f-e832e103151e
+     * @testdox Short word
+     */
+    public function testShortWord(): void
+    {
+        $word = 'at';
+        $this->assertEquals(2, score($word));
+    }
+
+    /**
+     * uuid 71e3d8fa-900d-4548-930e-68e7067c4615
+     * @testdox Short, valuable word
+     */
+    public function testShortValuableWord(): void
+    {
+        $word = 'zoo';
+        $this->assertEquals(12, score($word));
+    }
+
+    /**
+     * uuid d3088ad9-570c-4b51-8764-c75d5a430e99
+     * @testdox Medium word
+     */
+    public function testMediumWord(): void
+    {
+        $word = 'street';
+        $this->assertEquals(6, score($word));
+    }
+
+    /**
+     * uuid fa20c572-ad86-400a-8511-64512daac352
+     * @testdox Medium, valuable word
+     */
+    public function testMediumValuableWord(): void
+    {
+        $word = 'quirky';
+        $this->assertEquals(22, score($word));
+    }
+
+    /**
+     * uuid 9336f0ba-9c2b-4fa0-bd1c-2e2d328cf967
+     * @testdox Long, mixed-case word
+     */
+    public function testLongMixedCaseWord(): void
+    {
+        $word = 'OxyphenButazone';
+        $this->assertEquals(41, score($word));
+    }
+
+    /**
+     * uuid 1e34e2c3-e444-4ea7-b598-3c2b46fd2c10
+     * @testdox English-like word
+     */
+    public function testEnglishLikeWord(): void
+    {
+        $word = 'pinata';
+        $this->assertEquals(8, score($word));
+    }
+
+    /**
+     * uuid 4efe3169-b3b6-4334-8bae-ff4ef24a7e4f
+     * @testdox Empty input
+     */
+    public function testEmptyWordScore(): void
+    {
+        $word = '';
+        $this->assertEquals(0, score($word));
+    }
+
+    /*
+     * uuid 3b305c1c-f260-4e15-a5b5-cb7d3ea7c3d7
+     * @testdox Entire alphabet available
+     */
+    public function testEntireAlphabetWord(): void
+    {
+        $word = 'abcdefghijklmnopqrstuvwxyz';
+        $this->assertEquals(87, score($word));
+    }
+}