Skip to content

Commit 1c9c297

Browse files
committed
added omikuji
1 parent c309a55 commit 1c9c297

File tree

4 files changed

+65
-39
lines changed

4 files changed

+65
-39
lines changed

kadai4/main.go

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,32 @@ package main
33
import (
44
"encoding/json"
55
"log"
6-
"net/http"
7-
)
8-
9-
func main() {
10-
http.HandleFunc("/", handler)
11-
http.ListenAndServe(":8080", nil)
12-
}
13-
14-
type Response struct {
15-
Msg string `json:"msg"`
16-
}
17-
18-
func handler(w http.ResponseWriter, r *http.Request) {
19-
w.Header().Set("Content-Type", "application/json; charset=utf-8")
20-
21-
res := Response{Msg: "json response"}
22-
23-
if err := json.NewEncoder(w).Encode(res); err != nil {
24-
log.Println("Error:", err)
25-
}
26-
}
27-
28-
/*
29-
package main
30-
31-
import (
32-
"encoding/json"
33-
"log"
6+
"math/rand"
347
"net/http"
358
"time"
369

37-
"github.com/isuzuki/omikuji/omikuji"
10+
"github.com/gopherdojo/dojo6/kadai4/omikuji"
3811
)
3912

4013
func main() {
14+
rand.Seed(time.Now().UnixNano())
15+
4116
http.HandleFunc("/", handler)
4217
http.ListenAndServe(":8080", nil)
4318
}
4419

45-
type Lottery struct {
20+
type Response struct {
4621
Msg string `json:"msg"`
4722
}
4823

4924
func handler(w http.ResponseWriter, r *http.Request) {
5025
w.Header().Set("Content-Type", "application/json; charset=utf-8")
5126

5227
_, m, d := time.Now().Date()
53-
ret, ok := omikuji.Lottery(int(m), d)
54-
var res Lottery
55-
if ok {
56-
res = Lottery{Msg: ret}
57-
} else {
58-
res = Lottery{Msg: "エラーが発生しました。"}
59-
}
28+
ret := omikuji.Do(int(m), d)
29+
res := Response{Msg: ret}
6030

6131
if err := json.NewEncoder(w).Encode(res); err != nil {
6232
log.Println("Error:", err)
6333
}
6434
}
65-
*/

kadai4/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestHandler(t *testing.T) {
2424
t.Fatal("unexpected error")
2525
}
2626

27-
const expected = `{"msg":"json response"}`
27+
const expected = `{"msg":"大吉"}`
2828
if s := string(b); s != expected {
2929
t.Fatalf("unexpected response: %s", s)
3030
}

kadai4/omikuji/omikuji.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package omikuji
2+
3+
import (
4+
"math/rand"
5+
)
6+
7+
var omikuji = map[int]string{
8+
0: "凶",
9+
1: "末吉",
10+
2: "小吉",
11+
3: "中吉",
12+
4: "吉",
13+
5: "大吉",
14+
}
15+
16+
func Do(month, day int) string {
17+
var i int
18+
// 1/1 ~ 1/3のみ大吉を出す
19+
if month == 1 && day >= 1 && day <= 3 {
20+
i = 5
21+
} else {
22+
i = rand.Intn(len(omikuji))
23+
}
24+
25+
s, ok := omikuji[i]
26+
if !ok {
27+
panic("omikuji panic.")
28+
}
29+
30+
return s
31+
}

kadai4/omikuji/omikuji_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package omikuji_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gopherdojo/dojo6/kadai4/omikuji"
7+
)
8+
9+
func Test_SpecificPeriodLottery(t *testing.T) {
10+
periods := map[int][]int{
11+
1: {1, 2, 3},
12+
}
13+
14+
expect := "大吉"
15+
16+
for m, days := range periods {
17+
for _, d := range days {
18+
for i := 0; i < 20; i++ {
19+
actual := omikuji.Do(m, d)
20+
if expect != actual {
21+
t.Errorf(`Omikuji error: expect="%s" actual="%s"`, expect, actual)
22+
}
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)