diff --git a/README.md b/README.md index ddb4579..2c56687 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,15 @@ ### **7not7** ### *A 7x7 clone written in java* +This page should be viewed on [Github](https://github.com/id101010/7not7) + --- ### Summary 7x7 is a concentration game which challenges you to connect 4 or more blocks of the same color on the game field. To make things harder each round n blocks of random colors get thrown upon the field and the number n increases with each level. -You can choose the field size to be eighter 7x7, 8x8, 9x9 or 10x10. +You can choose the field size to be either 7x7, 8x8, 9x9 or 10x10. ![Moving Blocks](https://github.com/id101010/7not7/blob/master/doc/movingBlocks.png) @@ -15,6 +17,7 @@ You can choose the field size to be eighter 7x7, 8x8, 9x9 or 10x10. The goal is to clear as much lines as possible by stacking blocks of the same color in any direction. So you can eighter build up blocks vertically, horizontally or diagonal. +To move blocks click on the block you want to move, hold the mouse and release it on the target position. ### Tools and how to get them diff --git a/src/ch/bfh/sevennotseven/FieldCanvas.java b/src/ch/bfh/sevennotseven/FieldCanvas.java index 684b339..b1de3fc 100644 --- a/src/ch/bfh/sevennotseven/FieldCanvas.java +++ b/src/ch/bfh/sevennotseven/FieldCanvas.java @@ -24,6 +24,7 @@ public class FieldCanvas extends JPanel{ static final int borderTop = 5; static final int borderBottom = 5; + //Colors to use to paint the blocks. Chosen with a color scheme designer public static final Color[] colors = { new Color(0xD66436), new Color(0x486F70), @@ -34,11 +35,11 @@ public class FieldCanvas extends JPanel{ private Game game; - private Point src; - private Point dst; - private List path; - private List blockedFields; - private boolean freeMoveMode = false; + private Point src; //Position of the Block that the user wants to move (can be null) + private Point dst; //Destination Position (can be null) + private List path; //Path that visualizes src->dst (can be null) + private List blockedFields; //Fields that should be marked as blocked (can be null) + private boolean freeMoveMode = false; //Whether or not we're in free moving mode /** * Constructor of FieldCanvas @@ -53,9 +54,9 @@ public class FieldCanvas extends JPanel{ super.mousePressed(e); Point p = FieldCanvas.this.getClickPoint(e.getPoint()); - if(p==null || game.getField()[p.x][p.y]==0) { //invalid click + if(p==null || game.getField()[p.x][p.y]==0) { //invalid click (outside of bounds or on empty position) src = null; - } else { + } else { //valid click src = p; if(freeMoveMode) { blockedFields = game.getReachablePoints(src); @@ -69,24 +70,26 @@ public class FieldCanvas extends JPanel{ @Override public void mouseDragged(MouseEvent e) { super.mouseDragged(e); - if(src!=null) { + if(src!=null) { //we have a valid point that the users wants to move Point lastDst = dst; dst = FieldCanvas.this.getClickPoint(e.getPoint()); if(lastDst!=dst && dst!=null) { //hovered field changed if(freeMoveMode) { + //Check if the target position is empty and we could not move the block there in normal mode if(!game.canMove(src, dst) && game.getField()[dst.x][dst.y]==0) { + //Create fake path with only src and dst path = new ArrayList(); path.add(src); path.add(dst); } else { path= null; } - } else { - path= game.getPath(src, dst); + } else { //not in freemove mode + path= game.getPath(src, dst); //calculate path from src to dst (pathfinding) } repaint(); } - }else { + } else { //no valid src dst = null; path = null; } @@ -96,13 +99,13 @@ public class FieldCanvas extends JPanel{ public void mouseReleased(MouseEvent e) { super.mouseReleased(e); dst = FieldCanvas.this.getClickPoint(e.getPoint()); - path = null; + path = null; //do no longer paint path if(freeMoveMode) { - if(!game.canMove(src, dst)) { + if(!game.canMove(src, dst)) { //if we couldn't move there in normal mode game.doFreeMove(src, dst); } - } else { - if(dst != null && src!=null && !src.equals(dst)) { + } else { //not in freemove mode + if(dst != null && src!=null && !src.equals(dst)) { //src and dst are valid System.out.println("Moving from "+src.toString()+ " to "+dst.toString()); game.doMove(src, dst); } @@ -147,11 +150,11 @@ public class FieldCanvas extends JPanel{ } /** - * Calculates the position in which a click event has happened. + * Maps a mouse position to game coordinates * * @author timo - * @param globalPos - * @return Position of clickevent + * @param globalPos Position of clickevent + * @return point in game coordinates */ private Point getClickPoint(Point globalPos) { int total = Math.min(this.getHeight()-borderTop-borderBottom,FieldCanvas.this.getWidth()-borderLeft-borderRight); @@ -169,6 +172,8 @@ public class FieldCanvas extends JPanel{ */ public void paintComponent(Graphics g) { super.paintComponent(g); + + if(game==null) return; //Draw field (background and lines) if(freeMoveMode) { @@ -180,21 +185,20 @@ public class FieldCanvas extends JPanel{ g.translate(borderLeft, borderTop); int total = Math.min(this.getHeight()-borderTop-borderBottom,FieldCanvas.this.getWidth()-borderLeft-borderRight); int space = total/game.getSize(); + total = space*game.getSize(); - g.setClip(0, 0, total-4,total-4); - g.fillRect(0, 0, total-4,total-4); + g.setClip(0, 0, total+1,total+1); + g.fillRect(0, 0, total,total); g.setColor(Color.white); for(int i=0; i<=game.getSize(); i++) { - g.drawLine(0,i*space,total-2,i*space); - g.drawLine(i*space,0,i*space,total-2); + g.drawLine(0,i*space,total,i*space); + g.drawLine(i*space,0,i*space,total); } - if(game==null) return; - - //Draw blocks + //Draw blocks for(int x=0; x0); - buttonUndo.setEnabled(game.getAvailUndo()>0); - buttonFreeMove.setText("Free Move ("+game.getAvailFreeMoves()+")"); - buttonUndo.setText("Undo ("+game.getAvailUndo()+")"); - labelLinesLeft.setText("Lines Left: "+game.getLinesLeft()); - labelLevel.setText("Level: "+game.getLevel()); - - } - }); - + JPanel gamePanel = new JPanel(); + gamePanel.setLayout(new BorderLayout()); + gamePanel.add(topPanel, BorderLayout.NORTH); + gamePanel.add(field); + mainPanel.add(gamePanel); + } + + private void initWelcomeLayout() { JPanel welcomePanel = new JPanel(); + welcomePanel.setLayout(new BoxLayout(welcomePanel, BoxLayout.Y_AXIS)); + + JButton btnInstr = new JButton("Instructions & More"); + btnInstr.setAlignmentX(Component.CENTER_ALIGNMENT); + btnInstr.setActionCommand("github"); + btnInstr.addActionListener(this); + + welcomePanel.add(Box.createRigidArea(new Dimension(0,5))); + welcomePanel.add(btnInstr); + welcomePanel.add(Box.createRigidArea(new Dimension(0,10))); + int sizes [] = {7,8,9,10}; for(int i=0; i0); + buttonUndo.setEnabled(game.getAvailUndo()>0); + buttonFreeMove.setText("Free Move ("+game.getAvailFreeMoves()+")"); + buttonUndo.setText("Undo ("+game.getAvailUndo()+")"); + labelLinesLeft.setText("Lines Left: "+game.getLinesLeft()); + labelLevel.setText("Level: "+game.getLevel()); + + } /** * Main method