1

I have an Android crossword game made purely in Java, and i want to add WordSearch game to it. I used RecyclerView with GridLayout Manager and so far i've managed to do this

wordsearch screenshot

the problem is, i can't prevent wild drag. meaning i can drag horizontally, vertically, diagonally and whatever, in a single drag, like this.

wild drag issue screenshot

i have asked chatGPT but his solution didn't work.

this is my code

private void makeRecyclerView() {
    //Toast.makeText(this, data.toString(), Toast.LENGTH_SHORT).show();
    recyclerView = findViewById(R.id.my_recycler_view);
    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    recyclerView.setHasFixedSize(true);

    // use a linear layout manager
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this,10);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setNestedScrollingEnabled(false);

    cells=generateRandomLetters(10);
    fillWithWords();

    // specify an adapter (see also next example)
    adapter = new WordSearchAdapter(cells,this);
    recyclerView.setAdapter(adapter);

    recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent event) {
            Log.d("TouchTest", "Touch captured: " + event.getActionMasked());
            int action = event.getActionMasked();
            int position = getPosUnderTouch(event);

            if (position != RecyclerView.NO_POSITION) {
                int row = position / cells.get(0).size();
                int col = position % cells.get(0).size();
                Cell cell = cells.get(row).get(col);

                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        cell.setSelected(true);
                        currentSelectedCells.put(row + "-" + col, cell);
                        adapter.notifyItemChanged(position);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        isSelecting = true;
                        cell.setSelected(true);
                        currentSelectedCells.put(row + "-" + col, cell);
                        adapter.notifyItemChanged(position);
                        break;
                    case MotionEvent.ACTION_UP:
                        processSelectedCells(currentSelectedCells);
                        currentSelectedCells.clear();
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        currentSelectedCells.clear();
                        // Your logic here, maybe verify the word if selection is done
                        break;
                    default:
                        break;
                }
            }
            return false;  // Let the RecyclerView handle the event as it normally would.
        }


        @Override
        public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent event) {}

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
    });

}

    private void processSelectedCells(HashMap<String, Cell> selectedCells) {
        // 1. Concatenate the letters of all the selected cells
        StringBuilder selectedWordBuilder = new StringBuilder();
        ArrayList<String> keys = new ArrayList<>(selectedCells.keySet());
        Collections.sort(keys); // We might need a custom comparator here if the order doesn't align
        for (String key : keys) {
            selectedWordBuilder.append(selectedCells.get(key).getLetter());
        }
        String selectedWord = selectedWordBuilder.toString();

        txtSelected.setText(selectedWord);

        // 2. Check if this concatenated word exists in the list of words for the puzzle
        for (Word word : wordsOnBoard) {
            if(selectedWord.equals(word.getWordText())) {
                Toast.makeText(getApplicationContext(), "Word found: " + word.getWordText(), Toast.LENGTH_SHORT).show();
                word.setFound(true);
                for (Cell cell : word.getCells()) {
                    cell.setFound(true);
                }

                return; // Exit after finding a match
            }
        }


        // If no word is found, reset the selected cells to unselected state
        for (Cell cell : selectedCells.values()) {
            if(!cell.isFound()) {
                cell.setSelected(false);
            }
        }
        Toast.makeText(this, selectedWord+" dont exist", Toast.LENGTH_SHORT).show();
        adapter.notifyDataSetChanged();
    }

0 Answers0