Advertisement
LucasSousa

Scrambler 3x3 teste1

Nov 10th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. package javaapplication7;
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5.  
  6. class Teste{
  7. //metodo de teste
  8. public static void main(String args []){
  9. try{
  10. //Testa duas letras repetidas
  11. String exp_regular1 = "[A-Z]*([F]{2}|[B]{2}|[U]{2}|[D]{2}|[R]{2}|[L]{2})[A-Z]*";
  12. //Testa sequencia invalida LETRA1|LETRA|LETRA1
  13. String exp_regular2 = "[A-Z]*([F].[F]|[B].[B]|[U].[U]|[D].[D]|[R].[R]|[L].[L])[A-Z]*";
  14.  
  15. //Testa método que retorna array
  16. for(int i = 0; i < 1000;i++){
  17. String[] retorno = new Teste().retornaArray();
  18. String resultadoFinal = "";
  19. for(String elem : retorno){
  20. resultadoFinal += elem;
  21. }
  22. if(Pattern.matches(exp_regular1, resultadoFinal)){
  23. throw new Exception("ERRO RETORNA ARRAY [LETRAS REPETIDAS]= " + resultadoFinal);
  24. }
  25. if(Pattern.matches(exp_regular2, resultadoFinal)){
  26. throw new Exception("ERRO RETORNA ARRAY [LETRA1|LETRA|LETRA1]= " + resultadoFinal);
  27. }
  28. }
  29.  
  30. //Testa método que retorna String
  31. for(int i = 0; i < 1000;i++){
  32. String resultadoFinal = new Teste().retornaString();
  33. if(Pattern.matches(exp_regular1, resultadoFinal)){
  34. throw new Exception("ERRO RETORNA STRING [LETRAS REPETIDAS]= " + resultadoFinal);
  35. }
  36. if(Pattern.matches(exp_regular2, resultadoFinal)){
  37. throw new Exception("ERRO RETORNA STRING [LETRA1|LETRA|LETRA1]= " + resultadoFinal);
  38. }
  39. }
  40. }catch(Exception e){
  41. System.out.println("ERRO!! "+ e.getMessage());
  42. }
  43. }
  44.  
  45.  
  46. public String[] retornaArray(){
  47. String[] valores = {"F", "B", "U", "D", "R", "L"};
  48. String[] resultado = new String[25];
  49. Random r = new Random();
  50. for (int i = 0; i < 25; i++) {
  51. String sorteio = "";
  52. do{
  53. sorteio = valores[r.nextInt(6)];
  54. //verifica se valor é diferente dos dois anteriores (ou do anterior no caso de i == 1)
  55. }while((i == 1 && resultado[0].equals(sorteio)) ||
  56. (i>1 && (resultado[i-1].equals(sorteio) || resultado[i-2].equals(sorteio))));
  57. //ele só chega nesse ponto se a variavel sorteio bate com as especificações
  58. resultado[i] = sorteio;
  59. }
  60. return resultado;
  61. }
  62.  
  63. public String retornaString(){
  64. String[] valores = {"F", "B", "U", "D", "R", "L"};
  65. String resultado = "";
  66. Random r = new Random();
  67. for (int i = 0; i < 25; i++) {
  68. String sorteio = "";
  69. do{
  70. sorteio = valores[r.nextInt(6)];
  71. //verifica se valor é diferente dos dois anteriores (ou do anterior no caso de i == 1)
  72. }while((i == 1 && resultado.startsWith(sorteio)) ||
  73. (i>1 && (String.valueOf(resultado.charAt(i-1)).equals(sorteio) ||
  74. String.valueOf(resultado.charAt(i-2)).equals(sorteio))));
  75. //ele só chega nesse ponto se a variavel sorteio bate com as especificações
  76. resultado += sorteio;
  77. }
  78. return resultado;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement