AmidamaruZXC

Untitled

May 19th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 KB | None | 0 0
  1. private void getWordString() {
  2.         int row1 = 0;
  3.         int col1 = 0;
  4.         int length1 = 0, length2 = 0;
  5.         String word_last = "";
  6.         boolean isCanceled = false;
  7.  
  8.         ArrayList<String> newWords = new ArrayList<>();
  9.  
  10.         for (GridLetter gridLetter : gridLettersTemp) {
  11.  
  12.             int row = gridLetter.getRow();
  13.             StringBuilder word;
  14.             for (int a = 0; a < gridLetter.getRow(); a++) {
  15.                 int position = row * 15 + gridLetter.getCol() - a;
  16.                 TextView txtLetter = scrabble.getChildAt(position)
  17.                         .findViewById(R.id.gridLetter);
  18.                 if (txtLetter.getText().toString().equals("")) {
  19.                     col1 = (int) scrabble.getChildAt(position)
  20.                             .getTag(R.string.col) + 1;
  21.                     break;
  22.                 }
  23.             }
  24.             word = new StringBuilder();
  25.             for (int a = 0; a < 15 - col1; a++) {
  26.                 int position = row * 15 + col1 + a;
  27.                 TextView txtLetter = scrabble.getChildAt(position)
  28.                         .findViewById(R.id.gridLetter);
  29.                 word.append(txtLetter.getText().toString());
  30.                 if (txtLetter.getText().toString().equals("")) {
  31.                     length1 = word.length();
  32.                     if (word.length() > 1 && !word.toString().equals(word_last)) {
  33.                         word_last = word.toString();
  34.                         if (!words.contains(word_last)) {
  35.                             newWords.add(word_last);
  36.                         } else {
  37.                             isCanceled = true;
  38.                             break;
  39.                         }
  40.                     }
  41.                     break;
  42.                 }
  43.             }
  44.  
  45.             word = new StringBuilder();
  46.  
  47.             int col = gridLetter.getCol();
  48.             for (int a = 0; a < gridLetter.getRow(); a++) {
  49.                 int position = row * 15 + col - 15 * a;
  50.                 TextView txtLetter = scrabble.getChildAt(position)
  51.                         .findViewById(R.id.gridLetter);
  52.                 if (txtLetter.getText().toString().equals("")) {
  53.                     row1 = (int) scrabble.getChildAt(position)
  54.                             .getTag(R.string.row) + 1;
  55.                     break;
  56.                 }
  57.             }
  58.             for (int a = 0; a < 15 - row1; a++) {
  59.                 int position = row1 * 15 + col + 15 * a;
  60.                 TextView txtLetter = scrabble.getChildAt(position)
  61.                         .findViewById(R.id.gridLetter);
  62.                 word.append(txtLetter.getText().toString());
  63.                 if (txtLetter.getText().toString().equals("")) {
  64.                     length2 = word.length();
  65.                     if (word.length() > 1 && !word.toString().equals(word_last)) {
  66.                         word_last = word.toString();
  67.                         if (!words.contains(word_last)) {
  68.                             newWords.add(word_last);
  69.                         } else {
  70.                             isCanceled = true;
  71.                             break;
  72.                         }
  73.                     }
  74.                     break;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         if (isCanceled) {
  80.             cancelMove();
  81.         } else {
  82.             if (length1 == 1 && length2 == 1) {
  83.                 cancelMove();
  84.             } else {
  85.                 if (newWords.size() > 0) {
  86.                     boolean canceled = false;
  87.                     Set<String> words_final = new HashSet<>(newWords);
  88.                     for (String word : words_final) {
  89.                         if (!dictionary.contains(word.toLowerCase())) {
  90.                             canceled = true;
  91.                             break;
  92.                         }
  93.                     }
  94.                     if (!canceled) {
  95.                         alertDialog.show();
  96.  
  97.                         boolean cross = false;
  98.  
  99.                         if (!isFirstMove) {
  100.                             for (GridLetter gridLetter : gridLettersTemp) {
  101.                                 for (GridLetter gridLetter1 : gridLetters) {
  102.                                     if ((gridLetter.getRow() - 1 == gridLetter1.getRow()
  103.                                             || gridLetter.getRow() + 1 == gridLetter1.getRow())
  104.                                             && (gridLetter.getCol() - 1 == gridLetter1.getCol()
  105.                                             || gridLetter.getCol() + 1 == gridLetter1.getCol())) {
  106.                                         cross = true;
  107.                                         break;
  108.                                     }
  109.                                 }
  110.                             }
  111.                         } else {
  112.                             cross = true;
  113.                         }
  114.  
  115.                         if (cross) {
  116.                             setLetters();
  117.                             gridLetters.addAll(gridLettersTemp);
  118.  
  119.                             if (isFirstMove) isFirstMove = false;
  120.  
  121.                             int score = 0;
  122.                             for (String word : newWords) {
  123.                                 mDatabase.child("sessions").child(sessionId).child("words").push().setValue(word);
  124.                                 for (int i = 0; i < word.length(); i++) {
  125.                                     score += scores[letters.indexOf(word.charAt(i))];
  126.                                 }
  127.                             }
  128.  
  129.                             for (GridLetter gridLetter : gridLettersTemp) {
  130.                                 mDatabase.child("sessions").child(sessionId).child("letters").push().setValue(gridLetter);
  131.                             }
  132.                             gridLettersTemp.clear();
  133.                             mDatabase.child("sessions").child(sessionId).child("users").child(String.valueOf(userId))
  134.                                     .child("score").setValue(currentUser.getScore() + score);
  135.                             clearPasses();
  136.                         } else {
  137.                             alertDialog.dismiss();
  138.                             cancelMove();
  139.                         }
  140.  
  141.                     } else {
  142.                         alertDialog.dismiss();
  143.                         cancelMove();
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.     }
Add Comment
Please, Sign In to add comment