File tree Expand file tree Collapse file tree 4 files changed +25
-10
lines changed Expand file tree Collapse file tree 4 files changed +25
-10
lines changed Original file line number Diff line number Diff line change 1
- .idea
1
+ .idea /
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
- import "fmt"
3
+ import (
4
+ "fmt"
5
+ "math/rand"
6
+ "time"
7
+ )
4
8
5
9
// Create a new type of 'deck'
6
10
// type deck is a slice of strings
7
- type deck [] string
11
+ type deck []string
8
12
9
13
func newDeck () deck {
10
- cards := deck {}
11
-
12
- cardSuits := [] string {
14
+ cards := deck {}
15
+ cardSuits := []string {
13
16
"Spades" ,
14
17
"Diamonds" ,
15
18
"Hearts" ,
16
19
"Clubs" ,
17
20
}
18
21
19
- cardValues := [] string {
22
+ cardValues := []string {
20
23
"Ace" ,
21
24
"Two" ,
22
25
"Three" ,
@@ -34,10 +37,9 @@ func newDeck() deck {
34
37
35
38
for _ , suit := range cardSuits {
36
39
for _ , value := range cardValues {
37
- cards = append (cards , value + " of " + suit )
40
+ cards = append (cards , value + " of " + suit )
38
41
}
39
42
}
40
-
41
43
return cards
42
44
}
43
45
@@ -46,3 +48,15 @@ func (d deck) print() {
46
48
fmt .Println (i , card )
47
49
}
48
50
}
51
+
52
+ func (d deck ) shuffle () {
53
+ source := rand .NewSource (time .Now ().UnixNano ())
54
+ r := rand .New (source )
55
+
56
+ for i := range d {
57
+ newPosition := r .Intn (len (d ) - 1 )
58
+ // One line index swap
59
+ // x,y = y,x
60
+ d [i ], d [newPosition ] = d [newPosition ], d [i ]
61
+ }
62
+ }
Original file line number Diff line number Diff line change @@ -2,5 +2,6 @@ package main
2
2
3
3
func main () {
4
4
cards := newDeck ()
5
+ cards .shuffle ()
5
6
cards .print ()
6
- }
7
+ }
You can’t perform that action at this time.
0 commit comments