Pantalla de juego completamente funcional: Pantalla de pausa, victoria, derrota,…

Pantalla de juego completamente funcional: Pantalla de pausa, victoria, derrota, pistas, reinicio y cambio de mano implementados
parent b44b4d5e
export 'pista.dart';
\ No newline at end of file
import 'package:flutter/material.dart';
import 'package:peponator/widgets/pista_widget.dart';
class Pista{
static const primos = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
final ClasePista clase;
final VoidCallback? onPressed;
late final int divisible;
late final String mensaje;
EstadoPista estado = EstadoPista.BLOQUEADO;
Pista ({
required this.clase,
required int numero,
this.onPressed
}) {
if(clase != ClasePista.DIVISIBLE){
divisible = 0;
}
else{
bool found = false;
for(int i = 0; (i<primos.length) && (primos[i]<=numero/2); i++){
if(numero % primos[i] == 0){
divisible = primos[i];
found = true;
break;
}
}
if(!found){
if(numero < 100){
divisible = 1;
}
else{
divisible = -1;
}
}
}
mensaje = _generaPista(numero);
}
String _generaPista(int numero){
switch(this.clase){
case ClasePista.PAR_IMPAR:
if(numero%2 == 0){
return 'El número es par';
}
return 'El número es impar';
case ClasePista.DIVISIBLE:
if(divisible == 1){
return 'El número es primo inferior a 100';
}
if(divisible == -1){
return 'El número NO es divisible entre primos inferiores a 100';
}
return 'El número es divisible entre ${divisible}';
case ClasePista.SUMA_CIFRAS:
int pseudo = numero;
int suma = 0;
while(pseudo > 0){
suma += pseudo%10;
pseudo = (pseudo/10).floor();
}
return 'La suma de las cifras del número da ${suma}';
case ClasePista.NUMERO_CIFRAS:
int div = 10;
int num = 1;
while(numero/div > 1){
num++;
div *= 10;
}
if(num == 1){
return 'El número tiene 1 cifra';
}
return 'El número tiene ${num} cifras';
}
}
bool validarNumero(int adivinar, int numero){
switch(this.clase){
case ClasePista.PAR_IMPAR:
return adivinar%2 == numero%2;
case ClasePista.DIVISIBLE:
if(divisible == 1){
return primos.contains(numero);
}
if(divisible == -1){
for(int i = 0; (i<primos.length) && (primos[i]<=numero/2); i++){
if(numero % primos[i] == 0){
divisible = primos[i];
return false;
}
}
return true;
}
return adivinar%divisible == numero%divisible;
case ClasePista.SUMA_CIFRAS:
int a = adivinar, b = numero;
int sumaA = 0, sumaB = 0;
while(a > 0 && b > 0){
sumaA += a%10;
sumaB += b%10;
a = (a/10).floor();
b = (b/10).floor();
}
return sumaA == sumaB;
case ClasePista.NUMERO_CIFRAS:
int div = 10;
while(adivinar/div > 10){
div *= 10;
}
return (numero/div < 10 && numero/div >= 1);
}
}
Widget createWidget(){
return PistaWidget(
key: Key(this.clase.name),
titulo: this.clase.getTitle(),
mensaje: (estado.desbloqueada())? mensaje : estado.getMensaje(),
fondo: estado.getColorFondo(),
texto: estado.getColorTexto(),
onPressed: (estado == EstadoPista.DISPONIBLE || estado == EstadoPista.CONFIRMACION)? onPressed : null
);
}
}
enum ClasePista {
PAR_IMPAR,
DIVISIBLE,
SUMA_CIFRAS,
NUMERO_CIFRAS;
String getTitle(){
switch(this){
case PAR_IMPAR:
return '¿Es par o impar?';
case DIVISIBLE:
return '¿Entre qué número es divisible?';
case SUMA_CIFRAS:
return '¿Cuál es la suma de sus cifras?';
case NUMERO_CIFRAS:
return '¿Cuántas cifras tiene?';
}
}
}
enum EstadoPista {
BLOQUEADO,
DISPONIBLE,
CONFIRMACION,
DESBLOQUEADO,
CORRECTO,
INCORRECTO;
Color getColorFondo() {
switch(this){
case BLOQUEADO:
return Colors.black87;
case DISPONIBLE:
return Colors.orange;
case CONFIRMACION:
return Colors.yellow;
case DESBLOQUEADO:
return Colors.white;
case CORRECTO:
return Colors.green.shade400;
case INCORRECTO:
return Colors.red;
}
}
Color getColorTexto() {
if(this.index < 1 || this == EstadoPista.INCORRECTO){
return Colors.white;
}
return Colors.black;
}
String getMensaje(){
switch(this){
case BLOQUEADO:
return "PISTA BLOQUEADA";
case DISPONIBLE:
return "PULSA PARA DESBLOQUEAR";
case CONFIRMACION:
return "¿Seguro? Pulsa de nuevo para confirmar";
default:
return "";
}
}
bool desbloqueada(){
return this.index >= EstadoPista.DESBLOQUEADO.index;
}
}
......@@ -14,7 +14,7 @@ class PeponatorApp extends StatelessWidget {
),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.system,
home: PantallaJuego(),
home: PantallaJuego(fromHome: true,),
);
}
}
import 'package:flutter/material.dart';
class PantallaPausa extends StatelessWidget {
const PantallaPausa({Key? key}): super(key: key);
@override
Widget build(BuildContext context) {
return SimpleDialog(
title: Center(child: Text(
'Pausa',
style: Theme.of(context).textTheme.headlineLarge,
)),
children: OpcionPausa.values.map((element) {
return SimpleDialogOption(
child: TextButton.icon(
onPressed: () {
Navigator.pop(context, element);
},
style: TextButton.styleFrom(
side: BorderSide(
color: Colors.black26,
width: 3.0
)
),
icon: Icon(
element.icon,
color: element.color ?? Theme.of(context).colorScheme.primary,
size: 32.0
),
label: Text(
element.text,
style: Theme.of(context).textTheme.titleLarge?.copyWith(
color: element.color ?? Theme.of(context).colorScheme.primary
),
),
),
);
}).toList(),
);
}
}
enum OpcionPausa {
REANUDAR(Icons.play_arrow_outlined, "Reanudar", Color.fromARGB(255, 0, 125, 0)),
CAMBIO_MANO(Icons.front_hand_outlined, "Cambiar de mano"),
NUEVA_PARTIDA(Icons.refresh, "Nueva partida"),
CAMBIAR_DIFICULTAD(Icons.settings_outlined, "Cambiar dificultad"),
SALIR(Icons.logout, "Terminar partida", Color.fromARGB(255, 213, 0, 0));
final IconData icon;
final String text;
final Color? color;
const OpcionPausa(this.icon, this.text, [this.color]);
}
\ No newline at end of file
import 'package:flutter/material.dart';
\ No newline at end of file
import 'package:flutter/material.dart';
class PistaWidget extends StatelessWidget {
final String titulo;
final String mensaje;
final Color fondo;
final Color texto;
final VoidCallback? onPressed;
PistaWidget({Key? key, required this.titulo, required this.mensaje, required this.fondo, required this.texto, this.onPressed}):
super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: 12.0),
child: Column(
children: [
Text(
titulo,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold
),
),
SizedBox(
width: double.infinity,
child: TextButton(
onPressed: onPressed,
style: TextButton.styleFrom(
backgroundColor: fondo,
),
child: Text(
mensaje,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: texto
),
),
),
),
],
),
);
}
}
......@@ -15,6 +15,9 @@ class TecladoNumerico extends StatelessWidget {
final Color backspaceColor;
final Color enterColor;
final bool invertBackspaceEnter;
final bool disableEnter;
const TecladoNumerico({
Key? key,
this.onNumberPress,
......@@ -26,6 +29,8 @@ class TecladoNumerico extends StatelessWidget {
this.numberColor = const Color.fromARGB(255, 255, 255, 255),
this.backspaceColor = const Color.fromARGB(255, 255, 255, 255),
this.enterColor = const Color.fromARGB(255, 255, 255, 255),
this.invertBackspaceEnter = false,
this.disableEnter = false
}): super(key: key);
@override
......@@ -62,9 +67,9 @@ class TecladoNumerico extends StatelessWidget {
Expanded(
child: Row(
children: [
_buildBackspaceButton(context),
invertBackspaceEnter? _buildEnterButton(context) : _buildBackspaceButton(context),
_buildNumberButton(context, 0),
_buildEnterButton(context)
invertBackspaceEnter? _buildBackspaceButton(context) : _buildEnterButton(context),
],
),
),
......@@ -129,12 +134,12 @@ class TecladoNumerico extends StatelessWidget {
padding: const EdgeInsets.all(8.0),
child: TextButton(
style: TextButton.styleFrom(
backgroundColor: enterBackgroundColor,
backgroundColor: disableEnter? Theme.of(context).disabledColor : enterBackgroundColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(16.0))
)
),
onPressed: onEnterPress,
onPressed: disableEnter? null : onEnterPress,
child: Icon(
Icons.keyboard_return,
color: enterColor,
......
export 'tu_mensaje.dart';
export 'peponator_mensaje.dart';
export 'pista_widget.dart';
export 'teclado_numerico.dart';
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment