Skip to content

Commit

Permalink
Merge pull request #24 from vlasival/task_2_3_1
Browse files Browse the repository at this point in the history
task_2_3_1
  • Loading branch information
vlasival committed Jun 8, 2024
2 parents bfa3855 + 0ca66d7 commit 475d939
Show file tree
Hide file tree
Showing 27 changed files with 1,615 additions and 0 deletions.
9 changes: 9 additions & 0 deletions task_2_3_1/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

9 changes: 9 additions & 0 deletions task_2_3_1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build

bin

.idea
62 changes: 62 additions & 0 deletions task_2_3_1/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.6/userguide/building_java_projects.html in the Gradle documentation.
*/

plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
id 'org.openjfx.javafxplugin' version '0.1.0'
id 'jacoco'
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// Use JUnit Jupiter for testing.
testImplementation libs.junit.jupiter

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// This dependency is used by the application.
implementation libs.guava

compileOnly 'org.projectlombok:lombok:1.18.32'
annotationProcessor 'org.projectlombok:lombok:1.18.32'

implementation 'com.google.code.gson:gson:2.10.1'
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

application {
// Define the main class for the application.
mainClass = 'org.snake.SnakeGame'
}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

jacocoTestReport {
reports {
xml.required = true
}
}

javafx {
version = "22.0.1"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}

16 changes: 16 additions & 0 deletions task_2_3_1/app/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module org.snake {
requires javafx.controls;
requires javafx.fxml;
requires com.google.gson;
requires transitive javafx.graphics;


requires static lombok;
requires javafx.base;

opens org.snake.utils to javafx.graphics;
exports org.snake.utils;

opens org.snake.model to javafx.fxml;
exports org.snake.model;
}
18 changes: 18 additions & 0 deletions task_2_3_1/app/src/main/java/org/snake/SnakeGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.snake;

import org.snake.utils.SceneController;

/**
* Entry point to the application.
*/
public class SnakeGame {

/**
* Entry point.
*
* @param args args
*/
public static void main(String[] args) {
SceneController.launchGame();
}
}
39 changes: 39 additions & 0 deletions task_2_3_1/app/src/main/java/org/snake/logger/MyLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.snake.logger;

/**
* Simple dumb logger.
*/
public class MyLogger {
/**
* Information log.
*
* @param message specific message
* @param cls class where log was called
*/
public static void info(String message, Class<?> cls) {
System.out.println("INFO: " + cls.getName()
+ " " + message);
}

/**
* Warning log.
*
* @param message specific message
* @param cls class where log was called
*/
public static void warn(String message, Class<?> cls) {
System.out.println("WARNING: in class " + cls.getName()
+ " " + message);
}

/**
* Error log.
*
* @param message specific message
* @param cls class where log was called
*/
public static void err(String message, Class<?> cls) {
System.out.println("ERROR: in class " + cls.getName()
+ "\n" + message);
}
}
42 changes: 42 additions & 0 deletions task_2_3_1/app/src/main/java/org/snake/model/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.snake.model;

import java.util.Arrays;
import java.util.List;
import lombok.Getter;

/**
* Represents any possible movement direction in the game.
*/
public enum Direction {
UP(0, -1),
RIGHT(1, 0),
DOWN(0, 1),
LEFT(-1, 0);

@Getter
private int directionX;
@Getter
private int directionY;

/**
* Private constructor to set parameters.
*
* @param dirX parameter for x
* @param dirY parameter for y
*/
private Direction(int dirX, int dirY) {
this.directionX = dirX;
this.directionY = dirY;
}

/**
* Return for this direction it's opposite.
*
* @return opposite direction
*/
Direction opposite() {
List<Direction> values = Arrays.asList(Direction.values());
int index = (values.indexOf(this) + 2) % 4;
return values.get(index);
}
}
45 changes: 45 additions & 0 deletions task_2_3_1/app/src/main/java/org/snake/model/Element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.snake.model;

import java.util.List;
import lombok.Getter;
import lombok.Setter;

/**
* Represents a basic element in the Snake game, with x and y coordinates.
*/
public class Element {

@Getter
@Setter
private int xcord;
@Getter
@Setter
private int ycord;

/**
* Initializes a new element at the specified position.
*
* @param x the x-coordinate of the element
* @param y the y-coordinate of the element
*/
public Element(int x, int y) {
this.xcord = x;
this.ycord = y;
}

/**
* Checks collision of this element and list of another elements.
*
* @param elements list of elements
* @return true if collides, false othrewise
*/
public boolean checkCollisionWithOthers(List<Element> elements) {
for (Element e : elements) {
if (e.getXcord() == this.getXcord() && e.getYcord() == this.getYcord()) {
return true;
}
}
return false;
}

}
33 changes: 33 additions & 0 deletions task_2_3_1/app/src/main/java/org/snake/model/Food.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.snake.model;

import java.util.Random;

/**
* Represents a food object in the Snake game.
*/
public class Food extends Element {

private Random random = new Random();
private int width;
private int height;

/**
* Initializes a new food object and set field config.
*
* @param width the width of the field
* @param height the width of the field
*/
public Food(int width, int height) {
super(0, 0);
this.width = width;
this.height = height;
}

/**
* Generates a new random position for the food within the specified width and height.
*/
public void generateNew() {
setXcord(random.nextInt(width));
setYcord(random.nextInt(height));
}
}
84 changes: 84 additions & 0 deletions task_2_3_1/app/src/main/java/org/snake/model/GameModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.snake.model;

import lombok.Getter;
import lombok.Setter;
import org.snake.logger.MyLogger;

/**
* Business model of snake game.
*/
public class GameModel {

public boolean isGameOvered = false;

@Getter
private Snake playerSnake;
@Getter
private Snake gameSnake;
@Getter
private Food food;
private Intelligence intelligence;
@Getter
@Setter
private boolean moving = false;

/**
* GameModel constructor.
*
* @param width with of the firld
* @param height height of the firld
*/
public GameModel(int width, int height) {
playerSnake = new Snake(width, height);
gameSnake = new Snake(width, height);
gameSnake.getHead().setYcord(height - 1);
food = new Food(width, height);
food.generateNew();
intelligence = new Intelligence(gameSnake, playerSnake, food);
}

/**
* Updates the game state.
*/
public void update() {
if (isGameOvered) {
return;
}
moving = false;
playerSnake.move();
intelligence.manageGameSnake();
gameSnake.move();
if (playerSnake.checkHeadCollisionWithItsBody()
|| playerSnake.checkCollisionWithHead(gameSnake.getBody())) {
gameOver();
return;
}
if (gameSnake.checkHeadCollisionWithItsBody()
|| gameSnake.checkCollisionWithHead(playerSnake.getBody())) {
gameSnake = new Snake(playerSnake.getWidth(), playerSnake.getHeight());
MyLogger.info("Game snake was stain. But it'll be back", getClass());
}
if (playerSnake.checkCollisionWithHead(food)) {
playerSnake.growUp();
do {
food.generateNew();
} while (playerSnake.checkCollisionWithBody(food));
MyLogger.info("Player snake grown up", getClass());
}
if (gameSnake.checkCollisionWithHead(food)) {
gameSnake.growUp();
do {
food.generateNew();
} while (gameSnake.checkCollisionWithBody(food));
MyLogger.info("Game snake grown up", getClass());
}
}

/**
* Calls if player's snake was died.
*/
private void gameOver() {
isGameOvered = true;
MyLogger.info("Game over XD", getClass());
}
}
Loading

0 comments on commit 475d939

Please sign in to comment.