/** Pastes Items from the Clipboard on the Board */ public void paste() { ComponentSelection clipboardContent = (ComponentSelection) tbe.getClipboard().getContents(this); if ((clipboardContent != null) && (clipboardContent.isDataFlavorSupported(ComponentSelection.itemFlavor))) { Object[] tempItems = null; try { tempItems = board.cloneItems(clipboardContent.getTransferData(ComponentSelection.itemFlavor)); } catch (UnsupportedFlavorException e1) { e1.printStackTrace(); } ItemComponent[] items = new ItemComponent[tempItems.length]; for (int i = 0; i < tempItems.length; i++) { items[i] = (ItemComponent) tempItems[i]; } PasteCommand del = new PasteCommand(items); ArrayList<Command> actCommands = new ArrayList<Command>(); actCommands.add(del); tbe.addCommands(actCommands); board.addItem(items); } }
public String castleQueenside(Square candidateSquare) { if (piece == 0) { int row; int column; if (color == 1) { row = 1; column = 1; } else { row = 8; column = 1; } int cell = square.getCellFromCoord(row, column); Square sq = (Square) board.getComponent(cell); Piece pc = (Piece) sq.getComponent(0); int newCell; if (pc.getColor() == 1) { newCell = square.getCellFromCoord(1, 4); } else { newCell = square.getCellFromCoord(8, 4); } Square newSquare = (Square) board.getComponent(newCell); // move rook movePiece(pc, newSquare, sq); return ("0-0-0"); } return ""; }
/** * Sets the current Tool, the right listeners and the Cursor * * @param tool, Tool to set as current * @param button, JButton to set as current */ public void setTool(Tool tool, JButton button) { // IF NO CURSORTOOL if (this.currentTool instanceof CursorTool && !(tool instanceof CursorTool)) { board.removeMouseListener(listeners[0]); // IF CURSORTOOL } else if (tool instanceof CursorTool && !(this.currentTool instanceof CursorTool)) { board.addMouseListener(listeners[0]); } if (tool == null) throw new IllegalArgumentException("Tool must not be null."); if (this.currentTool != tool) { if (this.currentButton != null) { this.currentButton.setEnabled(true); } this.currentButton = button; this.currentTool = tool; } if (tool instanceof CursorTool || tool instanceof ArrowTool || tool instanceof TextBoxTool) { board.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } else { board.setCursor(tool.getItemType().getCursor()); } }
public boolean queensideClear() { boolean[] flags = ChessGameDemo.getFlags(); int row = square.getRow(); int column = square.getColumn(); if ((row == 1) && (flags[0] || flags[4])) { return false; } if ((row == 8) && (flags[2] || flags[5])) { return false; } if (((row == 1) && (column == 5)) || ((row == 8) && (column == 5))) { for (int i = 1; i < 4; i++) { int r = row; int c = column - i; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if ((sq.getComponentCount() == 1) || (r < 1)) { return false; } else if (color == 1) { if ((board.getBlackCheckList()).contains(sq.getPosition())) { return false; } } else { if ((board.getWhiteCheckList()).contains(sq.getPosition())) { return false; } } } return true; } return false; }
private void setListForPawn() { moveList.clear(); if (color == 1) { int row = square.getRow(); int column = square.getColumn(); int cell1 = square.getCellFromCoord(row + 1, column); int cell2 = square.getCellFromCoord(row + 2, column); int cell3 = square.getCellFromCoord(row + 1, column + 1); int cell4 = square.getCellFromCoord(row + 1, column - 1); Square s1 = (Square) board.getComponent(cell1); Square s2 = (Square) board.getComponent(cell2); Square s3 = (Square) board.getComponent(cell3); Square s4 = (Square) board.getComponent(cell4); if (s1.getComponentCount() == 0) { moveList.add(s1.getPosition()); squareList.add(s1); } if (s2.getComponentCount() == 0) { moveList.add(s2.getPosition()); squareList.add(s2); } if ((s3.getComponentCount() == 1) && ((Piece) s3.getComponent(0)).getColor() == 0) { moveList.add(s3.getPosition()); squareList.add(s3); } if ((s4.getComponentCount() == 1) && ((Piece) s4.getComponent(0)).getColor() == 0) { moveList.add(s4.getPosition()); squareList.add(s4); } } if (color == 0) { int row = square.getRow(); int column = square.getColumn(); int cell1 = square.getCellFromCoord(row - 1, column); int cell2 = square.getCellFromCoord(row - 2, column); int cell3 = square.getCellFromCoord(row - 1, column + 1); int cell4 = square.getCellFromCoord(row - 1, column - 1); Square s1 = (Square) board.getComponent(cell1); Square s2 = (Square) board.getComponent(cell2); Square s3 = (Square) board.getComponent(cell3); Square s4 = (Square) board.getComponent(cell4); if (s1.getComponentCount() == 0) { moveList.add(s1.getPosition()); squareList.add(s1); } if (s2.getComponentCount() == 0) { moveList.add(s2.getPosition()); squareList.add(s2); } if ((s3.getComponentCount() == 1) && ((Piece) s3.getComponent(0)).getColor() == 1) { moveList.add(s3.getPosition()); squareList.add(s3); } if ((s4.getComponentCount() == 1) && ((Piece) s4.getComponent(0)).getColor() == 1) { moveList.add(s4.getPosition()); squareList.add(s4); } } }
// ---------makes the animation for the remote player's (computer) bead--------- private void startRemoteMoving(String position) { System.out.println("procesing " + position); if (isWin(position)) { winner = parseWinner(position); if (winner == 1) startImageLoader(WIN_IMAGE_FILE); else { isWinLoseTimer = new Timer(IMAGE_LOADERS_TIMER, this); isWinLoseTimer.setActionCommand("is win_lose"); isWinLoseTimer.setRepeats(false); // occurs only one time isWinLoseTimer.start(); } repaint(); return; } if (isDraw(position)) { winner = 0; startImageLoader(TIE_IMAGE_FILE); repaint(); return; } insertedInPegNum = mapPosToPeg(position); availableBeads[currentBead].setX( PEG_INITIAL_X[insertedInPegNum - 1] - (int) (availableBeads[currentBead].getBounds().getWidth() / 2)); availableBeads[currentBead].setY(PEG_INITIAL_Y[insertedInPegNum - 1]); pegs[insertedInPegNum - 1].addBead(availableBeads[currentBead]); remoteBeadAnimTimer = new Timer(ANIMATION_REFRESH_RATE, this); remoteBeadAnimTimer.setActionCommand("remote bead animator"); // sets the peg remoteBeadAnimTimer.setRepeats(true); remoteBeadAnimTimer.start(); // creates and inserts new bead currentBead++; // System.out.println( "Type of game: " + typeOfGame ); if (typeOfGame.equals("Gertrudis vs Computer")) { if (currentBead % 2 != 0) { availableBeads[currentBead] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'b'); availableBeads[currentBead].setImage(board.getBlackBeadImage()); } else { availableBeads[currentBead] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'w'); availableBeads[currentBead].setImage(board.getWhiteBeadImage()); } } else { availableBeads[currentBead] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'w'); availableBeads[currentBead].setImage(board.getWhiteBeadImage()); } }
/** * Constructor for open a Board * * @param board */ public WorkingView(Board board) { this.sport = board.getSport(); this.board = board; tbe.setSaved(true); int value = board.getPath().lastIndexOf("\\"); tbe.getFrame().setTitle("TBE - Tactic Board Editor - " + board.getPath().substring(value + 1)); createWorkingView(); }
/** Deletes the selected Item of the Board and creates a DeleteCommand */ public void delete() { ItemComponent[] items = board.getSelectedItems(); DeleteCommand del = new DeleteCommand(items); ArrayList<Command> actCommands = new ArrayList<Command>(); actCommands.add(del); tbe.addCommands(actCommands); board.removeItem(items); }
// -----------draws names on screen----------- private void displayNames(Graphics g) { // draw players names drawText(g, "Player 1", TEXT_P1_INITIAL_X, TEXT_P1_INITIAL_Y + 4, Color.green, 22); drawText(g, board.player1(), TEXT_P1_INITIAL_X, TEXT_P1_INITIAL_Y, Color.blue, 22); drawText(g, "Player 2", TEXT_P2_INITIAL_X, TEXT_P2_INITIAL_Y + 4, Color.green, 22); drawText(g, board.player2(), TEXT_P2_INITIAL_X, TEXT_P2_INITIAL_Y, Color.blue, 22); drawText(g, "___________", BEAD_TEXT_INITIAL_X, BEAD_TEXT_INITIAL_Y - 4, Color.BLACK, 17); drawText(g, " Free Bead", BEAD_TEXT_INITIAL_X, BEAD_TEXT_INITIAL_Y, Color.BLACK, 17); }
/** Creates the WorkingView */ private void createWorkingView() { workingViewLabels = getResourceBundle(tbe.getLang()); this.setLayout(new BorderLayout()); this.setBackground(Color.WHITE); Invoker.getInstance().clear(); // Toolbar this.add(toolbar, BorderLayout.NORTH); // Attributebar sideBar = new SideBar(board); this.add(sideBar, BorderLayout.WEST); // gemeinsames Panel für Board und Legend rightPanel.setLayout(new BorderLayout()); rightPanel.add(new JScrollPane(board), BorderLayout.CENTER); class ViewMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { if (e.getButton() == 3 && !(currentTool instanceof CursorTool)) { setTool(cursorTool, cursorButton); } else { Point p = new Point(e.getX(), e.getY()); WorkingView.this.getTool().mouseDown(p.x, p.y, e); } if (currentTool instanceof ArrowTool || currentTool instanceof TextBoxTool) { setTool(cursorTool, cursorButton); } board.requestFocus(); } public void mouseReleased(MouseEvent e) { checkDefaultButtonVisibility(); } } initDefaultTools(); initSportTools(); listeners[0] = board.getMouseListeners()[0]; listeners[1] = new ViewMouseListener(); board.addMouseListener(listeners[1]); // Legend legendBar = new LegendBar(board); rightPanel.add(legendBar, BorderLayout.SOUTH); this.add(rightPanel, BorderLayout.CENTER); this.activatePoints(false); tbe.getMenu().setVisibleToolbar(!this.toolbar.isVisible()); tbe.getMenu().setVisibleLegend(!this.legendBar.isVisible()); tbe.getMenu().setVisibleSidebar(!this.sideBar.isVisible()); }
/** Cuts selected Iems of the Board and put it into the ClipBoard */ public void cut() { ItemComponent[] items = board.getSelectedItems(); CutCommand cut = new CutCommand(items); ArrayList<Command> actCommands = new ArrayList<Command>(); actCommands.add(cut); tbe.addCommands(actCommands); ComponentSelection contents = new ComponentSelection(this.getBoard().cloneItems(items)); tbe.getClipboard().setContents(contents, cut); board.removeItem(items); }
/** * Checks if the Rotate, Add and Remove Buttons should be activated or not. Add and Remove only if * Arrow, Rotate only if Shape */ public void checkDefaultButtonVisibility() { if (board.getSelectionCount() == 1 && board.getSelectionCell() instanceof ArrowItem) { this.activatePoints(true); } else { this.activatePoints(false); } if (board.getSelectionCount() == 1 && board.getSelectionCell() instanceof ShapeItem) { this.activateRotation(true); } else { this.activateRotation(false); } }
// ---------draws winner on screen-------------- private void displayWinner(Graphics g) { if (winner == 1) { // System.out.println( "Printing winner " + 1 ); drawText( g, board.player1() + " wins!", LOSE_WIN_TIE_TEXT_X, LOSE_WIN_TIE_TEXT_Y, Color.red, 38); } else if (winner == 2) { // System.out.println( "Printing winner " + 2 ); drawText( g, board.player2() + " wins!", LOSE_WIN_TIE_TEXT_X, LOSE_WIN_TIE_TEXT_Y, Color.red, 38); } else if (winner == 0) { // System.out.println( "Printing tie" ); drawText(g, "DRAW", LOSE_WIN_TIE_TEXT_X, LOSE_WIN_TIE_TEXT_Y, Color.red, 38); } }
/** Updates the matrix with current board and current pentomino */ public void updateMatrix() { int[][] tmpBoard = new int[gameBoard.getBoard().length][gameBoard.getBoard()[0].length]; // Copy the old board to the tmp for (int i = 0; i < gameBoard.getBoard().length; i++) { System.arraycopy(gameBoard.getBoard()[i], 0, tmpBoard[i], 0, gameBoard.getBoard()[i].length); } // Add the pentomino to the tmpBoard for (Point p : activePentomino.getLocation()) { int newX = (int) (pentominoLocation.getX() + p.getX()); int newY = (int) (pentominoLocation.getY() + p.getY()); if (newY >= 0) tmpBoard[newX][newY] = activePentomino.getID(); } matrix = tmpBoard; }
/** Sets the location for predicted pentomino */ public void predictDrop() { predictedLocation = (Point) pentominoLocation.clone(); while (nextDropLegal(activePentomino, gameBoard.getBoard(), predictedLocation)) { predictedLocation.setLocation(predictedLocation.getX(), predictedLocation.getY() + 1); } }
@Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; displayNames(g); displayWinner(g); g2d.drawImage(board.getImage(), board.getX(), board.getY(), null, null); for (int i = 0; i < 16; i++) { g2d.drawImage(pegs[i].getImage(), pegs[i].getX(), pegs[i].getY(), null, null); for (int j = 0; j < pegs[i].amountOfBeads2(); j++) { g2d.drawImage( pegs[i].beadAt(j).getImage(), pegs[i].beadAt(j).getX(), pegs[i].beadAt(j).getY(), null, null); } } if (availableBeads[currentBead].getImage() == null) { availableBeads[currentBead].loadImage(); } g2d.drawImage( availableBeads[currentBead].getImage(), availableBeads[currentBead].getX(), availableBeads[currentBead].getY(), null, null); if (beadMustAnimate) { animationTimer = new Timer(ANIMATION_REFRESH_RATE, this); // every // REFRESH_RATE // ms animationTimer.setActionCommand("animation"); animationTimer.start(); beadMustAnimate = false; currentBead++; // creates next new bead availableBeads[currentBead] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'b'); availableBeads[currentBead].setImage(board.getBlackBeadImage()); } }
/** * Constructor for a new Board * * @param sport */ public WorkingView(Sport sport) { this.sport = sport; GraphModel model = new DefaultGraphModel(); GraphLayoutCache view = new GraphLayoutCache(model, new TBECellViewFactory()); this.board = new Board(model, view, sport); board.getDescription().setDescription(sport.getName()); tbe.setSaved(false); createWorkingView(); }
//////////////////////////////////////////////////////////// // CONSTRUCTOR //////////////////////////////////////////////////////////// public Panel( String p1, String p2, LinkedBlockingQueue<String> sendQueue, LinkedBlockingQueue<String> recvQueue, JMenuBar menuBar) throws InterruptedException, ExecutionException { winner = NO_WINNER; typeOfGame = ""; this.menuBar = menuBar; this.sendQueue = sendQueue; // receives queues this.recvQueue = recvQueue; board = new Board(BOARD_INITIAL_X, BOARD_INITIAL_Y); // creates board board.setPlayerNames(p1, p2); pegs = new Peg[MAX_NUM_PEGS]; beadMustAnimate = false; isFirstTransaction = true; insertedInPegNum = 0; currentBead = 0; // its referree's responsibility to stop the game when no more bead are avasilable availableBeads = new Bead[MAX_NUM_BEADS * 2]; // sets first free bead availableBeads[0] = new Bead(BEAD_INITIAL_X, BEAD_INITIAL_Y, 'w'); availableBeads[0].setImage(board.getWhiteBeadImage()); for (int i = 0; i < 16; i++) { pegs[i] = new Peg(PEG_INITIAL_X[i], PEG_INITIAL_Y[i]); } setFocusable(true); // makes panel focusable // adds listeners mouseListener = new MouseListener(); addMouseMotionListener(mouseListener); addMouseListener(mouseListener); // when this timer fires, images will be displayed updateWhenLoaded = new Timer(IMAGE_LOADERS_TIMER, this); updateWhenLoaded.setActionCommand("images are loaded"); updateWhenLoaded.setRepeats(false); // occurs only one time updateWhenLoaded.start(); }
/** * the paintComponent(Graphics g) method paints the view component. It begins by painting the * black and white squares, and then iterates throught the board to paint all the pieces on the * board. */ @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; boolean isBlack = false; // keeps track of whether a square should be painted black or white /* * This for loop cycles through an 8x8 grid, alternating black and white squares */ for (int i = 0; i < 8; i++) { isBlack = !(isBlack); for (int j = 0; j < 8; j++) { isBlack = !(isBlack); Rectangle rect = new Rectangle(i * 62, j * 62, 62, 62); if (isBlack) { g2.setColor(Color.darkGray); } else { g2.setColor(Color.white); } g2.fill(rect); } } /* * This for loop cycles through the board and for any board square with a piece, * it paints draws the piece. */ for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board.hasPiece(i, j)) { // perform draw action if piece exists on board board.getSquare(i, j).draw(g2); } } } // draws selected pieces on top to ensure they are on the top layer for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (board.hasPiece(i, j)) { if (board.getSquare(i, j).isSelected()) { board.getSquare(i, j).draw(g2); } } } } }
/** * Activate/Deactivate Rotate-Button * * @param b */ public void activateRotation(boolean b) { if (showRotate && b) { rotatePanel.setVisible(b); rotateSlider.setValue(((ShapeItem) board.getSelectedItems()[0]).getRotation()); } else { rotatePanel.setVisible(false); } rotate.setEnabled(b); }
public TetrisWindow(String hostName, int serverPortNumber) throws IOException { super("Rainbow Tetris"); connection = new TetrisClient(hostName, serverPortNumber); myID = connection.getID(); board = new Board(); message = new JLabel("Waiting for two players to connect.", JLabel.CENTER); board.setBackground(Color.WHITE); board.setPreferredSize(new Dimension(300, 660)); board.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent evt) { doMouseClick(); } }); message.setBackground(Color.LIGHT_GRAY); message.setOpaque(true); JPanel content = new JPanel(); content.setLayout(new BorderLayout(2, 2)); content.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2)); content.setBackground(Color.GRAY); content.add(board, BorderLayout.CENTER); content.add(message, BorderLayout.SOUTH); setContentPane(content); pack(); setResizable(false); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent evt) { dispose(); connection.disconnect(); try { Thread.sleep(333); } catch (InterruptedException e) { } System.exit(0); } }); setLocation(200, 100); setVisible(true); }
/** * * Method that deals with the menu options * @param event the event that triggered this method */ public void actionPerformed(ActionEvent event) { if (event.getSource() == newMenuItem) { try { tableArea.newGame(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (event.getSource() == topScoresOption) { } else if (event.getSource() == quitMenuItem) { System.exit(0); } else if (event.getSource() == aboutMenuItem) { JOptionPane.showMessageDialog( tableArea, "By Jeffrey Wang and Hayes Lee\n\u00a9 2015", "About Neon Hex", JOptionPane.INFORMATION_MESSAGE); } }
/** * Add/Remove a Point to/from an Arrow * * @param b boolean, true = add, false = remove */ public void addRemovePoint(boolean b) { if (board.getSelectionCount() == 1 && board.getSelectionCell() instanceof ArrowItem) { MoveCommand mc = new MoveCommand(board.getSelectedItems()); ArrowItem a = (ArrowItem) board.getSelectionCell(); if (b) { a.addPoint(); } else { a.removePoint(); } WorkingView.this.refresh(); board.setSelectionCell(a); setTool(cursorTool, cursorButton); board.addItem(a); mc.setMoveEnd(board.getSelectedItems()); ArrayList<Command> actCommands = new ArrayList<Command>(); actCommands.add(mc); tbe.addCommands(actCommands); } }
private void newState(TetrisGameState state) { if (state.playerDisconnected) { JOptionPane.showMessageDialog(this, "Your opponent has disconnected.\nThe game is ended."); System.exit(0); } this.state = state; board.repaint(); if (!state.gameInProgress || state == null) { return; } else if (state.winner != -1 || state.tie) { setTitle("Game Over"); } else { setTitle("Game In Progress"); if (myID == state.player1) message.setText( "You: " + state.score1 + " pts " + state.KO1 + " KOs Opponent: " + state.score2 + " pts " + state.KO2 + " KOs"); else message.setText( "You: " + state.score2 + " pts " + state.KO2 + " KOs Opponent: " + state.score1 + " pts " + state.KO1 + " KOs"); } }
/** * Checks if the current game is finished * * @return True if game is finished */ public boolean gameFinished() { return suddenDeath || gameBoard.isTheGameLost(); }
/** * Handles the events for keypresses * * @param e the pressed key */ public void keyPressed(KeyEvent e) { if (activePentomino != null) { // Keys for moveing the pentomino sideways if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) { // Initialize direction to 0 to make sure we don't get null pointer exception // or do something weird with the movement int direction = 0; if (e.getKeyCode() == KeyEvent.VK_LEFT) direction = -1; if (e.getKeyCode() == KeyEvent.VK_RIGHT) direction = 1; // +1 right, -1 left // Check if we are allowed to actually move the pentomino to the new position boolean legalMove = true; for (Point p : activePentomino.getLocation()) { int newX = (int) (p.getX() + pentominoLocation.getX() + direction); int newY = (int) (p.getY() + pentominoLocation.getY()); // Check only if the pentomino is not above the board if (newY >= 0) { // Check that we are horizontally within the board and not trying to overlap another // piece if (newX < 0 || newX >= gameBoard.getBoard().length || gameBoard.getBoard()[newX][newY] != -1) legalMove = false; } } // Check passed so we do the actual movement of the pentomino if (legalMove) { pentominoLocation.setLocation( (pentominoLocation.getX() + direction), pentominoLocation.getY()); predictDrop(); updateMatrix(); } } // Key for rotating the pentomino if (e.getKeyCode() == KeyEvent.VK_UP) { // Create a temporary pentomino to check if the rotated position free Pentomino tmpPentomino = activePentomino.copy(); tmpPentomino.rotate(); boolean legalMove = true; // For reach block of the position check that it's free and within the grid // ignoring the check if we're above the grid for (Point p : tmpPentomino.getLocation()) { int newX = (int) (p.getX() + pentominoLocation.getX()); int newY = (int) (p.getY() + pentominoLocation.getY()); if (newY > 0) { if (newY >= gameBoard.getBoard()[0].length || newX < 0 || newX >= gameBoard.getBoard().length || gameBoard.getBoard()[newX][newY] != -1) { legalMove = false; } } } // If the check passed then simply do the rotation if (legalMove) { activePentomino.rotate(); predictDrop(); } // Update the matrix so we don't have lag in display updateMatrix(); } // Key for speeding up the fall if (e.getKeyCode() == KeyEvent.VK_DOWN) { // If next position is available, put the current pentomino to it if (nextDropLegal(activePentomino, gameBoard.getBoard(), pentominoLocation)) { pentominoLocation.setLocation(pentominoLocation.getX(), pentominoLocation.getY() + 1); } // Update to avoid lag updateMatrix(); } // Key for entirely dropping the pentomino if (e.getKeyCode() == KeyEvent.VK_ENTER) { // We make the pentomino fall as many times as it can while (nextDropLegal(activePentomino, gameBoard.getBoard(), pentominoLocation)) { pentominoLocation.setLocation(pentominoLocation.getX(), pentominoLocation.getY() + 1); } // Update to avoid lag updateMatrix(); } // Key for flipping the pentomino if (e.getKeyCode() == KeyEvent.VK_SPACE) { // Create a temporary pentomino for checking the flipped position Pentomino tmpPentomino = activePentomino.copy(); tmpPentomino.reflect(); // Check that each position in the temporary pentomino is available and within the matrix boolean legalMove = true; for (Point p : tmpPentomino.getLocation()) { int newX = (int) (p.getX() + pentominoLocation.getX()); int newY = (int) (p.getY() + pentominoLocation.getY()); if (newY < 0 || newY >= gameBoard.getBoard()[0].length || newX < 0 || newX >= gameBoard.getBoard().length || gameBoard.getBoard()[newX][newY] != -1) { legalMove = false; } } // Flip the pentomino if check is passed if (legalMove) { activePentomino.reflect(); predictDrop(); } // Update to avoid lag updateMatrix(); } // Key for storing the currently falling pentomino if (e.getKeyCode() == KeyEvent.VK_SHIFT) { // If we haven't stored after fixing the pentomino to the grid if (storageable) { // Change the active and stored pentomino and set the location to the top of the screen pentominoLocation = new Point(gameBoard.getWidth() / 2, 0); Pentomino tmp = activePentomino; activePentomino = storagedPentomino; storagedPentomino = tmp; storageable = false; // Get new prediction predictDrop(); // Update to avoid lag updateMatrix(); } } // Key for a chect for testing purposes if (e.getKeyCode() == KeyEvent.VK_X) { // If pressed enough, add score cheat++; if (cheat > 10) { cheat = 0; addScore(12); } } } }
private void setListForKing() { moveList.clear(); squareList.clear(); int row = square.getRow(); int column = square.getColumn(); int c1, c2, c3, c4, c5, c6, c7, c8; String cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8; if (((row + 1) < 9) && (column + 1) < 9) { c1 = square.getCellFromCoord(row + 1, column + 1); cell1 = square.getPositionWithArg(c1); Square s = (Square) board.getComponent(c1); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell1); squareList.add(s); } } else { moveList.add(cell1); squareList.add(s); } } if (((row + 1) < 9) && (column - 1) > 0) { c2 = square.getCellFromCoord(row + 1, column - 1); cell2 = square.getPositionWithArg(c2); Square s = (Square) board.getComponent(c2); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell2); squareList.add(s); } } else { moveList.add(cell2); squareList.add(s); } } if ((row + 1) < 9) { c3 = square.getCellFromCoord(row + 1, column); cell3 = square.getPositionWithArg(c3); Square s = (Square) board.getComponent(c3); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell3); squareList.add(s); } } else { moveList.add(cell3); squareList.add(s); } } if ((column + 1) < 9) { c4 = square.getCellFromCoord(row, column + 1); cell4 = square.getPositionWithArg(c4); Square s = (Square) board.getComponent(c4); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell4); squareList.add(s); } } else { moveList.add(cell4); squareList.add(s); } } if ((column - 1) > 0) { c5 = square.getCellFromCoord(row, column - 1); cell5 = square.getPositionWithArg(c5); Square s = (Square) board.getComponent(c5); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell5); squareList.add(s); } } else { moveList.add(cell5); squareList.add(s); } } if (((row - 1) > 0) && (column - 1) > 0) { c6 = square.getCellFromCoord(row - 1, column - 1); cell6 = square.getPositionWithArg(c6); Square s = (Square) board.getComponent(c6); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell6); squareList.add(s); } } else { moveList.add(cell6); squareList.add(s); } } if (((row - 1) > 0) && (column + 1) < 9) { c7 = square.getCellFromCoord(row - 1, column + 1); cell7 = square.getPositionWithArg(c7); Square s = (Square) board.getComponent(c7); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell7); squareList.add(s); } } else { moveList.add(cell7); squareList.add(s); } } if ((row - 1) > 0) { c8 = square.getCellFromCoord(row - 1, column); cell8 = square.getPositionWithArg(c8); Square s = (Square) board.getComponent(c8); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell8); squareList.add(s); } } else { moveList.add(cell8); squareList.add(s); } } if (queensideClear()) { int cell = square.getCellFromCoord(1, 3); String c = square.getPositionWithArg(cell); moveList.add(c); cell = square.getCellFromCoord(8, 3); c = square.getPositionWithArg(cell); moveList.add(c); } if (kingsideClear()) { int cell = square.getCellFromCoord(1, 7); String c = square.getPositionWithArg(cell); moveList.add(c); cell = square.getCellFromCoord(8, 7); c = square.getPositionWithArg(cell); moveList.add(c); } }
private void setListForKnight() { moveList.clear(); squareList.clear(); /*switch(square.getCellNumber()) { case 56 : moveList.add("c2");moveList.add("b3"); break; case 57 : moveList.add("d2");moveList.add("a3");moveList.add("c3"); break; case 58 : moveList.add("a2");moveList.add("e2");moveList.add("b3");moveList.add("d3"); break; case 59 : moveList.add("b2");moveList.add("f2");moveList.add("c3");moveList.add("e3"); break; case 60 : moveList.add("c2");moveList.add("g2");moveList.add("d3");moveList.add("f3"); break; case 61 : moveList.add("d2");moveList.add("h2");moveList.add("e3");moveList.add("g3"); break; case 62 : moveList.add("e2");moveList.add("f3");moveList.add("h3"); break; case 63 : moveList.add("f2");moveList.add("g3"); break; case 48 : moveList.add("c1");moveList.add("c3");moveList.add("b4"); break; case 49 : moveList.add("d1");moveList.add("d3");moveList.add("a4");moveList.add("c4"); break; case 50 : moveList.add("a1");moveList.add("e1");moveList.add("a3");moveList.add("e3");moveList.add("b4");moveList.add("d4"); break; case 51 : moveList.add("b1");moveList.add("f1");moveList.add("b3");moveList.add("f3");moveList.add("c4");moveList.add("e4"); break; case 52 : moveList.add("c1");moveList.add("g1");moveList.add("c3");moveList.add("g3");moveList.add("d4");moveList.add("f4"); break; case 53 : moveList.add("d1");moveList.add("h1");moveList.add("d3");moveList.add("h3");moveList.add("e4");moveList.add("g4"); break; case 54 : moveList.add("e1");moveList.add("e3");moveList.add("f4");moveList.add("h4"); break; case 55 : moveList.add("f1");moveList.add("f3");moveList.add("g4"); break; case 40 : moveList.add("b1");moveList.add("c2");moveList.add("c4");moveList.add("b5"); break; case 41 : moveList.add("a1");moveList.add("c1");moveList.add("d2");moveList.add("d4");moveList.add("a5");moveList.add("c5"); break; case 42 : moveList.add("b1");moveList.add("d1");moveList.add("a2");moveList.add("e2");moveList.add("a4");moveList.add("e4");moveList.add("b5");moveList.add("d5"); break; case 43 : moveList.add("c1");moveList.add("e1");moveList.add("b2");moveList.add("f2");moveList.add("b4");moveList.add("f4");moveList.add("c5");moveList.add("e5"); break; case 44 : moveList.add("d1");moveList.add("f1");moveList.add("c2");moveList.add("g2");moveList.add("c4");moveList.add("g4");moveList.add("d5");moveList.add("f5"); break; case 45 : moveList.add("e1");moveList.add("g1");moveList.add("d2");moveList.add("h2");moveList.add("d4");moveList.add("h4");moveList.add("e5");moveList.add("g5"); break; case 46 : moveList.add("f1");moveList.add("h1");moveList.add("e2");moveList.add("e4");moveList.add("f5");moveList.add("h5"); break; case 47 : moveList.add("g1");moveList.add("f2");moveList.add("f4");moveList.add("g5"); break; case 32 : moveList.add("b2");moveList.add("c3");moveList.add("c5");moveList.add("b6"); break; case 33 : moveList.add("a2");moveList.add("c2");moveList.add("d3");moveList.add("d5");moveList.add("a6");moveList.add("c6"); break; case 34 : moveList.add("b2");moveList.add("d2");moveList.add("a3");moveList.add("e3");moveList.add("a5");moveList.add("e5");moveList.add("b6");moveList.add("d6"); break; case 35 : moveList.add("c2");moveList.add("e2");moveList.add("b3");moveList.add("f3");moveList.add("b5");moveList.add("f5");moveList.add("c6");moveList.add("e6"); break; case 36 : moveList.add("d2");moveList.add("f2");moveList.add("c3");moveList.add("g3");moveList.add("c5");moveList.add("g5");moveList.add("d6");moveList.add("f6"); break; case 37 : moveList.add("e2");moveList.add("g2");moveList.add("d3");moveList.add("h3");moveList.add("d5");moveList.add("h5");moveList.add("e6");moveList.add("g6"); break; case 38 : moveList.add("f2");moveList.add("h2");moveList.add("e3");moveList.add("e5");moveList.add("f6");moveList.add("h6"); break; case 39 : moveList.add("g2");moveList.add("f3");moveList.add("f5");moveList.add("g6"); break; case 24 : moveList.add("b3");moveList.add("c4");moveList.add("c6");moveList.add("b7"); break; case 25 : moveList.add("a3");moveList.add("c3");moveList.add("d4");moveList.add("d6");moveList.add("a7");moveList.add("c7"); break; case 26 : moveList.add("b3");moveList.add("d3");moveList.add("a4");moveList.add("e4");moveList.add("a6");moveList.add("e6");moveList.add("b7");moveList.add("d7"); break; case 27 : moveList.add("c3");moveList.add("e3");moveList.add("b4");moveList.add("f4");moveList.add("b6");moveList.add("f6");moveList.add("c7");moveList.add("e7"); break; case 28 : moveList.add("d3");moveList.add("f3");moveList.add("c4");moveList.add("g4");moveList.add("c6");moveList.add("g6");moveList.add("d7");moveList.add("f7"); break; case 29 : moveList.add("e3");moveList.add("g3");moveList.add("d4");moveList.add("h4");moveList.add("d6");moveList.add("h6");moveList.add("e7");moveList.add("g7"); break; case 30 : moveList.add("f3");moveList.add("h3");moveList.add("e4");moveList.add("e6");moveList.add("f7");moveList.add("h7"); break; case 31 : moveList.add("g3");moveList.add("f4");moveList.add("f6");moveList.add("g7"); break; case 16 : moveList.add("b4");moveList.add("c5");moveList.add("c7");moveList.add("b8"); break; case 17 : moveList.add("a4");moveList.add("c4");moveList.add("d5");moveList.add("d7");moveList.add("a8");moveList.add("c8"); break; case 18 : moveList.add("b4");moveList.add("d4");moveList.add("a5");moveList.add("e5");moveList.add("a7");moveList.add("e7");moveList.add("b8");moveList.add("d8"); break; case 19 : moveList.add("c4");moveList.add("e4");moveList.add("b5");moveList.add("f5");moveList.add("b7");moveList.add("f7");moveList.add("c8");moveList.add("e8"); break; case 20 : moveList.add("d4");moveList.add("f4");moveList.add("c5");moveList.add("g5");moveList.add("c7");moveList.add("g7");moveList.add("d8");moveList.add("f8"); break; case 21 : moveList.add("e4");moveList.add("g4");moveList.add("d5");moveList.add("h5");moveList.add("d7");moveList.add("h7");moveList.add("e8");moveList.add("g8"); break; case 22 : moveList.add("f4");moveList.add("h4");moveList.add("e5");moveList.add("e7");moveList.add("f8");moveList.add("h8"); break; case 23 : moveList.add("g4");moveList.add("f5");moveList.add("f7");moveList.add("g8"); break; case 8 : moveList.add("b5");moveList.add("c6");moveList.add("c8"); break; case 9 : moveList.add("a5");moveList.add("c5");moveList.add("d6");moveList.add("d8"); break; case 10 : moveList.add("b5");moveList.add("d5");moveList.add("a6");moveList.add("e6");moveList.add("a8");moveList.add("e8"); break; case 11 : moveList.add("c5");moveList.add("e5");moveList.add("b6");moveList.add("f6");moveList.add("b8");moveList.add("f8"); break; case 12 : moveList.add("d5");moveList.add("f5");moveList.add("c6");moveList.add("g6");moveList.add("c8");moveList.add("g8"); break; case 13 : moveList.add("e5");moveList.add("g5");moveList.add("d6");moveList.add("h6");moveList.add("d8");moveList.add("h8"); break; case 14 : moveList.add("f5");moveList.add("h5");moveList.add("e6");moveList.add("e8"); break; case 15 : moveList.add("g5");moveList.add("f6");moveList.add("f8"); break; case 0 : moveList.add("b6");moveList.add("c7"); break; case 1 : moveList.add("a6");moveList.add("c6");moveList.add("d7"); break; case 2 : moveList.add("b6");moveList.add("d6");moveList.add("a7");moveList.add("e7"); break; case 3 : moveList.add("c6");moveList.add("e6");moveList.add("b7");moveList.add("f7"); break; case 4 : moveList.add("d6");moveList.add("f6");moveList.add("c7");moveList.add("g7"); break; case 5 : moveList.add("e6");moveList.add("g6");moveList.add("d7");moveList.add("h7"); break; case 6 : moveList.add("f6");moveList.add("h6");moveList.add("e7");moveList.add("NS"); break; case 7 : moveList.add("g6");moveList.add("f7"); break; }*/ int row = square.getRow(); int column = square.getColumn(); int c1, c2, c3, c4, c5, c6, c7, c8; String cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8; if (((row + 1) < 9) && (column - 2) > 0) { c1 = square.getCellFromCoord(row + 1, column - 2); cell1 = square.getPositionWithArg(c1); Square s = (Square) board.getComponent(c1); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell1); squareList.add(s); } } else { moveList.add(cell1); squareList.add(s); } } if (((row + 2) < 9) && (column - 1) > 0) { c2 = square.getCellFromCoord(row + 2, column - 1); cell2 = square.getPositionWithArg(c2); Square s = (Square) board.getComponent(c2); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell2); squareList.add(s); } } else { moveList.add(cell2); squareList.add(s); } } if (((row + 1) < 9) && (column + 1) < 9) { c3 = square.getCellFromCoord(row + 2, column + 1); cell3 = square.getPositionWithArg(c3); Square s = (Square) board.getComponent(c3); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell3); squareList.add(s); } } else { moveList.add(cell3); squareList.add(s); } } if (((row + 1) < 9) && (column + 2) < 9) { c4 = square.getCellFromCoord(row + 1, column + 2); cell4 = square.getPositionWithArg(c4); Square s = (Square) board.getComponent(c4); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell4); squareList.add(s); } } else { moveList.add(cell4); squareList.add(s); } } if (((row - 1) > 0) && (column - 2) > 0) { c5 = square.getCellFromCoord(row - 1, column - 2); cell5 = square.getPositionWithArg(c5); Square s = (Square) board.getComponent(c5); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell5); squareList.add(s); } } else { moveList.add(cell5); squareList.add(s); } } if (((row - 2) > 0) && (column - 1) > 0) { c6 = square.getCellFromCoord(row - 2, column - 1); cell6 = square.getPositionWithArg(c6); Square s = (Square) board.getComponent(c6); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell6); squareList.add(s); } } else { moveList.add(cell6); squareList.add(s); } } if (((row - 2) > 0) && (column + 1) < 9) { c7 = square.getCellFromCoord(row - 2, column + 1); cell7 = square.getPositionWithArg(c7); Square s = (Square) board.getComponent(c7); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell7); squareList.add(s); } } else { moveList.add(cell7); squareList.add(s); } } if (((row - 1) > 0) && (column + 2) < 9) { c8 = square.getCellFromCoord(row - 1, column + 2); cell8 = square.getPositionWithArg(c8); Square s = (Square) board.getComponent(c8); if (s.getComponentCount() > 0) { Piece p = (Piece) s.getComponent(0); if (p.getColor() != color) { moveList.add(cell8); squareList.add(s); } } else { moveList.add(cell8); squareList.add(s); } } }
private void setListForRook() { int row = square.getRow(); int column = square.getColumn(); L1: for (int i = 1; i < 8; i++) { int r = row; int c = column + i; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if (c < 9) { if (sq.getComponentCount() == 0) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } else { if (((Piece) sq.getComponent(0)).getColor() != color) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } break L1; } } } L2: for (int i = 1; i < 8; i++) { int r = row; int c = column - i; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if (c > 0) { if (sq.getComponentCount() == 0) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } else { if (((Piece) sq.getComponent(0)).getColor() != color) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } break L2; } } } L3: for (int i = 1; i < 8; i++) { int r = row - i; int c = column; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if (r > 0) { if (sq.getComponentCount() == 0) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } else { if (((Piece) sq.getComponent(0)).getColor() != color) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } break L3; } } } L4: for (int i = 1; i < 8; i++) { int r = row + i; int c = column; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if (r < 9) { if (sq.getComponentCount() == 0) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } else { if (((Piece) sq.getComponent(0)).getColor() != color) { moveList.add(sq.getPositionWithArg(cell)); squareList.add(sq); } break L4; } } } }
/** Selects all Items of the Board */ public void selectAllItems() { board.setSelectionCells(board.getItems()); }