Created
February 26, 2020 06:39
-
-
Save efturtle/1504ce0ced3cc4ca1f13cab142dd4033 to your computer and use it in GitHub Desktop.
codigo del Juego de Adivinanza
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.juegoadivinanza; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| import androidx.appcompat.app.AlertDialog; | |
| import android.content.Intent; | |
| import android.content.DialogInterface; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.widget.EditText; | |
| import android.widget.ImageView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import org.w3c.dom.Text; | |
| public class MainActivity extends AppCompatActivity { | |
| private ImageView img01, img02, img03; | |
| private int puntaje = 0; | |
| private int intentos = 0; | |
| private int numeroSecreto; | |
| private EditText usuarioInput; | |
| private TextView acumulacion; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| img01 = (ImageView) findViewById(R.id.imageView); | |
| img02 = (ImageView) findViewById(R.id.imageView4); | |
| img03 = (ImageView) findViewById(R.id.imageView5); | |
| usuarioInput = (EditText) findViewById(R.id.editText); | |
| acumulacion = (TextView) findViewById(R.id.textView4); | |
| inicializarJuego(); | |
| } | |
| private void inicializarJuego(){ | |
| //intentos utilizados | |
| this.intentos = 3; | |
| //random number to play | |
| this.numeroSecreto = (int)(Math.random()*9)+1; | |
| //taches invisibles | |
| img01.setVisibility(View.INVISIBLE); | |
| img02.setVisibility(View.INVISIBLE); | |
| img03.setVisibility(View.INVISIBLE); | |
| usuarioInput.setText(""); | |
| //TextView debug = findViewById(R.id.textView5); | |
| //los puntos del usuario | |
| acumulacion.setText("Puntaje: "+this.puntaje); | |
| //temp value with the numero secreto as a string | |
| //String temp = String.valueOf(randomNumber); | |
| //debug.setText(temp); | |
| } | |
| public void comprobar(View view){ | |
| //El usuario ya registro un numero, comprobemoslo | |
| if(usuarioInput!=null){ | |
| String tempo = usuarioInput.getText().toString(); | |
| int temp = Integer.parseInt(tempo); | |
| switch (this.intentos){ | |
| case 3: | |
| if(temp == this.numeroSecreto) { | |
| Toast.makeText(this, "Felicitaciones adivinaste el numero!", Toast.LENGTH_LONG).show(); | |
| this.puntaje+=10; | |
| builderSI(); | |
| }else{ | |
| img01.setVisibility(View.VISIBLE); | |
| Toast.makeText(this, "Te restan 2 intentos!", Toast.LENGTH_LONG).show(); | |
| this.intentos = 2; | |
| } | |
| break; | |
| case 2: | |
| if (temp == this.numeroSecreto){ | |
| Toast.makeText(this, "Felicitaciones adivinaste el numero!", Toast.LENGTH_LONG).show(); | |
| this.puntaje+=8; | |
| builderSI(); | |
| }else{ | |
| img02.setVisibility(View.VISIBLE); | |
| Toast.makeText(this, "Te resta 1 intento!", Toast.LENGTH_LONG).show(); | |
| this.intentos = 1; | |
| } | |
| break; | |
| case 1: | |
| if (temp == this.numeroSecreto){ | |
| Toast.makeText(this, "Felicitaciones adivinaste el numero!", Toast.LENGTH_LONG).show(); | |
| this.puntaje+=6; | |
| builderSI(); | |
| }else{ | |
| img03.setVisibility(View.VISIBLE); | |
| Toast.makeText(this, "La respuesta era "+ this.numeroSecreto, Toast.LENGTH_LONG).show(); | |
| builderN0(); | |
| } | |
| break; | |
| } | |
| }else{ | |
| Toast.makeText(this, "Ingrese un numero del 1 al 10", Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| public void reiniciarJuego (){ | |
| } | |
| private void builderN0(){ | |
| AlertDialog.Builder builder; | |
| builder = new AlertDialog.Builder(this); | |
| builder.setMessage("¿Jugar de nuevo?").setTitle("El numero era = "+this.numeroSecreto); | |
| builder.setPositiveButton("Jugar de nuevo", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialogInterface, int i) { | |
| inicializarJuego(); | |
| } | |
| }); | |
| builder.setNegativeButton("Salir", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialogInterface, int i) { | |
| Intent intent = new Intent(Intent.ACTION_MAIN); | |
| finish(); | |
| } | |
| }); | |
| builder.show(); | |
| } | |
| private void builderSI(){ | |
| AlertDialog.Builder builder; | |
| builder = new AlertDialog.Builder(this); | |
| builder.setMessage("¿Jugar de nuevo?").setTitle("Felicitaciones!!"); | |
| builder.setPositiveButton("Jugar de nuevo", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialogInterface, int i) { | |
| inicializarJuego(); | |
| } | |
| }); | |
| builder.setNegativeButton("Salir", new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialogInterface, int i) { | |
| Intent intent = new Intent(Intent.ACTION_MAIN); | |
| finish(); | |
| } | |
| }); | |
| builder.show(); | |
| } | |
| public void helpMe(View view){ | |
| if (usuarioInput != null){ | |
| //converts the values for use | |
| String tempo = usuarioInput.getText().toString(); | |
| int temp = Integer.parseInt(tempo); | |
| if(this.numeroSecreto > temp){ | |
| Toast.makeText(this, "El numero es mayor", Toast.LENGTH_SHORT).show(); | |
| }else{ | |
| Toast.makeText(this, "El numero es menor", Toast.LENGTH_SHORT).show(); | |
| } | |
| if(this.puntaje > 2){ | |
| this.puntaje-=2; | |
| }else if (this.puntaje < 2){ | |
| this.puntaje = 0; | |
| }else{ | |
| this.puntaje = 0; | |
| } | |
| }else{ | |
| Toast.makeText(this, "Ingresa un numero y te ayudo!", Toast.LENGTH_SHORT).show(); | |
| } | |
| } | |
| @Override | |
| protected void onResume() { | |
| super.onResume(); | |
| inicializarJuego(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment