-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
90 lines (75 loc) · 3 KB
/
Game.java
File metadata and controls
90 lines (75 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.Scanner;
public class Game {
public Game(){
int prow = (int)(Math.random()*5);
int pcol = (int)(Math.random()*5);
int erow,ecol;
int exitrow,exitcol;
int count=2;
boolean win = true;
boolean loss = true;
Scanner sc = new Scanner(System.in);
Player player = new Player(prow, pcol);
do{
erow = (int)(Math.random()*5);
ecol = (int)(Math.random()*5);
}while(prow == erow && pcol == ecol);
Enemy enemy = new Enemy(erow,ecol);
do{
exitrow = (int)(Math.random()*5);
exitcol = (int)(Math.random()*5);
}while(prow == exitrow && pcol == exitcol);
while(win && loss){
boolean playerTurn = true;
if(count%2==0){
while(playerTurn){
System.out.println("📍 You are in room "+"("+player.getRow()+","+player.getCol()+")");
System.out.println("🧟 You hear distant footsteps...");
System.out.print("Where do you want to move? (w/a/s/d): ");
String input = sc.nextLine().trim();
char option = input.isEmpty() ? ' ' : input.charAt(0);
System.out.println();
switch(option){
case 'w' :
player.moveNorth();
playerTurn = false;
break;
case 's' :
player.moveSouth();
playerTurn = false;
break;
case 'a' :
player.moveWest();
playerTurn = false;
break;
case 'd' :
player.moveEast();
playerTurn = false;
break;
default:
System.out.println("invalid input!");
break;
}
}
}else{
System.out.println("🧟 The enemy is moving...");
double chance = Math.random();
if(chance< 0.7){
enemy.moveTowards(player);
}else{
enemy.moveRandom();
}
}
win = !(exitrow == player.getRow() && exitcol == player.getCol());
loss = !(enemy.getRow() == player.getRow() && enemy.getCol() == player.getCol());
if(!loss){
System.out.println("🧟 caught you game over.");
}
if(!win){
System.out.println("🎉 You found the exit. You escaped! ");
}
count++;
}
sc.close();
}
}