Rock paper scissors code issue Const string and ints

Started by SinSin, Tue 25/06/2013 11:55:10

Previous topic - Next topic

SinSin

Hey

I'm trying to build a Rock, Paper, Scissors game with AGS however I have come across a problem while trying to set the string for my " computerPlayer "   can someone take a look at this and see what I'm doing wrong.

I am trying to set a selection based on the outcome of Random   you might see what I mean here


Code: AGS
computerChoice = Random(2);

if ( computerChoice == 0) {
computerChoice = "paper";}
else if ( computerChoice == 1) {
computerChoice = "rock";}
else {
computerChoice = "scissors";}


}


I also have a Global variable for computerChoice set as an int
Thanks in advance
Currently working on a project!

Adeel

#1
Quote from: Sinsin on Tue 25/06/2013 11:55:10
I also have a Global variable for computerChoice set as an int
Thanks in advance

    Int is used to store the numerical data only. I took a look at manual after reading your post. It says:

Quote from: Manualint  32-bit integer, can store from â€"2,147,483,648 to 2,147,483,647 

    So, what you need now  is 'String' rather than 'int'.

Easier Method:

Second option would be to create two separate variables. One as an int and the other one as String.

Tell me, if it helps.

SinSin

The thing is it needs to be an integer to be able to become a random selection doesn't it? 

What I've done

declared computerChoice as an integer
made the integer into a random number between 0-2
the problem lies when I try to convert the integer into a string

Is it just impossible to do ... AHHHH I GOT IT!!  maybe if I declare another variable instead of using the same one it will work

IE  computerDecision = an int then after it has randomised the number I can then make another variable be set as a string   

I hope I make sense here  lol

**edit thanks Adeel
Currently working on a project!

Adeel

#3
Quote from: Sinsin on Tue 25/06/2013 12:14:20
AHHHH I GOT IT!!  maybe if I declare another variable instead of using the same one it will work

IE  computerDecision = an int then after it has randomised the number I can then make another variable be set as a string   

I hope I make sense here  lol

    That was what I were trying to say. (roll) Take a look at this, you'll understand it although you have already got the hang of it.

Code: AGS
computerChoice = Random(2);    // where computerChoice is 'int' variable

 

if ( computerChoice == 0) {

computerDecision = "paper";}     // while computerDecision is 'String' variable

else if ( computerChoice == 1) {

computerDecision = "rock";}

else {

computerDecision = "scissors";}
 
}


EDIT:   You are welcome, Sinsin. I'm Glad I were of some help to you.

E.E:      You are welcome again, Sinsin.

SinSin

Currently working on a project!

Crimson Wizard

#5
Another quirky way to write this.

Code: ags

#define MAX_CHOICES 3;
String choices[MAX_CHOICES];

game_start()
{
   choices[0] = "paper";
   choices[1] = "rock";
   choices[2] = "scissors";
}

...

computerChoice = choices[Random(MAX_CHOICES - 1)];

Adeel

Quote from: Crimson Wizard on Tue 25/06/2013 12:45:19
Another quirky way to write this.

Code: ags

#define MAX_CHOICES 3;
String choices[MAX_CHOICES];

game_start()
{
   choices[0] = "paper";
   choices[1] = "rock";
   choices[2] = "scissors";
}

...

computerChoice = choices[Random(MAX_CHOICES - 1)];


    You are the boss, Crimson! :cheesy: I hope someday I learn enough scripting to provide solutions like these. So, Sinsin, you now have two options. Lucky guy.  :)

SinSin

Thank you very very much <---   double the thanks
Currently working on a project!

monkey0506

#8
Just a point of interest, but why is "const string" in the title of this thread? None of the code snippets I see here are using them. To be clear though, there is almost no reason for you (the reader) to use "const string" or even "string" any more. The few edge cases where it would be needed (working with a 2.7 or earlier project, or extremely quirky abuse of the language) are those where you would know well in advance. (Edit: On further reflection, I realized you were trying to assign a string-literal into an integer variable, so the error message (which wasn't specified) was probably something along the lines of "Cannot convert 'const string' to 'int'." Nevermind this first bit then.)

Expanding on CW's example, if the array does need to be global (e.g., you need to access it from multiple scripts), then you could do that like this:

Code: ags
// Script.ash
#define MAX_CHOICES 3
import String choices[MAX_CHOICES];

// Script.asc
String choices[MAX_CHOICES];
export choices;


I wanted to make note of that since you were originally using Global Variables.

Crimson Wizard

Quote from: monkey_05_06 on Thu 27/06/2013 05:59:34
Expanding on CW's example, if the array does need to be global (e.g., you need to access it from multiple scripts), then you could do that like this:
I am afraid this sentence may cause confusion (actually it did to me). What do you call "global" array? In your example it remains global, just kept in different module.

monkey0506

In AGS "global" means "global to the entire project's script files" (as per the "Global variables" pane's use of "global"). Something being "global at file scope" is still ultimately a "local" scope in terms of the project.

And I didn't say anything about moving the definition to a different script. I just showed the proper means of importing and exporting static arrays (as it's a relatively common follow-up question).

Crimson Wizard

Ooops, sorry monkey, I re-read that sentence and I see that I previously read it opposite to what it sais. Sorry, nevermind.

johanvepa

Quote from: Sinsin on Tue 25/06/2013 12:14:20
The thing is it needs to be an integer to be able to become a random selection doesn't it? 

What I've done

declared computerChoice as an integer
made the integer into a random number between 0-2
the problem lies when I try to convert the integer into a string

Is it just impossible to do ... AHHHH I GOT IT!!  maybe if I declare another variable instead of using the same one it will work

IE  computerDecision = an int then after it has randomised the number I can then make another variable be set as a string   

I hope I make sense here  lol

**edit thanks Adeel


But why do you need to convert the computer's choice into strings? Since there's only 3 options, it seems you could perfectly well (and simpler!) use the integers just as they are...?

Adeel

Quote from: Nanuaraq on Mon 01/07/2013 20:56:31
But why do you need to convert the computer's choice into strings? Since there's only 3 options, it seems you could perfectly well (and simpler!) use the integers just as they are...?

    That's because he wanted to compare the value which the player would type. Yes, you can do this with that integer variable only but adding another variable can make your life a lot easier. So it doesn't hurt to have a go...

SMF spam blocked by CleanTalk