From be7954f7f22bb86227e0d92e7ded79e3b80aae9d Mon Sep 17 00:00:00 2001 From: Mr Zany And Ilya Date: Tue, 25 Apr 2017 19:27:16 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B0=20=D0=BE=D0=BF=D1=80=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=B1=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- getPokerHand.js | 103 ++++++++++++++++++++++++++++++++++++- tests/getPokerHand-test.js | 31 ++++++++++- 2 files changed, 131 insertions(+), 3 deletions(-) diff --git a/getPokerHand.js b/getPokerHand.js index 1990681..84ee968 100644 --- a/getPokerHand.js +++ b/getPokerHand.js @@ -4,10 +4,109 @@ * @param {Array} dice пять костей, для которых нужно определить комбинацию * @returns {String} название комбинации */ + function getPokerHand(dice) { - // Напишите ваш замечательный код здесь + if(isPoker(dice)) + return 'Покер'; + count = getCount(dice) + if(isCare(count)) + return('Каре'); + if(isFullHouse(count)) + return('Фулл хаус'); + if(isTriple(count)) + return('Тройка'); + countOfPairs = getCountOfPairs(count); + //WSH.Echo('countOfPairs = ' + countOfPairs) + if(isTwoPairs(countOfPairs)) + return ('Две пары'); + if(isPair(countOfPairs)) + return('Пара'); + return('Наивысшее очко'); +} + +function getCount(dice){ + count = new Array(6); + for(i = 0; i < count.length; i++){ + count[i] = 0; + } + + for(i = 0; i < 5; i++){ + switch(dice[i]){ + case '1': + count[0]++; + break; + case '2': + count[1]++; + break; + case '3': + count[2]++; + break; + case '4': + count[3]++; + break; + case '5': + count[4]++; + break; + case '6': + count[5]++; + break; + } + } + //WSH.Echo('count ' + count) + return count; +} + +function isPoker(dice) { + oneDice = dice[0]; + count = 1; + for(i = 1; i < 5; i++) { + if(dice[i] == oneDice) + count++; + } + if(count == 5) + return true; + return false; +} + +function isCare(count){ + if(count[0] == 4 || count[1] == 4 || count[2] == 4 || count[3] == 4 || count[4] == 4 || count[5] == 4){ + return true + } + return false +} + +function isFullHouse(count){ + if((count[0] == 3 || count[1] == 3 || count[2] == 3 || count[3] == 3 || count[4] == 3 || count[5] == 3) && (count[0] == 2 || count[1] == 2 || count[2] == 2 || count[3] == 2 || count[4] == 2 || count[5] == 2)){ + return true; + return false; + } +} + +function isTriple(count){ + if(count[0] == 3 || count[1] == 3 || count[2] == 3 || count[3] == 3 || count[4] == 3 || count[5] == 3) + return true; + return false; +} + +function getCountOfPairs(count){ + countOfPairs = 0; + for(i = 0; i < count.length; i++){ + if(count[i] == 2) + countOfPairs++; + } + return countOfPairs; +} + +function isTwoPairs(count){ + if(count == 2) + return true; + return false; +} - return 'Покер'; +function isPair(count){ + if(count == 1) + return true; + return false; } module.exports = getPokerHand; diff --git a/tests/getPokerHand-test.js b/tests/getPokerHand-test.js index aaa1e59..2b8b2c1 100644 --- a/tests/getPokerHand-test.js +++ b/tests/getPokerHand-test.js @@ -8,5 +8,34 @@ describe('getPokerHand', () => { assert.equal(actual, 'Покер'); }); - // Напишите тесты на ваш замечательный код здесь + it('should return `Каре` for [1, 1, 1, 1, 2]', () => { + const actual = getPokerHand([1, 1, 1, 1, 2]); + + assert.equal(actual, 'Каре'); + }); + it('should return `Фулл хаус` for [1, 1, 1, 2, 2]', () => { + const actual = getPokerHand([1, 1, 1, 2, 2]); + + assert.equal(actual, 'Фулл хаус'); + }); + it('should return `Тройка` for [1, 1, 1, 2, 3]', () => { + const actual = getPokerHand([1, 1, 1, 2, 3]); + + assert.equal(actual, 'Тройка'); + }); + it('should return `Две пары` for [1, 1, 2, 2, 3]', () => { + const actual = getPokerHand([1, 1, 2, 2, 3]); + + assert.equal(actual, 'Две пары'); + }); + it('should return `Пара` for [1, 1, 2, 3, 4]', () => { + const actual = getPokerHand([1, 1, 2, 3, 4]); + + assert.equal(actual, 'Пара'); + }); + it('should return `Наивысшее очко` for [1, 2, 3, 4, 5]', () => { + const actual = getPokerHand([1, 2, 3, 4, 5]); + + assert.equal(actual, 'Наивысшее очко'); + }); }); From e30f97a61845645c1c8ee5d5fd5c7251e463cd54 Mon Sep 17 00:00:00 2001 From: Mr Zany And Ilya Date: Tue, 25 Apr 2017 19:56:27 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B0=20=D0=BE=D0=BF=D1=80=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=B1=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- getPokerHand.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/getPokerHand.js b/getPokerHand.js index 84ee968..a5fce1b 100644 --- a/getPokerHand.js +++ b/getPokerHand.js @@ -10,18 +10,18 @@ function getPokerHand(dice) { return 'Покер'; count = getCount(dice) if(isCare(count)) - return('Каре'); + return 'Каре'; if(isFullHouse(count)) - return('Фулл хаус'); + return 'Фулл хаус'; if(isTriple(count)) - return('Тройка'); + return 'Тройка'; countOfPairs = getCountOfPairs(count); //WSH.Echo('countOfPairs = ' + countOfPairs) if(isTwoPairs(countOfPairs)) - return ('Две пары'); + return 'Две пары'; if(isPair(countOfPairs)) - return('Пара'); - return('Наивысшее очко'); + return 'Пара'; + return 'Наивысшее очко'; } function getCount(dice){