Guest User

Untitled

a guest
Mar 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. class Bowling {
  2. constructor() {
  3. this.scoreBoard = new Array(22);
  4. this.STRIKE = 10;
  5. this.SPARE = 10;
  6. }
  7.  
  8. startNewRound() {
  9. this.scoreBoard.fill(0);
  10. this.currentRoll = 0;
  11. this.currentScore = 0;
  12. }
  13.  
  14. printScoreBoard() {
  15. //Row 1: Headers
  16. for (var i = 1; i <= 10; i++) {
  17. process.stdout.write(i + "\t");
  18. }
  19. console.log("Extra");
  20.  
  21. //Row 2: Round scores
  22. for (var i = 0; i < this.scoreBoard.length; i=i+2) {
  23. if (this.scoreBoard[i] == this.STRIKE) {
  24. process.stdout.write("X\t");
  25. } else if(this.scoreBoard[i] + this.scoreBoard[i+1] == this.SPARE) {
  26. process.stdout.write(this.scoreBoard[i].toString() + " /\t");
  27. } else {
  28. process.stdout.write(this.scoreBoard[i].toString() + " " + this.scoreBoard[i+1].toString() + "\t");
  29. }
  30. }
  31. console.log("");
  32.  
  33. //Row 3: Accumulated scores
  34. for (var i = 0; i < 20; i=i+2) {
  35. if (this.scoreBoard[i] == this.STRIKE) {
  36. this.currentScore = this.currentScore + this.STRIKE + this.scoreBoard[i+2] + this.scoreBoard[i+3];
  37. } else if(this.scoreBoard[i] + this.scoreBoard[i+1] == this.SPARE) {
  38. this.currentScore = this.currentScore + this.SPARE + this.scoreBoard[i+2];
  39. } else {
  40. this.currentScore = this.currentScore + this.scoreBoard[i] + this.scoreBoard[i+1];
  41. }
  42. process.stdout.write(this.currentScore + "\t")
  43. }
  44. console.log("")
  45. }
  46.  
  47. roll() {
  48. var isEvenRoll = this.currentRoll % 2 == 0 ? true : false;
  49. var availablePins = isEvenRoll ? 10 : 10 - this.scoreBoard[this.currentRoll-1];
  50. var pinsDown = 0;
  51. var rand = Math.random();
  52.  
  53. if (availablePins != 0) {
  54. pinsDown = (rand != 0) ? Math.floor((rand*availablePins) + 1) : 0;
  55. }
  56. this.scoreBoard[this.currentRoll++] = pinsDown;
  57. }
  58.  
  59. canRollAgain() {
  60. if (this.currentRoll < 20) {
  61. return true;
  62. } else if ((this.currentRoll == 20) && (this.scoreBoard[this.currentRoll-2] == this.STRIKE)) {
  63. return true;
  64. } else if ((this.currentRoll == 21) && (this.scoreBoard[this.currentRoll-3] == this.STRIKE)) {
  65. return true;
  66. } else if (this.currentRoll == 20 && (this.scoreBoard[this.currentRoll-1] + this.scoreBoard[this.currentRoll-2] == this.SPARE)) {
  67. return true;
  68. } else {
  69. return false;
  70. }
  71. }
  72. }
  73.  
  74. var bowling = new Bowling();
  75. bowling.startNewRound();
  76. while (bowling.canRollAgain()) {
  77. bowling.roll();
  78. }
  79.  
  80. bowling.printScoreBoard();
Add Comment
Please, Sign In to add comment