public void actionPerformed(ActionEvent e) { if (e.getSource() == timer) { if (finishedFalling) { finishedFalling = false; makeNewPiece(); } else { dropOneLine(); } } if (e.getSource() == timer1 && state.time > 0 && state != null) { connection.send("time"); if (state.time <= 1) { paused = true; } repaint(); } if (e.getSource() == timer2) { if (state != null && startDone == false) { paused = false; start(); startDone = true; } if (state != null && state.needToAdd1 && myID == state.player1) addLine(state.addLines1); else if (state != null && state.needToAdd2 && myID == state.player2) addLine(state.addLines2); } }
private void addLine(int num) { for (int i = 0; i < staticY.size(); i++) staticY.set(i, staticY.get(i) - num); for (int x = 0; x < FinalWidth; x++) { for (int y = 0; y < num; y++) { staticX.add(x); staticY.add(FinalHeight - 1 - y); } } findShadow(); if (state != null) { connection.send("done"); } }
private void removeLine() { int[] allCols = new int[FinalHeight]; for (int i = 0; i < FinalHeight; i++) allCols[i] = 0; for (int j = 0; j < staticY.size(); j++) { allCols[staticY.get(j)]++; } ArrayList<Integer> fullRows = new ArrayList<Integer>(); for (int c = 0; c < allCols.length; c++) { if (allCols[c] >= FinalWidth && (c == tempY[0] || c == tempY[1] || c == tempY[2] || c == tempY[3])) { fullRows.add(c); } } if (fullRows.size() > 0) { for (int a = 0; a < fullRows.size(); a++) { for (int b = 0; b < staticY.size(); b++) { if (fullRows.get(a) == staticY.get(b)) { staticX.remove(b); staticY.remove(b); b--; } } } for (int k = 0; k < fullRows.size(); k++) { for (int w = 0; w < staticY.size(); w++) { if (staticY.get(w) < fullRows.get(k)) staticY.set(w, staticY.get(w) + 1); } } score += fullRows.size(); if (state != null) { connection.send(score); connection.send((double) fullRows.size()); } findShadow(); makeNewPiece(); repaint(); } }
private void makeNewPiece() { if (!paused) { randNum = (int) (Math.random() * 7); currentX = findXCoords(randNum); currentY = findYCoords(randNum); turnAmount = 0; findShadow(); boolean possibility = true; for (int i = 0; i < 4; i++) { if (!attemptMove(currentX[i], currentY[i])) { possibility = false; } } if (state != null && !possibility) { timer.stop(); connection.send("KO"); start(); } } }