-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
98 lines (90 loc) · 3.41 KB
/
Copy pathMain.java
File metadata and controls
98 lines (90 loc) · 3.41 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
91
92
93
94
95
96
97
98
import java.util.*;
public class CatchTheBot{
public static void main()
{
int mapSize = 10;
char[][] map = new char[2][mapSize];
Random random = new Random();
// Player and bot starting positions
int playerLine = 0;
int playerPosition = 0;
int botLine = random.nextInt(2);
int botPosition = random.nextInt(mapSize);
System.out.println("Welcome to Catch the Bot!");
// Game loop
for (int turn = 0; turn < 10; turn++)
{
displayMap(map, playerLine, playerPosition, botLine, botPosition);
Scanner in = new Scanner(System.in);
System.out.print("Enter your move (L for left, R for right, U for up, D for down): ");
char move =in.next().charAt(0);
// Update player's position based on input
if (move == 'L' && playerPosition > 0)
{
playerPosition--;
}
else if (move == 'R' && playerPosition < mapSize - 1)
{
playerPosition++;
}
else if (move == 'U' && playerLine > 0)
{
playerLine--;
}
else if (move == 'D' && playerLine < 1)
{
playerLine++;
}
else
{
System.out.println("Invalid move! Please try again.");
continue; // Skip this turn if input is invalid
}
// Bot moves randomly within bounds of its line
botPosition += random.nextInt(3) - 1;
if (botPosition < 0)
{
botPosition = 0;
}
if (botPosition >= mapSize)
{
botPosition = mapSize - 1;
}
// Randomly change bot's line with a small chance
if (random.nextBoolean())
{
botLine = (botLine == 0) ? 1 : 0; // Tertiary Operator goes brr
}
System.out.println("You moved to position (" + playerLine + ", " + playerPosition + "). The bot moved to position (" + botLine + ", " + botPosition + ").");
// Check for catch condition
if (playerLine == botLine && playerPosition == botPosition)
{
System.out.println("Congratulations! You caught the bot!");
return; // End game on catch
}
}
System.out.println("Game Over! You failed to catch the bot. It was at position (" + botLine + ", " + botPosition + ").");
}
// Method to display the current state of the map with positions of player and bot
public static void displayMap(char[][] map, int playerLine, int playerPos, int botLine, int botPos) {
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map[i].length; j++)
{
if (i == playerLine && j == playerPos)
{
System.out.print("P "); // P for Player
}
else if (i == botLine && j == botPos)
{
System.out.print("B "); // B for Bot
}
else
{
System.out.print(". "); // Empty space represented by dot
}
}
System.out.println(); // New line after each row of the map
}
}
}