Refactoring
- Simplified FEN-string generation - Made GameConfigurationDialog a utility class
This commit is contained in:
		| @@ -480,39 +480,18 @@ public class Board implements Cloneable { | ||||
| 		// Piece placement (from white's perspective) | ||||
| 		for (int i = 0; i < 8; i++) { | ||||
| 			int emptyCount = 0; | ||||
| 			for (int j = 0; j < 8; j++) | ||||
| 				if (boardArr[j][i] == null) ++emptyCount; | ||||
| 			for (int j = 0; j < 8; j++) { | ||||
| 				final Piece piece = boardArr[j][i]; | ||||
| 				if (piece == null) ++emptyCount; | ||||
| 				else { | ||||
| 					if (emptyCount != 0) { | ||||
| 						sb.append(emptyCount); | ||||
| 						emptyCount = 0; | ||||
| 					} | ||||
| 					char piece; | ||||
| 					switch (boardArr[j][i].getType()) { | ||||
| 						case KING: | ||||
| 							piece = 'K'; | ||||
| 							break; | ||||
| 						case QUEEN: | ||||
| 							piece = 'Q'; | ||||
| 							break; | ||||
| 						case ROOK: | ||||
| 							piece = 'R'; | ||||
| 							break; | ||||
| 						case KNIGHT: | ||||
| 							piece = 'N'; | ||||
| 							break; | ||||
| 						case BISHOP: | ||||
| 							piece = 'B'; | ||||
| 							break; | ||||
| 						case PAWN: | ||||
| 							piece = 'P'; | ||||
| 							break; | ||||
| 						default: | ||||
| 							piece = '-'; | ||||
| 					} | ||||
| 					if (boardArr[j][i].getColor() == Color.BLACK) piece = Character.toLowerCase(piece); | ||||
| 					sb.append(piece); | ||||
| 					char p = boardArr[j][i].getType().firstChar(); | ||||
| 					sb.append(piece.getColor() == Color.WHITE ? Character.toUpperCase(p) : p); | ||||
| 				} | ||||
| 			} | ||||
| 			if (emptyCount != 0) sb.append(emptyCount); | ||||
| 			if (i < 7) sb.append('/'); | ||||
| 		} | ||||
|   | ||||
| @@ -85,7 +85,14 @@ public abstract class Piece implements Cloneable { | ||||
| 	} | ||||
|  | ||||
| 	public static enum Type { | ||||
| 		KING, QUEEN, ROOK, KNIGHT, BISHOP, PAWN | ||||
| 		KING, QUEEN, ROOK, KNIGHT, BISHOP, PAWN; | ||||
| 		 | ||||
| 		/** | ||||
| 		 * @return The first character of this {@link Type} in algebraic notation and lower case | ||||
| 		 */ | ||||
| 		public char firstChar() { | ||||
| 			return this == KNIGHT ? 'n' : Character.toLowerCase(this.toString().charAt(0)); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	public static enum Color { | ||||
|   | ||||
| @@ -36,7 +36,7 @@ public class FENDropTarget extends DropTargetAdapter { | ||||
| 				try (BufferedReader br = new BufferedReader(new FileReader(file))) { | ||||
| 					final GamePane	gamePane	= mainWindow.addGamePane(); | ||||
| 					final String	fen			= br.readLine(); | ||||
| 					new GameConfigurationDialog((whiteName, blackName) -> { | ||||
| 					GameConfigurationDialog.show((whiteName, blackName) -> { | ||||
| 						final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, fen); | ||||
| 						gamePane.setGame(game); | ||||
| 						game.start(); | ||||
|   | ||||
| @@ -4,6 +4,7 @@ import java.awt.Font; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import java.util.function.BiConsumer; | ||||
|  | ||||
| import javax.swing.DefaultComboBoxModel; | ||||
| import javax.swing.JButton; | ||||
| @@ -17,61 +18,59 @@ import javax.swing.JLabel; | ||||
|  * Created: <strong>24.07.2019</strong><br> | ||||
|  * Author: <strong>Kai S. K. Engelbart</strong> | ||||
|  */ | ||||
| public class GameConfigurationDialog extends JDialog { | ||||
| public class GameConfigurationDialog { | ||||
|  | ||||
| 	private static final long serialVersionUID = 9080577278529876972L; | ||||
| 	private GameConfigurationDialog() {} | ||||
|  | ||||
| 	/** | ||||
| 	 * Create the dialog. | ||||
| 	 */ | ||||
| 	public GameConfigurationDialog(ActionWithConfiguration action) { | ||||
| 		setTitle("Game Configuration"); | ||||
| 		setBounds(100, 100, 281, 142); | ||||
| 		setModal(true); | ||||
| 		setLocationRelativeTo(null); | ||||
| 		getContentPane().setLayout(null); | ||||
| 	public static void show(BiConsumer<String, String> action) { | ||||
| 		new JDialog() { | ||||
|  | ||||
| 		List<String> options = new ArrayList<>(Arrays.asList("Natural Player", "AI Player")); | ||||
| 		EngineUtil.getEngineInfos().forEach(info -> options.add(info.name)); | ||||
| 			private static final long serialVersionUID = -5768339760489440385L; | ||||
|  | ||||
| 		JLabel lblWhite = new JLabel("White:"); | ||||
| 		lblWhite.setFont(new Font("Tahoma", Font.PLAIN, 14)); | ||||
| 		lblWhite.setBounds(10, 11, 49, 14); | ||||
| 		getContentPane().add(lblWhite); | ||||
| 			{ | ||||
| 				setTitle("Game Configuration"); | ||||
| 				setBounds(100, 100, 281, 142); | ||||
| 				setModal(true); | ||||
| 				setLocationRelativeTo(null); | ||||
| 				getContentPane().setLayout(null); | ||||
|  | ||||
| 		JComboBox<Object> cbWhite = new JComboBox<>(); | ||||
| 		cbWhite.setModel(new DefaultComboBoxModel<Object>(options.toArray())); | ||||
| 		cbWhite.setBounds(98, 9, 159, 22); | ||||
| 		getContentPane().add(cbWhite); | ||||
| 				List<String> options = new ArrayList<>(Arrays.asList("Natural Player", "AI Player")); | ||||
| 				EngineUtil.getEngineInfos().forEach(info -> options.add(info.name)); | ||||
|  | ||||
| 		JLabel lblBlack = new JLabel("Black:"); | ||||
| 		lblBlack.setFont(new Font("Tahoma", Font.PLAIN, 14)); | ||||
| 		lblBlack.setBounds(10, 38, 49, 14); | ||||
| 		getContentPane().add(lblBlack); | ||||
| 				JLabel lblWhite = new JLabel("White:"); | ||||
| 				lblWhite.setFont(new Font("Tahoma", Font.PLAIN, 14)); | ||||
| 				lblWhite.setBounds(10, 11, 49, 14); | ||||
| 				getContentPane().add(lblWhite); | ||||
|  | ||||
| 		JComboBox<Object> cbBlack = new JComboBox<>(); | ||||
| 		cbBlack.setModel(new DefaultComboBoxModel<Object>(options.toArray())); | ||||
| 		cbBlack.setBounds(98, 36, 159, 22); | ||||
| 		getContentPane().add(cbBlack); | ||||
| 				JComboBox<Object> cbWhite = new JComboBox<>(); | ||||
| 				cbWhite.setModel(new DefaultComboBoxModel<Object>(options.toArray())); | ||||
| 				cbWhite.setBounds(98, 9, 159, 22); | ||||
| 				getContentPane().add(cbWhite); | ||||
|  | ||||
| 		JButton btnStart = new JButton("Start"); | ||||
| 		btnStart.addActionListener((evt) -> { | ||||
| 			dispose(); | ||||
| 			action.onConfigurationSet(options.get(cbWhite.getSelectedIndex()), options.get(cbBlack.getSelectedIndex())); | ||||
| 		}); | ||||
| 		btnStart.setBounds(20, 73, 89, 23); | ||||
| 		getContentPane().add(btnStart); | ||||
| 				JLabel lblBlack = new JLabel("Black:"); | ||||
| 				lblBlack.setFont(new Font("Tahoma", Font.PLAIN, 14)); | ||||
| 				lblBlack.setBounds(10, 38, 49, 14); | ||||
| 				getContentPane().add(lblBlack); | ||||
|  | ||||
| 		JButton btnCancel = new JButton("Cancel"); | ||||
| 		btnCancel.addActionListener((evt) -> dispose()); | ||||
| 		btnCancel.setBounds(157, 73, 89, 23); | ||||
| 		getContentPane().add(btnCancel); | ||||
| 				JComboBox<Object> cbBlack = new JComboBox<>(); | ||||
| 				cbBlack.setModel(new DefaultComboBoxModel<Object>(options.toArray())); | ||||
| 				cbBlack.setBounds(98, 36, 159, 22); | ||||
| 				getContentPane().add(cbBlack); | ||||
|  | ||||
| 		setVisible(true); | ||||
| 	} | ||||
| 				JButton btnStart = new JButton("Start"); | ||||
| 				btnStart.addActionListener((evt) -> { | ||||
| 					dispose(); | ||||
| 					action.accept(options.get(cbWhite.getSelectedIndex()), options.get(cbBlack.getSelectedIndex())); | ||||
| 				}); | ||||
| 				btnStart.setBounds(20, 73, 89, 23); | ||||
| 				getContentPane().add(btnStart); | ||||
|  | ||||
| 	public static interface ActionWithConfiguration { | ||||
| 				JButton btnCancel = new JButton("Cancel"); | ||||
| 				btnCancel.addActionListener((evt) -> dispose()); | ||||
| 				btnCancel.setBounds(157, 73, 89, 23); | ||||
| 				getContentPane().add(btnCancel); | ||||
|  | ||||
| 		void onConfigurationSet(String whiteName, String blackName); | ||||
| 			} | ||||
| 		}.setVisible(true); | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -34,7 +34,7 @@ public class MenuBar extends JMenuBar { | ||||
| 		JMenu		gameMenu		= new JMenu("Game"); | ||||
| 		JMenuItem	newGameMenuItem	= new JMenuItem("New Game"); | ||||
| 		newGameMenuItem.addActionListener((evt) -> { | ||||
| 			new GameConfigurationDialog((whiteName, blackName) -> { | ||||
| 			GameConfigurationDialog.show((whiteName, blackName) -> { | ||||
| 				GamePane	gamePane	= mainWindow.addGamePane(); | ||||
| 				Game		game		= new Game(gamePane.getBoardPane(), whiteName, blackName); | ||||
| 				gamePane.setGame(game); | ||||
| @@ -80,7 +80,7 @@ public class MenuBar extends JMenuBar { | ||||
| 		loadFromFENMenuItem.addActionListener((evt) -> { | ||||
| 			final GamePane	gamePane	= mainWindow.addGamePane(); | ||||
| 			final String	fen			= JOptionPane.showInputDialog("Enter a FEN string: "); | ||||
| 			new GameConfigurationDialog((whiteName, blackName) -> { | ||||
| 			GameConfigurationDialog.show((whiteName, blackName) -> { | ||||
| 				final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, fen); | ||||
| 				gamePane.setGame(game); | ||||
| 				game.start(); | ||||
|   | ||||
| @@ -19,11 +19,9 @@ import dev.kske.chess.board.Piece; | ||||
|  */ | ||||
| public class TextureUtil { | ||||
|  | ||||
| 	private static Map<String, Image> textures, scaledTextures; | ||||
| 	private static Map<String, Image> textures = new HashMap<>(), scaledTextures = new HashMap<>(); | ||||
|  | ||||
| 	static { | ||||
| 		textures = new HashMap<>(); | ||||
| 		scaledTextures	= new HashMap<>(); | ||||
| 		loadPieceTextures(); | ||||
| 		scaledTextures.putAll(textures); | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user