//Testing win condition func TestTwelve(test *testing.T) { p0 := player.NewPlayer(0) players := []*player.Player{p0} t := table.NewTable(players) t.SetFirstPlayed(0) t.PlayCard(card.NewCard(12, card.Spade), 0) t.SendTrick() t.PlayCard(card.NewCard(12, card.Spade), 0) t.SendTrick() t.PlayCard(card.NewCard(12, card.Spade), 0) t.SendTrick() winner := t.EndRound() expect := -1 if winner != expect { test.Errorf("Expected %d, got %d", expect, winner) } t.NewRound() t.PlayCard(card.NewCard(12, card.Spade), 0) t.SendTrick() t.PlayCard(card.NewCard(12, card.Spade), 0) t.SendTrick() t.PlayCard(card.NewCard(12, card.Spade), 0) t.SendTrick() t.PlayCard(card.NewCard(12, card.Spade), 0) t.SendTrick() t.PlayCard(card.NewCard(12, card.Spade), 0) t.SendTrick() t.PlayCard(card.NewCard(12, card.Spade), 0) t.SendTrick() winner = t.EndRound() expect = 0 if winner != expect { test.Errorf("Expected %d, got %d", expect, winner) } }
//Testing playing a card-- ValidPlay() testing breaking Hearts rule func TestTen(test *testing.T) { p0 := player.NewPlayer(0) p1 := player.NewPlayer(1) p0.AddToHand(card.NewCard(5, card.Heart)) p1.AddToHand(card.NewCard(2, card.Heart)) p1.AddToHand(card.NewCard(3, card.Diamond)) players := []*player.Player{p0, p1} t := table.NewTable(players) t.SetFirstPlayed(0) t.PlayCard(card.NewCard(2, card.Club), 0) t.PlayCard(card.NewCard(3, card.Club), 1) t.SendTrick() t.SetFirstPlayed(0) if t.ValidPlay(card.NewCard(5, card.Heart), 0) == false { test.Errorf("Expected valid play for opener rightfully breaking Hearts") } t.SetFirstPlayed(1) if t.ValidPlay(card.NewCard(2, card.Heart), 1) == true { test.Errorf("Expected invalid play for opener wrongfully breaking Hearts") } t.PlayCard(card.NewCard(3, card.Diamond), 1) if t.ValidPlay(card.NewCard(5, card.Heart), 0) == false { test.Errorf("Expected valid play for follower rightfully breaking Hearts") } p0.AddToHand(card.NewCard(7, card.Diamond)) if t.ValidPlay(card.NewCard(5, card.Heart), 0) == true { test.Errorf("Expected invalid play for follower wrongfully breaking Hearts") } }
//Testing scoring after 1 trick func TestOne(test *testing.T) { p2Expect := 15 otherExpect := 0 p0 := player.NewPlayer(0) p1 := player.NewPlayer(1) p2 := player.NewPlayer(2) p3 := player.NewPlayer(3) players := []*player.Player{p0, p1, p2, p3} t := table.NewTable(players) t.SetFirstPlayed(1) t.PlayCard(card.NewCard(3, card.Heart), 1) t.PlayCard(card.NewCard(7, card.Heart), 2) t.PlayCard(card.NewCard(12, card.Spade), 3) t.PlayCard(card.NewCard(4, card.Diamond), 0) t.SendTrick() t.EndRound() score := p0.GetScore() if score != otherExpect { test.Errorf("Expected %d, got %d", otherExpect, score) } score = p1.GetScore() if score != otherExpect { test.Errorf("Expected %d, got %d", otherExpect, score) } score = p2.GetScore() if score != p2Expect { test.Errorf("Expected %d, got %d", p2Expect, score) } score = p3.GetScore() if score != otherExpect { test.Errorf("Expected %d, got %d", otherExpect, score) } }
//Testing playing a card-- ValidPlay() testing 2 of Clubs rule func TestEight(test *testing.T) { p0 := player.NewPlayer(0) players := []*player.Player{p0} t := table.NewTable(players) t.SetFirstPlayed(0) if t.ValidPlay(card.NewCard(8, card.Club), 0) == true { test.Errorf("Expected invalid play for starting round with card other than 2 of Clubs") } else if t.ValidPlay(card.NewCard(2, card.Club), 0) == false { test.Errorf("Expected valid play for starting round with 2 of Clubs") } }
//Testing playing a card-- ValidPlay() testing first round points rule func TestNine(test *testing.T) { p0 := player.NewPlayer(0) p1 := player.NewPlayer(1) p1.AddToHand(card.NewCard(12, card.Spade)) p1.AddToHand(card.NewCard(3, card.Diamond)) players := []*player.Player{p0, p1} t := table.NewTable(players) t.SetFirstPlayed(0) t.PlayCard(card.NewCard(2, card.Club), 0) if t.ValidPlay(card.NewCard(12, card.Spade), 1) == true { test.Errorf("Expected invalid play for points on the first round") } }
//Testing dealing to make sure enough cards are dealt func TestFive(test *testing.T) { expect := 13 p0 := player.NewPlayer(0) p1 := player.NewPlayer(1) p2 := player.NewPlayer(2) p3 := player.NewPlayer(3) players := []*player.Player{p0, p1, p2, p3} t := table.NewTable(players) t.GenerateCards() t.Deal() for i := 0; i < 4; i++ { if len(players[i].GetHand()) != expect { test.Errorf("Expected %d cards in the hand of player %d, got %d cards", expect, i, len(players[i].GetHand())) } } }
func main() { players := []*player.Player{player.NewPlayer(0), player.NewPlayer(1), player.NewPlayer(2), player.NewPlayer(3)} t := table.NewTable(players) t.Deal() hand := players[0].GetHand() log.Println(hand[0].GetNum(), hand[0].GetSuit()) log.Println(hand[1].GetNum(), hand[1].GetSuit()) log.Println(hand[2].GetNum(), hand[2].GetSuit()) log.Println(hand[3].GetNum(), hand[3].GetSuit()) log.Println(hand[4].GetNum(), hand[4].GetSuit()) log.Println("-------------") t.Deal() hand = players[0].GetHand() log.Println(hand[0].GetNum(), hand[0].GetSuit()) log.Println(hand[1].GetNum(), hand[1].GetSuit()) log.Println(hand[2].GetNum(), hand[2].GetSuit()) log.Println(hand[3].GetNum(), hand[3].GetSuit()) log.Println(hand[4].GetNum(), hand[4].GetSuit()) }
//Testing playing a card-- ValidPlay() testing following suit rule func TestEleven(test *testing.T) { p0 := player.NewPlayer(0) p1 := player.NewPlayer(1) p0.AddToHand(card.NewCard(2, card.Club)) p1.AddToHand(card.NewCard(3, card.Diamond)) players := []*player.Player{p0, p1} t := table.NewTable(players) t.SetFirstPlayed(0) t.PlayCard(card.NewCard(2, card.Club), 0) if t.ValidPlay(card.NewCard(3, card.Diamond), 1) == false { test.Errorf("Expected valid play for not following suit when player doesn't have suit") } p1.AddToHand(card.NewCard(5, card.Club)) if t.ValidPlay(card.NewCard(5, card.Club), 1) == false { test.Errorf("Expected valid play for following suit") } if t.ValidPlay(card.NewCard(3, card.Diamond), 1) == true { test.Errorf("Expected invalid play for not following suit when player has suit") } }
//Testing dealing to make sure no duplicates are dealt func TestFour(test *testing.T) { p0 := player.NewPlayer(0) p1 := player.NewPlayer(1) p2 := player.NewPlayer(2) p3 := player.NewPlayer(3) players := []*player.Player{p0, p1, p2, p3} t := table.NewTable(players) t.GenerateCards() t.Deal() p0Hand := p0.GetHand() p1Hand := p1.GetHand() p2Hand := p2.GetHand() p3Hand := p3.GetHand() testMap := make(map[*card.Card]int) for i := 0; i < 13; i++ { if testMap[p0Hand[i]] == 0 { testMap[p0Hand[i]] = 1 } else { test.Errorf("Duplicate card") } if testMap[p1Hand[i]] == 0 { testMap[p1Hand[i]] = 1 } else { test.Errorf("Duplicate card") } if testMap[p2Hand[i]] == 0 { testMap[p2Hand[i]] = 1 } else { test.Errorf("Duplicate card") } if testMap[p3Hand[i]] == 0 { testMap[p3Hand[i]] = 1 } else { test.Errorf("Duplicate card") } } }