Advertisement
NelloRizzo

GuessTheNumber

May 30th, 2020
2,837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2.  
  3. @Component({
  4.   selector: 'app-guessthenumber',
  5.   templateUrl: './guessthenumber.component.html',
  6.   styleUrls: ['./guessthenumber.component.css']
  7. })
  8. export class GuessthenumberComponent implements OnInit {
  9.  
  10.   tentativi: number; // tentativi effettuati dall'utente
  11.   target: number; // numero da indovinare
  12.   numero_digitato: number; // numero digitato dall'utente in UI
  13.   messaggio: string; // messaggio di feedback all'utente
  14.   partita_in_corso: boolean; // indica se la partita è in corso...
  15.  
  16.   iniziaPartita(): void {
  17.     this.partita_in_corso = true;
  18.     this.tentativi = 0;
  19.     this.target = Math.random() * 1000 + 1;
  20.     this.messaggio = "";
  21.   }
  22.  
  23.   terminaPartita() : void {
  24.     this.partita_in_corso = false;
  25.   }
  26.  
  27.   vinci(): void {
  28.     this.messaggio = "Hai vinto! Hai indovinato al " +
  29.       this.tentativi + "^ tentativo!";
  30.   }
  31.  
  32.   perdi() : void {
  33.     this.messaggio = "Hai perso! Il numero era " + this.target;
  34.   }
  35.  
  36.   nuovoTentativo() : void {
  37.     this.tentativi++; // incremento il numero di tentativi
  38.     // controllo se si è indovinato...
  39.     if (this.numero_digitato < this.target)
  40.       this.messaggio = "Hai digitato un numero troppo piccolo";
  41.     else if (this.numero_digitato > this.target)
  42.       this.messaggio = "Hai digitato un numero troppo grande";
  43.     else { // ... qui numero = target, quindi c'è vittoria!
  44.       this.terminaPartita();
  45.       this.vinci();
  46.     }
  47.     // se non c'è vittoria, controllo se i tentativi sono esauriti!
  48.     if (this.tentativi == 10 && this.partita_in_corso) {
  49.       this.terminaPartita();
  50.       this.perdi();
  51.     }
  52.   }
  53.  
  54.   constructor() { }
  55.  
  56.   ngOnInit(): void {
  57.     console.log("metodo ngOnInit")
  58.   }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement