Last active
June 2, 2019 14:32
-
-
Save manucorporat/2d53a6397beb1227f5c9 to your computer and use it in GitHub Desktop.
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" | |
"math/rand" | |
"time" | |
) | |
var manos [][]int | |
var baraja []int | |
func main() { | |
rand.Seed(time.Now().UTC().UnixNano()) | |
inicializarManos() | |
crearBaraja() | |
start() | |
} | |
func inicializarManos() { | |
manos = make([][]int, 4) | |
for i := 0; i < 4; i++ { | |
manos[i] = make([]int, 4) | |
} | |
} | |
func crearBaraja() { | |
baraja = make([]int, 40) | |
for i := range baraja { | |
if i < 8 { | |
baraja[i] = 1 // cerdos | |
} else { | |
baraja[i] = 0 // resto | |
} | |
} | |
} | |
func start() { | |
var i = 0 | |
for { | |
if jugada() { | |
fmt.Println("Encontrado ", i) | |
fmt.Println(manos[0]) | |
fmt.Println(manos[1]) | |
fmt.Println(manos[2]) | |
fmt.Println(manos[3]) | |
} | |
i++ | |
} | |
} | |
func barajar() { | |
Shuffle(baraja) | |
} | |
func jugada() bool { | |
barajar() | |
for i := 0; i < 4*4; i++ { | |
indiceMano := i % 4 | |
indiceCarta := i / 4 | |
manos[indiceMano][indiceCarta] = baraja[i] | |
} | |
return comprobar() | |
} | |
func comprobar() bool { | |
return (todoReyes(manos[0]) && todoReyes(manos[1])) || | |
(todoReyes(manos[0]) && todoReyes(manos[3])) || | |
(todoReyes(manos[1]) && todoReyes(manos[2])) || | |
(todoReyes(manos[2]) && todoReyes(manos[3])) | |
} | |
func todoReyes(mano []int) bool { | |
for i := 0; i < 4; i++ { | |
valor := mano[i] | |
if valor != 1 { | |
return false | |
} | |
} | |
return true | |
} | |
func Shuffle(a []int) { | |
for i := range a { | |
j := rand.Intn(i + 1) | |
a[i], a[j] = a[j], a[i] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment