Commit a9d7d0e7 authored by Gaston Comba Ures's avatar Gaston Comba Ures
Browse files

choringa

parent 6b5020a8
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="jmetal-auto" />
<module name="jmetal-core" />
<module name="TareaFinal" />
<module name="jmetal-problem" />
<module name="jmetal-example" />
<module name="jmetal-algorithm" />
<module name="jmetal-lab" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="snake" target="16" />
</bytecodeTargetLevel>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-algorithm/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-algorithm/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-auto/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-auto/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-core/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-core/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-example/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-lab/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-lab/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-problem/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../jMetal-jmetal-5.10/jmetal-problem/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
<option value="$PROJECT_DIR$/../jMetal-jmetal-5.10/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" default="true" project-jdk-name="openjdk-16" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
-1.0
0 1 0 1 1 1 0 1 0 0 1 0 0 1 0 1 0 0 2 0 3 1 0 0 3 0 1 3 0 0 2 0 0 2 0 0 1 0 0 0 0 1 0 1 0 1 1 0 1 3 0 3 0 0 0 2 0 1 0 1 0 2 0 0 2 2 0 0 2 1 0 0 0 0 2 0 2 0 1 1 2 0 1 2 0 3 0 1 0 1 0 0 0 0 0 2 0 3 0 1 2 0 3 2 1 2 0 1 0 1 3 0 0 1 3 0 0 2 2 2 0 2 1 1 0 0 1 1 2 0 1 0 0 0 1 3 0 0 0 0 2 3 2 0 2 3 0 1 0 0 0 3 2 0 0 0 2 1 0 0 0 3 3 2 0 3 2 2 0 0 1 0 0 0 0 0 0 0 0 0 1 3 2 0 0 2 0 0 0 0 0 2 0 0 0 0 1 0 0 1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2021-11-01T23:32:56.556007Z</date>
<millis>1635809576556</millis>
<nanos>7000</nanos>
<sequence>0</sequence>
<logger>org.uma.jmetal.util.JMetalLogger</logger>
<level>INFO</level>
<class>org.uma.jmetal.util.JMetalLogger</class>
<method>configureLoggers</method>
<thread>1</thread>
<message>Loggers configured with null</message>
</record>
<record>
<date>2021-11-01T23:32:56.609006700Z</date>
<millis>1635809576609</millis>
<nanos>6700</nanos>
<sequence>1</sequence>
<logger>org.uma.jmetal.util.JMetalLogger</logger>
<level>INFO</level>
<class>TareaFinal.TareaFinalRunner</class>
<method>main</method>
<thread>1</thread>
<message>Total execution time: 23186ms</message>
</record>
<record>
<date>2021-11-01T23:32:56.612002100Z</date>
<millis>1635809576612</millis>
<nanos>2100</nanos>
<sequence>2</sequence>
<logger>org.uma.jmetal.util.JMetalLogger</logger>
<level>INFO</level>
<class>TareaFinal.TareaFinalRunner</class>
<method>main</method>
<thread>1</thread>
<message>Fitness: -101.6</message>
</record>
<record>
<date>2021-11-01T23:32:56.613002300Z</date>
<millis>1635809576613</millis>
<nanos>2300</nanos>
<sequence>3</sequence>
<logger>org.uma.jmetal.util.JMetalLogger</logger>
<level>INFO</level>
<class>TareaFinal.TareaFinalRunner</class>
<method>main</method>
<thread>1</thread>
<message>Solution: [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 2, 2, 0, 1, 0, 1, 2, 0, 3, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 1, 0, 1, 2, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 3, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 1]</message>
</record>
</log>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>TareaFinal</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.uma.jmetal</groupId>
<artifactId>jmetal-core</artifactId>
<version>5.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>snake</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.uma.jmetal</groupId>
<artifactId>jmetal-example</artifactId>
<version>5.10</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
package Snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Game {
static final int SCREEN_WIDTH = 1300;
static final int SCREEN_HEIGHT = 750;
static final int UNIT_SIZE = 50;
static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
final int x[] = new int[GAME_UNITS];
final int y[] = new int[GAME_UNITS];
int bodyParts = 6;
int applesEaten;
int[][] apples;
int appleX;
int appleY;
boolean running = false;
int movementCounter;
int direction = 1;
public Game() {
this.movementCounter = 0;
}
public int[] Simulate(List<Integer> movements, int[][] apples) {
this.apples = apples;
newApple();
running = true;
while (running && movementCounter < movements.size()) {
move(movements.get(movementCounter));
checkApple();
checkCollisions();
}
int[] result = new int[2];
result[0] = applesEaten;
result[1] = movementCounter;
return result;
}
public void newApple() {
if(applesEaten < apples.length) {
appleX = apples[applesEaten][0];
appleY = apples[applesEaten][1];
}else{
running = false;
}
}
public void move(int moveDirection) {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
if(moveDirection != 0 ){
direction = moveDirection;
}
switch (direction) {
case 1://ARRIBA
y[0] = y[0] - UNIT_SIZE;
case 2://IZQUIERDA
x[0] = x[0] - UNIT_SIZE;
case 3://ABAJO
y[0] = y[0] + UNIT_SIZE;
break;
case 4://DERECHA
x[0] = x[0] + UNIT_SIZE;
break;
}
movementCounter++;
}
public void checkApple() {
if ((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
applesEaten++;
newApple();
}
}
public void checkCollisions() {
//checks if head collides with body
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
}
}
//check if head touches left border
if (x[0] < 0) {
running = false;
}
//check if head touches right border
if (x[0] > SCREEN_WIDTH) {
running = false;
}
//check if head touches top border
if (y[0] < 0) {
running = false;
}
//check if head touches bottom border
if (y[0] > SCREEN_HEIGHT) {
running = false;
}
}
}
package Snake;
import javax.swing.*;
import java.util.Arrays;
import java.util.List;
public class GameFrame extends JFrame {
GameFrame() {
int[][] apples = new int[5][2];
// apples[0][0] = 300;
// apples[0][1] = 350;
//
// apples[1][0] = 300;
// apples[1][1] = 400;
//
// apples[2][0] = 300;
// apples[2][1] = 500;
//
// apples[3][0] = 300;
// apples[3][1] = 600;
//
// apples[4][0] = 300;
// apples[4][1] = 700;
apples[0][0] = 300;
apples[0][1] = 350;
apples[1][0] = 600;
apples[1][1] = 600;
apples[2][0] = 100;
apples[2][1] = 350;
apples[3][0] = 400;
apples[3][1] = 500;
apples[4][0] = 1100;
apples[4][1] = 700;
// List<Integer> movements = Arrays.asList(3, 4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0);
List<Integer> movements = Arrays.asList(4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 3, 1, 1, 0, 0, 0, 0);
this.add(new GamePanel(apples, movements));
this.setTitle("Snake");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
\ No newline at end of file
package Snake;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GamePanel extends JPanel implements ActionListener {
static final int SCREEN_WIDTH = 1350;
static final int SCREEN_HEIGHT = 800;
static final int UNIT_SIZE = 50;
static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
static final int DELAY = 800;
final int x[] = new int[GAME_UNITS];
final int y[] = new int[GAME_UNITS];
int bodyParts = 6;
int applesEaten;
int appleX;
int appleY;
boolean running = false;
Timer timer;
Random random;
int movementCounter;
int[][] apples;
List<Integer> movements;
int direction = 1;
GamePanel(int[][] apples, List<Integer> movements) {
random = new Random();
this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
this.setBackground(Color.black);
this.setFocusable(true);
this.apples = apples;
this.movements = movements;
startGame();
}
public void startGame() {
newApple();
running = true;
timer = new Timer(DELAY, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (running) {
/*
for(int i=0;i<SCREEN_HEIGHT/UNIT_SIZE;i++) {
g.drawLine(i*UNIT_SIZE, 0, i*UNIT_SIZE, SCREEN_HEIGHT);
g.drawLine(0, i*UNIT_SIZE, SCREEN_WIDTH, i*UNIT_SIZE);
}
*/
g.setColor(Color.red);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.green);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
g.setColor(new Color(45, 180, 0));
//g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
g.setColor(Color.red);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score: " + applesEaten, (SCREEN_WIDTH - metrics.stringWidth("Score: " + applesEaten)) / 2, g.getFont().getSize());
} else {
gameOver(g);
}
}
public void newApple() {
if(applesEaten < apples.length) {
appleX = apples[applesEaten][0];
appleY = apples[applesEaten][1];
}else{
running = false;
}
}
public void move() {
int moveDirection = GetMovement();
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
if(moveDirection != 0 ){
direction = moveDirection;
}
switch (direction) {
case 1://ARRIBA
y[0] = y[0] - UNIT_SIZE;
case 2://IZQUIERDA
x[0] = x[0] - UNIT_SIZE;
case 3://ABAJO
y[0] = y[0] + UNIT_SIZE;
break;
case 4://DERECHA
x[0] = x[0] + UNIT_SIZE;
break;
}
movementCounter++;
}
public void checkApple() {
if ((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
applesEaten++;
newApple();
}
}
public void checkCollisions() {
//checks if head collides with body
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
}
}
//check if head touches left border
if (x[0] < 0) {
running = false;
}
//check if head touches right border
if (x[0] > SCREEN_WIDTH) {
running = false;
}
//check if head touches top border
if (y[0] < 0) {
running = false;
}
//check if head touches bottom border
if (y[0] > SCREEN_HEIGHT) {
running = false;
}
if (!running) {
timer.stop();
}
}
public void gameOver(Graphics g) {
//Score
g.setColor(Color.red);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
FontMetrics metrics1 = getFontMetrics(g.getFont());
g.drawString("Score: " + applesEaten, (SCREEN_WIDTH - metrics1.stringWidth("Score: " + applesEaten)) / 2, g.getFont().getSize());
//Game Over text
g.setColor(Color.red);
g.setFont(new Font("Ink Free", Font.BOLD, 75));
FontMetrics metrics2 = getFontMetrics(g.getFont());
g.drawString("Game Over", (SCREEN_WIDTH - metrics2.stringWidth("Game Over")) / 2, SCREEN_HEIGHT / 2);
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkApple();
checkCollisions();
}
repaint();
}
public int GetMovement() {
return movements.get(movementCounter);
}
}
\ No newline at end of file
package Snake;
import javax.swing.*;
public class SnakeGame {
public static void main(String[] args) {
var frame = new GameFrame();
}
}
\ No newline at end of file
package TareaFinal;
import Snake.Game;
import Snake.SnakeGame;
import org.uma.jmetal.problem.integerproblem.impl.AbstractIntegerProblem;
import org.uma.jmetal.solution.integersolution.IntegerSolution;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TareaFinalProblem extends AbstractIntegerProblem {
public TareaFinalProblem() throws FileNotFoundException {
this(100, 0, 4);
}
/**
* Constructor
*/
public TareaFinalProblem(int numberOfVariables, int lowerBound, int upperBound) {
setNumberOfVariables(numberOfVariables);