void readEdges(Scanner in, int count, boolean bridges) { for (int i = 0; i < count; i++) { Island land1 = islands[in.nextInt() - 1]; Island land2 = islands[in.nextInt() - 1]; land1.getList(bridges).add(land2); land2.getList(bridges).add(land1); } }
/** Checks all collisions between collidables */ public void checkCollisions() { ArrayList<Bullet> bulletList = boat .getBulletsList(); // fire methodu çağıtıldığında boatun içinde oluşan bulletların // listesi Rectangle boatBound = boat.getBounds(); // boat ve ada çarpışması for (int i = 0; i < islands.size(); i++) { Island is = (Island) islands.get(i); Rectangle islandBound = is.getBounds(); if (boatBound.intersects(islandBound)) { boat.setVisible(false); } } // boat ve buoy çarpışması for (int i = 0; i < buoys.size(); i++) { Buoy b = (Buoy) buoys.get(i); Rectangle buoyBound = b.getBounds(); if (boatBound.intersects(buoyBound)) { b.setVisible(false); // timer.stop(); } } // boat ve uncracked/cracked bonus çarpışması for (int i = 0; i < bonuses.size(); i++) { Bonus b = (Bonus) bonuses.get(i); Rectangle bonusBound = b.getBounds(); if (boatBound.intersects(bonusBound)) { if (!b.isCracked()) { // boat&uncracked bonus çarpışması boat.setVisible(false); } else { // boat&bonus çarpışması b.setVisible(false); } } } // bullet ve bonus(uncracked) çarpışması for (int i = 0; i < bulletList.size(); i++) { Bullet bu = (Bullet) bulletList.get(i); Rectangle bulletBound = bu.getBounds(); for (int k = 0; k < bonuses.size(); k++) { Bonus bo = (Bonus) bonuses.get(k); Rectangle bonusBound = bo.getBounds(); if (bonusBound.intersects(bulletBound) && !bo.isCracked()) { bo.setCracked(true); // bo.setBonusImage(bo.getBonusType());//burası çok önemli, bonus baştan yaratılıyor // sayılır. bo.setRandomBonusImage(); bu.setVisible(false); } } } }
private Island nextIslandLocation(Island lastIsland) { // Gets the next position of an Island based on the last one. // Generates new Islands in a spiral. int x = lastIsland.x; int z = lastIsland.z; Island nextPos = new Island(); nextPos.x = x; nextPos.z = z; if (x < z) { if (((-1) * x) < z) { nextPos.x = nextPos.x + plugin.getISLAND_SPACING(); return nextPos; } nextPos.z = nextPos.z + plugin.getISLAND_SPACING(); return nextPos; } if (x > z) { if (((-1) * x) >= z) { nextPos.x = nextPos.x - plugin.getISLAND_SPACING(); return nextPos; } nextPos.z = nextPos.z - plugin.getISLAND_SPACING(); return nextPos; } if (x <= 0) { nextPos.z = nextPos.z + plugin.getISLAND_SPACING(); return nextPos; } nextPos.z = nextPos.z - plugin.getISLAND_SPACING(); return nextPos; }
private int sortIslandToMoveIn(final Island o1, final Island o2) { if ((o1.getPlayer() == -1) && (o2.getPlayer() == -1)) { if ((o1.getAllPositions().size() > 1) && (o2.getAllPositions().size() > 1)) return -((Integer) o1.getAllPositions().size()).compareTo(o2.getAllPositions().size()); else { if (o1.getAllPositions().size() > 1) return -1; if (o2.getAllPositions().size() > 1) return 1; } } else { if (o1.getPlayer() == -1) return -1; if (o2.getPlayer() == -1) return 1; } return 0; }
void solve(Scanner in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); int m = in.nextInt(); islands = new Island[n]; for (int i = 0; i < n; i++) { islands[i] = new Island(); } readEdges(in, k, false); readEdges(in, m, true); int bridgesUsed = 0; Queue<Island> bfs = new LinkedList<Island>(); Queue<Island> candidates = new LinkedList<Island>(); Island nextRoot = islands[0]; while (nextRoot != null) { bfs.add(nextRoot); nextRoot = null; while (!bfs.isEmpty()) { Island land = bfs.remove(); land.visited = true; for (Island other : land.tunnels) { if (!other.visited) { bfs.add(other); } } for (Island other : land.bridges) { if (!other.visited) { candidates.add(other); } } } while (!candidates.isEmpty()) { Island land = candidates.remove(); if (!land.visited) { nextRoot = land; bridgesUsed++; break; } } } out.println(bridgesUsed); }
@Override public GameCommand<StopGame> getMove(final StopGame game, final Participant player) throws GameException { if (game.getCurrentPhase() == GamePhase.Playing) { final int findPlayer = game.getPlayerManager().getPlayerIndex(player); final int otherPlayer = (0 == findPlayer) ? 1 : 0; final TokenArray tokenArray = game.getTokenArray(); final ArrayList<StopIsland> islands = tokenArray.getIslands(); Collections.sort( islands, new Comparator<Island>() { public int compare(final Island o1, final Island o2) { return sortIslandToMoveIn(o1, o2); } }); final Island bestIsland = islands.get(0); final ArrayList<ArrayPosition> allPositions = bestIsland.getAllPositions(); Collections.sort( allPositions, new Comparator<ArrayPosition>() { public int compare(final ArrayPosition o1, final ArrayPosition o2) { return sortPositionToMoveIn(otherPlayer, tokenArray, o1, o2); } }); for (int i = 0; i < allPositions.size(); i++) { final ArrayPosition bestPosition = allPositions.get(0); return new GameCommand<StopGame>() { @Override public CommandResult<StopGame> doCommand(Participant p, String parameters) throws GameException { StopGame newGame = game.doMove(new IndexPosition(bestPosition.row, bestPosition.col), player); return new CommandResult<StopGame>(newGame); } }; } } else { LOG.warning("Cannot move in phase: " + game.getCurrentPhase()); } throw new SawdustSystemError("No move?"); }
public List<Integer> numIslands2(int m, int n, int[][] positions) { HashSet<Island> islands = new HashSet<>(); List<Integer> numsLst = new LinkedList<>(); Island[][] map = new Island[m][n]; for (int[] pos : positions) { Island curIsland = new Island(pos[0], pos[1]); map[pos[0]][pos[1]] = curIsland; for (int i = 0; i < 4; i++) { int x = pos[0] + DIR[i]; int y = pos[1] + DIR[i + 1]; if (x >= 0 && x < map.length && y >= 0 && y < map[0].length && map[x][y] != null) { Island mergedIsland = map[x][y].merge(curIsland); islands.remove(mergedIsland); } } islands.add(curIsland.getRoot()); numsLst.add(islands.size()); } return numsLst; }
public Island merge(Island other) { Island largeIsland = this.getRoot(); Island smallIsland = other.getRoot(); if (largeIsland.size < smallIsland.size) { largeIsland = other.getRoot(); smallIsland = this.getRoot(); } largeIsland.size += smallIsland.size; smallIsland.root = largeIsland; return smallIsland; }
public Beaver(World world, Island island) { this.island = island; float scale = 2.0f; float radius = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyDef.BodyType.DynamicBody; Vector2 pos = island.physicsBody.getPosition(); ang = island.physicsBody.getAngle(); float islandW = island.getPhysicsWidth(); float islandH = island.getPhysicsHeight(); Vector2 off = new Vector2(islandW * MathUtils.random(0.3f, 0.6f), islandH); off.rotate(MathUtils.radiansToDegrees * ang); bd.position.set(new Vector2(pos.x + off.x, pos.y + off.y)); bd.angle = ang; off.set(islandW * 0.075f, islandH); off.rotate(MathUtils.radiansToDegrees * ang); Vector2 rightEdge = new Vector2(pos.x + off.x, pos.y + off.y).scl(Mane.PTM_RATIO); off.set(islandW * 0.85f, islandH); off.rotate(MathUtils.radiansToDegrees * ang); Vector2 leftEdge = new Vector2(pos.x + off.x, pos.y + off.y).scl(Mane.PTM_RATIO); float speed = MathUtils.random(1.0f, 3.0f); addAction( Actions.delay( MathUtils.random(0.0f, 1.0f), Actions.forever( Actions.sequence( Actions.moveTo(rightEdge.x, rightEdge.y, speed), Actions.moveTo(leftEdge.x, leftEdge.y, speed))))); bd.linearDamping = 0.2f; Body body = world.createBody(bd); FixtureDef fd = new FixtureDef(); fd.density = 1.0f; fd.filter.categoryBits = Collision.BEAVER; fd.filter.maskBits = Collision.SHARK; fd.restitution = 0.0f; fd.friction = 0.0f; fd.isSensor = true; CircleShape cs = new CircleShape(); cs.setRadius(radius); cs.setPosition(new Vector2(radius, radius)); fd.shape = cs; body.createFixture(fd); cs.dispose(); super.initPhysicsBody(body); setSize(scale * Mane.PTM_RATIO, scale * Mane.PTM_RATIO); setOrigin(0.0f, 0.0f); }
private void solve(TimeStep step) { m_profile.solveInit = 0; m_profile.solveVelocity = 0; m_profile.solvePosition = 0; // Size the island for the worst case. island.init( m_bodyCount, m_contactManager.m_contactCount, m_jointCount, m_contactManager.m_contactListener); // Clear all the island flags. for (Body b = m_bodyList; b != null; b = b.m_next) { b.m_flags &= ~Body.e_islandFlag; } for (Contact c = m_contactManager.m_contactList; c != null; c = c.m_next) { c.m_flags &= ~Contact.ISLAND_FLAG; } for (Joint j = m_jointList; j != null; j = j.m_next) { j.m_islandFlag = false; } // Build and simulate all awake islands. int stackSize = m_bodyCount; if (stack.length < stackSize) { stack = new Body[stackSize]; } for (Body seed = m_bodyList; seed != null; seed = seed.m_next) { if ((seed.m_flags & Body.e_islandFlag) == Body.e_islandFlag) { continue; } if (seed.isAwake() == false || seed.isActive() == false) { continue; } // The seed can be dynamic or kinematic. if (seed.getType() == BodyType.STATIC) { continue; } // Reset island and stack. island.clear(); int stackCount = 0; stack[stackCount++] = seed; seed.m_flags |= Body.e_islandFlag; // Perform a depth first search (DFS) on the constraint graph. while (stackCount > 0) { // Grab the next body off the stack and add it to the island. Body b = stack[--stackCount]; assert (b.isActive() == true); island.add(b); // Make sure the body is awake. b.setAwake(true); // To keep islands as small as possible, we don't // propagate islands across static bodies. if (b.getType() == BodyType.STATIC) { continue; } // Search all contacts connected to this body. for (ContactEdge ce = b.m_contactList; ce != null; ce = ce.next) { Contact contact = ce.contact; // Has this contact already been added to an island? if ((contact.m_flags & Contact.ISLAND_FLAG) == Contact.ISLAND_FLAG) { continue; } // Is this contact solid and touching? if (contact.isEnabled() == false || contact.isTouching() == false) { continue; } // Skip sensors. boolean sensorA = contact.m_fixtureA.m_isSensor; boolean sensorB = contact.m_fixtureB.m_isSensor; if (sensorA || sensorB) { continue; } island.add(contact); contact.m_flags |= Contact.ISLAND_FLAG; Body other = ce.other; // Was the other body already added to this island? if ((other.m_flags & Body.e_islandFlag) == Body.e_islandFlag) { continue; } assert (stackCount < stackSize); stack[stackCount++] = other; other.m_flags |= Body.e_islandFlag; } // Search all joints connect to this body. for (JointEdge je = b.m_jointList; je != null; je = je.next) { if (je.joint.m_islandFlag == true) { continue; } Body other = je.other; // Don't simulate joints connected to inactive bodies. if (other.isActive() == false) { continue; } island.add(je.joint); je.joint.m_islandFlag = true; if ((other.m_flags & Body.e_islandFlag) == Body.e_islandFlag) { continue; } assert (stackCount < stackSize); stack[stackCount++] = other; other.m_flags |= Body.e_islandFlag; } } island.solve(islandProfile, step, m_gravity, m_allowSleep); m_profile.solveInit += islandProfile.solveInit; m_profile.solveVelocity += islandProfile.solveVelocity; m_profile.solvePosition += islandProfile.solvePosition; // Post solve cleanup. for (int i = 0; i < island.m_bodyCount; ++i) { // Allow static bodies to participate in other islands. Body b = island.m_bodies[i]; if (b.getType() == BodyType.STATIC) { b.m_flags &= ~Body.e_islandFlag; } } } broadphaseTimer.reset(); // Synchronize fixtures, check for out of range bodies. for (Body b = m_bodyList; b != null; b = b.getNext()) { // If a body was not in an island then it did not move. if ((b.m_flags & Body.e_islandFlag) == 0) { continue; } if (b.getType() == BodyType.STATIC) { continue; } // Update fixtures (for broad-phase). b.synchronizeFixtures(); } // Look for new contacts. m_contactManager.findNewContacts(); m_profile.broadphase = broadphaseTimer.getMilliseconds(); }
private void solveTOI(final TimeStep step) { final Island island = toiIsland; island.init( 2 * Settings.maxTOIContacts, Settings.maxTOIContacts, 0, m_contactManager.m_contactListener); if (m_stepComplete) { for (Body b = m_bodyList; b != null; b = b.m_next) { b.m_flags &= ~Body.e_islandFlag; b.m_sweep.alpha0 = 0.0f; } for (Contact c = m_contactManager.m_contactList; c != null; c = c.m_next) { // Invalidate TOI c.m_flags &= ~(Contact.TOI_FLAG | Contact.ISLAND_FLAG); c.m_toiCount = 0; c.m_toi = 1.0f; } } // Find TOI events and solve them. for (; ; ) { // Find the first TOI. Contact minContact = null; float minAlpha = 1.0f; for (Contact c = m_contactManager.m_contactList; c != null; c = c.m_next) { // Is this contact disabled? if (c.isEnabled() == false) { continue; } // Prevent excessive sub-stepping. if (c.m_toiCount > Settings.maxSubSteps) { continue; } float alpha = 1.0f; if ((c.m_flags & Contact.TOI_FLAG) != 0) { // This contact has a valid cached TOI. alpha = c.m_toi; } else { Fixture fA = c.getFixtureA(); Fixture fB = c.getFixtureB(); // Is there a sensor? if (fA.isSensor() || fB.isSensor()) { continue; } Body bA = fA.getBody(); Body bB = fB.getBody(); BodyType typeA = bA.m_type; BodyType typeB = bB.m_type; assert (typeA == BodyType.DYNAMIC || typeB == BodyType.DYNAMIC); boolean activeA = bA.isAwake() && typeA != BodyType.STATIC; boolean activeB = bB.isAwake() && typeB != BodyType.STATIC; // Is at least one body active (awake and dynamic or kinematic)? if (activeA == false && activeB == false) { continue; } boolean collideA = bA.isBullet() || typeA != BodyType.DYNAMIC; boolean collideB = bB.isBullet() || typeB != BodyType.DYNAMIC; // Are these two non-bullet dynamic bodies? if (collideA == false && collideB == false) { continue; } // Compute the TOI for this contact. // Put the sweeps onto the same time interval. float alpha0 = bA.m_sweep.alpha0; if (bA.m_sweep.alpha0 < bB.m_sweep.alpha0) { alpha0 = bB.m_sweep.alpha0; bA.m_sweep.advance(alpha0); } else if (bB.m_sweep.alpha0 < bA.m_sweep.alpha0) { alpha0 = bA.m_sweep.alpha0; bB.m_sweep.advance(alpha0); } assert (alpha0 < 1.0f); int indexA = c.getChildIndexA(); int indexB = c.getChildIndexB(); // Compute the time of impact in interval [0, minTOI] final TOIInput input = toiInput; input.proxyA.set(fA.getShape(), indexA); input.proxyB.set(fB.getShape(), indexB); input.sweepA.set(bA.m_sweep); input.sweepB.set(bB.m_sweep); input.tMax = 1.0f; pool.getTimeOfImpact().timeOfImpact(toiOutput, input); // Beta is the fraction of the remaining portion of the . float beta = toiOutput.t; if (toiOutput.state == TOIOutputState.TOUCHING) { alpha = MathUtils.min(alpha0 + (1.0f - alpha0) * beta, 1.0f); } else { alpha = 1.0f; } c.m_toi = alpha; c.m_flags |= Contact.TOI_FLAG; } if (alpha < minAlpha) { // This is the minimum TOI found so far. minContact = c; minAlpha = alpha; } } if (minContact == null || 1.0f - 10.0f * Settings.EPSILON < minAlpha) { // No more TOI events. Done! m_stepComplete = true; break; } // Advance the bodies to the TOI. Fixture fA = minContact.getFixtureA(); Fixture fB = minContact.getFixtureB(); Body bA = fA.getBody(); Body bB = fB.getBody(); backup1.set(bA.m_sweep); backup2.set(bB.m_sweep); bA.advance(minAlpha); bB.advance(minAlpha); // The TOI contact likely has some new contact points. minContact.update(m_contactManager.m_contactListener); minContact.m_flags &= ~Contact.TOI_FLAG; ++minContact.m_toiCount; // Is the contact solid? if (minContact.isEnabled() == false || minContact.isTouching() == false) { // Restore the sweeps. minContact.setEnabled(false); bA.m_sweep.set(backup1); bB.m_sweep.set(backup2); bA.synchronizeTransform(); bB.synchronizeTransform(); continue; } bA.setAwake(true); bB.setAwake(true); // Build the island island.clear(); island.add(bA); island.add(bB); island.add(minContact); bA.m_flags |= Body.e_islandFlag; bB.m_flags |= Body.e_islandFlag; minContact.m_flags |= Contact.ISLAND_FLAG; // Get contacts on bodyA and bodyB. tempBodies[0] = bA; tempBodies[1] = bB; for (int i = 0; i < 2; ++i) { Body body = tempBodies[i]; if (body.m_type == BodyType.DYNAMIC) { for (ContactEdge ce = body.m_contactList; ce != null; ce = ce.next) { if (island.m_bodyCount == island.m_bodyCapacity) { break; } if (island.m_contactCount == island.m_contactCapacity) { break; } Contact contact = ce.contact; // Has this contact already been added to the island? if ((contact.m_flags & Contact.ISLAND_FLAG) != 0) { continue; } // Only add static, kinematic, or bullet bodies. Body other = ce.other; if (other.m_type == BodyType.DYNAMIC && body.isBullet() == false && other.isBullet() == false) { continue; } // Skip sensors. boolean sensorA = contact.m_fixtureA.m_isSensor; boolean sensorB = contact.m_fixtureB.m_isSensor; if (sensorA || sensorB) { continue; } // Tentatively advance the body to the TOI. backup1.set(other.m_sweep); if ((other.m_flags & Body.e_islandFlag) == 0) { other.advance(minAlpha); } // Update the contact points contact.update(m_contactManager.m_contactListener); // Was the contact disabled by the user? if (contact.isEnabled() == false) { other.m_sweep.set(backup1); other.synchronizeTransform(); continue; } // Are there contact points? if (contact.isTouching() == false) { other.m_sweep.set(backup1); other.synchronizeTransform(); continue; } // Add the contact to the island contact.m_flags |= Contact.ISLAND_FLAG; island.add(contact); // Has the other body already been added to the island? if ((other.m_flags & Body.e_islandFlag) != 0) { continue; } // Add the other body to the island. other.m_flags |= Body.e_islandFlag; if (other.m_type != BodyType.STATIC) { other.setAwake(true); } island.add(other); } } } subStep.dt = (1.0f - minAlpha) * step.dt; subStep.inv_dt = 1.0f / subStep.dt; subStep.dtRatio = 1.0f; subStep.positionIterations = 20; subStep.velocityIterations = step.velocityIterations; subStep.warmStarting = false; island.solveTOI(subStep, bA.m_islandIndex, bB.m_islandIndex); // Reset island flags and synchronize broad-phase proxies. for (int i = 0; i < island.m_bodyCount; ++i) { Body body = island.m_bodies[i]; body.m_flags &= ~Body.e_islandFlag; if (body.m_type != BodyType.DYNAMIC) { continue; } body.synchronizeFixtures(); // Invalidate all contact TOIs on this displaced body. for (ContactEdge ce = body.m_contactList; ce != null; ce = ce.next) { ce.contact.m_flags &= ~(Contact.TOI_FLAG | Contact.ISLAND_FLAG); } } // Commit fixture proxy movements to the broad-phase so that new contacts are created. // Also, some contacts can be destroyed. m_contactManager.findNewContacts(); if (m_subStepping) { m_stepComplete = false; break; } } }
public Island getRoot() { if (root == this) { return root; } return root.getRoot(); }
// private void initLevel(int level){//initializes levels according to the level parameter // // } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; // Windmill drawing g2d.rotate(wind.getDirection() * Math.PI / 180, 636, 64); g2d.drawImage(wind.getImage(), 572, 0, this); g2d.rotate(-wind.getDirection() * Math.PI / 180, 636, 64); // Boat drawing if (boat.isVisible()) { g2d.rotate( TIGHT_TURN_FACTOR * boat.getDirection() * Math.PI / 180, boat.getX() + 16, boat.getY() + 16); // g2d.drawImage(boat.getImage(), boat.getX(), boat.getY(), this); g2d.drawImage(boat.getImage(), boat.getX(), boat.getY(), this); // TEST-sınırları görmek için // Rectangle r1 = boat.getBounds(); // g2d.drawRect(r1.x, r1.y, r1.width, r1.height); g2d.rotate( -TIGHT_TURN_FACTOR * boat.getDirection() * Math.PI / 180, boat.getX() + 16, boat.getY() + 16); if ((buoys.size() == 0) && (currentLevel <= LAST_LEVEL)) { // level atlama ve başarılı bir şekilde oyunu bitirme burada olacak System.out.println("All buoys finished, well done!"); int tp = computeTotalScore(); System.out.println("Total Score: " + tp); nextLevelFlag = true; // Actionlistenera tek seferlik girebilmek için bir flag // System.exit(0); } } else { // If boat is dead then check for remaining lives if (boat.getNumOfLives() > 0) { boat.decrementNumOfLives(); boat.setPosition(0, 20); boat.setDirection(0); boat.setVisible(true); } else { // No remaining lives case // g2d.drawString("Game Over!", 80, 15); System.out.println("Game Over"); int tp = computeTotalScore(); System.out.println("Total Score: " + tp); currentLevel = 1; CardLayout c2 = (CardLayout) (mgf.getCanvas().getLayout()); c2.show(mgf.getCanvas(), "GUI"); // System.exit(0); } } // Island drawing for (int i = 0; i < islands.size(); i++) { Island is = (Island) islands.get(i); g2d.drawImage(is.getImage(), is.getX(), is.getY(), this); } // Bullet drawing ArrayList<Bullet> bs = boat.getBulletsList(); for (int i = 0; i < bs.size(); i++) { Bullet b = (Bullet) bs.get(i); g2d.drawImage(b.getImage(), b.getX(), b.getY(), this); } // Buoy drawing for (int i = 0; i < buoys.size(); i++) { Buoy b = (Buoy) buoys.get(i); if (b.isVisible()) { g2d.drawImage(b.getImage(), b.getX(), b.getY(), this); } } // Bonus drawing for (int i = 0; i < bonuses.size(); i++) { Bonus b = (Bonus) bonuses.get(i); if (b.isVisible()) { g2d.drawImage(b.getImage(), b.getX(), b.getY(), this); } else { if (b.getBonusType().equalsIgnoreCase("lifebonus")) { boat.incrementNumOfLives(); } if (b.getBonusType().equalsIgnoreCase("speedbonus")) { boat.setFlashForward(true); } if (b.getBonusType().equalsIgnoreCase("bulletbonus")) { boat.addBonusBullets(); } } } // Information g2d.drawString("Buoys left: " + buoys.size(), 5, 15); if (boat.getNumOfLives() == 0) g2d.drawString("Last Chance!", 80, 15); else g2d.drawString("Lives: " + boat.getNumOfLives(), 90, 15); // g2d.drawString("-" + (int)System.currentTimeMillis()/1000, 5, 25); g2d.drawString("Score: " + gamePoint, 160, 15); g2d.drawString("Bullets: " + boat.getRemainingNumOfBullets(), 235, 15); g2d.drawString("Level: " + currentLevel, 320, 15); // Generates new random wind wind.generateWindDirection(); // Default system methods Toolkit.getDefaultToolkit().sync(); // ? g.dispose(); // ? }