Skip to content

Commit 90ec759

Browse files
authored
sopaDeLetras
1 parent dfc0a0e commit 90ec759

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

TipsExamenJava/TipsExamenJava.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
2.03 KB
Binary file not shown.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.security.spec.RSAOtherPrimeInfo;
2+
import java.util.Random;
3+
4+
public class SopaDeLetras {
5+
static String palabras[] ={
6+
"helada",
7+
"mesa",
8+
"patata"
9+
};
10+
static char[] letras = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
11+
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
12+
13+
static char sopaLetras[][] = new char[10][10];
14+
static char sopaLetrasCopia[][] = new char[10][10];
15+
static Random ale = new Random();
16+
static void rellenarMatriz(){
17+
for (int i = 0; i < 10; i++) {
18+
for (int j = 0; j < 10; j++) {
19+
int numAle = ale.nextInt(26);
20+
sopaLetras[i][j] = letras[numAle];
21+
}
22+
}
23+
}
24+
static void rellenarMatrizCopia(){
25+
for (int i = 0; i < 10; i++) {
26+
for (int j = 0; j < 10; j++) {
27+
sopaLetrasCopia[i][j] = '0';
28+
}
29+
}
30+
}
31+
public static void imprimirMatriz(){
32+
for (int i = 0; i < 10; i++) {
33+
for (int j = 0; j < 10; j++) {
34+
System.out.print(sopaLetras[i][j] + " ");
35+
}
36+
System.out.println();
37+
}
38+
}
39+
public static void imprimirMatrizCopia(){
40+
// IMPRIMIR MATRIZ COPIA
41+
for (int i = 0; i < 10; i++) {
42+
for (int j = 0; j < 10; j++) {
43+
System.out.print(sopaLetrasCopia[i][j] + " ");
44+
}
45+
System.out.println();
46+
}
47+
}
48+
public static void main(String[] args) {
49+
rellenarMatriz();
50+
rellenarMatrizCopia();
51+
imprimirMatriz();
52+
System.out.println();
53+
imprimirMatrizCopia();
54+
55+
for (int i = 0; i < palabras.length; i++) {
56+
// ¿CUÁNTAS PALABRAS HAY EN EL ARRAY?
57+
// 1º) Buscar posición libre
58+
int filaAl = 0;
59+
int colAl = 0;
60+
// 1ª CASILLA INICIAL LIBRE
61+
do{
62+
filaAl = ale.nextInt(10);
63+
colAl = ale.nextInt(10);
64+
}while(sopaLetrasCopia[filaAl][colAl]!='0');
65+
//
66+
// 2ª) No superar límites
67+
// LÍMITE -> DCHA, columna
68+
if(colAl + (palabras[i].length()-1) > 10){
69+
i--;
70+
}else{
71+
boolean isLibre = true;
72+
int max = colAl + (palabras[i].length()-1); // 5 +(6-1) = 10
73+
for (int z=colAl;z<=max; z++){ //z={5,6,7,8,9,10}
74+
if(sopaLetrasCopia[filaAl][z]!='0'){
75+
isLibre = false;
76+
break;
77+
}
78+
}
79+
if(isLibre){ // PUEDO EMPEZAR A ESCRIBIR!!!,
80+
int contador = 0;
81+
for (int z=colAl;z<=max; z++){ //z={5,6,7,8,9,10}
82+
sopaLetras[filaAl][z] = palabras[i].charAt(contador) ; //"helada"
83+
sopaLetrasCopia[filaAl][z] = palabras[i].charAt(contador);
84+
contador++;
85+
}
86+
}
87+
}
88+
}
89+
imprimirMatriz();
90+
System.out.println();
91+
imprimirMatrizCopia();
92+
}
93+
}

0 commit comments

Comments
 (0)