Skip to content

Commit 45d5415

Browse files
authored
Merge pull request #5546 from blackriper/main
Reto#33-kotlin
2 parents 04e1e5f + 687273b commit 45d5415

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
//1.-Declaracion de varibles auxiliares e interfaces
3+
typealias ObstacleList=MutableList<Pair<Int,Int>>
4+
5+
interface Drawable{
6+
fun drawLaberit(mickeyLoc:Pair<Int,Int>)
7+
fun searchObstacle(location:Pair<Int,Int>):Boolean
8+
}
9+
10+
interface Move{
11+
fun moveMickey(y: Int,x: Int):Pair<Int,Int>
12+
fun mickeyIsFree(mickeyPosition: Pair<Int, Int>):Boolean
13+
}
14+
15+
//2.-implementar clases y interfaces
16+
17+
class DrawableLaberit:Drawable{
18+
private var laberit: String=""
19+
private var listObstacle:ObstacleList = mutableListOf()
20+
private var completed=false
21+
22+
override fun drawLaberit(mickeyLoc: Pair<Int, Int>) {
23+
for (x in 1..6){
24+
for (y in 1..6) {
25+
laberit += if (x==mickeyLoc.first && y==mickeyLoc.second) "🐭"
26+
else if (y==6 && x==6) "🚪" else drawObstacle(x,y)
27+
if (y%6==0) laberit+="\n"
28+
29+
}
30+
}
31+
completed=true
32+
println(laberit)
33+
laberit=""
34+
35+
}
36+
37+
override fun searchObstacle(location: Pair<Int, Int>): Boolean {
38+
if(listObstacle.count { it.first == location.first && it.second == location.second } == 1){
39+
println("Mickey found obstacle he need move to another way")
40+
return true
41+
}
42+
return false
43+
}
44+
45+
private fun drawObstacle(x:Int,y:Int):String=
46+
if (completed) generateExistObstacle(x,y)
47+
else generateNewObstacles(x,y)
48+
49+
50+
private fun generateNewObstacles(x: Int,y: Int):String{
51+
val generator=(1..10).random()
52+
if (x==generator || y==generator) {
53+
listObstacle.add(Pair(x,y))
54+
return "⬛️"
55+
}
56+
return "⬜️"
57+
}
58+
59+
private fun generateExistObstacle(x: Int,y: Int):String{
60+
if (listObstacle.count { x==it.first && y==it.second }==1) return "⬛️"
61+
return "⬜️"
62+
}
63+
64+
}
65+
66+
class Mickey(
67+
val blockObs:(Pair<Int,Int>)->Boolean
68+
):Move{
69+
70+
override fun moveMickey(y: Int,x: Int):Pair<Int,Int> {
71+
println("Select direction to move Mickey 1 [Up], 2 [Down] , 3 [Left], 4 [Right] ")
72+
val position= readlnOrNull()?.toInt()?:0
73+
val oldPosition=Pair(y,x)
74+
75+
return when(position){
76+
1->if(isLimit(Pair(y-1,x))) oldPosition else isObstacle(Pair(y-1,x),oldPosition)
77+
2->if(isLimit(Pair(y+1,x))) oldPosition else isObstacle(Pair(y+1,x),oldPosition)
78+
3->if(isLimit(Pair(y,x+1))) oldPosition else isObstacle(Pair(y,x+1),oldPosition)
79+
4->if(isLimit(Pair(y,x-1))) oldPosition else isObstacle(Pair(y,x-1),oldPosition)
80+
else -> oldPosition
81+
}
82+
}
83+
84+
override fun mickeyIsFree(mickeyPosition:Pair<Int,Int>): Boolean {
85+
if (mickeyPosition.first == 6 && mickeyPosition.second == 6){
86+
println("Mickey found exit of laberit")
87+
return true
88+
}
89+
return false
90+
}
91+
92+
private fun isLimit(newPosition: Pair<Int, Int>):Boolean {
93+
if(newPosition.first == 0 || newPosition.first>=7|| newPosition.second>6){
94+
println("Mickey can't go any further, he has reached a wall in the maze ")
95+
return true
96+
}
97+
return false
98+
}
99+
100+
private fun isObstacle(newPosition:Pair<Int,Int>,position:Pair<Int,Int>):Pair<Int,Int> =
101+
if (blockObs(newPosition)) position else newPosition
102+
103+
}
104+
105+
fun laberitD23(){
106+
val laberit= DrawableLaberit()
107+
val mickey= Mickey(laberit::searchObstacle)
108+
var mickeyPosition=Pair(1,1)
109+
110+
111+
while (!mickey.mickeyIsFree(mickeyPosition)){
112+
laberit.drawLaberit(mickeyPosition)
113+
mickeyPosition=mickeyPosition.let { mickey.moveMickey(it.first,it.second) }
114+
}
115+
116+
}
117+
118+
119+
120+
fun main() {
121+
laberitD23()
122+
}

0 commit comments

Comments
 (0)