001package ball.game.scrabble; 002/*- 003 * ########################################################################## 004 * Game Applications and Utilities 005 * $Id: Board.html 5431 2020-02-12 19:03:17Z ball $ 006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/hcf-dev/blog/2019-07-08-fivethirtyeight-best-scrabble-string/src/main/resources/javadoc/src-html/ball/game/scrabble/Board.html $ 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.util.CoordinateMap; 024import java.util.ArrayList; 025import java.util.List; 026import java.util.ResourceBundle; 027import java.util.TreeMap; 028 029import static java.util.Collections.copy; 030 031/** 032 * Scrabble {@link Board}. 033 * 034 * {@include Board.properties} 035 * 036 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 037 * @version $Revision: 5431 $ 038 */ 039public class Board extends CoordinateMap<SQ> { 040 private static final long serialVersionUID = -399826193477575690L; 041 042 private static final ResourceBundleMap MAP = 043 new ResourceBundleMap(Board.class); 044 045 /** 046 * Sole public constructor. 047 */ 048 public Board() { this(MAP); } 049 050 private Board(ResourceBundleMap map) { 051 super(map.size(), map.size()); 052 053 copy(asList(), squares(map)); 054 } 055 056 private static List<? extends SQ> squares(ResourceBundleMap map) { 057 ArrayList<SQ> list = null; 058 059 try { 060 int n = map.size(); 061 062 list = new ArrayList<SQ>(n * n); 063 064 String pkg = SQ.class.getPackage().getName(); 065 066 for (String value : map.values()) { 067 String[] names = value.split("[\\p{Space}]+", n); 068 069 for (int i = 0; i < n; i += 1) { 070 String name = pkg + "." + names[i]; 071 072 try { 073 list.add((SQ) Class.forName(name).newInstance()); 074 } catch (Exception exception) { 075 list.add(new SQ()); 076 } 077 } 078 } 079 } catch (Exception exception) { 080 throw new ExceptionInInitializerError(exception); 081 } 082 083 return list; 084 } 085 086 private static class ResourceBundleMap extends TreeMap<String,String> { 087 private static final long serialVersionUID = 5871194943402587641L; 088 089 public ResourceBundleMap(Class<? extends Board> type) { 090 super(); 091 092 ResourceBundle bundle = ResourceBundle.getBundle(type.getName()); 093 094 for (String key : bundle.keySet()) { 095 put(key, bundle.getString(key).trim()); 096 } 097 } 098 } 099}