Games
Fundamentals
Basic Language Features
const whoShouldServe = (scoreLeft, scoreRight, servesCount) => { return Math.floor((scoreLeft + scoreRight) / servesCount) % 2 === 0 ? 'first' : 'second'; };
function whoShouldServe($scoreLeft, $scoreRight, $servesCount) {return floor(($scoreLeft + $scoreRight) / $servesCount) % 2 === 0 ? 'first' : 'second';- const whoShouldServe = (scoreLeft, scoreRight, servesCount) => {
- return Math.floor((scoreLeft + scoreRight) / servesCount) % 2 === 0 ? 'first' : 'second';
- };
describe("Solution", function(){ it("Basic tests", function(){ a = whoShouldServe(0, 0, 2); Test.assertEquals(a, 'first', "expected: 'first' got: " + a); assert.strictEqual(a, 'first'); a = whoShouldServe(1, 0, 2); Test.assertEquals(a, 'first', "expected: 'first' got: " + a); assert.strictEqual(a, 'first'); a = whoShouldServe(1, 2, 2); Test.assertEquals(a, 'second', "expected: 'second' got: " + a); assert.strictEqual(a, 'second'); a = whoShouldServe(1, 3, 2); Test.assertEquals(a, 'first', "expected: 'first' got: " + a); assert.strictEqual(a, 'first'); a = whoShouldServe(7, 8, 2); Test.assertEquals(a, 'second', "expected: 'second' got: " + a); assert.strictEqual(a, 'second'); a = whoShouldServe(7, 8, 5); Test.assertEquals(a, 'second', "expected: 'second' got: " + a); assert.strictEqual(a, 'second'); a = whoShouldServe(0, 6, 5); Test.assertEquals(a, 'second', "expected: 'second' got: " + a); assert.strictEqual(a, 'second'); a = whoShouldServe(1, 10, 5); Test.assertEquals(a, 'first', "expected: 'first' got: " + a); assert.strictEqual(a, 'first'); }); const whoIs = (scoreLeft, scoreRight, servesCount) => Math.floor((scoreLeft + scoreRight) / servesCount) % 2 === 0 ? 'first' : 'second'; const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min; it("Random tests", function(){ scoreLeft = getRandomInt(1, 12); scoreRight = getRandomInt(1, 12); a = whoShouldServe(scoreLeft, scoreRight, 2); Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a); assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2)); scoreLeft = getRandomInt(1, 12); scoreRight = getRandomInt(1, 12); a = whoShouldServe(scoreLeft, scoreRight, 2); Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a); assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2)); scoreLeft = getRandomInt(1, 12); scoreRight = getRandomInt(1, 12); a = whoShouldServe(scoreLeft, scoreRight, 2); Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a); assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2)); scoreLeft = getRandomInt(1, 12); scoreRight = getRandomInt(1, 12); a = whoShouldServe(scoreLeft, scoreRight, 2); Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a); assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2)); scoreLeft = getRandomInt(1, 12); scoreRight = getRandomInt(1, 12); a = whoShouldServe(scoreLeft, scoreRight, 2); Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a); assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2)); }); });
class EqualsTest extends TestCase{public function testBasic(){$a = whoShouldServe(0, 0, 2);$this->assertEquals($a, 'first');$a = whoShouldServe(1, 0, 2);$this->assertEquals($a, 'first');$a = whoShouldServe(1, 2, 2);$this->assertEquals($a, 'second');$a = whoShouldServe(1, 3, 2);$this->assertEquals($a, 'first');$a = whoShouldServe(7, 8, 2);$this->assertEquals($a, 'second');$a = whoShouldServe(7, 8, 5);$this->assertEquals($a, 'second');$a = whoShouldServe(0, 6, 5);$this->assertEquals($a, 'second');$a = whoShouldServe(1, 10, 5);$this->assertEquals($a, 'first');}protected function whoIs($scoreLeft, $scoreRight, $servesCount) {return floor(($scoreLeft + $scoreRight) / $servesCount) % 2 === 0 ? 'first' : 'second';}public function testRandom(){$scoreLeft = rand(1, 11);$scoreRight = rand(1, 11);$a = whoShouldServe($scoreLeft, $scoreRight, 2);$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));$scoreLeft = rand(1, 11);$scoreRight = rand(1, 11);$a = whoShouldServe($scoreLeft, $scoreRight, 2);$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));$scoreLeft = rand(1, 11);$scoreRight = rand(1, 11);$a = whoShouldServe($scoreLeft, $scoreRight, 2);$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));$scoreLeft = rand(1, 11);$scoreRight = rand(1, 11);$a = whoShouldServe($scoreLeft, $scoreRight, 2);$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));$scoreLeft = rand(1, 11);$scoreRight = rand(1, 11);$a = whoShouldServe($scoreLeft, $scoreRight, 2);$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));}}- describe("Solution", function(){
- it("Basic tests", function(){
- a = whoShouldServe(0, 0, 2);
- Test.assertEquals(a, 'first', "expected: 'first' got: " + a);
- assert.strictEqual(a, 'first');
- a = whoShouldServe(1, 0, 2);
- Test.assertEquals(a, 'first', "expected: 'first' got: " + a);
- assert.strictEqual(a, 'first');
- a = whoShouldServe(1, 2, 2);
- Test.assertEquals(a, 'second', "expected: 'second' got: " + a);
- assert.strictEqual(a, 'second');
- a = whoShouldServe(1, 3, 2);
- Test.assertEquals(a, 'first', "expected: 'first' got: " + a);
- assert.strictEqual(a, 'first');
- a = whoShouldServe(7, 8, 2);
- Test.assertEquals(a, 'second', "expected: 'second' got: " + a);
- assert.strictEqual(a, 'second');
- a = whoShouldServe(7, 8, 5);
- Test.assertEquals(a, 'second', "expected: 'second' got: " + a);
- assert.strictEqual(a, 'second');
- a = whoShouldServe(0, 6, 5);
- Test.assertEquals(a, 'second', "expected: 'second' got: " + a);
- assert.strictEqual(a, 'second');
- a = whoShouldServe(1, 10, 5);
- Test.assertEquals(a, 'first', "expected: 'first' got: " + a);
- assert.strictEqual(a, 'first');
- });
- const whoIs = (scoreLeft, scoreRight, servesCount) => Math.floor((scoreLeft + scoreRight) / servesCount) % 2 === 0 ? 'first' : 'second';
- const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min;
- it("Random tests", function(){
- scoreLeft = getRandomInt(1, 12);
- scoreRight = getRandomInt(1, 12);
- a = whoShouldServe(scoreLeft, scoreRight, 2);
- Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a);
- assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2));
- scoreLeft = getRandomInt(1, 12);
- scoreRight = getRandomInt(1, 12);
- a = whoShouldServe(scoreLeft, scoreRight, 2);
- Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a);
- assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2));
- scoreLeft = getRandomInt(1, 12);
- scoreRight = getRandomInt(1, 12);
- a = whoShouldServe(scoreLeft, scoreRight, 2);
- Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a);
- assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2));
- scoreLeft = getRandomInt(1, 12);
- scoreRight = getRandomInt(1, 12);
- a = whoShouldServe(scoreLeft, scoreRight, 2);
- Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a);
- assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2));
- scoreLeft = getRandomInt(1, 12);
- scoreRight = getRandomInt(1, 12);
- a = whoShouldServe(scoreLeft, scoreRight, 2);
- Test.assertEquals(a, whoIs(scoreLeft, scoreRight, 2), "expected: " + whoIs(scoreLeft, scoreRight, 2) + " got: " + a);
- assert.strictEqual(a, whoIs(scoreLeft, scoreRight, 2));
- });
- });
Games
Fundamentals
Basic Language Features
This kata is about PING PONG game.
By receiveing a current score of the game and amount of serves per one player method should return a person ("first"
or "second"
) who should do a next serve.
For example:
- Score 0:0 and 2 serves per player - then
whoShouldServe(0, 0, 2) === "first"
- Score 4:5 and 2 serves per player - then
whoShouldServe(4, 5, 2) === "second"
function whoShouldServe($scoreLeft, $scoreRight, $servesCount) {
return floor(($scoreLeft + $scoreRight) / $servesCount) % 2 === 0 ? 'first' : 'second';
};
class EqualsTest extends TestCase
{
public function testBasic()
{
$a = whoShouldServe(0, 0, 2);
$this->assertEquals($a, 'first');
$a = whoShouldServe(1, 0, 2);
$this->assertEquals($a, 'first');
$a = whoShouldServe(1, 2, 2);
$this->assertEquals($a, 'second');
$a = whoShouldServe(1, 3, 2);
$this->assertEquals($a, 'first');
$a = whoShouldServe(7, 8, 2);
$this->assertEquals($a, 'second');
$a = whoShouldServe(7, 8, 5);
$this->assertEquals($a, 'second');
$a = whoShouldServe(0, 6, 5);
$this->assertEquals($a, 'second');
$a = whoShouldServe(1, 10, 5);
$this->assertEquals($a, 'first');
}
protected function whoIs($scoreLeft, $scoreRight, $servesCount) {
return floor(($scoreLeft + $scoreRight) / $servesCount) % 2 === 0 ? 'first' : 'second';
}
public function testRandom()
{
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
}
}