Skip to content

Commit f6eb680

Browse files
committed
Adding files to the project.
0 parents  commit f6eb680

32 files changed

+100607
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/ci/cache/
2+
/ci/phpunit/
3+
/src/**/*.php
4+
!/src/Training/Easy/ASCIIArt/ASCIIArt.php
5+
!/src/Training/Easy/HorseRacingDuals/HorseRacingDuals.php
6+
!/src/Puzzle.php
7+
/tools/
8+
/vendor/

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [1.0.0] - 2022-02-07
8+
### Added
9+
- Base files to the project.
10+
- Tests for "Horse-racing Duals".
11+
- Tests for "ASCII Art".

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM php:7.3-cli-alpine
2+
RUN apk update \
3+
&& apk add gpg $PHPIZE_DEPS \
4+
&& docker-php-ext-install mbstring \
5+
&& pecl install xdebug \
6+
&& docker-php-ext-enable mbstring xdebug \
7+
&& wget -O /usr/local/bin/phive https://phar.io/releases/phive.phar \
8+
&& chmod +x /usr/local/bin/phive \
9+
&& apk del --purge $PHPIZE_DEPS \
10+
&& echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
11+
12+
COPY --from=composer /usr/bin/composer /usr/bin/composer
13+
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
14+
ENV COMPOSER_ALLOW_SUPERUSER=1

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Cyril VERLOOP
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Codingame
2+
3+
A project to test your [Codingame](https://www.codingame.com/) PHP code. It is not intended to have solutions.
4+
It only contains PHPUnit tests to let you code in your favorite IDE, outside of the Codingame web site.
5+
6+
[![License](https://img.shields.io/github/license/cyrilverloop/codingame)](https://github.com/cyrilverloop/codingame/blob/trunk/LICENSE)
7+
[![PHP version](https://img.shields.io/badge/php-%3D7.3-%23777BB4?logo=php&style=flat)](https://www.php.net/)
8+
9+
10+
## Installation
11+
12+
Downloading the project :
13+
```shellsession
14+
user@host ~$ cd [PATH_WHERE_TO_PUT_THE_PROJECT] # E.g. ~/projects/
15+
user@host projects$ git clone https://github.com/cyrilverloop/codingame.git
16+
user@host projects$ cd codingame
17+
```
18+
19+
Installing the dependencies :
20+
```shellsession
21+
user@host codingame$ docker compose run --rm app composer install -o
22+
user@host codingame$ docker compose run --rm app phive install --trust-gpg-keys 4AA394086372C20A,12CE0F1D262429A5,31C7E470E2138192
23+
```
24+
25+
26+
## Usage
27+
28+
Every classes in `./src/` contains an `execute()` method with the default Codingame code.
29+
You can add every PHP code you want in this directory to solve the puzzles.
30+
31+
Executing all the tests :
32+
```shellsession
33+
user@host codingame$ docker compose run --rm app ./tools/phpunit -c ./ci/phpunit.xml
34+
```
35+
36+
Executing tests for a particular puzzle :
37+
```shellsession
38+
user@host codingame$ docker compose run --rm app ./tools/phpunit -c ./ci/phpunit.xml --group [GROUP_NAME]
39+
```
40+
41+
To view the list of test groups :
42+
```shellsession
43+
user@host codingame$ docker compose run --rm app ./tools/phpunit -c ./ci/phpunit.xml --list-groups
44+
```
45+
46+
47+
## Back to Codingame
48+
49+
When you copy your code back to Codingame, you must change every `$stdin` in `fscanf()`, `stream_get_line()`, ...
50+
by `STDIN` :
51+
```php
52+
// In this project :
53+
fscanf($stdin, "%d", $N);
54+
```
55+
56+
```php
57+
// On Codingame :
58+
fscanf(STDIN, "%d", $N);
59+
```
60+
61+
62+
## Timed out
63+
64+
Your code can work through this project and timed out on Codingame.
65+
It means your solution is not optimum and you should find another algorithm.

ci/phpunit.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
4+
bootstrap="../vendor/autoload.php"
5+
cacheResultFile="./cache/.phpunit.result.cache"
6+
forceCoversAnnotation="true"
7+
beStrictAboutCoversAnnotation="true"
8+
beStrictAboutOutputDuringTests="true"
9+
beStrictAboutTodoAnnotatedTests="true"
10+
verbose="true"
11+
colors="true"
12+
>
13+
<coverage processUncoveredFiles="true">
14+
<include>
15+
<directory suffix=".php">../src</directory>
16+
</include>
17+
18+
<report>
19+
<clover outputFile="phpunit/clover.xml" />
20+
<crap4j outputFile="phpunit/crap4j.xml" />
21+
<html outputDirectory="phpunit/html" lowUpperBound="35" highLowerBound="70" />
22+
<xml outputDirectory="phpunit/xml" />
23+
</report>
24+
</coverage>
25+
26+
<testsuites>
27+
<testsuite name="Project Test Suite">
28+
<directory>../tests</directory>
29+
</testsuite>
30+
</testsuites>
31+
32+
<logging>
33+
<junit outputFile="phpunit/junit.xml" />
34+
<testdoxHtml outputFile="phpunit/testdox.html" />
35+
</logging>
36+
</phpunit>

ci/psalm.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
autoloader="../vendor/autoload.php"
4+
errorLevel="1"
5+
resolveFromConfigFile="true"
6+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7+
xmlns="https://getpsalm.org/schema/config"
8+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
9+
>
10+
<projectFiles>
11+
<directory name="../src/" />
12+
<ignoreFiles>
13+
<directory name="../vendor/" />
14+
</ignoreFiles>
15+
</projectFiles>
16+
</psalm>

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "cyril-verloop/codingame",
3+
"description": "A project to test your Codingame PHP code.",
4+
"type": "project",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"CyrilVerloop\\Codingame\\": "src/"
9+
}
10+
},
11+
"autoload-dev": {
12+
"psr-4": {
13+
"CyrilVerloop\\Codingame\\Tests\\": "tests/"
14+
}
15+
},
16+
"authors": [
17+
{
18+
"name": "Cyril VERLOOP",
19+
"email": "[email protected]"
20+
}
21+
],
22+
"require": {
23+
"php": "7.3.*"
24+
}
25+
}

composer.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
app:
3+
build: ./
4+
volumes:
5+
- ./:/var/www/
6+
working_dir: /var/www/

phive.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phive xmlns="https://phar.io/phive">
3+
<phar name="phpunit" version="^9.5.13" installed="9.5.13" location="./tools/phpunit" copy="true"/>
4+
<phar name="psalm" version="^4.18.1" installed="4.19.0" location="./tools/psalm" copy="true"/>
5+
<phar name="phpcs" version="^3.6.2" installed="3.6.2" location="./tools/phpcs" copy="true"/>
6+
<phar name="phpcbf" version="^3.6.2" installed="3.6.2" location="./tools/phpcbf" copy="true"/>
7+
</phive>

src/Puzzle.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CyrilVerloop\Codingame;
6+
7+
abstract class Puzzle
8+
{
9+
// Methods :
10+
11+
abstract public function execute($stdin): void;
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CyrilVerloop\Codingame\Training\Easy\ASCIIArt;
6+
7+
use CyrilVerloop\Codingame\Puzzle;
8+
9+
class ASCIIArt extends Puzzle
10+
{
11+
// Methods :
12+
13+
public function execute($stdin): void
14+
{
15+
fscanf($stdin, "%d", $L);
16+
fscanf($stdin, "%d", $H);
17+
$T = stream_get_line($stdin, 256 + 1, "\n");
18+
for ($i = 0; $i < $H; $i++) {
19+
$ROW = stream_get_line($stdin, 1024 + 1, "\n");
20+
}
21+
22+
// Write an answer using echo(). DON'T FORGET THE TRAILING \n
23+
24+
echo("answer\n");
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CyrilVerloop\Codingame\Training\Easy\HorseRacingDuals;
6+
7+
use CyrilVerloop\Codingame\Puzzle;
8+
9+
class HorseRacingDuals extends Puzzle
10+
{
11+
// Methods :
12+
13+
public function execute($stdin): void
14+
{
15+
fscanf($stdin, "%d", $N);
16+
for ($i = 0; $i < $N; $i++) {
17+
fscanf($stdin, "%d", $pi);
18+
}
19+
20+
// Write an answer using echo(). DON'T FORGET THE TRAILING \n
21+
22+
echo("answer\n");
23+
}
24+
}

tests/PuzzleTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CyrilVerloop\Codingame\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @coversDefaultClass \CyrilVerloop\Codingame\Training\Easy\HorseRacingDuals\HorseRacingDuals
11+
*/
12+
abstract class PuzzleTest extends TestCase
13+
{
14+
// Properties :
15+
16+
/**
17+
* @var \CyrilVerloop\Codingame\Puzzle the puzzle.
18+
*/
19+
protected $puzzle;
20+
21+
22+
// Methods :
23+
24+
/**
25+
* Expects execute to output answer.
26+
*/
27+
protected function expectExecuteOutputAnswer(string $filename, string $expectedAnswer): void
28+
{
29+
$stdin = fopen($filename, 'r');
30+
31+
$this->expectOutputString($expectedAnswer);
32+
33+
$this->puzzle->execute($stdin);
34+
}
35+
}

0 commit comments

Comments
 (0)