mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-29 14:28:40 +01:00
53: poker hands
read from file
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fi, err := os.Open("./p054_poker.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer fi.Close()
|
||||
|
||||
br := bufio.NewReader(fi)
|
||||
var input []hand
|
||||
count := 0
|
||||
for {
|
||||
a, _, e := br.ReadLine()
|
||||
if e == io.EOF {
|
||||
break
|
||||
}
|
||||
line := string(a)
|
||||
cards := strings.Split(line, " ")
|
||||
if len(cards) != 10 {
|
||||
panic("invalid line")
|
||||
}
|
||||
h := hand{
|
||||
player1: [5]string{
|
||||
cards[0],
|
||||
cards[1],
|
||||
cards[2],
|
||||
cards[3],
|
||||
cards[4],
|
||||
},
|
||||
player2: [5]string{
|
||||
cards[5],
|
||||
cards[6],
|
||||
cards[7],
|
||||
cards[8],
|
||||
cards[9],
|
||||
},
|
||||
}
|
||||
input = append(input, h)
|
||||
if h.isPlayer1Win() {
|
||||
count++
|
||||
}
|
||||
}
|
||||
fmt.Println(count)
|
||||
}
|
||||
|
||||
type hand struct {
|
||||
player1 [5]string
|
||||
player2 [5]string
|
||||
}
|
||||
|
||||
func (hand) isPlayer1Win() bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_hand_isPlayer1Win(t *testing.T) {
|
||||
type fields struct {
|
||||
player1 [5]string
|
||||
player2 [5]string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
fields{
|
||||
player1: [5]string{"5H", "5C", "6S", "7S", "KD"},
|
||||
player2: [5]string{"2C", "3S", "8S", "8D", "TD"},
|
||||
},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ha := hand{
|
||||
player1: tt.fields.player1,
|
||||
player2: tt.fields.player2,
|
||||
}
|
||||
if got := ha.isPlayer1Win(); got != tt.want {
|
||||
t.Errorf("isPlayer1Win() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user