Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- //Creación y llenado de la matríz con un valor "x" por default
- String[][] m = new String[9][9];
- for (int i = 0; i < m.length; i++) {
- for (int j = 0; j < m[0].length; j++) {
- m[i][j]="x";
- }
- }
- //Cantidad de letras que cambian (Pro, medio, facil)
- int valor=3;
- //Contador para el control de letras insertadas
- int cont=0;
- //Mientras no se hayan puesto la cantidad escogida de letras(valor) no puede seguir
- while (cont!=valor) {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- //Valor aleatorio de 0 a 4
- int aux=(int) (Math.random()*4);
- //Coinsidencia aleatoria
- if(aux==0){
- //Se revisa que ya no haya sido modificado y que la cantidad de cambios no
- //sobrepasen la variable valor
- if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
- //Agrega el cambio a la matriz 9x9
- m[i][j]="o";
- //Suma el cambio
- cont++;
- }
- }
- }
- }
- }
- //Se reinicia el contador anterior
- cont=0;
- while (cont!=valor) {
- //El valor de "i" y "j" es a conveniencia y así sucesivamente para cada cuadricula 3x3
- for (int i = 0; i < 3; i++) {
- for (int j = 3; j < 6; j++) {
- int aux=(int) (Math.random()*4);
- if(aux==0){
- if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
- m[i][j]="o";
- cont++;
- }
- }
- }
- }
- }
- cont=0;
- while (cont!=valor) {
- for (int i = 0; i < 3; i++) {
- for (int j = 6; j < 9; j++) {
- int aux=(int) (Math.random()*4);
- if(aux==0){
- if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
- m[i][j]="o";
- cont++;
- }
- }
- }
- }
- }
- cont=0;
- while (cont!=valor) {
- for (int i = 3; i < 6; i++) {
- for (int j = 0; j < 3; j++) {
- int aux=(int) (Math.random()*4);
- if(aux==0){
- if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
- m[i][j]="o";
- cont++;
- }
- }
- }
- }
- }
- cont=0;
- while (cont!=valor) {
- for (int i = 3; i < 6; i++) {
- for (int j = 3; j < 6; j++) {
- int aux=(int) (Math.random()*4);
- if(aux==0){
- if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
- m[i][j]="o";
- cont++;
- }
- }
- }
- }
- }
- cont=0;
- while (cont!=valor) {
- for (int i = 3; i < 6; i++) {
- for (int j = 6; j < 9; j++) {
- int aux=(int) (Math.random()*4);
- if(aux==0){
- if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
- m[i][j]="o";
- cont++;
- }
- }
- }
- }
- }
- cont=0;
- while (cont!=valor) {
- for (int i = 6; i < 9; i++) {
- for (int j = 0; j < 3; j++) {
- int aux=(int) (Math.random()*4);
- if(aux==0){
- if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
- m[i][j]="o";
- cont++;
- }
- }
- }
- }
- }
- cont=0;
- while (cont!=valor) {
- for (int i = 6; i < 9; i++) {
- for (int j = 3; j < 6; j++) {
- int aux=(int) (Math.random()*4);
- if(aux==0){
- if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
- m[i][j]="o";
- cont++;
- }
- }
- }
- }
- }
- cont=0;
- while (cont!=valor) {
- for (int i = 6; i < 9; i++) {
- for (int j = 6; j < 9; j++) {
- int aux=(int) (Math.random()*4);
- if(aux==0){
- if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
- m[i][j]="o";
- cont++;
- }
- }
- }
- }
- }
- //Imprime la matriz 9x9
- for (int i = 0; i < m.length; i++) {
- for (int j = 0; j < m[0].length; j++) {
- //m[i][j]="x";
- System.out.print(m[i][j]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement