Search in sources :

Example 1 with PlayIdea

use of com.csc480.game.Engine.Model.PlayIdea in project Red-Team by CSC480-18S.

the class WordVerification method TESTgetWordsFromHand.

public ArrayList<PlayIdea> TESTgetWordsFromHand(String hand, char[] constraints, int index, TileData root, boolean horrizontal) {
    // //////////
    String TESTPRINTconstraints = "";
    for (int i = 0; i < constraints.length; i++) {
        if (constraints[i] == 0)
            TESTPRINTconstraints += '-';
        else
            TESTPRINTconstraints += constraints[i];
    }
    // System.out.println("TESTgetWordsFromHand called with: tiles ="+hand+", constraints="+TESTPRINTconstraints+", index="+index+"isHor="+horrizontal);
    // //////////
    String handAndReleventBoardTiles = hand;
    // generate the regex template for the current tile.
    String regex = genRegex(constraints, index);
    for (int i = 0; i < constraints.length; i++) if (constraints[i] != 0)
        handAndReleventBoardTiles += constraints[i];
    ArrayList<PlayIdea> possiblePlays = new ArrayList<PlayIdea>();
    if (root.letter == 0) {
        // System.out.println("First play, so we can skip a bunch");
        for (String e : validWords) {
            String temp = handAndReleventBoardTiles;
            boolean isGoodFlag = true;
            if (e.length() <= constraints.length) {
                for (int i = 0; i < e.length(); i++) {
                    if (temp.contains(e.charAt(i) + "")) {
                        temp = temp.replaceFirst(e.charAt(i) + "", '_' + "");
                    } else {
                        // System.out.println("letter: "+ e.charAt(i) +" of "+e + " doesnt fit temp: "+temp+" of handntiles: "+handAndReleventBoardTiles);
                        isGoodFlag = false;
                        break;
                    }
                }
                if (isGoodFlag) {
                    // System.out.println(e+" was good");
                    ArrayList<Placement> play = new ArrayList<Placement>();
                    int playRoot = e.length() / 2;
                    for (int i = 0; i < e.length(); i++) {
                        play.add(new Placement(e.charAt(i), ((int) root.my_position.x - playRoot + i), (int) root.my_position.y));
                    }
                    PlayIdea p = new PlayIdea(e, play, (byte) play.size());
                    possiblePlays.add(p);
                }
            }
        }
        return possiblePlays;
    }
    for (String e : validWords) {
        String temp = handAndReleventBoardTiles;
        boolean isGoodFlag = true;
        if (e.length() <= constraints.length) {
            for (int i = 0; i < e.length(); i++) {
                if (temp.contains(e.charAt(i) + "")) {
                    temp = temp.replaceFirst(e.charAt(i) + "", '_' + "");
                } else {
                    // System.out.println("letter: "+ e.charAt(i) +" of "+e + " doesnt fit temp: "+temp+" of handntiles: "+handAndReleventBoardTiles);
                    isGoodFlag = false;
                    break;
                }
            }
            if (isGoodFlag) {
                try {
                    // System.out.println("e: "+e+" matched? regex "+regex+"="+e.matches(regex));
                    if (e.matches(regex)) {
                        // System.out.println(e + " matched regex: "+regex);
                        // Transform result to placement
                        ArrayList<Placement> play = new ArrayList<Placement>();
                        if (root.letter != 0) {
                            // System.out.println("not first play case");
                            // THERE IS A MASSIVE BUG HERE WHERE IF THE WORD HAS 2+ letters the same!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                            int playRoot = e.indexOf(constraints[index]);
                            // parse left
                            for (int i = playRoot; i > 0; i--) {
                                // create a new placement @
                                if (index - i >= constraints.length)
                                    break;
                                // System.out.println("constraint@" + (index+i-playRoot-1) + " = 0:" + (constraints[index+i-playRoot-1] == 0));
                                if (constraints[index + i - playRoot - 1] == 0) {
                                    if (horrizontal) {
                                        // System.out.println("adding a placement of "+e.charAt(i-1)+"("+(i-1)+") @ ("+(index+i-playRoot-1)+","+(int) root.my_position.y+") cause its blank here");
                                        play.add(new Placement(e.charAt(i - 1), index + i - playRoot - 1, (int) root.my_position.y));
                                    } else {
                                        // System.out.println("adding a placement of "+e.charAt(i-1)+"("+(i-1)+") @ ("+((int) root.my_position.x)+","+(index+i-playRoot-1)+") cause its blank here");
                                        play.add(new Placement(e.charAt(i - 1), (int) root.my_position.x, 10 - (index + i - playRoot - 1)));
                                    }
                                }
                            }
                            // parse right
                            for (int i = playRoot + 1; i < e.length(); i++) {
                                // create a new placement @
                                if (index + i - playRoot >= constraints.length) {
                                    // System.out.println("breaking greater");
                                    break;
                                }
                                if (index + i - playRoot < 0) {
                                    // System.out.println("breaking lower");
                                    break;
                                }
                                // System.out.println("constraint@" + (index+i-playRoot) + " = 0:" + (constraints[index + i - playRoot] == 0));
                                if (constraints[index + i - playRoot] == 0) {
                                    if (horrizontal) {
                                        // System.out.println("adding a placement of "+e.charAt(i)+"("+(i)+") @ ("+(index + i - playRoot)+","+(int) root.my_position.y+") cause its blank here");
                                        play.add(new Placement(e.charAt(i), index + i - playRoot, (int) root.my_position.y));
                                    } else {
                                        // System.out.println("adding a placement of "+e.charAt(i)+"("+(i)+") @ ("+((int) root.my_position.x)+","+(index+i-playRoot)+") cause its blank here");
                                        play.add(new Placement(e.charAt(i), (int) root.my_position.x, 10 - (index + i - playRoot)));
                                    }
                                }
                            }
                            // System.out.println("Adding to possible plays from Word Verification:" + PrintPlay(play));
                            PlayIdea p = new PlayIdea(e, play, (byte) play.size());
                            possiblePlays.add(p);
                        } else {
                            // if (root.letter == 0)
                            if (horrizontal) {
                                for (int w = 0; w < e.length(); w++) {
                                    play.add(new Placement(e.charAt(w), (index - e.length() / 2) + w, (int) root.my_position.y));
                                }
                            } else {
                                for (int w = 0; w < e.length(); w++) {
                                    play.add(new Placement(e.charAt(w), (int) root.my_position.y, 10 - ((index - e.length() / 2) + w)));
                                }
                            }
                            // play.add(new Placement(e.charAt(playRoot),(int)root.my_position.y,(int)root.my_position.y));
                            // System.out.println("Adding to possible plays from Word Verification:" + PrintPlay(play));
                            PlayIdea p = new PlayIdea(e, play, (byte) play.size());
                            possiblePlays.add(p);
                        }
                    }
                } catch (PatternSyntaxException exp) {
                    // System.err.println(exp.getMessage());
                    exp.printStackTrace();
                }
            }
        } else {
        // System.out.println("word cant be longer than constraints");
        }
    }
    // System.out.println("Found "+possiblePlays.size()+" possible words.");
    return possiblePlays;
}
Also used : Placement(com.csc480.game.Engine.Model.Placement) ArrayList(java.util.ArrayList) PlayIdea(com.csc480.game.Engine.Model.PlayIdea) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with PlayIdea

use of com.csc480.game.Engine.Model.PlayIdea in project Red-Team by CSC480-18S.

the class TestingInputProcessor method keyTyped.

/**
 * Sets the current character, clears, and submits tiles
 * @param character
 * @return
 */
@Override
public boolean keyTyped(char character) {
    if (character == '=') {
        System.out.println("clearing");
        GameManager.getInstance().placementsUnderConsideration.clear();
    } else if (character == '1') {
        System.out.println("logging");
        GameManager.getInstance().LogEvent("event" + c++);
    } else if (character == '2') {
        // System.out.println("logging");
        GameManager.getInstance().PrintBoardState();
    } else if (character == '3') {
        // System.out.println("logging");
        GameManager.getInstance().updatePlayers(GameManager.getInstance().theAIs);
    } else if (character == '\'') {
        System.out.println("entering");
        Long startTime = System.nanoTime();
        boolean isValid = GameManager.getInstance().theBoard.verifyWordPlacement(GameManager.getInstance().placementsUnderConsideration);
        System.out.println("verification took nanos: " + (System.nanoTime() - startTime));
        if (isValid)
            GameManager.getInstance().theBoard.addWord(GameManager.getInstance().placementsUnderConsideration);
        GameManager.getInstance().placementsUnderConsideration.clear();
    } else if (character == '\\') {
        System.out.println("Finding all words for queue");
        Long startTime = System.nanoTime();
        String hand = "";
        for (Placement p : GameManager.getInstance().placementsUnderConsideration) {
            hand += p.letter;
        }
        char[] constraints = new char[hand.length()];
        Arrays.fill(constraints, (char) 0);
        ArrayList<String> results = WordVerification.getInstance().getWordsFromHand(hand, constraints, 5);
        System.out.println("finding all possible words took nanos: " + (System.nanoTime() - startTime));
        for (String s : results) System.out.println(s);
    } else if (character == ']') {
        System.out.println("AI TEST YOUR THING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        for (int i = 0; i < testHandQueue.length; i++) {
            if (testHandQueue[i] != 0) {
                System.out.println("adding to ai:" + testHandQueue[i]);
                GameManager.getInstance().theAIs[0].tiles[i] = testHandQueue[i];
            }
        }
        GameManager.getInstance().updatePlayers(GameManager.getInstance().theAIs);
        System.out.println("Finding all AI plays for tiles");
        Long startTime = System.nanoTime();
        GameManager.getInstance().theAIs[0].TESTFindPlays(GameManager.getInstance().theBoard);
        System.out.println("finding all possible AI plays took nanos: " + (System.nanoTime() - startTime));
        PlayIdea bestPlay = GameManager.getInstance().theAIs[0].PlayBestWord();
        while (bestPlay != null && !GameManager.getInstance().theBoard.verifyWordPlacement(bestPlay.placements)) {
            bestPlay = GameManager.getInstance().theAIs[0].PlayBestWord();
            if (bestPlay == null)
                break;
        }
        if (bestPlay != null && bestPlay.myWord != null && GameManager.getInstance().theBoard.verifyWordPlacement(bestPlay.placements)) {
            System.out.println("The AI found made a decent play");
            /*
                for(int i = 0; i < bestPlay.size(); i++){
                    for(int j = 0; j < testHandQueue.length; j++){
                        if(bestPlay.get(i).letter == testHandQueue[j]) {
                            testHandQueue[j] = (char) 0;
                        }
                    }
                }
                for(int i = 0; i < bestPlay.size(); i++){
                    for(int j = 0; j < testingAI.tiles.length; j++){
                        if(bestPlay.get(i).letter == testingAI.tiles[j]) {
                            testingAI.tiles[j] = (char) 0;
                        }
                    }
                }
                */
            for (int i = 0; i < testHandQueue.length; i++) {
            // if(testHandQueue[i] == 0) testHandQueue[i] = GameManager.getInstance().getNewTiles(1).get(0).charValue();
            }
            GameManager.getInstance().theBoard.addWord(bestPlay.placements);
            GameManager.getInstance().placementsUnderConsideration.clear();
        }
    } else {
        System.out.println("changing to " + character);
        System.out.println("Adding to AI tiles " + character);
        lastInput = character;
        testHandQueue[aiHandCount] = character;
        aiHandCount++;
        aiHandCount = (aiHandCount) % 7;
    }
    return true;
}
Also used : Placement(com.csc480.game.Engine.Model.Placement) ArrayList(java.util.ArrayList) PlayIdea(com.csc480.game.Engine.Model.PlayIdea)

Aggregations

Placement (com.csc480.game.Engine.Model.Placement)2 PlayIdea (com.csc480.game.Engine.Model.PlayIdea)2 ArrayList (java.util.ArrayList)2 PatternSyntaxException (java.util.regex.PatternSyntaxException)1