func (tp *termPlayer) routine() { me := -1 for { line := <-tp.stdin.c tp.stdin.finalizeWrite() fmt.Printf("The name of the game is: %v\n", line) hand_line := <-tp.stdin.c tp.stdin.finalizeWrite() hand := util.Hand(strings.Fields(hand_line)) hand.Sort() for i := 0; i < 13; i++ { fmt.Printf("\n") before_line := <-tp.stdin.c fmt.Printf("Played before you this trick: %v", before_line) before := strings.Fields(before_line) if me == -1 { me = len(before) } fmt.Printf("Play a card from your hand: %v\n", hand) var card string fmt.Scanf("%s", &card) hand.Remove(card) tp.stdout.base.WriteString(fmt.Sprintf("%s\n", card)) tp.stdin.finalizeWrite() after_line := <-tp.stdin.c tp.stdin.finalizeWrite() fmt.Printf("Played after you this trick: %v", after_line) after := strings.Fields(after_line) var all []string for _, c := range before { all = append(all, c) } all = append(all, card) for _, c := range after { all = append(all, c) } lead := all[0][1] max := -1 index := -1 for i := range all { if all[i][1] != lead { continue } if rank_map[all[i][0]] > max { max = rank_map[all[i][0]] index = i } } player := (me - len(before) + index) % 4 fmt.Printf("Player %d won the trick with %s\n", player, all[index]) } } }
func makeRavage(players []Player, hands [][]string) BarbuGame { var r Ravage r.StandardDoubling.Players = players[:] r.StandardTrickTaking.Players = players[:] for _, hand := range hands { r.StandardTrickTaking.Hands = append(r.StandardTrickTaking.Hands, util.Hand(hand)) } return &r }
func (tp *netPlayer) routine() { me := -1 for { line := <-tp.stdin.c tp.stdin.finalizeWrite() _, err := tp.conn.Write([]byte(line)) if err != nil { panic(err) } hand_line := <-tp.stdin.c tp.stdin.finalizeWrite() hand := util.Hand(strings.Fields(hand_line)) hand.Sort() _, err = tp.conn.Write([]byte(hand_line)) if err != nil { panic(err) } for i := 0; i < 13; i++ { fmt.Printf("\n") before_line := <-tp.stdin.c _, err = tp.conn.Write([]byte(before_line)) if err != nil { panic(err) } before := strings.Fields(before_line) if me == -1 { me = len(before) } card_bytes := make([]byte, 2) _, err = tp.conn.Read(card_bytes) if err != nil { panic(err) } card := string(card_bytes) hand.Remove(card) tp.stdout.base.WriteString(card) tp.stdin.finalizeWrite() after_line := <-tp.stdin.c tp.stdin.finalizeWrite() _, err = tp.conn.Write([]byte(after_line)) if err != nil { panic(err) } } } }
func makeBarbu(players []Player, hands [][]string) BarbuGame { var lt Barbu lt.StandardDoubling.Players = players[:] lt.StandardTrickTaking.Players = players[:] for _, hand := range hands { lt.StandardTrickTaking.Hands = append(lt.StandardTrickTaking.Hands, util.Hand(hand)) } lt.End_early = func(tricks []Trick) bool { for _, card := range tricks[len(tricks)-1].Cards { if card == "kh" { return true } } return false } return < }
func makeKillerQueens(players []Player, hands [][]string) BarbuGame { var kq KillerQueens kq.StandardDoubling.Players = players[:] kq.StandardTrickTaking.Players = players[:] for _, hand := range hands { kq.StandardTrickTaking.Hands = append(kq.StandardTrickTaking.Hands, util.Hand(hand)) } kq.End_early = func(tricks []Trick) bool { count := 0 for _, trick := range tricks { for _, card := range trick.Cards { if card[0] == 'q' { count++ } } } return count == 4 } return &kq }
func interactiveMode(conn net.Conn) { buf := bufio.NewReader(conn) stdin := bufio.NewReader(os.Stdin) var hand util.Hand for { // Read in what seat you are line, err := buf.ReadString('\n') if err != nil { if err == io.EOF { return } panic(err) } line = strings.TrimSpace(line) fmt.Printf("You are: %s\n", line) // Read in your hand line, err = buf.ReadString('\n') hand = util.Hand(strings.Fields(line)) hand.Sort() fmt.Printf("Your hand is: %s\n", hand) // Read in the game line, err = buf.ReadString('\n') line = strings.TrimSpace(line) fmt.Printf("The game is: %s\n", line) // TODO: Need to support non-trick-taking games // Read in 'DOUBLING', then all of the doubling info line, err = buf.ReadString('\n') line = strings.TrimSpace(line) fmt.Printf("READ DOUBLING: %s\n", line) for i := 0; i < 4; i++ { fmt.Printf("Reading ... ") line, err = buf.ReadString('\n') fmt.Printf(" Read: '%s'\n", line) line = strings.TrimSpace(line) if line == "DOUBLE" { fmt.Printf("Time to double!\n") line, err = stdin.ReadString('\n') _, err = conn.Write([]byte(line)) } else { fmt.Printf("DOUBLING INFO: %s\n", line) } } for { line, err = buf.ReadString('\n') line = strings.TrimSpace(line) if line != "TRICK" { break } fmt.Printf("\nYour hand: %v\n", hand) for i := 0; i < 4; i++ { line, err = buf.ReadString('\n') line = strings.TrimSpace(line) if line == "PLAY" { fmt.Printf("Your turn: ") line, err = stdin.ReadString('\n') _, err = conn.Write([]byte(line)) hand.Remove(strings.TrimSpace(line)) } else { fmt.Printf("TRICK INFO: %s\n", line) } } } fmt.Printf("END: %s\n", line) } }
func main() { flag.Parse() if !(*player >= 0 && *player <= 3) { fmt.Printf("player must be in [0, 3].\n") return } raddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", *ip, 5200+*player)) if err != nil { fmt.Printf("Unable to resolve tcp addr: %v\n", err) return } conn, err := net.DialTCP("tcp", nil, raddr) if err != nil { fmt.Printf("Unable to dial host: %v\n", err) return } read := bufio.NewReader(conn) for { line, _, err := read.ReadLine() if err != nil { panic(err) } fmt.Printf("Game is %s\n", string(line)) line, _, err = read.ReadLine() if err != nil { panic(err) } hand := util.Hand(strings.Fields(string(line))) for i := 0; i < 13; i++ { hand.Sort() fmt.Printf("Hand is %v\n", hand) fmt.Printf("\n") line, _, err = read.ReadLine() if err != nil { panic(err) } fmt.Printf("Played before you this round: %s\n", string(line)) var card string fmt.Scanf("%s", &card) hand.Remove(card) _, err = conn.Write([]byte(card)) if err != nil { panic(err) } line, _, err = read.ReadLine() if err != nil { panic(err) } fmt.Printf("Played after you this round: %s\n", string(line)) fmt.Printf("\n\n") } } }