Jump to content
Assassin7

A week of work, can't get this player code to run (C#)

Recommended Posts

As i'm not actually sure what you are trying to do, let me give a counter-example:

Random random = new Random();
int[] array = new int[] { 13, 23, 54, 3 };

int randomArrayIndex = random.Next(array.Length);
int randomArrayValue = array[randomArrayIndex];

That Array class you are using there is a pure utility class from what i can say without reading it up, you better not use it to create actual arrays.

(You may want to use it once you get into reflections - but you better not go there, it's a weird place.. :D )

Link to post
Share on other sites

hmm.

 

what I was trying to do, is have 2 arrays with the numbers 0-9 in them, so, when the user clicks the button to ask a question, the program will select a random number from each array and then display them in labels. say it selects 3 from the first arrray and 7 from the second array.

the user than will have to answer the question, which is an addition problem, I.E 3+7. the user inputs the answer (10) into a text box, he then clicks a button to say he has answered the question, the program then takes his answer and checks it against the answer it will calculate from the 2 values, displaying "Correct" or "Incorrect" in a label below the question box based on whether the user got the answer correct or not.

 

currently, the second part all works, or it does as much as I can test it. because the Arrays currently only return a value of 0, which gets set to the number variables, if I input 0 into the text box as the answer (0+0=0) it shows correct, any other number it shows incorrect. the problem I have is that it wont pick any values from the arrays. 

Link to post
Share on other sites

hmm.

 

what I was trying to do, is have 2 arrays with the numbers 0-9 in them, so, when the user clicks the button to ask a question, the program will select a random number from each array and then display them in labels. say it selects 3 from the first arrray and 7 from the second array.

the user than will have to answer the question, which is an addition problem, I.E 3+7. the user inputs the answer (10) into a text box, he then clicks a button to say he has answered the question, the program then takes his answer and checks it against the answer it will calculate from the 2 values, displaying "Correct" or "Incorrect" in a label below the question box based on whether the user got the answer correct or not.

 

currently, the second part all works, or it does as much as I can test it. because the Arrays currently only return a value of 0, which gets set to the number variables, if I input 0 into the text box as the answer (0+0=0) it shows correct, any other number it shows incorrect. the problem I have is that it wont pick any values from the arrays. 

arr, it won't take hints!! :(

So what you are doing with

 Array Random1 = new Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

is adding a generic multi-dimensional general purpose wrapper (i'm not even sure what the fuck that is).

 

Do this instead:

int[] Random1 = new int[] { 1,2,3,4,5,6,7,8,9,0 };

which creates a one-dimensional (as indicated by [] - [2,3] would mean two dimensions, first dimension with 2 items, 2nd dimension with 3 items, and so on..) array of elements of type integer, filled with the numbers 1-0 (the stuff inside the { } )

instead of

value1 = randomnumber.Next(Random1.Length);

which won't pull you a number out of that array but generate a random number between 0 and the number of items in that array (which are zero in your case, as you did specify dimensions inside that []'s, not items)

 

do the following:

int index1 = randomnumber.Next(Random1.Length);
int value1 = Random1[index1];

which gets you a random index (position in the array)

and then gets the value on that position and puts it into value1

 

Now dance and be happy. (especially the dancing part is important)

Edited by TheTac
Link to post
Share on other sites

You don't need arrays to generate random numbers in a range. Just use the random numbers directly. The arrays are superfluous and just confusing the issue. 

Link to post
Share on other sites

You don't need arrays to generate random numbers in a range. Just use the random numbers directly. The arrays are superfluous and just confusing the issue. 

cant, assessmemt criteria requires an array or list and as im technically overdue and nowhere near finishing I need all the points i can get to pass. :-/ 

Link to post
Share on other sites

The assessment criteria for this sound, quite literally, fucking retarded.

Shit like this RNG array nonsense is why TheDailyWTF is full of articles involving the discovery of actual, real-world usage of retarded RNG schemes like the one detailed above.

I guess now I know where newbie programmers learn this bullshit...

Edited by OOPMan
Link to post
Share on other sites

Overly complicated, inefficient and practically useless programming techniques.

Sounds just like the education system in general.

Link to post
Share on other sites

You sure like arguing with your teachers over there in the americas, huh? :)

Im not even from america, New Zealand :P

 

yeah its probably stupid, but W/E, I just need the pass to get the credits to get university entrance. 

 

okay, so I've added this new set of code in, it is picking random values from the array, unfortunately both values for both labels are the same number (I have tried adding 2 arrays with 2 different random classes, still no)

 

looks like this: also shown is the form running, with the answer showing and in the label below showing the value that b gets (which is supposedly adding both the values in the other labels together)  Edit: fixed that, had value1 and value2 declared as int twice which was ending up in them always returning a value of 0.

 

pgBrmZQ.jpg

GcqvHo0.jpg?1

Edited by Assassin7
Link to post
Share on other sites

also, how do I create an if statement that requires 2 criteria to be filled?

 

I.E I want it so that when the player touches the end of the maze at the finish label for a message box to pop up to say hes finished. 

something like

if (PlayerRec.X <=801, andif playerRec.Y <= 369)

{

messagebox.show("Congrats on winning!")

}

 

Link to post
Share on other sites

Normally you use &&, like so:

if (PlayerRec.X <=801 && playerRec.Y <= 369)

If you put methods in your if that you need to run, you use a single &. For example:

if (FirstMethod() & SecondMethod())

That's because with the && operator, it stops immediately if the first condition is false. If you need to run both methods, even if one were to return false, then you can use a single & to make sure both run.

If you need an OR, then it's ||

Link to post
Share on other sites

You sure like arguing with your teachers over there in the americas, huh? :)

I for one like to subtly persuade ignorant instructors to correct the error of their ways. I've found direct confrontation to be impolitic. In this case, we really don't know if the assignment itself or the OP's interpretation of those instructions are faulty.  What we do know is that a 1-to-1 identity mapping of randomly generated numbers using arrays isn't teaching good habits. 

Link to post
Share on other sites

Normally you use &&, like so:

if (PlayerRec.X <=801 && playerRec.Y <= 369)

If you put methods in your if that you need to run, you use a single &. For example:

if (FirstMethod() & SecondMethod())

That's because with the && operator, it stops immediately if the first condition is false. If you need to run both methods, even if one were to return false, then you can use a single & to make sure both run.

If you need an OR, then it's ||

Indeed. The former is what we call a boolean AND, the latter is a bitwise AND.

Link to post
Share on other sites

Never and OOPMan are absolutely correct. However, I would advise someone at the OP's knowledge level to store function results in temporary variables before performing logic operations with them. There are severe gotchas if you don't have a full understanding of operator preference and short circuit rules. I'd also avoid bit wise operators unless you explicitly need to do bit twiddling. It's just safer that way. I've tried to get in the habit of simplifying things like this so less experienced developers that might come later don't get bitten. 

Link to post
Share on other sites

ohgod, it works now, the messagebox pops up. except, about 10 of them pop up because the code keeps looping (I called it in the timer code)

is there a way to only perform the method once if its called?

 

also does the same thing with it in the gamepanel code area.

Edited by Assassin7
Link to post
Share on other sites

that works, but it needs to do it when it touches the end, rather than when you press the button while its touching the end.

Link to post
Share on other sites

Oh, then just use a variable to keep state (e.g. True means show the message, false means you've already shown it and reset to true when you restart the maze. )

Link to post
Share on other sites

I for one like to subtly persuade ignorant instructors to correct the error of their ways. I've found direct confrontation to be impolitic. In this case, we really don't know if the assignment itself or the OP's interpretation of those instructions are faulty.  What we do know is that a 1-to-1 identity mapping of randomly generated numbers using arrays isn't teaching good habits. 

True, true. I'm probably a bit too far on the "hoping" side. Like, hoping that teacher has an awesome plan that needs this build-up.. or bananas.

Link to post
Share on other sites

True, true. I'm probably a bit too far on the "hoping" side. Like, hoping that teacher has an awesome plan that needs this build-up.. or bananas.

The teacher doesnt set the standards, the Ministry of Education does. He just has to follow them. Its not entirely his fault.

 

Anyway, handed it in this morning. Fingers crossed its enough

Link to post
Share on other sites

The teacher doesnt set the standards, the Ministry of Education does. He just has to follow them. Its not entirely his fault.

 

Anyway, handed it in this morning. Fingers crossed its enough

I see, bad ministry, shame on you. :D

Well, good luck to you, hope all is working and well!

Probably not the right community for me here, so i guess i wont be logging back on, cya! :)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...