-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.java
68 lines (63 loc) · 1.91 KB
/
game.java
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
import java.util.Scanner;
public class game {
static void show_grid(char arr[][],int num)
{
for(int i=0;i<num;i++)
{
for(int j=0;j<num;j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int num = s.nextInt();
char[][]arr = new char[num][num];
for(int i=0;i<num;i++)
for(int j=0;j<num;j++)
arr[i][j] = '*';
arr[0][0] = '|';
show_grid(arr,num);
int pos,cr=0,cc=0;
while(true)
{
System.out.println("Enter the positon to move : ");
System.out.println("1.Up");
System.out.println("2.Down");
System.out.println("3.Left");
System.out.println("4.Right");
pos = s.nextInt();
switch(pos)
{
case 1 :
arr[cr][cc] = '*';
move(arr,--cr, cc,num);
break;
case 2:
arr[cr][cc] = '*';
move(arr,++cr,cc,num);
break;
case 3:
arr[cr][cc] = '*';
move(arr,cr,--cc,num);
break;
case 4:
arr[cr][cc] = '*';
move(arr,cr,++cc,num);
break;
default:
System.out.println("Enter Posibble Position to move");
}
}
}
static void move(char[][]arr , int cr , int cc, int num)
{
if(cr < 0 || cc < 0 || cr >= num || cc >= num )
{
System.out.println("Game Over");
return;
}
arr[cr][cc] = '|';
show_grid(arr, num);
}
}