Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.67 KB | None | 0 0
  1. public static void main(String[] args) {
  2.        
  3.         //Creación y llenado de la matríz con un valor "x" por default
  4.         String[][] m = new String[9][9];        
  5.         for (int i = 0; i < m.length; i++) {
  6.             for (int j = 0; j < m[0].length; j++) {
  7.                 m[i][j]="x";
  8.             }
  9.         }
  10.        
  11.         //Cantidad de letras que cambian (Pro, medio, facil)
  12.         int valor=3;
  13.        
  14.         //Contador para el control de letras insertadas
  15.         int cont=0;
  16.         //Mientras no se hayan puesto la cantidad escogida de letras(valor) no puede seguir
  17.         while (cont!=valor) {  
  18.         for (int i = 0; i < 3; i++) {              
  19.                 for (int j = 0; j < 3; j++) {
  20.                     //Valor aleatorio de 0 a 4
  21.                     int aux=(int) (Math.random()*4);
  22.                     //Coinsidencia aleatoria
  23.                     if(aux==0){
  24.                         //Se revisa que ya no haya sido modificado y que la cantidad de cambios no
  25.                         //sobrepasen la variable valor
  26.                         if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
  27.                             //Agrega el cambio a la matriz 9x9
  28.                             m[i][j]="o";
  29.                             //Suma el cambio
  30.                             cont++;
  31.                         }
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.        
  37.         //Se reinicia el contador anterior
  38.         cont=0;
  39.         while (cont!=valor) {
  40.         //El valor de "i" y "j" es a conveniencia y así sucesivamente para cada cuadricula 3x3
  41.         for (int i = 0; i < 3; i++) {              
  42.                 for (int j = 3; j < 6; j++) {
  43.                     int aux=(int) (Math.random()*4);
  44.                     if(aux==0){
  45.                         if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
  46.                             m[i][j]="o";
  47.                             cont++;
  48.                         }
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.        
  54.         cont=0;
  55.         while (cont!=valor) {  
  56.         for (int i = 0; i < 3; i++) {              
  57.                 for (int j = 6; j < 9; j++) {
  58.                     int aux=(int) (Math.random()*4);
  59.                     if(aux==0){
  60.                         if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
  61.                             m[i][j]="o";
  62.                             cont++;
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.        
  69.         cont=0;
  70.         while (cont!=valor) {  
  71.         for (int i = 3; i < 6; i++) {              
  72.                 for (int j = 0; j < 3; j++) {
  73.                     int aux=(int) (Math.random()*4);
  74.                     if(aux==0){
  75.                         if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
  76.                             m[i][j]="o";
  77.                             cont++;
  78.                         }
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.        
  84.         cont=0;
  85.         while (cont!=valor) {  
  86.         for (int i = 3; i < 6; i++) {              
  87.                 for (int j = 3; j < 6; j++) {
  88.                     int aux=(int) (Math.random()*4);
  89.                     if(aux==0){
  90.                         if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
  91.                             m[i][j]="o";
  92.                             cont++;
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.        
  99.         cont=0;
  100.         while (cont!=valor) {  
  101.         for (int i = 3; i < 6; i++) {              
  102.                 for (int j = 6; j < 9; j++) {
  103.                     int aux=(int) (Math.random()*4);
  104.                     if(aux==0){
  105.                         if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
  106.                             m[i][j]="o";
  107.                             cont++;
  108.                         }
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.        
  114.         cont=0;
  115.         while (cont!=valor) {  
  116.         for (int i = 6; i < 9; i++) {              
  117.                 for (int j = 0; j < 3; j++) {
  118.                     int aux=(int) (Math.random()*4);
  119.                     if(aux==0){
  120.                         if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
  121.                             m[i][j]="o";
  122.                             cont++;
  123.                         }
  124.                     }
  125.                 }
  126.             }
  127.         }
  128.        
  129.         cont=0;
  130.         while (cont!=valor) {  
  131.         for (int i = 6; i < 9; i++) {              
  132.                 for (int j = 3; j < 6; j++) {
  133.                     int aux=(int) (Math.random()*4);
  134.                     if(aux==0){
  135.                         if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
  136.                             m[i][j]="o";
  137.                             cont++;
  138.                         }
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.        
  144.         cont=0;
  145.         while (cont!=valor) {  
  146.         for (int i = 6; i < 9; i++) {              
  147.                 for (int j = 6; j < 9; j++) {
  148.                     int aux=(int) (Math.random()*4);
  149.                     if(aux==0){
  150.                         if(m[i][j].equalsIgnoreCase("x")&&cont<valor){
  151.                             m[i][j]="o";
  152.                             cont++;
  153.                         }
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.         //Imprime la matriz 9x9
  159.         for (int i = 0; i < m.length; i++) {
  160.             for (int j = 0; j < m[0].length; j++) {
  161.                 //m[i][j]="x";
  162.                 System.out.print(m[i][j]);
  163.             }
  164.             System.out.println();
  165.         }
  166.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement