Jump to content
  • Advertisement
Sign in to follow this  
agm_ultimatex

Euchre - Problems dealer ordering up

This topic is 3605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I seem to have a specific bug in the text-based euchre card game im working on. When the dealer orders him/herself up the flipped card, and discards one from his/her hand, the program crashes. If another player orders the dealer up, the dealer is still asked to discard, its fine. Project files: http://agm2004.fileave.com/Euchre.zip I know euchre.cpp is quite a hack job right now, so bare with me on that. Im thinking it has to do with maybe the fact i use break in the for loop of SelectDealerTrump(), and maybe the memory wasnt released correctly? Euchre.cpp:
#include "Euchre.h"

#include <cctype> // std::tolower
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <string>

#include "Team.h"
// Note: This file needs a lot of work.
//       Many of the functions are unimplemented, or incomplete, or simply untested.

//global as value cannot be returned when required.
Team* trumpTeam;
Card::Suit currentSuit;
bool leadPlay;
const int TRUMP_CARD_VALUE = 10;

char toLowercase(char in)
{ 
	return static_cast<char>(std::tolower(in));
}

// checks if a player is forced to follow suit or not.
bool FollowSuit(Hand &currentHand, Card &chosenCard)
{
	bool hasToFollowSuit = false;
	for(int i = 0; i != currentHand.size(); i++)
	{
		Card* temp = &currentHand;
		if(temp->getSuit() == currentSuit)
		{
			hasToFollowSuit = true;
		}
	}
	if(hasToFollowSuit && currentSuit != chosenCard.getSuit())
	{
		std::cout << "You did not follow suit, please choose another card.\n";
	}
	else
	{
		hasToFollowSuit = false;
	}

	return hasToFollowSuit;
}
std::string SuitOutput(Card::Suit &s)
{
	std::string output;
	if(s == Card::CLUBS)
	{
		output = "Clubs";
	}
	else if(s == Card::DIAMONDS)
	{
		output = "Diamonds";
	}
	else if(s == Card::HEARTS)
	{
		output = "Hearts";
	}
	else if(s == Card::SPADES)
	{
		output = "Spades";
	}
	else
	{
		output = "No Trump Chosen";
	}
	return output;
}
Team CreateTeam(int teamNumber)
{
	std::string names[2];
	for(int i = 0 ; i != 2 ; ++i )
	{
		std::cout << "Player " << (i + 1) << " of team " << teamNumber << ", please enter your name: ";
		std::getline(std::cin, names);
	}
	return Team(Player(names[0]),Player(names[1]));
}

// Use rotation algoritm if appropriate.
std::vector<Player*> OrganizePlayers(std::vector<Player *> &players, int lead)
{
	if(lead > 0)
	{
		std::rotate(players.begin(), players.begin() + lead, players.end());
		return players;
	}
	return players;
}

//populates the given vector with the players
std::vector<Player*> PopulatePlayers(std::vector<Player *> players, Team &one, Team &two)
{
	players.push_back(&one.GetPlayerOne());
	players.push_back(&two.GetPlayerOne());
	players.push_back(&one.GetPlayerTwo());
	players.push_back(&two.GetPlayerTwo());

	return players;
}

int SelectDealer()
{
	return std::rand() % 4;
}

bool OrderUp(Player &player, const Card &card)
{
	std::string orderUp;
	do
	{
		std::cout << player << ", your hand is: ";
		player.ShowHand(std::cout);
		std::cout << "Do you wish to order " << card << " to the dealer? y/n\n";
		std::getline(std::cin, orderUp);
		std::transform(orderUp.begin(), orderUp.end(), orderUp.begin(), &toLowercase);
	}while(orderUp != "n" && orderUp != "y");
	if(orderUp == "y")
	{
		return true;
	}
	return false;
}

//clears the hands of the cards if no suit is chosen. 
void ClearCards(std::vector<Player*> &players)
{
	for(int i = 0; i != players.size(); i++)
	{
		Player* temp = players;
		temp->ClearHand();
	}
}

// Pass any state required to determine a winner.
bool GameOver(int team1Points, int team2Points)
{ 
	if(team1Points >= 10 || team2Points >= 10)
	{
		return true;
	}
	else return false;
}

// We can return the last card for display.
// This may not be necessary, as the caller can just use deck.back()
Card Deal(std::vector<Player *> players, Deck &deck)
{
	// loop 5 times for 20 cards to be dealt
	if(deck.size() == 24)
	{
		for(int i = 0; i < 5; i++)
		{
			for(std::vector<Player *>::iterator it = players.begin() ; it != players.end() ; ++it)
			{
				Player& player = **it;

				assert(!deck.empty());

				player.Add(deck.back());
				deck.pop_back();
			}
		}
	}
	assert(!deck.empty());
	return deck.back();
}

Team SelectTrumpTeam(int player, Team& one, Team& two)
{
	if(player == 0 || player == 2)
	{
		trumpTeam = &one;
	}
	else if(player == 1 || player == 2)
	{
		trumpTeam = &two;
	}
	return *trumpTeam;
}

// discards a card from the persons hand and puts it in play.
Card PlayCard(Player &currentPlayer)
{
	std::string card;
	bool followSuit = false;
	Card* temp = NULL;
	Hand* h = NULL;
	int c;
	//this loop will continue if the person is not following the led suit, and has a hard that can.
	do 
	{
		do
		{
			std::cout << currentPlayer << ", please choose a card to play. ";
			currentPlayer.ShowHand(std::cout);
			std::getline(std::cin, card);
		}while(card != "1" && card != "2" && card != "3" && card != "4" && card != "5");
					
		// following code gets the dealers hand and removes the card.
		h = &currentPlayer.GetHand();
		c = atoi(card.c_str());
		c--;
		temp = &currentPlayer.GetCardFromHand(c);
		if(!leadPlay)
		{
			followSuit = FollowSuit(*h, *temp);
		}
		else
		{
			leadPlay = false;
		}		
	}while (followSuit);

	h->erase(h->begin()+c);
	return *temp;
}

void HeartPoints(Card::Suit &trumpSuit, Card &assignTo)
{
	if(trumpSuit == Card::DIAMONDS && assignTo.GetRank() == Card::JACK)
	{
		assignTo.SetValue(6);
	}
	else
	{
		if(trumpSuit == Card::HEARTS)
		{
			if(assignTo.GetRank() == Card::NINE)
			{
				assignTo.SetValue(1);
			}
			else if(assignTo.GetRank() == Card::TEN)
			{
				assignTo.SetValue(2);
			}
			else if(assignTo.GetRank() == Card::JACK)
			{
				assignTo.SetValue(7);
			}
			else if(assignTo.GetRank() == Card::QUEEN)
			{
				assignTo.SetValue(3);
			}
			else if(assignTo.GetRank() == Card::KING)
			{
				assignTo.SetValue(4);
			}
			else if(assignTo.GetRank() == Card::ACE)
			{
				assignTo.SetValue(5);
			}
		}
		else
		{
			if(assignTo.GetRank() == Card::NINE)
			{
				assignTo.SetValue(1);
			}
			else if(assignTo.GetRank() == Card::TEN)
			{
				assignTo.SetValue(2);
			}
			else if(assignTo.GetRank() == Card::JACK)
			{
				assignTo.SetValue(3);
			}
			else if(assignTo.GetRank() == Card::QUEEN)
			{
				assignTo.SetValue(4);
			}
			else if(assignTo.GetRank() == Card::KING)
			{
				assignTo.SetValue(5);
			}
			else if(assignTo.GetRank() == Card::ACE)
			{
				assignTo.SetValue(6);
			}
		}
	}
}
void DiamondPoints(Card::Suit &trumpSuit, Card &assignTo)
{
	if(trumpSuit == Card::HEARTS && assignTo.GetRank() == Card::JACK)
	{
		assignTo.SetValue(6);
	}
	else
	{
		if(trumpSuit == Card::DIAMONDS)
		{
			if(assignTo.GetRank() == Card::NINE)
			{
				assignTo.SetValue(1);
			}
			else if(assignTo.GetRank() == Card::TEN)
			{
				assignTo.SetValue(2);
			}
			else if(assignTo.GetRank() == Card::JACK)
			{
				assignTo.SetValue(7);
			}
			else if(assignTo.GetRank() == Card::QUEEN)
			{
				assignTo.SetValue(3);
			}
			else if(assignTo.GetRank() == Card::KING)
			{
				assignTo.SetValue(4);
			}
			else if(assignTo.GetRank() == Card::ACE)
			{
				assignTo.SetValue(5);
			}
		}
		else
		{
			if(assignTo.GetRank() == Card::NINE)
			{
				assignTo.SetValue(1);
			}
			else if(assignTo.GetRank() == Card::TEN)
			{
				assignTo.SetValue(2);
			}
			else if(assignTo.GetRank() == Card::JACK)
			{
				assignTo.SetValue(3);
			}
			else if(assignTo.GetRank() == Card::QUEEN)
			{
				assignTo.SetValue(4);
			}
			else if(assignTo.GetRank() == Card::KING)
			{
				assignTo.SetValue(5);
			}
			else if(assignTo.GetRank() == Card::ACE)
			{
				assignTo.SetValue(6);
			}
		}
	}
}
void SpadePoints(Card::Suit &trumpSuit, Card &assignTo)
{
	if(trumpSuit == Card::CLUBS && assignTo.GetRank() == Card::JACK)
	{
		assignTo.SetValue(6);
	}
	else
	{
		if(trumpSuit == Card::SPADES)
		{
			if(assignTo.GetRank() == Card::NINE)
			{
				assignTo.SetValue(1);
			}
			else if(assignTo.GetRank() == Card::TEN)
			{
				assignTo.SetValue(2);
			}
			else if(assignTo.GetRank() == Card::JACK)
			{
				assignTo.SetValue(7);
			}
			else if(assignTo.GetRank() == Card::QUEEN)
			{
				assignTo.SetValue(3);
			}
			else if(assignTo.GetRank() == Card::KING)
			{
				assignTo.SetValue(4);
			}
			else if(assignTo.GetRank() == Card::ACE)
			{
				assignTo.SetValue(5);
			}
		}
		else
		{
			if(assignTo.GetRank() == Card::NINE)
			{
				assignTo.SetValue(1);
			}
			else if(assignTo.GetRank() == Card::TEN)
			{
				assignTo.SetValue(2);
			}
			else if(assignTo.GetRank() == Card::JACK)
			{
				assignTo.SetValue(3);
			}
			else if(assignTo.GetRank() == Card::QUEEN)
			{
				assignTo.SetValue(4);
			}
			else if(assignTo.GetRank() == Card::KING)
			{
				assignTo.SetValue(5);
			}
			else if(assignTo.GetRank() == Card::ACE)
			{
				assignTo.SetValue(6);
			}
		}
	}
}
void ClubPoints(Card::Suit &trumpSuit, Card &assignTo)
{
	if(trumpSuit == Card::SPADES && assignTo.GetRank() == Card::JACK)
	{
		assignTo.SetValue(6);
	}
	else
	{
		if(trumpSuit == Card::CLUBS)
		{
			if(assignTo.GetRank() == Card::NINE)
			{
				assignTo.SetValue(1);
			}
			else if(assignTo.GetRank() == Card::TEN)
			{
				assignTo.SetValue(2);
			}
			else if(assignTo.GetRank() == Card::JACK)
			{
				assignTo.SetValue(7);
			}
			else if(assignTo.GetRank() == Card::QUEEN)
			{
				assignTo.SetValue(3);
			}
			else if(assignTo.GetRank() == Card::KING)
			{
				assignTo.SetValue(4);
			}
			else if(assignTo.GetRank() == Card::ACE)
			{
				assignTo.SetValue(5);
			}
		}
		else
		{
			if(assignTo.GetRank() == Card::NINE)
			{
				assignTo.SetValue(1);
			}
			else if(assignTo.GetRank() == Card::TEN)
			{
				assignTo.SetValue(2);
			}
			else if(assignTo.GetRank() == Card::JACK)
			{
				assignTo.SetValue(3);
			}
			else if(assignTo.GetRank() == Card::QUEEN)
			{
				assignTo.SetValue(4);
			}
			else if(assignTo.GetRank() == Card::KING)
			{
				assignTo.SetValue(5);
			}
			else if(assignTo.GetRank() == Card::ACE)
			{
				assignTo.SetValue(6);
			}
		}
	}
}
void SetCardValue(Card::Suit &trumpSuit, Card &assignTo, Card::Suit &leadSuit)
{
	if(assignTo.getSuit() == trumpSuit || assignTo.getSuit() == leadSuit)
	{
		if(assignTo.getSuit() == Card::HEARTS)
		{
			HeartPoints(trumpSuit, assignTo);
		}
		else if(assignTo.getSuit() == Card::DIAMONDS)
		{
			DiamondPoints(trumpSuit, assignTo);
		}
		else if(assignTo.getSuit() == Card::SPADES)
		{
			SpadePoints(trumpSuit, assignTo);
		}
		else if(assignTo.getSuit() == Card::CLUBS)
		{
			ClubPoints(trumpSuit, assignTo);
		}
	}
}

// this displays the cards in a vector
void DisplayCards(std::vector<Card*> &cards)
{
	std::cout << "In play: ";
	for(int i = 0; i != cards.size(); i++)
	{
		std::cout << *cards;
	}
	std::cout << "\n";
}

// discards a card from the dealer, and picks up the flipped card
void DiscardCard(Player &dealer, Card &trumpCard)
{
	std::cout << dealer << ", please choose a card from your hand to discard (type a number of range 1-5)\n";
	dealer.ShowHand(std::cout);
	std::string card;
	std::getline(std::cin, card);
	while(card != "1" && card != "2" && card != "3" && card != "4" && card != "5")
	{
		std::cout << "Invalid number, please enter a number 1-5: ";
		std::getline(std::cin, card);
	}
	//following code gets the dealers hand and removes the card.
	Hand* h = &dealer.GetHand();
	int c = atoi(card.c_str());
	c--;
	h->erase(h->begin()+c);
	//adds face up card to the dealer.
	h->push_back(trumpCard);
	std::cout << dealer << ", your hand is now: ";
	dealer.ShowHand(std::cout);
}

// This function helps the SelectChoiceTrump by having each player choose a suit or pass.
Card::Suit ChooseSuit(std::vector<Player*> &players, std::vector<Card::Suit> &availableSuits, std::string &choiceOfSuits, 
					  Card::Suit &trumpSuit, Team &one, Team &two)
{
	int choice;
	for(int i = 0; i != players.size(); i++)
	{
		Player* temp = players;
		std::cout << *temp << ", please choose one of the following suits for trump, or pass\n";
		std::cout << choiceOfSuits << ".\n Your hand: ";
		temp->ShowHand(std::cout);
		std::string c;
		std::getline(std::cin, c);
		choice = atoi(c.c_str());
		if(choice != 3) 
		{
			trumpSuit = availableSuits[choice];
			trumpTeam = &SelectTrumpTeam(i, one, two);
			break;
		}
	}
	if(choice == 3)
	{
		std::cout << "No suit was chosen, clearing the cards.\n";
		ClearCards(players);
	}
	return trumpSuit;
}

// figures out the inital lead 
int SelectLead(int dealer)
{
	if(dealer < 3)
	{
		dealer += 1;
		return dealer;
	}
	else
	{
		return 0;
	}
}

// gets the current dealer, and figures out who the next dealer is.
int NextDealer(Player& theDealer, std::vector<Player*> &players)
{
	int dealer = NULL;
	for(int i = 0; i != players.size(); i++)
	{
		if(players == &theDealer)
		{
			dealer = i;
		}
	}
	if(dealer < 3)
	{
		dealer++;
	}
	else
	{
		dealer = 0;
	}
	return dealer;
}

void DisplayPoints(Team &one, Team &two)
{
	std::cout << "Team 1 points: " << one.GetTeamPoints() << ", Team 2 points: " << two.GetTeamPoints() << "\n";
}
// allows users to choose the suit for trump
Card::Suit SelectChoiceTrump(Card::Suit &trumpSuit, std::vector<Player*> &players, Team* trumpTeam, Team& one, Team& two)
{
	std::vector<Card::Suit> availableSuits;
	std::string choiceOfSuits;
	if(trumpSuit == Card::HEARTS)
	{
		availableSuits.push_back(Card::CLUBS);
		availableSuits.push_back(Card::DIAMONDS);
		availableSuits.push_back(Card::SPADES);
		choiceOfSuits = "options - CLUBS = 0, DIAMONDS = 1, SPADES = 2, PASS = 3";
	}
	else if(trumpSuit == Card::CLUBS)
	{
		availableSuits.push_back(Card::DIAMONDS);
		availableSuits.push_back(Card::HEARTS);
		availableSuits.push_back(Card::SPADES);
		choiceOfSuits = "options - DIAMONDS = 0, HEARTS = 1, SPADES = 2, PASS = 3";
	}
	else if(trumpSuit == Card::DIAMONDS)
	{
		availableSuits.push_back(Card::CLUBS);
		availableSuits.push_back(Card::HEARTS);
		availableSuits.push_back(Card::SPADES);
		choiceOfSuits = "options - CLUBS = 0, HEARTS = 1, SPADES = 2, PASS = 3";
	}
	else if(trumpSuit == Card::SPADES)
	{
		availableSuits.push_back(Card::CLUBS);
		availableSuits.push_back(Card::DIAMONDS);
		availableSuits.push_back(Card::HEARTS);
		choiceOfSuits = "options - CLUBS = 0, DIAMONDS = 1, HEARTS = 2, PASS = 3";
	}
	else
	{
		assert(false && "Suit ifs did not work.");
	}
	if(availableSuits.size() > 0)
	{
		int player = -1;
		trumpSuit = ChooseSuit(players, availableSuits, choiceOfSuits, trumpSuit, one, two);
	}
	return trumpSuit;
}

bool CheckForLeftBour(Card& playedCard, Card::Suit &trumpSuit)
{
	if(playedCard.GetRank() == Card::JACK)
	{
		if(trumpSuit == Card::CLUBS && playedCard.getSuit() == Card::SPADES)
		{
			return true;
		}
		else if(trumpSuit == Card::SPADES && playedCard.getSuit() == Card::CLUBS)
		{
			return true;
		}
		else if(trumpSuit == Card::DIAMONDS && playedCard.getSuit() == Card::HEARTS)
		{
			return true;
		}
		else if(trumpSuit == Card::HEARTS && playedCard.getSuit() == Card::DIAMONDS)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

Team RelatePlayerToTeam(Player &player, Team &one, Team &two)
{
	if(&one.GetPlayerOne() == &player || &one.GetPlayerTwo() == &player)
	{
		return one;
	}
	else if(&two.GetPlayerOne() == &player || &two.GetPlayerTwo() == &player)
	{
		return two;
	}
	else
	{
		assert(false && "RelatePlayerToTeam did not find an associated player.");
		return one;
	}
}

// Checks to see who won trick
Team CheckWinner(std::vector<Card*> &cardsInPlay, Card::Suit &trump, std::vector<Player *> &players, Team &one, Team &two)
{
	Card* leadCard = cardsInPlay[0]; 
	Card::Suit leadSuit = leadCard->getSuit();
	int winner = 0;
	for(int i = 1; i != cardsInPlay.size(); i++)
	{
		bool playingLeftBour = CheckForLeftBour(*cardsInPlay, trump);
		if(cardsInPlay->getSuit() == leadSuit && !playingLeftBour) 
		{
			if(cardsInPlay->GetValue() > cardsInPlay[winner]->GetValue())
			{
				winner = i;
			}
		}
		else if(cardsInPlay->getSuit() == trump || playingLeftBour)
		{
			if(cardsInPlay[winner]->getSuit() == trump)
			{
				if(cardsInPlay->GetValue() > cardsInPlay[winner]->GetValue())
				{
					winner = i;
				}
			}
			else
			{
				winner = i;
			}
		}
	}
	return RelatePlayerToTeam(*players[winner], one, two);
}

// Please note that this function would ideally be split many times
// try make functions serve a single purpose
int SelectDealerTrump(Card &trumpCard, const std::vector<Player*> &players, Player &dealer)
{
	std::cout << "The flipped card for this round is: " << trumpCard << "\n";
	bool response = false;
	int i = 0;
	for(i; i != players.size(); i++)
	{
		Player* temp = players;
		response = OrderUp(*temp, trumpCard);
		if(response) break;
	}
	if(!response)
	{
		return -1;
	}
	else
	{
		DiscardCard(dealer, trumpCard);
		return i;
	}
}

void DistributePoints(int teamOneTricks, int teamTwoTricks, bool alone, Team &one, Team &two)
{
	if(teamOneTricks > teamTwoTricks)
	{
		// if the team is euchred.
		if(&two == trumpTeam)
		{
			one.GivePoints(2);
		}
		else if(teamOneTricks == 5)
		{
			if(alone)
			{
				one.GivePoints(4);
			}
			else
			{
				one.GivePoints(2);
			}
		}
		else
		{
			one.GivePoints(1);
		}
	}
	if(teamTwoTricks > teamOneTricks)
	{
		// if the team is euchred
		if(&one == trumpTeam)
		{
			two.GivePoints(2);
		}
		else if(teamTwoTricks == 5)
		{
			if(alone)
			{
				two.GivePoints(4);
			}
			else
			{
				two.GivePoints(2);
			}
		}
		else
		{
			two.GivePoints(1);
		}
	}
}

void PlayEuchre()
{
	Deck deck = CreateDeck();
	Team one = CreateTeam(1);
	Team two = CreateTeam(2);
	int dealer = SelectDealer();
	std::vector<Player *> players = PopulatePlayers(players, one, two);
	while(!GameOver(one.GetTeamPoints(), two.GetTeamPoints()))
	{
		int lead = SelectLead(dealer);
		Player* theDealer = players[dealer];
		players	= OrganizePlayers(players,lead);
		std::cout << "The dealer for this round: " << *theDealer << '\n';
		Card trumpCard = Deal(players,deck);
		Card::Suit trumpSuit = trumpCard.getSuit();
		Card::Suit flippedSuit = trumpSuit;
		int playerThatCalled = SelectDealerTrump(trumpCard, players, *theDealer);		
		if(playerThatCalled == -1)
		{
			trumpSuit = SelectChoiceTrump(flippedSuit, players, trumpTeam, one, two);
		}
		else
		{
			trumpTeam = &SelectTrumpTeam(playerThatCalled, one, two);
		}
		std::string trump = SuitOutput(trumpSuit);
		std::cout << "The trump for this hand is: " << trump << "\n";
		// this if statement checks if the trumpSuit is still the same of the flipped suit
		// and if it went to the chosen suit. That way, the game knows everyone passed, and to go back to the start
		bool alone = false;
		if((trumpSuit != flippedSuit && playerThatCalled == -1) || trumpSuit == flippedSuit)
		{
			// integers to track tricks won by a team
			int teamOneTricks = 0;
			int teamTwoTricks = 0;
			for(int i = 0; i < 5; i ++)
			{
				std::vector<Card*> cardsInPlay;
				leadPlay = true;
				for(int i = 0;  i != players.size(); i++)
				{
					Player* temp = players;
					Card cardInPlay = PlayCard(*temp);
					cardsInPlay.push_back(&cardInPlay);
					DisplayCards(cardsInPlay);
				}
				// see which card won the trick. 
				Team wonTrick = CheckWinner(cardsInPlay, trumpSuit, players, one, two);
				if(&wonTrick == &one)
				{
					teamOneTricks++;
				}
				else if(&wonTrick == &two)
				{
					teamTwoTricks++;
				}
			}
			DistributePoints(teamOneTricks, teamTwoTricks, alone, one, two);
		}
		dealer = NextDealer(*theDealer, players);
		DisplayPoints(one, two);
		//go back to start of loop to start next round.
	}
}



[Edited by - agm_ultimatex on November 28, 2008 9:06:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
The break isn't very good programming style, but it should work the way you want it to. You should probably change it to something like this, just for the sake of style.



int SelectDealerTrump(Card &trumpCard, const std::vector<Player*> &players, Player &dealer)
{
std::cout << "The flipped card for this round is: " << trumpCard << "\n";
bool response = false;
//int i = 0;
for(int i=0; i < players.size(); i++)
{
Player* temp = players;
response = OrderUp(*temp, trumpCard);
if(response) break;
}




At first I thought the != was the problem, but after thinking about it for a while, I don't think that's the case. If you KNOW for a fact that it is crashing in this function, then the only things it could really be is that index i is outside the bounds of the array, or that whatever is in the array is not a valid pointer to the class player.

What I would do to continue from here is to cout some sort of message just before you return something, like:

cout << "here i am, returning from the function in location x" << endl;

Make sure you include the endl, or it won't flush the output buffer to the screen before the program crashes.. couldn't even tell you how many hours that has costed me, haha.

For good measure, put one at the start of the function as well.

If you see both messages, that means this function isn't the problem, and you need to look elsewhere, further along in the program.

If you see only the entry message, you KNOW there is a problem in the function somewhere. In this case, cout i just before you try to look in the array, as well as the size of the array.

If you don't see either message, then it's not even getting to the function, however I doubt this is the case..

Share this post


Link to post
Share on other sites
The reason i had i declared outside the loop, was so the program knows who called the suit. That way it knows which team is on offense, and which is on defense. Which can change how many points a team gets wether they win or not.

Share this post


Link to post
Share on other sites
been trying some of your suggestions. Here's the function now.


int SelectDealerTrump(Card &trumpCard, const std::vector<Player*> &players, Player &dealer)
{
std::cout << "The flipped card for this round is: " << trumpCard << "\n";
bool response = false;
int player = 0;
for(int i = 0; i != players.size(); i++)
{
Player* temp = players;
response = OrderUp(*temp, trumpCard);
player = i;
if(response) break;
}
if(!response)
{
return -1;
}
else
{
DiscardCard(dealer, trumpCard);
std::cout << "here i am, returning from the function in location x" << std::endl;
return player;
}
}



I thought that DiscardCard may have been causing the error. But that line after its called displays. The error i get from visual studio is: Unhandled exception at 0x693f0fb6 (msvcp90d.dll) in Euchre.exe: 0xC0000005: Access violation reading location 0x00000000.

Share this post


Link to post
Share on other sites
After some more debugging, I found the error is generated from this function:


Team SelectTrumpTeam(int player, Team& one, Team& two)
{
if(player == 0 || player == 2)
{
trumpTeam = &one;
}
else if(player == 1 || player == 2)
{
trumpTeam = &two;
}
return *trumpTeam;
}




It is called from:

void PlayEuchre()
{
Deck deck = CreateDeck();
Team one = CreateTeam(1);
Team two = CreateTeam(2);
int dealer = SelectDealer();
std::vector<Player *> players = PopulatePlayers(players, one, two);
while(!GameOver(one.GetTeamPoints(), two.GetTeamPoints()))
{
int lead = SelectLead(dealer);
Player* theDealer = players[dealer];
players = OrganizePlayers(players,lead);
std::cout << "The dealer for this round: " << *theDealer << '\n';
Card trumpCard = Deal(players,deck);
Card::Suit trumpSuit = trumpCard.getSuit();
Card::Suit flippedSuit = trumpSuit;
int playerThatCalled = SelectDealerTrump(trumpCard, players, *theDealer);
if(playerThatCalled == -1)
{
trumpSuit = SelectChoiceTrump(flippedSuit, players, trumpTeam, one, two);
}
else
{
trumpTeam = &SelectTrumpTeam(playerThatCalled, one, two);
}
std::string trump = SuitOutput(trumpSuit);
std::cout << "The trump for this hand is: " << trump << "\n";
// this if statement checks if the trumpSuit is still the same of the flipped suit
// and if it went to the chosen suit. That way, the game knows everyone passed, and to go back to the start
bool alone = false;
if((trumpSuit != flippedSuit && playerThatCalled == -1) || trumpSuit == flippedSuit)
{
// integers to track tricks won by a team
int teamOneTricks = 0;
int teamTwoTricks = 0;
for(int i = 0; i < 5; i ++)
{
std::vector<Card*> cardsInPlay;
leadPlay = true;
for(int i = 0; i != players.size(); i++)
{
Player* temp = players;
Card cardInPlay = PlayCard(*temp);
cardsInPlay.push_back(&cardInPlay);
DisplayCards(cardsInPlay);
}
// see which card won the trick.
Team wonTrick = CheckWinner(cardsInPlay, trumpSuit, players, one, two);
if(&wonTrick == &one)
{
teamOneTricks++;
}
else if(&wonTrick == &two)
{
teamTwoTricks++;
}
}
DistributePoints(teamOneTricks, teamTwoTricks, alone, one, two);
}
dealer = NextDealer(*theDealer, players);
DisplayPoints(one, two);
//go back to start of loop to start next round.
}
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!