Created
May 27, 2019 06:24
-
-
Save arbaieffendi/3f4cc1c0b089ab7436c4f7082280da0f to your computer and use it in GitHub Desktop.
Maze SxS. S = 4n-1, n = bilangan bulat ganjil positif
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
maze(3) | |
} | |
// Labirin SxS. S = 4n-1, n = bilangan bulat ganjil positif | |
// row ganjil maka harus punya pintu | |
// row genap hanya ujungnya saja yang terisi | |
func maze(n int) { | |
s := 4*n - 1 | |
var door int = 0 // 0 left 1 right | |
for r := 1; r <= s; r++ { | |
if isOdd(r) { | |
//# ## or ## # | |
for c := 0; c < s; c++ { | |
if c == 1 && door == 0 { | |
fmt.Print(" ") | |
} else if c == s-2 && door == 1 { | |
fmt.Print(" ") | |
} else { | |
fmt.Print("#") | |
} | |
} | |
if door == 0 { | |
door = 1 | |
} else { | |
door = 0 | |
} | |
} else { | |
//# # | |
for c := 0; c < s; c++ { | |
if c == 0 || c == s-1 { | |
fmt.Print("#") | |
} else { | |
fmt.Printf(" ") | |
} | |
} | |
} | |
fmt.Println("") | |
} | |
} | |
func isOdd(value int) bool { | |
if value%2 == 1 { | |
return true | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment