Skip to content

Instantly share code, notes, and snippets.

@kkiningh
Last active January 31, 2018 21:15
Show Gist options
  • Save kkiningh/08ebaeda851f279c28522e9ccc63b4b2 to your computer and use it in GitHub Desktop.
Save kkiningh/08ebaeda851f279c28522e9ccc63b4b2 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Game {
Random random = new Random();
int playerHealth = 10;
public void playTurn(Scanner reader) {
playMovement(reader);
int mon = random.nextInt(2);
if (mon == 0) {
System.out.println("A goblin appears");
playCombat(reader);
}
}
public void playMovement(Scanner reader) {
int exit = random.nextInt(3);
if (exit == 0) {
System.out.println("There is a door ahead of you");
} else if (exit == 1) {
System.out.println("There is a door ahead of you and to your right");
} else if (exit == 2) {
System.out.println("There is a door ahead of you, to your right and to your left");
}
System.out.println("Where do you want to go?");
while (true) {
String direction = reader.next();
if (direction.contains("forward")) {
System.out.println("you go forward");
break;
} else if (direction.contains("right") && exit > 0) {
System.out.println("you go right");
break;
} else if (direction.contains("left") && exit == 2) {
System.out.println("you go left");
break;
} else if (direction.contains("end")) {
System.out.println("Thanks for playing");
System.exit(0);
} else {
System.out.println("that is not an option");
}
}
}
public void playCombat(Scanner reader) {
int monsterHealth = 10;
while (true) {
System.out.println("What will you do?");
while (true) {
String combat = reader.next();
if (combat.contains("attack")) {
break;
} else {
System.out.println("Not an option");
}
}
int playerRoll = random.nextInt(20);
if (playerRoll >= 10) {
System.out.println("You hit the Goblin");
monsterHealth--;
} else {
System.out.println("You miss");
}
if(monsterHealth == 0){
System.out.println("The goblin dies");
return;
}
int monsterRoll = random.nextInt();
if (monsterRoll >= 15) {
playerHealth--;
System.out.println("The goblin hits you, you take 1 damage, you have " + playerHealth + " health");
} else {
System.out.println("The goblin misses");
}
System.out.println(playerHealth);
if(playerHealth == 0){
System.out.println("You die");
System.exit(0);
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
Game game = new Game();
System.out.println("Welcome to v1 game");
while (true) {
game.playTurn(reader);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment