Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Add This Thread To: Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb
 « Previous Thread | Next Thread » Thread Tools Search this Thread Rate Thread Display Modes
 Dev Shed Forums Sponsor:
#1
February 16th, 2013, 01:00 PM
 wholegrain
Registered User

Join Date: Feb 2013
Posts: 4
Time spent in forums: 32 m 54 sec
Reputation Power: 0
Detecting collision

I am asked to do the following:

Write a method in the Board class that takes a row and column index and a two-dimensional array as parameters, and checks to see that the non-zero entries of the given array correspond to empty positions on the grid, assuming that the given array's upper left were positioned at the given row and column. You'll use this to determine if a piece can move into a given position.

So I have a Piece object and two variable currentX and currentY. I am assuming those will be passed into the method described above. currentX and currentY represent the top corner of the shapes. Now, how do I implement collision detection? I think we need to have 2 for loops and find the indexes of the shape array that contains 1. After that, I might have to create an array of the same size as shape array and then do two for loops to see if there is a collision if the shape moves below.

So I need to have 3 methods implementing the same logic. One for move right, another for move left and another for move down, right? Isn't there a simpler method?

Code:

Code:
```import java.awt.*;

public class Board extends Grid {
public static final int COLUMNS = 16;
public static final int ROWS = 32;
public static final Color BLUE = new Color(0,0,128,40);

public Board() {
super(new int[ROWS][COLUMNS]);
setSize(COLUMNS*Tetris.SQUARE_SIZE,
ROWS*Tetris.SQUARE_SIZE);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(BLUE);
paintStripes(g);
}

void paintStripes(Graphics g) {
for (int i = 0; i < COLUMNS; i += 4) {
g.fillRect(i*Tetris.SQUARE_SIZE,0,
Tetris.SQUARE_SIZE*2,Tetris.SQUARE_SIZE*ROWS);
}
}

}```

Code:
Code:
```public class PieceFactory {

public static final int[][] L1 =
{{1,1,0,0},
{0,1,0,0},
{0,1,0,0},
{0,0,0,0},
};

public static final int[][] L2 =
{{0,1,0,0},
{0,1,0,0},
{1,1,0,0},
{0,0,0,0},
};

public static final int[][] T =
{{0,1,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0},
};

public static final int[][] BOX =
{{1,1,0,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0},
};

public static final int[][] BAR =
{{1,1,1,1},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
};

public static final int[][] STEP1 =
{{1,0,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0},
};

public static final int[][] STEP2 =
{{0,1,0,0},
{1,1,0,0},
{1,0,0,0},
{0,0,0,0},
};

public static final int[][][] SHAPES = {L1,L2,T,BOX,BAR,STEP1,STEP2};

public static Piece createPiece() {
int[][] s = SHAPES[(int) (Math.random()*SHAPES.length)];
switch ((int) (Math.random()*10)) {
case 0:
case 1:
case 2:
case 3:
default: return new Piece(s);
}

}

}```

Code:
Code:
```import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Tetris extends JFrame implements KeyListener {
public static final int SQUARE_SIZE = 10; // 10 by 10 pixels

static Board board;
static Tetris game;

JPanel mainPanel;
public Piece currentPiece;
int score = 0;
JButton scoreButton;

public Tetris() {
super("Tetris");
game = this;
Container pane = getContentPane();
pane.setLayout(new BorderLayout());

scoreButton = new JButton("0");
scoreButton.setEnabled(false);
pane.add(scoreButton,BorderLayout.NORTH);

board = new Board();
mainPanel = new JPanel();
mainPanel.setLayout(null);
mainPanel.add(board);
mainPanel.setPreferredSize
(new Dimension(Board.COLUMNS*SQUARE_SIZE,
Board.ROWS*SQUARE_SIZE));
pane.add(mainPanel);

addKeyListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
pack();
show();
setResizable(false);
}

public void addToScore(int v) {
score += v;
scoreButton.setText(""+score);
}

public int getScore() {
return score;
}

static Board getBoard() {
return board;
}

static Tetris getGame() {
return game;
}

static void sleep(int milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException ie) {
}
}

public static void main(String[] args) {
Tetris game = new Tetris();
while (true) {
game.dropPiece();
}
}

void dropPiece() {
currentPiece = PieceFactory.createPiece();
mainPanel.add(currentPiece);
currentPiece.repaint();
currentPiece.fall();
//mainPanel.remove(currentPiece);
board.repaint();
addToScore(1);
}

public void keyPressed(KeyEvent event) {
int key = event.getKeyCode();
switch (key) {
case KeyEvent.VK_UP:  // up arrow
case KeyEvent.VK_KP_UP:
currentPiece.rotateCounterclockwise();
break;
case KeyEvent.VK_DOWN:  // down arrow
case KeyEvent.VK_KP_DOWN:
currentPiece.rotateClockwise();
break;
case KeyEvent.VK_LEFT:  // left arrow
case KeyEvent.VK_KP_LEFT:
currentPiece.moveLeft();
break;
case KeyEvent.VK_RIGHT:  // right arrow
case KeyEvent.VK_KP_RIGHT:
currentPiece.moveRight();
break;
case KeyEvent.VK_SPACE:  //  space bar
currentPiece.drop();
}
}

public void keyReleased(KeyEvent arg0) {
}

public void keyTyped(KeyEvent arg0) {
}

}```

Code:

Code:
```public class Piece extends Grid {
int currentX;     // current X location on the board
double currentY;  // current Y location on the board

public Piece(int shape[][]) {
super(shape);
currentX = 7;
currentY = 2;
updateLocation();
}

public Piece(int shape[][], currentX, currentY) {
super(shape);
this.currentX = currentX;
this.currentY = currentY;
updateLocation()
}

void updateSize() {
setSize(Tetris.SQUARE_SIZE*getColumns(),
Tetris.SQUARE_SIZE*getRows());
}

void updateLocation() {
setLocation(Tetris.SQUARE_SIZE*currentX,
(int) (Tetris.SQUARE_SIZE*currentY));
}

synchronized void moveDown() {

}

synchronized void moveLeft() {
currentX--;
updateLocation();
}

synchronized void moveRight() {
currentX++;
updateLocation();
}

synchronized void rotateClockwise() {
int x = currentX;
int y = currentY;

currentPiece = new Piece(s, x, y);

updateLocation();
}

synchronized void rotateCounterclockwise() {
}

void fall() {
// replace the following by your code
Tetris.sleep(2000);
}

synchronized void drop() {
currentY++;
updateLocation();
}

public static int [][] multiplyMatrix(int [][] m1)
{
int [][] m2 =
{{0,0,0,1},
{0,0,1,0},
{0,1,0,0},
{1,0,0,0},
};

int[][] result = new int[4][4];

// multiply
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
for (int k=0; k<4; k++)
if (m1[i][k] * m2[k][j] > 0)
{
result[i][j] = 1;
}
else
{
result[i][j] = 0;
}

return result;
}

}
```

#2
February 18th, 2013, 08:57 PM
 wholegrain
Registered User

Join Date: Feb 2013
Posts: 4
Time spent in forums: 32 m 54 sec
Reputation Power: 0
Code:
```void fall(Board board) {

int [][] a = getContents();
int [][] b = board.getContents();

while(Board.legalDown(currentX, currentY, a, a) && Board.isNotOutOfBoundDown(currentX, currentY, a, a))
{
currentX--;
updateLocation();
updateSize();
}

Tetris.sleep(2000);
}```

error: method legalDown in class Board cannot be applied to given types;

error: method isNotOutOfBoundDown in class Board cannot be applied to given types;

Code:
```public static boolean legalDown(int currentX, int currentY, int[][] board, int[][] shape)

public static boolean isNotOutOfBoundDown(int currentX, int currentY, int[][] board, int[][] shape)```

Code:
```	public int[][] getContents()
{
return contents;
}```

What the?? I don't get it...

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Detecting collision

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap