mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 09:57:40 +01:00
feat
This commit is contained in:
@@ -6,7 +6,7 @@ public class Ejercicio04_EncontrarPalabraMasLarga {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println("Palabra más larga: " +
|
||||
System.out.println("Contar ocurrencias: " +
|
||||
palabraMasLarga("rafa está afónico hoy"));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque06;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Ej01_EjerciciosMatrices {
|
||||
|
||||
@@ -19,20 +21,8 @@ public class Ej01_EjerciciosMatrices {
|
||||
imprimeMatriz(m);
|
||||
System.out.println("Es positiva: " + esMatrizPositiva(m));
|
||||
System.out.println("Es diagonal: " + esMatrizDiagonal(m));
|
||||
System.out.println("Es triangular superior: " + esMatrizTriangularSuperior(m));
|
||||
System.out.println("Es dispersa: " + esMatrizDispersa(m));
|
||||
|
||||
int array[] = arrayFromMatriz(m);
|
||||
UtilsArrays.imprimeArray(array);
|
||||
|
||||
System.out.println("Es simétrica: " + esMatrizSimetrica(m));
|
||||
|
||||
int traspuesta[][] = dameMatrizTraspuesta(m);
|
||||
imprimeMatriz(traspuesta);
|
||||
|
||||
matrizOpuesta(m);
|
||||
imprimeMatriz(m);
|
||||
|
||||
int t[][] = eliminaFila(m, 2);
|
||||
imprimeMatriz(t);
|
||||
}
|
||||
@@ -218,7 +208,7 @@ public class Ej01_EjerciciosMatrices {
|
||||
* @param m
|
||||
* @return
|
||||
*/
|
||||
public static void matrizOpuesta (int m[][]) {
|
||||
public static void dameMatrizOpuesta (int m[][]) {
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
m[i][j] = -m[i][j];
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque07_tresEnRaya;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class TresEnRaya {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// int t[][] = new int[3][3];
|
||||
int t[][] = new int[][] {{2, 1, 1},
|
||||
{1, 2, 2},
|
||||
{2, 2, 0}};
|
||||
int turno = 1;
|
||||
|
||||
// inicializaTablero(t);
|
||||
|
||||
do {
|
||||
// Muestro tablero
|
||||
imprimieTablero(t);
|
||||
|
||||
// Pido jugada a jugador (turno)
|
||||
pidoJugadaAJugador(t, turno);
|
||||
|
||||
// Cambiar turno
|
||||
turno = cambiaTurno(turno);
|
||||
|
||||
} while (ganadorDelJuego(t) == 0 && !hayEmpate(t));
|
||||
|
||||
// Imprimo ganador o empate
|
||||
int ganador = ganadorDelJuego(t);
|
||||
if (ganador != 0) {
|
||||
System.out.println("Ha ganado el jugador " + ganador);
|
||||
}
|
||||
else {
|
||||
System.out.println("Hay un empate");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param t
|
||||
*/
|
||||
public static void inicializaTablero(int t[][]) {
|
||||
// Ponemos todos los valores a "0"
|
||||
for (int i = 0; i < t.length; i++) {
|
||||
for (int j = 0; j < t[i].length; j++) {
|
||||
t[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param t
|
||||
*/
|
||||
public static void imprimieTablero(int t[][]) {
|
||||
System.out.println("\nTablero actual: ");
|
||||
UtilsArrays.imprimeMatriz(t);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param t
|
||||
* @return
|
||||
*/
|
||||
public static int ganadorDelJuego(int t[][]) {
|
||||
// Compruebo cada fila
|
||||
for (int i = 0; i < t.length; i++) {
|
||||
if (t[i][0] == t[i][1] && t[i][1] == t[i][2]) {
|
||||
return t[i][0];
|
||||
}
|
||||
}
|
||||
|
||||
// Compruebo cada columna
|
||||
for (int i = 0; i < t[0].length; i++) {
|
||||
if (t[0][i] == t[1][i] && t[1][i] == t[2][i]) {
|
||||
return t[0][i];
|
||||
}
|
||||
}
|
||||
|
||||
// Compruebo las diagonales
|
||||
if (t[0][0] == t[1][1] && t[1][1] == t[2][2]) {
|
||||
return t[0][0];
|
||||
}
|
||||
|
||||
if (t[0][2] == t[1][1] && t[1][1] == t[2][0]) {
|
||||
return t[0][2];
|
||||
}
|
||||
|
||||
// si llego hasta aquí, sé que el juego no se ha acabado.
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static boolean hayEmpate (int t[][]) {
|
||||
for (int i = 0; i < t.length; i++) {
|
||||
for (int j = 0; j < t[i].length; j++) {
|
||||
if (t[i][j] == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param t
|
||||
* @param jugador
|
||||
*/
|
||||
public static void pidoJugadaAJugador (int t[][], int jugador) {
|
||||
int filaJugada = 0, columnaJugada = 0;
|
||||
boolean casillaOcupada = false;
|
||||
|
||||
System.out.println("Turno del jugador: " + jugador);
|
||||
|
||||
do {
|
||||
casillaOcupada = false;
|
||||
filaJugada = Utils.obtenerEnteroEntreLimites("Introduzca la fila de su jugada: ", 0, 2);
|
||||
columnaJugada = Utils.obtenerEnteroEntreLimites("Introduzca la columna de su jugada: ", 0, 2);
|
||||
|
||||
if (t[filaJugada][columnaJugada] != 0) {
|
||||
System.out.println("La posición que ha pedido está ocupada, vuelva a intentarlo.");
|
||||
casillaOcupada = true;
|
||||
}
|
||||
} while (casillaOcupada);
|
||||
|
||||
// Establezco la jugada del usuario
|
||||
t[filaJugada][columnaJugada] = jugador;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param turno
|
||||
* @return
|
||||
*/
|
||||
public static int cambiaTurno(int turno) {
|
||||
return (turno % 2) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque08;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class Ej02_CiclicoEnArrayRespetandoIntervaloInterior {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int posIni, posFin;
|
||||
int a[] = new int[10];
|
||||
UtilsArrays.inicializaArray(a, 0, 9);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
do {
|
||||
posIni = Utils.obtenerEnteroEntreLimites("Introduzca posición Inicial: ", 0, 9);
|
||||
posFin = Utils.obtenerEnteroEntreLimites("Introduzca posición Final: ", 0, 9);
|
||||
if (posIni > posFin) {
|
||||
System.out.println("Error. La posición inicial no puede ser mayor que "
|
||||
+ " la posición final");
|
||||
}
|
||||
} while (posIni > posFin);
|
||||
|
||||
int aux = a[a.length - 1];
|
||||
|
||||
for (int i = a.length - 2; i > -1; i--) {
|
||||
if (i < posIni || i > posFin) {
|
||||
if (i == (posIni - 1)){
|
||||
a[i + posFin - posIni + 2] = a[i];
|
||||
}
|
||||
else {
|
||||
a[i + 1] = a[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
a[0] = aux;
|
||||
|
||||
UtilsArrays.imprimeArray(a);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque08;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Ej03_LetraDNI {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String letrasDNI = "TRWAGMYFPDXBNJZSQVHLCKE";
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int valorDni = 0;
|
||||
|
||||
boolean dniIntroducidoValido;
|
||||
do {
|
||||
try {
|
||||
System.out.println("Introduzca su DNI sin letra: ");
|
||||
String dniSinLetra = sc.nextLine();
|
||||
valorDni = Integer.parseInt(dniSinLetra);
|
||||
dniIntroducidoValido = true;
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("DNI introducido no es válido");
|
||||
dniIntroducidoValido = false;
|
||||
}
|
||||
} while (!dniIntroducidoValido);
|
||||
|
||||
System.out.println();
|
||||
|
||||
char letraDni = letrasDNI.charAt(valorDni % 23);
|
||||
System.out.println("Letra DNI: " + letraDni);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque08;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class Ej04_MinYMaxEnMatriz {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int m[][] = UtilsArrays.creaEInicializaMatriz(6, 10, -1);
|
||||
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
// Genero un número al azar que no existe en la matriz
|
||||
int azar;
|
||||
do {
|
||||
azar = Utils.obtenerNumeroAzar(0, 1000);
|
||||
} while (existeValorEnMatriz(m, azar));
|
||||
|
||||
// Aquí sé que el valor de 'azar' no se encuentra repetido
|
||||
m[i][j] = azar;
|
||||
}
|
||||
}
|
||||
|
||||
UtilsArrays.imprimeMatriz(m);
|
||||
|
||||
// Obtengo min y max
|
||||
int min = m[0][0];
|
||||
int max = m[0][0];
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
if (m[i][j] > max) max = m[i][j];
|
||||
if (m[i][j] < min) min = m[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Máximo: " + max + " - Mínimo: " + min);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param m
|
||||
* @param valorBuscado
|
||||
* @return
|
||||
*/
|
||||
public static boolean existeValorEnMatriz (int m[][], int valorBuscado) {
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
if (m[i][j] == valorBuscado) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque08;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class Ej06_PosiblesMovimientosAlfil {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// String cabecerasFilas = "abcdefgh";
|
||||
int xAlfil, yAlfil;
|
||||
int tablero[][] = UtilsArrays.creaEInicializaMatriz(8, 8, 0);
|
||||
|
||||
System.out.println("Tablero del ajedrez");
|
||||
UtilsArrays.imprimeMatriz(tablero);
|
||||
|
||||
// System.out.println("Dame la letra de la fila: ");
|
||||
// Scanner sc = new Scanner(System.in);
|
||||
// char letraFila = sc.nextLine().charAt(0);
|
||||
// yAlfil = cabecerasFilas.indexOf(letraFila);
|
||||
|
||||
yAlfil = Utils.obtenerEnteroEntreLimites("Introduzca la fila del Alfil", 1, 8) - 1;
|
||||
|
||||
xAlfil = Utils.obtenerEnteroEntreLimites("Introduzca la columna del Alfil", 1, 8) - 1;
|
||||
|
||||
// Coloco el alfil en el tablero
|
||||
tablero[yAlfil][xAlfil] = 2;
|
||||
|
||||
// Coloco valores en Diagonal principal sobre el alfil
|
||||
for (int i = yAlfil - 1, j = xAlfil - 1; i > -1 && j > -1; i--, j--) {
|
||||
tablero[i][j] = 1;
|
||||
}
|
||||
|
||||
// Coloco valores en Diagonal principal bajo el alfil
|
||||
for (int i = yAlfil + 1, j = xAlfil + 1; i < tablero.length && j < tablero[i].length ; i++, j++) {
|
||||
tablero[i][j] = 1;
|
||||
}
|
||||
|
||||
// Coloco valores en Diagonal secundaria sobre el alfil
|
||||
for (int i = yAlfil - 1, j = xAlfil + 1; i > -1 && j < tablero[i].length ; i--, j++) {
|
||||
tablero[i][j] = 1;
|
||||
}
|
||||
|
||||
// Coloco valores en Diagonal secundaria bajo el alfil
|
||||
for (int i = yAlfil + 1, j = xAlfil -1; i < tablero.length && j > -1 ; i++, j--) {
|
||||
tablero[i][j] = 1;
|
||||
}
|
||||
|
||||
UtilsArrays.imprimeMatriz(tablero);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package tutorialJava.capitulo4_Arrays.ejercicios.bloque08;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class Ejercicio01_arrayImparesYPares {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int a[] = new int[20];
|
||||
|
||||
UtilsArrays.inicializaArray(a, 0, 100);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
|
||||
ordenaParesEImpares(a);
|
||||
|
||||
UtilsArrays.imprimeArray(a);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param a
|
||||
*/
|
||||
public static void ordenaParesEImpares (int a[]) {
|
||||
|
||||
while ( !(posicionUltimoPar(a) < posicionPrimerImpar(a))) {
|
||||
int primeraPosImpar = posicionPrimerImpar(a);
|
||||
int ultimaPosPar = posicionUltimoPar(a);
|
||||
|
||||
int aux = a[primeraPosImpar];
|
||||
a[primeraPosImpar] = a[ultimaPosPar];
|
||||
a[ultimaPosPar] = aux;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static int posicionUltimoPar (int a[]) {
|
||||
int posicion = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (a[i] % 2 == 0) {
|
||||
posicion = i;
|
||||
}
|
||||
}
|
||||
return posicion;
|
||||
}
|
||||
|
||||
|
||||
public static int posicionPrimerImpar (int a[]) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (a[i] % 2 == 1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user