Exemple #1
0
func setupGame(net *websocket.Conn, cp *ConnectionPool) {
	Log("Connection received")
	human := createHuman(net)
	for {
		for {
			human.Tell(sdz.CreateMessage("Do you want to join a game, create a new game, or quit? (join, create, quit)"))
			action := sdz.CreateHello("")
			human.Tell(action)
			action, open := human.Listen()
			if !open {
				return
			}
			if action.Message == "create" {
				break
			} else if action.Message == "join" {
				human.Tell(sdz.CreateMessage("Waiting on a game to be started that you can join..."))
				cp.Push(human)
				<-human.finished // this will block to keep the websocket open
				continue
			} else if action.Message == "quit" {
				human.Tell(sdz.CreateMessage("Ok, bye bye!"))
				human.Close()
				return
			}
		}
		for {
			human.Tell(sdz.CreateMessage("Option 1 - Play against three AI players and start immediately"))
			human.Tell(sdz.CreateMessage("Option 2 - Play with a human partner against two AI players"))
			human.Tell(sdz.CreateMessage("Option 3 - Play with a human partner against one AI players and 1 Human"))
			human.Tell(sdz.CreateMessage("Option 4 - Play with a human partner against two humans"))
			human.Tell(sdz.CreateMessage("Option 5 - Play against a human with AI partners"))
			human.Tell(sdz.CreateMessage("Option 6 - Go back"))
			human.Tell(sdz.CreateGame(0))
			action, open := human.Listen()
			if !open {
				return
			}
			switch action.Option {
			case 1:
				fallthrough
			case 2:
				fallthrough
			case 3:
				fallthrough
			case 4:
				fallthrough
			case 5:
				human.createGame(action.Option, cp)
			case 6:
				break
			default:
				human.Tell(sdz.CreateMessage("Not a valid option"))
			}
			break // after their game is over, let's set them up again'
		}
	}
}
Exemple #2
0
func main() {
	autoClient := false
	for _, s := range os.Args {
		fmt.Println(s)
		if s == "auto" {
			autoClient = true
		}
	}
	conn, err := websocket.Dial("ws://localhost:10080/connect", "", "http://localhost:10080/")
	var playerid int
	var hand *sdz.Hand
	var bidAmount int
	var trump sdz.Suit
	if err != nil {
		sdz.Log("Error - %v", err)
		return
	}
	defer conn.Close()
	var previousPlay *sdz.Action
	var previousCard sdz.Card
	for {
		var action sdz.Action
		err := websocket.JSON.Receive(conn, &action)
		if err != nil {
			sdz.Log("Error decoding - %v", err)
			return
		}
		switch action.Type {
		case "Bid":
			if action.Playerid == playerid {
				sdz.Log("How much would you like to bid?:")
				fmt.Scan(&bidAmount)
				send(conn, sdz.CreateBid(bidAmount, playerid))
			} else {
				// received someone else's bid value'
				sdz.Log("Player #%d bid %d", action.Playerid, action.Bid)
			}
		case "Play":
			if action.Playerid == playerid {
				if previousPlay == nil || action.Lead == "" {
					previousPlay = &action
				} else {
					sdz.Log("Server rejected the play of %s, invalid play", previousCard)
					*hand = append(*hand, previousCard)
					sort.Sort(hand)
				}
				var card sdz.Card
				if action.Lead == "" {
					card = (*hand)[0]
				} else {
					for _, c := range *hand {
						if sdz.ValidPlay(c, action.WinningCard, action.Lead, hand, trump) {
							card = c
						}
					}
				}
				sdz.Log("Your turn, in your hand is %s - what would you like to play? Trump is %s - valid play is %s:", hand, trump, card)
				for {
					if !autoClient {
						fmt.Scan(&card)
					}
					//sdz.Log("Received input %s", card)
					if hand.Remove(card) {
						send(conn, sdz.CreatePlay(card, playerid))
						previousCard = card
						break
					} else {
						sdz.Log("Invalid play, card does not exist in your hand")
					}
				}
			} else {
				sdz.Log("Player %d played card %s", action.Playerid, action.PlayedCard)
				previousPlay = nil // not going to ask us to replay since the next response was another player's play
				// received someone else's play'
			}
		case "Trump":
			if action.Playerid == playerid {
				sdz.Log("What would you like to make trump?")
				fmt.Scan(&trump)
				send(conn, sdz.CreateTrump(trump, playerid))
			} else {
				sdz.Log("Player %d says trump is %s", action.Playerid, action.Trump)
				trump = action.Trump
			}
		case "Throwin":
			sdz.Log("Player %d threw in", action.Playerid)
		case "Deal":
			playerid = action.Playerid
			hand = &action.Hand
			sdz.Log("Your hand is - %s", hand)
		case "Meld":
			sdz.Log("Player %d is melding %s for %d points", action.Playerid, action.Hand, action.Amount)
		case "Score": // this client does not have to implement this type as it's already told through Message actions
		case "Message":
			sdz.Log(action.Message)
		case "Hello":
			var response string
			fmt.Scan(&response)
			send(conn, sdz.CreateHello(response))
		case "Game":
			var option int
			fmt.Scan(&option)
			send(conn, sdz.CreateGame(option))
		default:
			sdz.Log("Received an action I didn't understand - %v", action)
		}

	}
}