C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-07-2007, 09:11 AM   #1
Absent Minded Programmer
 
Join Date: May 2005
Posts: 933
Having trouble making a function do what I want.

Okay, I'm trying to create a text interface with my console for a blackjack game, I'm trying to make a function called init_output to setup a sort of template for output where certain fields can be updated.

This is the structure defining the "menu" of sorts.
Granted it isn't pretty, but eventually I want to add some serialization to this class and make a text editor that can save these structures to the hard drive, so I can load masses of them for a text adventure or something.

Code:
struct text_menu
{
	typedef std::map<int, std::string> tmenu;
	text_menu()			//Construct Blackjack text interface
	{
		int id;			//declare ID for output item
		std::string output;		//declare the actual output item
		id = 0;
		output = "Dealer's Cards";
		text.insert(std::make_pair(id, output));
		id = 1;
		output = "--------------";
		text.insert(std::make_pair(id, output));
		id = 2;
		output = "Nothing Here.";
		text.insert(std::make_pair(id, output));
		id = 1;
		output = "--------------";
		text.insert(std::make_pair(id, output));
		id = 3;
		output = "Player's Cards";
		text.insert(std::make_pair(id, output));
		id = 1;
		output = "--------------";
		text.insert(std::make_pair(id, output));
		id = 4;
		output = "Nothing Here.";
		text.insert(std::make_pair(id, output));
		id = 1;
		output = "--------------";
		text.insert(std::make_pair(id, output));
		id = 5;
		output = "What would you like to do?\n1) Hit me\n2) Stay\n3) Fold\n";
		text.insert(std::make_pair(id, output));
	}
	tmenu text;
};
All I'm doing with that is creating a map of strings and ID's to those strings. I may want to make a list of 0's and 1's for every entry into the map so I know which items are dynamic, or updateable, and which items are static.

Here is the IO class that I will use to manage these structures in massive quantities (eventually), right now I have the structure hard coded but I've realized essentially it's a file type.

Code:
class IO
{
public:
	typedef	std::map<int, std::string> output;
	
	void update_output(int id, std::string update)
	{
		output::iterator index = coutput.find(id);
		(*index).second = update;
	}

	void init_output(text_menu text)
	{
		
	}

	void display_output()
	{
		for( output::iterator index = coutput.begin(); index != coutput.end(); ++index)
		{
			cout << (*index).second << std::endl;
		}
	}
	int get_input()
	{
		std::cin >> input;
		return input;
	}
	void clear_screen()
	{
		system("cls");
	}
	output coutput;
	int input;
	
};
#endif
While the update_output function strictly is for usage of the game to update certain complex strings like a players hand or something, if I had a list of 1's and 0's in the structure I could accomplish this better, because then I could return a variable to the system to automatically connect game updates with text updates and such.

Essentially though I'm having trouble with the init_output function. I need to copy the map in tmenu.text over to coutput, I've been googling copy stuff for maps but I havn't found anything that really explained it, can anyone help?
__________________
Sometimes I forget what I am doing when I enter a room, actually, quite often.
Shamino is offline   Reply With Quote
Old 12-07-2007, 09:40 AM   #2
ZuK
Registered User
 
Join Date: Aug 2005
Posts: 1,330
text_menu::text and IO::coutput are of the same type, both text_menu::text and IO::coutput are declared public -> Simple assignement could be used.
e.g.
Code:
	void init_output(text_menu text)
	{
		coutput = text.text;
	}
Kurt
ZuK is offline   Reply With Quote
Old 12-07-2007, 09:58 AM   #3
Absent Minded Programmer
 
Join Date: May 2005
Posts: 933
Wow, "that was easy"

Can I insert it rather than set it equal to?

So I can load multiple menu's into the coutput map.
__________________
Sometimes I forget what I am doing when I enter a room, actually, quite often.
Shamino is offline   Reply With Quote
Old 12-07-2007, 10:22 AM   #4
Absent Minded Programmer
 
Join Date: May 2005
Posts: 933
I'm having a problem with the output, with this function:
Code:
	void play_game()
	{
		dealer.shuffle_deck(deck);
		int gameloop = 1;
		io.init_output(tmenu);
		while(gameloop != 0)
		{
			io.display_output();
			gameloop = blackjack(io.get_input()); 

			io.clear_screen();
		}	
		
	}
For some reason it's not copying over all my dividers as you see I created here, are duplicate ID's not allowed?

Code:
struct text_menu
{
	typedef std::map<int, std::string> tmenu;
	text_menu()			//Construct Blackjack text interface
	{
		int id;			//declare ID for output item
		std::string output;		//declare the actual output item
		id = 0;
		output = "Dealer's Cards";
		text.insert(std::make_pair(id, output));
		id = 1;
		output = "--------------";
		text.insert(std::make_pair(id, output));
		id = 2;
		output = "Nothing Here.";
		text.insert(std::make_pair(id, output));
		id = 1;
		output = "--------------";
		text.insert(std::make_pair(id, output));
		id = 3;
		output = "Player's Cards";
		text.insert(std::make_pair(id, output));
		id = 1;
		output = "--------------";
		text.insert(std::make_pair(id, output));
		id = 4;
		output = "Nothing Here.";
		text.insert(std::make_pair(id, output));
		id = 1;
		output = "--------------";
		text.insert(std::make_pair(id, output));
		id = 5;
		output = "What would you like to do?\n1) Hit me\n2) Stay\n3) Fold\n";
		text.insert(std::make_pair(id, output));
	}
	tmenu text;
};
Any idea's? Here is the output:
Code:
Dealer's Cards
--------------
Nothing Here.
Player's Cards
Nothing Here.
What would you like to do?
1) Hit me
2) Stay
3) Fold
__________________
Sometimes I forget what I am doing when I enter a room, actually, quite often.
Shamino is offline   Reply With Quote
Old 12-07-2007, 10:47 AM   #5
The larch
 
Join Date: May 2006
Posts: 3,222
No, map doesn't allow duplicate keys.

Why don't you make a simple array (vector) of strings you want to print?
__________________
I might be wrong.

Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 12-07-2007, 10:57 AM   #6
Absent Minded Programmer
 
Join Date: May 2005
Posts: 933
Can I still search the vector using an ID? I guess the ID is the index? Because there are some items that I would want to reuse, like dividers for instance.

I could use string compare right?
__________________
Sometimes I forget what I am doing when I enter a room, actually, quite often.
Shamino is offline   Reply With Quote
Old 12-07-2007, 11:01 AM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by Shamino View Post
Can I still search the vector using an ID? I guess the ID is the index? Because there are some items that I would want to reuse, like dividers for instance.

I could use string compare right?
But if you are re-using the same thing, surely you only need one, and it can have ONE unique ID - I appologize if this isn't really what you asked - I haven't read ALL of the code and text in the thread...

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 12-07-2007, 11:03 AM   #8
The larch
 
Join Date: May 2006
Posts: 3,222
Well, you could have a vector of available strings:
Code:
vec[0] = " "
vec[1] = "This"
vec[2] = "is"
vec[3] = "a"
vec[4] = "sentence"
vec[5] = "."
And then you might have a vector of numbers that tell you which items to use in which order:
Code:
sentence[] = {1, 0, 2, 0, 3, 0, 4, 5}
__________________
I might be wrong.

Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 12-07-2007, 11:14 AM   #9
Absent Minded Programmer
 
Join Date: May 2005
Posts: 933
I'm having trouble getting the vector setup to compile right, it's mostly just synatx issues:
Code:
#ifndef IO_H
#define IO_H
#include <iostream>
#include <string>
#include <map>
#include <string>

struct text_menu
{
	typedef std::vector<std::string> tmenu;
	text_menu()			//Construct Blackjack text interface
	{
		std::string output;		//declare the actual output item
		output = "Dealer's Cards";
		text.push_back(output);
		output = "--------------";
		text.push_back(output);
		output = "Nothing Here.";
		text.push_back(output);
		output = "--------------";
		text.push_back(output);
		output = "Player's Cards";
		text.push_back(output);
		output = "--------------";
		text.push_back(output);
		output = "Nothing Here.";
		text.push_back(output);
		output = "--------------";
		text.push_back(output);
		output = "What would you like to do?\n1) Hit me\n2) Stay\n3) Fold\n";
		text.push_back(output);
	}
	tmenu text;
};

class IO
{
public:
	typedef	std::vector<std::string> output;
	
	void update_output(int id, std::string update)
	{
	}

	void init_output(text_menu text)
	{
		coutput = text.text;
	}

	void display_output()
	{
		for (int index = 0; index < coutput.size(); index++)
		{
		}
		/*
		for( output::iterator index = coutput.begin(); index != coutput.end(); ++index)
		{
			cout << (*index).second << std::endl;
		}
		*/
	}
	int get_input()
	{
		std::cin >> input;
		return input;
	}
	void clear_screen()
	{
		system("cls");
	}
	output coutput;
	int input;
	
};
#endif
That is the new text_menu structure and IO class.

I'm having trouble with the syntax for updating one of the elements, as well as the syntax for outputting the whole deal.

Any ideas?
__________________
Sometimes I forget what I am doing when I enter a room, actually, quite often.
Shamino is offline   Reply With Quote
Old 12-07-2007, 11:20 AM   #10
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,783
Code:
cout << coutput[index];
For update you can try:
Code:
coutput[id] = update;
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which Library Files for these unreferenced functions? lehe C++ Programming 3 01-31-2009 10:30 PM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
help on making int to string from function peter_hii C++ Programming 5 04-12-2006 02:58 PM
const at the end of a sub routine? Kleid-0 C++ Programming 14 10-23-2005 06:44 PM
c++ linking problem for x11 kron Linux Programming 1 11-19-2004 10:18 AM


All times are GMT -6. The time now is 08:14 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22