react on keywords.

This is a discussion on react on keywords. within the C++ Programming forums, part of the General Programming Boards category; hi , I'm making a textbased game right now and I want it to react on key words . for ...

  1. #1
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Question react on keywords.

    hi , I'm making a textbased game right now and I want it to react on key words . for example :

    Code:
        char sent20[] = "the elf leads you to the throne room";    
    
    printf(sent20);
    how can I manipulate the program to make elf green in the out put and throne room for example blue ?

    thanks ... I hope :P
    PLay MystWind beta , within two years

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Windows Console Tutorials

    You can use those as a starting point.

  3. #3
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    hmm not really :S so far..
    PLay MystWind beta , within two years

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I want it to react on key words
    Well, you'll have to make it happen.

    >>how can I manipulate the program to make elf green in the out put and throne room for example blue ?
    Read Lithorien's link to learn about how to change the colors of console output.

    >>hmm not really :S so far..
    Why not? Just because it doesn't automagically cause printf (you really should be using cout, since this is c++) to recognize your 'keywords' doesn't mean it's useless. It just means you'll have to create a function yourself that recognizes keywords and prints them in the appropriate color. After learning how to change output color, you can also look into std::map and std::string, as they will certainly be useful if you plan on going through with this.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Here's another hint. Create a function (ChangeToColor() makes sense) that will parse a string, delimiting by whitespace, and if what you parse equals something to change to a color, do it.



    Edit: I reccomend an 2D array. Something such as this:

    char foo[512][512].

    Take this line for example.. say I wanted to stuff it in that array.

    foo[0][0 to 3] = "Take";
    foo[1][0 to 3] = "this";
    foo[2][0 to 3] = "line";
    foo[3][0 to 2] = "for";
    ...

    You get the picture, I hope.
    Last edited by Lithorien; 03-09-2005 at 07:35 PM.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, just realized that Lith's link was a Google search..
    Well, there's a how-to for console colouring in the FAQ here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    Anyway, I'm going to have a shot at this myself
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Here's what I came up with; it can't handle multiple-word keywords though.
    Colour.h
    Code:
    #ifndef COLOUR_H_
    #define COLOUR_H_
    
    #include <iostream>
    #include <map>
    #include <string>
    #include <windows.h>
    
    class Colourful
    {
    public:
    	Colourful() :hOut(GetStdHandle(STD_OUTPUT_HANDLE)) {}
    
    	void setKeyword(const std::string& key, WORD colour);
    	void delKeyword(const std::string& key);
    
    	static struct Endl
    	{}endl;
    	static struct Flush
    	{}flush;
    
    	Colourful& operator << (const std::string& str);
    	Colourful& operator << (const char ch);
    	Colourful& operator << (const Flush&);
    	Colourful& operator << (const Endl&);
    
    
    protected:
    	typedef std::map<std::string, WORD> SWMap_t;
    
    	SWMap_t keywords;
    	HANDLE hOut;
    };
    
    #endif
    Colour.cpp
    Code:
    #include "colour.h"
    
    Colourful::Endl Colourful::endl;
    Colourful::Flush Colourful::flush;
    
    Colourful& Colourful::operator << (const std::string& str)
    {
    	using std::string;
    	string::size_type pos1, pos2;
    	CONSOLE_SCREEN_BUFFER_INFO oldInfo;
    
    	pos2 = -1;
    	for(;;)
    	{
    		//Get a token
    		pos1 = pos2 + 1;
    		pos2 = str.find_first_of("\'\t\n .", pos1);
    		string token = str.substr(pos1, pos2 - pos1);
    
    		//Check for keyword, changing colour if necessary.
    		SWMap_t::iterator it = keywords.find(token);
    		if(it != keywords.end())
    		{
    			GetConsoleScreenBufferInfo(hOut, &oldInfo);
    			SetConsoleTextAttribute(hOut, it->second);
    		}
    
    		//Output the token
    		std::cout << token;
    
    		//If colour changed, revert to old state.
    		if(it != keywords.end())
    			SetConsoleTextAttribute(hOut, oldInfo.wAttributes);
    
    		//Output the delimiter
    		if(pos2 != string::npos)
    			std::cout << str[pos2];
    		else
    			break;
    	}
    
    	return *this;
    }
    Colourful& Colourful::operator << (const char ch)
    {
    	char str[2] = {ch, '\0'};
    	return operator<<(std::string(str));
    }
    Colourful& Colourful::operator << (const Colourful::Endl&)
    {
    	std::cout << std::endl;
    	return *this;
    }
    Colourful& Colourful::operator << (const Colourful::Flush&)
    {
    	std::cout << std::flush;
    	return *this;
    }
    
    void Colourful::setKeyword(const std::string& key, WORD colour)
    {
    	keywords[key] = colour;
    }
    
    void Colourful::delKeyword(const std::string& key)
    {
    	SWMap_t::iterator it = keywords.find(key);
    	if(it != keywords.end())
    		keywords.erase(it);
    }
    And the main.cpp used as a driver:
    Code:
    #include <iostream>
    #include "colour.h"
    
    int main()
    {
    	Colourful c;
    	c.setKeyword("ef", FOREGROUND_RED);
    	c << "abc  d\'ef" << " " << "def\n";
    	c.delKeyword("ef");
    	c << "abc  d\'ef" << " " << "def\n";
    
    	c.setKeyword("Kevin", FOREGROUND_BLUE | BACKGROUND_INTENSITY);
    	c << "Kevin Lam = teh r0x0r, and Kevin's most l33t h4x0r skillz have been unleashed." << "\n";
    	c.setKeyword("l33t", FOREGROUND_INTENSITY);
    	c.setKeyword("h4x0r", BACKGROUND_RED);
    	c << "Kevin Lam = teh r0x0r, and Kevin's most l33t h4x0r skillz have been unleashed." << "\n";
    
    	c.setKeyword("elf", FOREGROUND_GREEN);
    	c.setKeyword("throne_room", FOREGROUND_BLUE);
    
    	c << "The elf leads you to the throne_room." << Colourful::endl;
    	c << "The elf" << ' ' << "leads you to the " << 't' << "hrone_room.\n" << Colourful::flush;
    
    	std::cin.get();
    	return 0;
    }
    The output works pretty nicely (colour constants are the same as you would use for SetConsoleTextAttribute()), and although it doesn't handle other built-in types like int, double, etc.., it shouldn't be too hard to add support for those as well.
    Last edited by Hunter2; 03-10-2005 at 05:09 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    great thanks

    >> c.setKeyword("elf", FOREGROUND_GREEN);
    >> c.setKeyword("throne_room", FOREGROUND_BLUE);

    this is what I was looking for ty !
    Last edited by MystWind; 03-10-2005 at 11:02 AM.
    PLay MystWind beta , within two years

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to color certain keywords?
    By Purity in forum Tech Board
    Replies: 5
    Last Post: 10-20-2005, 03:07 PM
  2. super/interface keywords
    By xErath in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2005, 08:06 PM
  3. Google remembering my keywords
    By m712 in forum Tech Board
    Replies: 4
    Last Post: 07-26-2003, 12:48 PM
  4. new keywords
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2002, 03:57 AM
  5. C# Keywords
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-28-2001, 11:00 AM

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