Advertisement
Guest User

Untitled

a guest
May 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import javax.swing.JComponent;
  2.  
  3. public class Game extends JComponent{
  4. /**
  5. *
  6. */
  7. private static final long serialVersionUID = 1L;
  8. public GamePiece[][] gameBoard;
  9.  
  10. public Game() {
  11. gameBoard = new GamePiece[4][4];
  12. for (int k = 0; k < 4; k++) {
  13. for (int j = 0; j < 4; j++) {
  14. int x = 12 + (160 + 12) * k;
  15. int y = 12 + (160 + 12) * j;
  16. gameBoard[k][j] = new GamePiece(x, y, 0);
  17. }
  18. }
  19. }
  20.  
  21. public void start() {
  22.  
  23. }
  24.  
  25. public void restart() {
  26. for (int k = 0; k < 4; k++) {
  27. for (int j = 0; j < 4; j++) {
  28. gameBoard[k][j].setValue(0);
  29. }
  30. }
  31. }
  32.  
  33. public void random() {
  34. int randomRow = (int) Math.random() * 4;
  35. int randomCol = (int) Math.random() * 4;
  36. while(gameBoard[randomRow][randomCol].getValue() == 0) {
  37. randomRow = (int) Math.random() * 4;
  38. randomCol = (int) Math.random() * 4;
  39. }
  40. gameBoard[randomRow][randomCol].setValue(2);
  41. }
  42.  
  43. public void up() {
  44. System.out.println("Up Arrow Pressed");
  45. for (int c = 0; c < 4; c++) {
  46. for (int r = 3; r > 0; r--) {
  47. if (gameBoard[r][c] == gameBoard[r - 1][c]) {
  48. gameBoard[r][c - 1].setValue(gameBoard[r][c].getValue() * 2);
  49. gameBoard[r][c].setValue(0);
  50. gameBoard[r][c].moveUp();
  51. }
  52. }
  53. }
  54. random();
  55. }
  56.  
  57. public void down() {
  58. System.out.println("Down Arrow Pressed");
  59. for (int c = 0; c < 4; c++) {
  60. for (int r = 0; r < 4; r++) {
  61. if (gameBoard[r][c] == gameBoard[r + 1][c]) {
  62. gameBoard[r][c + 1].setValue(gameBoard[r][c].getValue() * 2);
  63. gameBoard[r][c].setValue(0);
  64. gameBoard[r][c].moveDown();
  65. }
  66. }
  67. }
  68. random();
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement