Advertisement
am_dot_com

SW 2022-05-11

May 11th, 2022 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. //JogoDoAdivinha.js
  2. class JogoDoAdivinha{
  3. static DEFAULT_MIN = 1
  4. static DEFAULT_MAX = 999999
  5. static DEFAULT_NAME = "Anonymous"
  6.  
  7. static ESTADO_NAO_COMECOU = 1
  8. static ESTADO_JOGA_MENOR = 2
  9. static ESTADO_JOGA_MAIOR = 3
  10. static ESTADO_TERMINADO_PARABENS = 4
  11.  
  12. constructor(
  13. pOndeDarFeedback,
  14. pNome=JogoDoAdivinha.DEFAULT_NAME,
  15. pMin=JogoDoAdivinha.DEFAULT_MIN,
  16. pMax=JogoDoAdivinha.DEFAULT_MAX
  17. )
  18. {
  19. this.mOndeDarFeedback = pOndeDarFeedback
  20. this.mNome = pNome
  21. this.mMin = pMin
  22. this.mMax = pMax
  23.  
  24. this.mJogadas = []
  25. this.mQuandos = []
  26. this.mInicio = this.mFim = undefined
  27. this.mAdivinhaMe = numeroAleatorio(this.mMin, this.mMax)
  28.  
  29. this.mEstado = JogoDoAdivinha.ESTADO_NAO_COMECOU
  30.  
  31. this.mScore = undefined
  32. }//constructor
  33.  
  34. jogar(pN){
  35. var bPossoJogar = this.mEstado!=JogoDoAdivinha.ESTADO_TERMINADO_PARABENS
  36. if(bPossoJogar){
  37. this.mJogadas.push(pN)
  38. this.mQuandos.push(new Date())
  39.  
  40. var bPrimeiraJogada = this.mEstado==JogoDoAdivinha.ESTADO_NAO_COMECOU && this.mJogadas.length===1
  41. if (bPrimeiraJogada){
  42. this.mInicio = this.mQuandos[0]
  43. }
  44.  
  45. if(pN<this.mAdivinhaMe){
  46. this.mEstado = JogoDoAdivinha.ESTADO_JOGA_MAIOR
  47. }
  48.  
  49. if(pN>this.mAdivinhaMe){
  50. this.mEstado = JogoDoAdivinha.ESTADO_JOGA_MENOR
  51. }
  52.  
  53. if(pN===this.mAdivinhaMe){
  54. this.mEstado = JogoDoAdivinha.ESTADO_TERMINADO_PARABENS
  55. this.mFim = this.mQuandos[this.mQuandos-1]
  56. }
  57. }
  58. this.feedback()
  59. }//jogar
  60.  
  61. feedback(
  62. pbCheat=true
  63. ){
  64. var jogadaMaisRecente = this.mJogadas.length > 0 ?
  65. this.mJogadas[this.mJogadas.length-1]
  66. :
  67. undefined
  68.  
  69. //dashboard
  70. var strFeedback = ""
  71. strFeedback = this.dashboard()
  72. strFeedback+="<hr>"
  73.  
  74. //informar o utilizador
  75. var strUser = ""
  76. switch(this.mEstado){
  77. case JogoDoAdivinha.ESTADO_NAO_COMECOU:
  78. strUser = "Jogo por iniciar.<br>"
  79. strUser += "Faça uma primeira jogada, para começar.<br>"
  80. break
  81. case JogoDoAdivinha.ESTADO_JOGA_MENOR:
  82. strUser = pbCheat ?
  83. `O teu palpite ${jogadaMaisRecente} é maior do que o meu ${this.mAdivinhaMe}.`
  84. :
  85. `O teu palpite ${jogadaMaisRecente} é maior do que o meu.`
  86. strUser+="<br>Joga <mark>MENOR</mark><br>"
  87. break
  88.  
  89. case JogoDoAdivinha.ESTADO_JOGA_MAIOR:
  90. strUser = pbCheat ?
  91. `O teu palpite ${jogadaMaisRecente} é menor do que o meu ${this.mAdivinhaMe}.`
  92. :
  93. `O teu palpite ${jogadaMaisRecente} é menor do que o meu.`
  94. strUser+="<br>Joga <mark>MAIOR</mark><br>"
  95. break
  96.  
  97. case JogoDoAdivinha.ESTADO_TERMINADO_PARABENS:
  98. strUser = "<mark>Parabéns! Acertaste no meu número</mark><br>"
  99. //strUser += `<a href='ja1_config.html'>RESTART</a>`
  100. break
  101. }//switch
  102. this.mScore = this.score()
  103. strFeedback += strUser
  104. this.mOndeDarFeedback.innerHTML = strFeedback
  105. }//feedback
  106.  
  107.  
  108. dashboard(){
  109. var strDashboard = "<ul>"
  110.  
  111. for (var prop in this){
  112. if (prop!="mOndeDarFeedback"){
  113. strDashboard+=`<li><mark>${prop}:</mark>${this[prop]}</li>`
  114. }
  115. }//for
  116. strDashboard+=`<li><mark>tempo de jogo (décimos de segundo):</mark>${this.tempoDeJogoEmDecimosDeSegundo()}</li>`
  117. strDashboard+=`<li><mark>score:</mark>${this.score()}</li>`
  118. strDashboard+="</ul>"
  119. return strDashboard
  120. }//dashboard
  121.  
  122. score(){
  123. if (this.mJogadas.length>0){
  124. //recompensar? punir?
  125. var iAmp = Math.max(this.mMin, this.mMax) - Math.min(this.mMin, this.mMax)
  126. var recompensas = iAmp
  127. var punishments = this.mJogadas.length + this.tempoDeJogoEmDecimosDeSegundo()
  128. var score = Math.floor(recompensas/punishments)
  129. return score
  130. }
  131. }//score
  132.  
  133. /*
  134. confira às instâncias do tipo JogoDoAdivinha,
  135. um novo método, chamada "tempoDeJogoEmDecimosDeSegundo"
  136. que retorna quanto tempo já decorreu entre a jogada +recente
  137. e a primeira, em décimos de segundo.
  138. - assinatura
  139. - computação
  140. - retorno
  141. */
  142. tempoDeJogoEmDecimosDeSegundo(){
  143. if(this.mQuandos.length>0){
  144. var maisRecente = this.mQuandos[this.mQuandos.length-1]
  145. var primeira = this.mQuandos[0]
  146. var difMs = maisRecente-primeira
  147.  
  148. //difMs/1 //permanece em ms
  149. //difMs/10 //fica em cs (centésimos de segundo)
  150. return difMs/100 //décimos de segundo
  151. }
  152. return 0
  153. }//tempoDeJogoEmDecimosDeSegundo
  154. }//JogoDoAdivinha
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement