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