Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
Police
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
9631408
Police
Commits
6d06cbb5
Commit
6d06cbb5
authored
Apr 21, 2019
by
9631408
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init all
parent
f971b517
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
219 additions
and
60 deletions
+219
-60
Ground.java
src/Ground.java
+35
-27
Main.java
src/Main.java
+113
-3
Police.java
src/Police.java
+34
-27
Thief.java
src/Thief.java
+37
-3
No files found.
src/Ground.java
View file @
6d06cbb5
import
java.util.ArrayList
;
import
java.util.Scanner
;
import
java.util.Scanner
;
public
class
Ground
{
public
class
Ground
{
Scanner
scanner
=
new
Scanner
(
System
.
in
);
private
char
board
[][]
;
int
row
=
scanner
.
nextInt
();
private
int
n
,
m
;
int
column
=
scanner
.
nextInt
();
private
int
[][]
pos
=
new
int
[
row
][
column
];
public
Ground
(
int
n
,
int
m
)
{
public
int
getRow
()
{
board
=
new
char
[
n
][
m
]
;
return
row
;
this
.
n
=
n
;
}
this
.
m
=
m
;
public
int
getColumn
()
{
return
column
;
}
}
public
void
update
(
ArrayList
<
Police
>
polices
,
Thief
thief
)
{
public
void
setRow
(
int
row
)
{
for
(
int
i
=
0
;
i
<
n
;
++
i
)
this
.
row
=
row
;
for
(
int
j
=
0
;
j
<
m
;
++
j
)
board
[
i
][
j
]
=
' '
;
for
(
Police
p
:
polices
)
board
[
p
.
getRow
()][
p
.
getCol
()]
=
'P'
;
board
[
thief
.
getRow
()][
thief
.
getCol
()]
=
'D'
;
}
}
public
void
setColumn
(
int
column
)
{
public
void
printBoard
()
{
this
.
column
=
column
;
System
.
out
.
print
(
" "
)
;
for
(
int
i
=
1
;
i
<=
m
;
++
i
)
{
if
(
i
<
10
)
System
.
out
.
print
(
" "
+
i
)
;
else
System
.
out
.
print
(
" "
+
i
)
;
}
System
.
out
.
print
(
"\n "
)
;
for
(
int
i
=
0
;
i
<
m
;
++
i
)
System
.
out
.
print
(
"+---"
)
;
System
.
out
.
print
(
"+\n"
)
;
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
if
(
i
+
1
<
10
)
System
.
out
.
print
(
" "
)
;
System
.
out
.
print
(
i
+
1
)
;
for
(
int
j
=
0
;
j
<
m
;
++
j
)
{
System
.
out
.
print
(
"| "
+
board
[
i
][
j
]
+
" "
)
;
}
System
.
out
.
print
(
"|\n "
)
;
for
(
int
i2
=
0
;
i2
<
m
;
++
i2
)
System
.
out
.
print
(
"+---"
)
;
System
.
out
.
print
(
"+\n"
)
;
}
}
}
public
void
setPos
(
int
[][]
pos
)
{
this
.
pos
=
pos
;
}
public
int
print
(){
for
(
int
i
=
0
;
i
<
row
;
i
++){
for
(
int
j
=
0
;
j
<
column
;
j
++){
System
.
out
.
println
(
"-"
);
}
}
return
pos
[
row
][
column
];
}
}
}
\ No newline at end of file
src/Main.java
View file @
6d06cbb5
import
java.util.*
;
public
class
Main
{
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
new
Ground
();
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
int
n
,
m
,
numberOfPolice
,
time
=
0
;
Scanner
in
=
new
Scanner
(
System
.
in
)
;
Random
random
=
new
Random
()
;
int
dx
[]
=
{-
1
,-
1
,-
1
,
0
,
0
,+
1
,+
1
,+
1
}
;
int
dy
[]
=
{-
1
,
0
,+
1
,-
1
,+
1
,-
1
,
0
,+
1
}
;
boolean
isAware
=
false
;
ArrayList
<
Police
>
polices
=
new
ArrayList
<>()
;
Thief
thief
;
Set
<
Integer
>
st
=
new
HashSet
<>()
;
System
.
out
.
print
(
"Plaese enter n: "
)
;
n
=
in
.
nextInt
()
;
System
.
out
.
print
(
"Plaese enter m: "
)
;
m
=
in
.
nextInt
()
;
System
.
out
.
print
(
"Plaese enter number of police: "
)
;
numberOfPolice
=
in
.
nextInt
()
;
while
(
st
.
size
()
!=
numberOfPolice
)
{
int
r
=
random
.
nextInt
(
n
)
;
int
c
=
random
.
nextInt
(
m
)
;
Police
p
=
new
Police
(
r
,
c
)
;
int
a
=
st
.
size
()
;
st
.
add
(
r
*
1_000_000
+
c
)
;
int
b
=
st
.
size
()
;
if
(
b
-
a
==
1
)
{
polices
.
add
(
p
)
;
}
}
thief
=
new
Thief
(
random
.
nextInt
(
n
),
random
.
nextInt
(
m
))
;
Ground
g
=
new
Ground
(
n
,
m
)
;
while
(
true
)
{
//System.out.print("\f");
//Runtime.getRuntime().exec("cls") ;
//System.out.print("\033[H\033[2J");
//System.out.flush();
//Runtime.getRuntime().exec("cls");
g
.
update
(
polices
,
thief
)
;
System
.
out
.
println
(
"time: "
+
time
)
;
g
.
printBoard
()
;
time
++
;
// thief move
while
(
true
)
{
int
rand
=
random
.
nextInt
(
8
)
;
if
(
thief
.
update
(
dx
[
rand
],
dy
[
rand
],
n
,
m
))
break
;
}
// check finished program
for
(
Police
p
:
polices
)
{
if
(
p
.
getRow
()==
thief
.
getRow
()
&&
p
.
getCol
()==
thief
.
getCol
())
{
System
.
out
.
print
(
"Finished ! \n"
)
;
System
.
exit
(
0
)
;
}
}
// check aware from thief's cell
for
(
Police
p
:
polices
)
{
for
(
HashMap
<
Integer
,
Integer
>
c
:
p
.
vision
())
{
for
(
Map
.
Entry
mp
:
c
.
entrySet
())
{
int
row1
=
(
Integer
)
mp
.
getKey
()
;
int
col1
=
(
Integer
)
mp
.
getValue
()
;
if
(
row1
==
thief
.
getRow
()
&&
col1
==
thief
.
getCol
())
{
isAware
=
true
;
}
}
}
}
// move polices
if
(!
isAware
)
{
for
(
Police
p
:
polices
)
{
while
(
true
)
{
int
rand
=
random
.
nextInt
(
8
)
;
if
(
p
.
update
(
dx
[
rand
],
dy
[
rand
],
n
,
m
))
break
;
}
}
}
else
{
for
(
Police
p
:
polices
)
{
int
mi
=
Integer
.
MAX_VALUE
,
res
=
0
;
for
(
int
i
=
0
;
i
<
8
;
++
i
)
{
int
dis
=
Math
.
abs
(
thief
.
getRow
()
-
(
p
.
getRow
()+
dx
[
i
])
)
;
dis
+=
Math
.
abs
(
thief
.
getCol
()
-
(
p
.
getCol
()+
dy
[
i
])
)
;
if
(
dis
<
mi
)
{
mi
=
dis
;
res
=
i
;
}
}
p
.
update
(
dx
[
res
],
dy
[
res
],
n
,
m
)
;
}
}
// check finished program
for
(
Police
p
:
polices
)
{
if
(
p
.
getRow
()==
thief
.
getRow
()
&&
p
.
getCol
()==
thief
.
getCol
())
{
System
.
out
.
print
(
"Finished ! \n"
)
;
System
.
exit
(
0
)
;
}
}
Thread
.
sleep
(
2000
)
;
}
}
}
}
src/Police.java
View file @
6d06cbb5
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.Scanner
;
import
java.util.Scanner
;
public
class
Police
{
public
class
Police
{
int
x
;
private
int
row
,
col
;
int
y
;
boolean
isSeen
=
false
;
Scanner
scanner
=
new
Scanner
(
System
.
in
);
int
k
=
scanner
.
nextInt
();
int
[]
policeNumber
=
new
int
[
k
];
public
Police
(
int
x
,
int
y
,
int
isSeen
)
{
this
.
x
=
x
;
this
.
y
=
y
;
Police
(
int
r
,
int
c
)
{
this
.
row
=
r
;
this
.
col
=
c
;
}
}
public
int
get
X
()
{
public
int
get
Row
()
{
return
x
;
return
row
;
}
}
public
void
setX
(
int
x
)
{
public
int
getCol
(
)
{
this
.
x
=
x
;
return
col
;
}
}
public
int
getY
(
)
{
public
void
setRow
(
int
row
)
{
return
y
;
this
.
row
=
row
;
}
}
public
void
set
Y
(
int
y
)
{
public
void
set
Col
(
int
col
)
{
this
.
y
=
y
;
this
.
col
=
col
;
}
}
public
static
void
Notify
(
boolean
isSeen
)
{
ArrayList
<
HashMap
<
Integer
,
Integer
>>
vision
()
{
ArrayList
<
HashMap
<
Integer
,
Integer
>>
res
=
new
ArrayList
<>()
;
for
(
int
i
=
row
-
2
;
i
<
row
+
3
;
++
i
)
{
for
(
int
j
=
col
-
2
;
j
<
col
+
3
;
++
j
)
{
HashMap
<
Integer
,
Integer
>
mp
=
new
HashMap
<>()
;
mp
.
put
(
i
,
j
)
;
res
.
add
(
mp
)
;
}
}
return
res
;
}
}
public
void
move
()
{
public
boolean
update
(
int
dx
,
int
dy
,
int
n
,
int
m
)
{
if
(
row
+
dx
>=
0
&&
row
+
dx
<
n
&&
col
+
dy
>=
0
&&
col
+
dy
<
m
)
{
this
.
setRow
(
row
+
dx
)
;
this
.
setCol
(
col
+
dy
)
;
return
true
;
}
return
false
;
}
}
}
}
\ No newline at end of file
src/Thief.java
View file @
6d06cbb5
/**
*this class will make
* @author kimiaDorani
* @version 1.0
*/
public
class
Thief
{
public
class
Thief
{
int
x
;
private
int
row
,
col
;
int
y
;
Thief
(
int
r
,
int
c
)
{
this
.
row
=
r
;
this
.
col
=
c
;
}
public
int
getRow
()
{
return
row
;
}
public
int
getCol
()
{
return
col
;
}
public
void
setRow
(
int
row
)
{
this
.
row
=
row
;
}
public
void
setCol
(
int
col
)
{
this
.
col
=
col
;
}
public
boolean
update
(
int
dx
,
int
dy
,
int
n
,
int
m
)
{
if
(
row
+
dx
>=
0
&&
row
+
dx
<
n
&&
col
+
dy
>=
0
&&
col
+
dy
<
m
)
{
this
.
setRow
(
row
+
dx
)
;
this
.
setCol
(
col
+
dy
)
;
return
true
;
}
return
false
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment