Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lesson2 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# lessons
# lesson
98 changes: 98 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const term = require( 'terminal-kit' ).terminal ;

let progressBar , progress = 0 ;
let summ = Math.floor(Math.random() * 21);
const items = [
'Взять еще карту' ,
'Хватит' ,
];

const doProgress = () => {
progress += Math.random() / 10 ;
progressBar.update(progress) ;
if (progress >= 1) {
setTimeout(() => {term( '\n' ); next();},200);
} else {
setTimeout(doProgress , 100 + Math.random() * 100);
}
};

const doProgressAgain = () => {
progress += Math.random() / 10 ;
progressBar.update(progress) ;
if (progress >= 1) {
setTimeout(() => {term( '\n' ); playComputer();},200);
} else {
setTimeout(doProgressAgain , 100 + Math.random() * 100);
}
};

const next = () => {
term.cyan( `Сумма твоих карт ${summ}.\n` );
term.singleColumnMenu( items , function( error , response ) {
if (response.selectedIndex === 0) {
summ += Math.floor(Math.random() * 11);
if (summ > 21) {
exit();
} else if(summ === 21) {
term.green('BLACKJACK\n');
stop();
} else {
next();
}
} else {
stop();
}
});
};

const exit = () => {
term.red( `Сумма твоих карт ${summ}, ты проиграл\n` );
summ = Math.floor(Math.random() * 21);
progress = 0;
entryQuestion('Сыграем еще? [Y|n]\n' );
};

const stop = () => {
progress = 0;
progressBar = term.progressBar({
width: 50 ,
title: 'Сдаю карты себе:' ,
percent: true
});
doProgressAgain();
};

const playComputer = () => {
const computerSumm = Math.floor(Math.random()*40);
if (computerSumm <= 21 && computerSumm > summ) {
term.red(`У меня ${computerSumm}, я победил\n`);
} else if (computerSumm <= 21 && computerSumm < summ) {
term.green(`У меня ${computerSumm}, ты победил!\n`);
} else {
term.green('У меня перебор, ты победил!\n');
}
summ = Math.floor(Math.random() * 21);
progress = 0;
entryQuestion('Сыграем еще? [Y|n]\n' );
};

const entryQuestion = (text) => {
term(text);
term.yesOrNo({yes:['y','ENTER'], no:['n']} , (error, result) => {
if (result) {
progressBar = term.progressBar({
width: 50 ,
title: 'Сдаю карты:' ,
percent: true
});
doProgress();
}
else {
term.red("Ой, всё!\n");
process.exit();
}
});
};

entryQuestion('Поиграем BlackJack? [Y|n]\n');
187 changes: 187 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "lessons",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"terminal-kit": "^1.32.3"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/n-randin/lessons.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/n-randin/lessons/issues"
},
"homepage": "https://github.com/n-randin/lessons#readme"
}