GIVE ME YOUR CODE

Not really. Anyways, I'm looking to create a program that simulates dialog. Why? For my own personal amusement of course. *pets evil cat*

I've been jumping around languages until I find one that works. All I've learned is that I suck at every one of them. I have a good feeling about C++ though. Well, maybe. I'd like the main program to simply be a reference point to call other objects where the actual dialog is contained, like the program's town square. I'd like it to have two boxes, the bottom one for choices the user can... well, choose... and the top for the reply.

Now, obviously, the most important part would be those objects. I'd like it to contain both the choices and the corresponding dialog.

For example:

Reply Box: What are you doing here?
Choice Box: Waiting for Amanda / None of your business / Enjoying the coffee

if the user chooses to say he's waiting for Amanda...

Reply Box: Why are you waiting for her?
Choice Box: We've been sneaking around behind your back / None of your business / Did I say Amanda? I meant Emily

if the user chooses to say none of your business, then object 2 is loaded....
if the user chooses to say he meant Emily, then object 3 is loaded...

Reply Box: I think it is my business. She's a close friend.
Choice Box: Now she's my close friend too, if you know what I'm saying / There's nothing wrong with having another friend to rely on / I thought you two were done for

It would go on from there.

I hope I've been clear here. If I haven't, just tell me and I'll try again. What resources can you suggest that would help me to successfully complete this project? This is probably the easiest thing in the world to accomplish, amirite?

Recommended Answers

All 9 Replies

you already have a simple pseudo that is really straight forward

-propmpt user...
-accept input.
-if ....
      do...(can be call function)
-else if ....
     do....(can be call function)

what about that cant you get??

Choice A - Reply A
     Choice A1
             Reply A1
                 Choice A1.1
                      Reply A1.1
                         Choice A1.1.1
                             Reply A1.1.1
                                Choice A1.1.1.1
                                    Ending A1.1.1.1
                                Choice A1.1.1.2
                                    Ending A1.1.1.2
                         Choice A1.1.2
                             Reply A1.1.2
                                Choice A1.1.2.1
                                    Ending A1.1.2.1
                                Choice A1.1.2.2
                                    Ending A..1.2.2
                         Choice A1.1.3
                             Reply A1.1.3
                                Choice A1.1.3.1
                                    Ending A1.1.3.1
                                Choice A1.1.3.2
                                    Ending A1.1.3.2
                 Choice A1.2
                      Reply A1.2
                         Choice A1.2.1
                             Reply A1.2.1
                                Choice A1.2.1.1
                                    Ending A1.2.1.1
                                Choice A1.2.1.2
                                    Ending A1.2.1.2
                         Choice A1.2.2
                             Reply A1.2.2
                                Choice A1.2.2.1
                                    Ending A1.2.2.1
                                Choice A1.2.2.2
                                    Ending A1.2.2.2
                         Choice A1.2.3
                             Reply A1.2.3
                                Choice A1.2.3.1
                                    Ending A1.2.3.1
                                Choice A1.2.3.2
                                    Ending A1.2.3.2
     Choice A2
             Reply A2
                 Choice A2.1
                      Reply A2.1
                         Choice A2.1.1
                             Reply A2.1.1
                                Choice A2.1.1.1
                                    Ending A2.1.1.1
                                Choice A2.1.1.2
                                    Ending A2.1.1.2
                         Choice A2.1.2
                             Reply A2.1.2
                                Choice A2.1.2.1
                                    Ending A2.1.2.1
                                Choice A2.2.2
                                    Ending A2.1.2.2
                         Choice A2.1.3
                             Reply A2.1.3
                                Choice A2.1.3.1
                                    Ending A2.1.3.1
                                Choice A2.1.3.2
                                    Ending A2.1.3.2

Some of these will also tie into each other. For example, Choice A3.2.1 and Choice A4.1.1 could both lead to the same reply. I need to store the replies and choices in seperate objects that can call upon each other, then link that into the main program.

Possibly some finite state machine?
http://en.wikipedia.org/wiki/Finite_state_machine

First decide what the states are and how many there are, then decide what states can legally go to what other states. You have a starting state. You have prompt that goes with each state. You have one or more legal replies for each state, each of which lead to a different state. There are a variety of ways to organize this. It's kind of hard to explain in words. First I'd pick all of your state and the prompts that go with them. Put arrows from state to state for legal state transistions. Each arrow has a string response that goes along with it. So if you had 4 states , one way of doing it would be two have a 2-dimensional 4 x 4 array of string responses. Illegal state transitions would be empty strings.

const int NUM_STATES = 4;
const int START_STATE = 0;
string prompts[NUM_STATES];
string responses[NUM_STATES][NUM_STATES];
prompts[0] = "What are you doing here?";
// assign values for rest of prompts array.
responses[0][0] = ""; // illegal choice.  No transition from state 0 to state 0.
responses[0][1] = "Waiting for Amanda";
responses[0][2] = "None of your business";
responses[0][3] = "Enjoying the coffee";
// assign values for rest of responses array.
int curState = START_STATE;

while (true)
{
     cout << prompts[curState];
     // display legal responses.  Prompt user for response.
     // assign curState to be user response.
}

For now, I have 18 messages to display to the user, which are a combination of a question and your chosen response. There are a total of 13 decisions.

Before I go try to dig up some code for getting user input and displaying responses, did you mean something like this?

const int NUM_STATES = 13;
const int START_STATE = 0;
string prompts[NUM_STATES];
string responses[NUM_STATES][NUM_STATES]
prompts[0] = "What are you doing here?";
prompts[1] = "Amanda? Why would you be waiting for Amanda?";
prompts[2] = "Emily? Yea, right. Don't tell me you got college girl Emily.";
prompts[3] = "You're hiding something. You hate this cafe.";
prompts[4] = "Oh, so all of a sudden you enjoy their coffee? Just like that? No more talk of how it's straight from the gutters of Colombia?";

responses[0][0] ="";
responses[0][1] = "Say you're waiting for Amanda";
responses[0][2] = "Shake your head";
responses[0][3] = "Raise your cup";
responses[0][4] = "Tell him the whole story";
responses[0][5] = "Make something up";
responses[0][6] = "Bluff out of it";
responses[0][7] = "Say you did";
responses[0][8] = "Admit to lying";
responses[0][9] = "Say it's another Emily";
responses[0][10] = "Say no";
responses[0][11] = "Taunt him";
responses[0][12] = "Tell him the truth";
responses[0][13] = "Say you like it";
responses[0][14] = "Admit to lying";

int curState = START_STATE

while (true)
{
cout << prompts[curState];
"You: I'm waiting here for Amanda, buddy. Rob: Amanda? Why would you be waiting for Amanda?";
"You: It's not really any of your business. Rob: You're hiding something. You hate this cafe.";
// etc...
}

For now, I have 18 messages to display to the user, which are a combination of a question and your chosen response. There are a total of 13 decisions.

Before I go try to dig up some code for getting user input and displaying responses, did you mean something like this?

const int NUM_STATES = 13;
const int START_STATE = 0;
string prompts[NUM_STATES];
string responses[NUM_STATES][NUM_STATES]
prompts[0] = "What are you doing here?";
prompts[1] = "Amanda? Why would you be waiting for Amanda?";
prompts[2] = "Emily? Yea, right. Don't tell me you got college girl Emily.";
prompts[3] = "You're hiding something. You hate this cafe.";
prompts[4] = "Oh, so all of a sudden you enjoy their coffee? Just like that? No more talk of how it's straight from the gutters of Colombia?";

responses[0][0] ="";
responses[0][1] = "Say you're waiting for Amanda";
responses[0][2] = "Shake your head";
responses[0][3] = "Raise your cup";
responses[0][4] = "Tell him the whole story";
responses[0][5] = "Make something up";
responses[0][6] = "Bluff out of it";
responses[0][7] = "Say you did";
responses[0][8] = "Admit to lying";
responses[0][9] = "Say it's another Emily";
responses[0][10] = "Say no";
responses[0][11] = "Taunt him";
responses[0][12] = "Tell him the truth";
responses[0][13] = "Say you like it";
responses[0][14] = "Admit to lying";

int curState = START_STATE

while (true)
{
cout << prompts[curState];
"You: I'm waiting here for Amanda, buddy. Rob: Amanda? Why would you be waiting for Amanda?";
"You: It's not really any of your business. Rob: You're hiding something. You hate this cafe.";
// etc...
}

One, i don't know that you want to hard-code all the string values for the arrays. There are other ways to do it. I hard-coded them in my example since I only listed a few of them. The point is to get them into the arrays somehow.

Two, my understanding from your example was that there are three possible responses to this prompt:

Reply Box: What are you doing here?
Choice Box: Waiting for Amanda / None of your business / Enjoying the coffee

I chose the starting state to be this one since this is, from what I can tell, the beginning of the dialog. I called the STARTING_STATE state 0, so:

prompts[0] = "What are you doing here?";

You ahve three possible answers to this question, so you'll have three non-empty strings in your responses[0] array. These will lead to three different states. I chose those states to be states 1, 2, and 3.
Thus,

responses[0][0] = ""; // cannot get to state 0 from state 0.
responses[0][1] = "Waiting for Amanda";
responses[0][2] = "None of your business";
responses[0][3] = "Enjoying the coffee";
for (int i = 4; i < 13; i++)
     responses[0][i] = ""; // cannot get to state i from state 0

If they answer "Waiting for Amanda", you go to state 1. If they answer "None of your business", you go to state 2. If they go to "Enjoying the coffee", you go to state 3. The second index (in this case, 1, 2, and 3) is the state that the corresponding answer leads you to. You'll have 13 states, so you'll have a 13 element prompts array and a 13 x 13 element responses array, the vast majority of which will be empty strings, as in the example above. 10 of the 13 strings with 0 as the first index are empty strings, representing that you can't get there from state 0.

Say the user says he's waiting for Amanda. From your first post:

Reply Box: Why are you waiting for her?
Choice Box: We've been sneaking around behind your back / None of your business / Did I say Amanda? I meant Emily

prompts[1] = "Why are you waiting for her?";
responses[1][a] = "We've been sneaking around behind your back";
responses[1][b] = "None of your business";
responses[1][c] = "Did I say Amanda? I meant Emily";

Replace a, b, and c with whatever states you go to if that answer is picked. If the user picks "None of your business", you'll go to state b (for whatever value of b that is). responses[1][i] = "" for all values of i other than the three values above.

So start by filling out your prompts[] array. You'll have a number of possibilities for answers, each of which will end with another prompt. Fill in your responses[][] array according to that. If this conversation ever has a chance to end, you might have an end/terminating state too. From a coding standpoint, since most strings will be empty, you might want to initialize everything as a blank string, then overwrite those that aren't. So lines 11 through 25 in your code aren't what I had in mind. You would have only three non-empty strings where the first index was 0, and all would be less than the number of states, which is 13, so lines 24 and 25 would have indexes that are out of bounds.

Okay, I feel like a complete idiot. I have no idea what I'm doing here. I've been going through tutorials and examples for the past two days and have gotten nowhere.

I honestly have no idea how to accept user input from clicking a choice, and display an output of plain text, which is where I obviously need to start. I must be looking in the wrong place for tutorials, so can you guys recommend anything? Please?

Okay, I now have this

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{
	char name[50];
	int x = 0;

	cout << "Please enter your first name : ";
	cin >> name;
	system ("cls");

	cout << "How are you doing, " << name << "?" << endl
		<< "1: Fine." << endl
		<< "2: Go away." << endl
		<< "Reply: "; cin >> x;

	system ("cls");

	if(x==1)
	{
		cout << "Okay, then." << endl;
		system("pause");
	}
	else if (x==2)
	{
		cout << "Suit yourself." << endl;
		system("pause");
	}
	else
	{
		cout << "What?" << endl;
			system("pause");
	}

	return 0;
}

I couldn't figure out how to do this in a window so I used the console. It works, but it's going to be hell doing it like this, because it seems like I'll have to create an endless list of if else statements. I'd like to pull it from some kind of database.

Okay, I now have this

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{
	char name[50];
	int x = 0;

	cout << "Please enter your first name : ";
	cin >> name;
	system ("cls");

	cout << "How are you doing, " << name << "?" << endl
		<< "1: Fine." << endl
		<< "2: Go away." << endl
		<< "Reply: "; cin >> x;

	system ("cls");

	if(x==1)
	{
		cout << "Okay, then." << endl;
		system("pause");
	}
	else if (x==2)
	{
		cout << "Suit yourself." << endl;
		system("pause");
	}
	else
	{
		cout << "What?" << endl;
			system("pause");
	}

	return 0;
}

I couldn't figure out how to do this in a window so I used the console. It works, but it's going to be hell doing it like this, because it seems like I'll have to create an endless list of if else statements. I'd like to pull it from some kind of database.

I am somewhat of a novice as far as C++ GUI dialog boxes go, so I won't offer any suggestions there. My earlier suggestions regarding using a finite state machine are independent of how you receive and display the data. The reason for that suggestion is precisely to avoid having tons of if statements. You could use a database certainly, but in my view it is unnecessary, more complicated, and less efficient, and even if you used one, you'd probably want to end up sticking it in an array anyway, so I wouldn't bother. It is certainly feasible and something to consider though.

Display your prompt for your current state, then display the possible responses. Each response will lead to a new (or possibly the same) state. You'll assign the current state to be this new state and repeat the process. You'll either set up this conversation to go forever, or it will have some state that gets you out of the loop.

const int START_STATE = 0;
int curState = START_STATE;
while (true)
{
     cout << prompts[curState] << endl;
     DisplayPossibleAnswers (curState);
     curState = GetUserAnswer (curState);
}

I think that's your best bet. Before coding, you need to write out on paper the exact flow of the conversation. Develop your matrices from there. Any if or switch/case statements would be in these two functions:

void DisplayPossibleAnswers (int);
int GetUserAnswer (int);

Both of these functions will use the current state and the responses array. This is the easiest way I can think of to do what you are trying to do, but it requires you to diagram your conversation ahead of time. Software that could help you do that is:

JFLAP : http://www.cs.duke.edu/csed/jflap/
Visio : http://office.microsoft.com/en-us/visio/default.aspx
Dia : http://www.gnome.org/projects/dia/

Not sure if this is new to you or whether the concept makes sense or whether you want to go this route. The concept/algorithm and the coding implementation are fairly independent. Like I said, this is the route I would go. I definitely don't think you want to be hard-coding your cout statements. The advantage of using matrices is you can change the string values for new conversations without changing the program much, if it all. Your database idea is along the same lines.

Thanks alot. That Dia is good stuff. I'm not going to bother with this Amanda demo crap and get right into making the game I intended on. I'll write all the dialog in that program before I try to implement it in C++. I'll check out that array function, it sounds just like what I'm looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.