Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game finished - without considerations reference #1

Open
wants to merge 3 commits into
base: rafael.vanat
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.next
release.properties
output
.project
.settings
.classpath
*.log
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,22 @@ challenge-blind-warrior
=======================

A set of Blind Warrior implementations, a bunch of impressive ideas

## Objective
This a repository of implementations of [Blind Warrior OOP Game](https://gist.github.com/miere/f6b10b0e1ef433f6cf14).

This is a nice game to apply some basic principles of OOP.

## Enjoy this game
Do you want to enjoy this funny codding game?
* Fork this repo!
* Create a branch with your git user name
* Commit your code there
* Send us a pull request!

The intent of this game is to improve your codding skils. Especially OOP!
No one will analyze you code, unless you explicitly ask us for this. If you want some
experience OOP developer to help you to improve your code, you have the following options:
* Participate our codding hour on Thurstdays night - 19:30 ( UTC-3 )
* Open an issue on this repository asking for help ( please inform you Skype/Hangout address ). We will contact you as soon as possible.

139 changes: 139 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.skullabs.trainning.blindwarrior</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>rafael-vanat</artifactId>
<name>Blind Warrior: Rafael Vanat</name>
<packaging>jar</packaging>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<dependencies>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>${project.basedir}/source</sourceDirectory>
<testSourceDirectory>${project.basedir}/tests</testSourceDirectory>
<directory>${project.basedir}/output</directory>
<outputDirectory>${project.basedir}/output/classes</outputDirectory>
<testOutputDirectory>${project.basedir}/output/test-classes</testOutputDirectory>
<resources>
<resource>
<directory>${project.basedir}/source</directory>
<includes>
<include>**/*.*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/tests</directory>
<includes>
<include>**/*.*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<optimize>true</optimize>
<debug>false</debug>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>br.vanat.trainning.Game</mainClass>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<systemPropertyVariables>
<log4j.configuration>file:${project.build.testOutputDirectory}/resources/log4j.properties</log4j.configuration>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<!--<phase>process-sources</phase>-->
<phase>install</phase>

<goals>
<goal>copy-dependencies</goal>
</goals>

<configuration>
<outputDirectory>${project.basedir}/output/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
37 changes: 37 additions & 0 deletions source/br/vanat/trainning/Character.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package br.vanat.trainning;

import org.apache.log4j.Logger;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

@Data
public abstract class Character {
private static final Logger logger = Logger.getLogger(Character.class);

@Setter(AccessLevel.NONE) private int health;
private String name;
private Weapon weapon;
private boolean alive;

public Character(){
this.setInitialHealth();
this.setName(this.getClass().getSimpleName() + this.hashCode());
this.setAlive(true);
}

public void fight(Character enemy) throws Exception{
enemy.lowerHealth(this.getWeapon().getDamage());
enemy.setAlive((enemy.getHealth() > 0));
logger.info(this.getName() + " damages " + enemy.getName() + ". Damage taken = " + this.getWeapon().getDamage() + ". Remaing health = " + enemy.getHealth());
}

private void setInitialHealth(){
this.health = 100;
}

protected void lowerHealth(int damage) throws Exception {
this.health = this.health - damage;
}
}
30 changes: 30 additions & 0 deletions source/br/vanat/trainning/Citizen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package br.vanat.trainning;

public class Citizen extends Enemy{

public Citizen(){
super();
Weapon weapon = new Weapon(0, 0);
this.setWeapon(weapon);
}

@Override
public void fight(Character enemy) throws Exception{
if(!(enemy instanceof Player)){
super.fight(enemy);
}
}

@Override
public void setWeapon(Weapon weapon) {
super.setWeapon(new Weapon(0, weapon.getTimesUsed(), weapon.getUsageLimit()));
}

@Override
protected void lowerHealth(int damage) throws Exception{
super.lowerHealth(damage);
if(this.getHealth() <= 0){
throw new Exception("Zilska killed an inocent citizen!");
}
}
}
25 changes: 25 additions & 0 deletions source/br/vanat/trainning/Enemy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package br.vanat.trainning;

import java.util.Random;

public class Enemy extends Character{

public Enemy(){
super();
Random rand = new Random();
Weapon weapon = new Weapon(rand.nextInt(60), 0);
this.setWeapon(weapon);
}

@Override
public void setWeapon(Weapon weapon) {
Weapon newWeapon = weapon;
if(weapon.getDamage() > 59){
newWeapon = new Weapon(59, weapon.getTimesUsed(), weapon.getUsageLimit());
}
else if(weapon.getDamage() == 0 && this instanceof Enemy){
newWeapon = new Weapon(1, weapon.getTimesUsed(), weapon.getUsageLimit());
}
super.setWeapon(newWeapon);
}
}
97 changes: 97 additions & 0 deletions source/br/vanat/trainning/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package br.vanat.trainning;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import lombok.Getter;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Game {
private static final Logger logger = Logger.getLogger(Game.class);
private static final int TOTAL_ENEMIES = (GameParam.CITIZEN_NUMBER + GameParam.ENEMY_NUMBER);

@Getter private List<Enemy> deadEnemies = new ArrayList<Enemy>();

public Player createPlayer(){
return new Player();
}

public List<Enemy> createEnemyList(){
List<Enemy> enemies = new ArrayList<Enemy>();
for(int totalEnemies = TOTAL_ENEMIES; totalEnemies > 0; totalEnemies--){
if(totalEnemies % GameParam.CITIZEN_NUMBER == 0){
enemies.add(new Citizen());
}
else{
enemies.add(new Enemy());
}
}
return enemies;
}

public Exception newGame(){
Player zilska = createPlayer();
List<Enemy> enemies = createEnemyList();
Exception endClause = null;
try{
battle(zilska, enemies);
}
catch (Exception e) {
endClause = e;
logger.info(e.getMessage());
}
finally{
battleResult(endClause);
}
return endClause;
}

private void battleResult(Exception e) {
logger.info("BATTLE HAS COME TO AN END. SEE THE RESULTS");
logger.info("END REASON = " + ((e != null) ? e.getMessage() : ""));
logger.info("DEAD ENEMIES = " + this.getDeadEnemies().size());
}

private void battle(Player zilska, List<Enemy> enemies) throws Exception{
int totalEnemiesFought = 0;
Random rand = new Random();
while(totalEnemiesFought < TOTAL_ENEMIES){
int index = rand.nextInt(TOTAL_ENEMIES);
if(enemies.get(index).isAlive()){
this.fight(zilska, enemies.get(index));
totalEnemiesFought++;
}
}
}

private void fight(Player zilska, Enemy enemy) throws Exception {
do {
zilska.fight(enemy);
if(enemy.isAlive()){
enemy.fight(zilska);
}
} while(enemy.isAlive());
this.getDeadEnemies().add(enemy);
}

public static void main(String args[]) {
PropertyConfigurator.configure(getLog4jProperties());
Game game = new Game();
game.newGame();
}

public static Properties getLog4jProperties(){
Properties props = new Properties();
try {
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/log4j.properties"));
} catch (IOException e) {
e.printStackTrace();
}
return props;
}
}
7 changes: 7 additions & 0 deletions source/br/vanat/trainning/GameParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package br.vanat.trainning;


public class GameParam {
public static final int CITIZEN_NUMBER = 5;
public static final int ENEMY_NUMBER = 20;
}
Loading