001package ball.game.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: ScrabbleTask.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-game/trunk/src/main/java/ball/game/ant/taskdefs/ScrabbleTask.java $
007 * %%
008 * Copyright (C) 2010 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.game.scrabble.AI;
024import ball.game.scrabble.Board;
025import ball.game.scrabble.Game;
026import ball.game.scrabble.Player;
027import ball.game.scrabble.Rack;
028import ball.game.scrabble.Tile;
029import ball.util.ant.taskdefs.AnnotatedAntTask;
030import ball.util.ant.taskdefs.AntTask;
031import ball.util.ant.taskdefs.ClasspathDelegateAntTask;
032import ball.util.ant.taskdefs.ConfigurableAntTask;
033import ball.util.ant.taskdefs.NotNull;
034import java.util.Collections;
035import java.util.LinkedHashSet;
036import java.util.List;
037import lombok.Getter;
038import lombok.NoArgsConstructor;
039import lombok.Setter;
040import lombok.ToString;
041import lombok.experimental.Accessors;
042import org.apache.tools.ant.BuildException;
043import org.apache.tools.ant.Task;
044import org.apache.tools.ant.util.ClasspathUtils;
045
046import static java.util.stream.Collectors.toCollection;
047import static lombok.AccessLevel.PROTECTED;
048
049/**
050 * Abstract Scrabble {@link.uri http://ant.apache.org/ Ant} {@link Task}
051 * base class.
052 *
053 * {@ant.task}
054 *
055 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
056 * @version $Revision: 5285 $
057 */
058@NoArgsConstructor(access = PROTECTED)
059public abstract class ScrabbleTask extends Task
060                                   implements AnnotatedAntTask,
061                                              ClasspathDelegateAntTask,
062                                              ConfigurableAntTask {
063    @Getter @Setter @Accessors(chain = true, fluent = true)
064    private ClasspathUtils.Delegate delegate = null;
065
066    @Override
067    public void init() throws BuildException {
068        super.init();
069        ClasspathDelegateAntTask.super.init();
070        ConfigurableAntTask.super.init();
071    }
072
073    @Override
074    public void execute() throws BuildException {
075        super.execute();
076        AnnotatedAntTask.super.execute();
077    }
078
079    /**
080     * {@link.uri http://ant.apache.org/ Ant} {@link Task} to find possible
081     * words for a {@link Rack}.
082     *
083     * {@ant.task}
084     */
085    @AntTask("scrabble-words-for")
086    @NoArgsConstructor @ToString
087    public static class WordsFor extends ScrabbleTask {
088        @NotNull @Getter @Setter
089        private String rack = null;
090
091        @Override
092        public void execute() throws BuildException {
093            super.execute();
094
095            try {
096                Game game = new Game();
097                Player player = new AI();
098
099                for (char letter : getRack().toUpperCase().toCharArray()) {
100                    player.getRack().add(game.getBag().draw(letter));
101                }
102
103                log(String.valueOf(player.getRack()));
104
105                LinkedHashSet<String> set =
106                    player.getRack().combinations()
107                    .map(t -> Tile.toString(t))
108                    .collect(toCollection(LinkedHashSet::new));
109
110                set.retainAll(game.getWordList());
111
112                log(String.valueOf(set));
113            } catch (BuildException exception) {
114                throw exception;
115            } catch (Throwable throwable) {
116                throwable.printStackTrace();
117                throw new BuildException(throwable);
118            }
119        }
120    }
121}