|
-
October 21st, 2012, 06:38 AM
#12
Re: Which container to do what I need?
Is this the correct way to use Paul's code of the map of structs in an array for 4 players?
Code:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <map>
using namespace std;
struct BetInfo
{
int betTotal; // Total amount of a particular bet on the table.
int coordX; // Horz coordinate of where to draw the chips for that bet.
int coordY; // Vert coordinate of where to draw the chips for that bet.
BetInfo(int xVal=0, int yVal=0, int zVal=0) : betTotal(xVal), coordX(yVal), coordY(zVal) {}
};
map<string, BetInfo> betInfoMap[4]; // Put before tmain to make instance of betInfoMap global.
enum {player1, player2, player3, player4}; // Set player1 = 0, player2 = 1, etc.
int _tmain(int argc, _TCHAR* argv[])
{
// Note that each time "insert(make_pair" is called, it creates a new struct in betInfoMap.
// In that process the inline constructor is called which initializes the struct values.
betInfoMap[player1].insert(make_pair("PassLine", BetInfo(0, 1, 2)));
betInfoMap[player1].insert(make_pair("PassBkup", BetInfo(0, 3, 4)));
betInfoMap[player1].insert(make_pair("NoPass", BetInfo(0, 5, 6)));
betInfoMap[player1].insert(make_pair("NoPassBkup", BetInfo(0, 7, 8)));
betInfoMap[player2].insert(make_pair("PassLine", BetInfo(0, 9, 10)));
betInfoMap[player2].insert(make_pair("PassBkup", BetInfo(0, 11, 12)));
betInfoMap[player2].insert(make_pair("NoPass", BetInfo(0, 13, 14)));
betInfoMap[player2].insert(make_pair("NoPassBkup", BetInfo(0, 15, 16)));
betInfoMap[player3].insert(make_pair("PassLine", BetInfo(0, 17, 18)));
betInfoMap[player3].insert(make_pair("PassBkup", BetInfo(0, 19, 20)));
betInfoMap[player3].insert(make_pair("NoPass", BetInfo(0, 21, 22)));
betInfoMap[player3].insert(make_pair("NoPassBkup", BetInfo(0, 23, 24)));
betInfoMap[player4].insert(make_pair("PassLine", BetInfo(0, 25, 26)));
betInfoMap[player4].insert(make_pair("PassBkup", BetInfo(0, 27, 28)));
betInfoMap[player4].insert(make_pair("NoPass", BetInfo(0, 29, 30)));
betInfoMap[player4].insert(make_pair("NoPassBkup", BetInfo(0, 31, 32)));
// Print the map key and associated struct values for all players (1-4) on the console.
for (int playerNbr=0; playerNbr < 4; playerNbr++)
{
cout << "\nPlayer Number = " << playerNbr+1 << endl;
map<string, BetInfo>::iterator it = betInfoMap[playerNbr].begin();
while(it != betInfoMap[playerNbr].end() )
{
cout << it->first << " -> (" << it->second.betTotal << "," << it->second.coordX << "," << it->second.coordY << ")\n";
++ it;
}
}
char ch;
cin >> ch;
return 0;
}
Thanks,
Raptor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|