Guest User

Untitled

a guest
Feb 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. function newGame() {
  2. let check = true;
  3. let num = 0;
  4. while (check) {
  5. num = Math.floor(Math.random() * (899) + 100);
  6. if (!(isOverlap(num))) check = false;
  7. }
  8. game.ball = num;
  9. return num;
  10. }
  11. let game = {};
  12.  
  13. // 플레이 게임
  14. function playGame(myNum) {
  15. if (typeof game.ball === 'undefined') newGame();
  16. const COM_NUM = game.ball.toString();
  17.  
  18. if (typeof myNum !== 'number') return '숫자를 입력해야 합니다';
  19. if (!isRange(myNum)) return '세 자리 수를 입력해야 합니다';
  20. if (isOverlap(myNum)) return '중복되는 숫자가 없게 다시 입력해주세요'
  21. let result = throwBall(COM_NUM, myNum);
  22. if (result.strike === 3) return '3개의 숫자를 모두 맞추셨습니다! 게임 종료';
  23.  
  24. return (result.strike === 0 && result.ball === 0) ? 'Nothing' : `strike : ${result.strike}\nball : ${result.ball}`
  25.  
  26. }
  27.  
  28. // 숫자가 세 자리 정수인지 체크
  29. function isRange(num) {
  30. return num >= 100 && num <= 999;
  31. }
  32.  
  33. // 중복되는 숫자 있는지 체크
  34. function isOverlap(num) {
  35. num = num.toString().split('');
  36. var tested = new Set();
  37. tested = num.reduce(function(a, b) {
  38. if (a.indexOf(b) < 0) a.push(b);
  39. return a;
  40. }, []);
  41. return num.length !== tested.length;
  42. }
  43.  
  44. // 컴퓨터 숫자와 내 숫자 비교한 후 스트라이크, 볼 리턴
  45. function throwBall(comNum, myNum) {
  46. myNum = myNum.toString().split('');
  47. comNum = comNum.split('');
  48. let strike = strikeCheck(comNum, myNum);
  49. let ball = checkBall(comNum, myNum);
  50. return { strike: strike, ball: ball };
  51. }
  52.  
  53. // 볼 개수 측정
  54. function checkBall(comNum, myNum) {
  55. let ball = 0;
  56. myNum.map(function(obj, index) {
  57. for (let i = 0; i < comNum.length; i++) {
  58. if (obj === comNum[i] && i !== myNum.indexOf(obj)) ball++;
  59. }
  60. })
  61. return ball;
  62. }
  63.  
  64. function AreaOfCircle(radius) {
  65. var area = Math.PI * (radius * radius);
  66. return area.toFixed(0);
  67. }
  68. // 스트라이크 개수 측정
  69. function strikeCheck(comNum, myNum) {
  70. let strike = 0;
  71. myNum.map(function(obj, index) {
  72. if (obj === comNum[index]) strike++;
  73. })
  74.  
  75. comNum.map
  76. return strike;
  77. }
  78.  
  79.  
  80. // 플레이볼 comNum = 438
  81. console.log(playGame(439));
Add Comment
Please, Sign In to add comment