How to make a label with random generated vertical lines

How to make a lable with random generated vertical lines

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 5346 Views - Last Post: 11 March 2010 - 11:12 AM Rate Topic: -----

#1 liquidninja  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 03-March 10

How to make a label with random generated vertical lines

Posted 09 March 2010 - 05:22 AM

Hey guys,
i want to make a little game in C# / WPF
i made a label in which i want to display a random number of " | "s between 21 and 35.
            String content;
            String sign= "|";

            Random randomNumber = new Random();
            int iRandom = randomNumber.Next(21, 35);
            for (int i = 0, i = iRandom, i++)
                content = content.add(dunno..
            label1.Content = "iRandom";


so what i want to get as result is for example a row of 27 I's like this: ||||||||||||||||||||||||||||||||||||||

after that, the player can choose to remove 1, 2 or 3 of these lines.. i made a small switch but it doesn work either

void Decrease(int& iRandom, int iNumber)// function decrease by player, iNumber is an int thats chosen by the player, iRandom is the random list of I's.
{
	switch(iNumber)
	{
	case 1: 	iRandom -= 1;
			break;
	case 2: 	iRandom -= 2;
			break;
	case 3: 	iRandom -= 3;
			break;
	}


can anyone help me fix this??

grtzz

This post has been edited by JackOfAllTrades: 09 March 2010 - 06:23 AM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: How to make a label with random generated vertical lines

#2 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 06:06 AM

First and foremost please :code:.

Now that that is out of the way here is a little something to get you going.
Random randomNumber = new Random();
int iRandom = randomNumber.Next(21, 35);
for (int i = 0, i = iRandom, i++)
content += sign; // simply concat the sign with what is currently in the content variable.
label1.Text = content; // set the label to display the string you have created.



Since I don't understand the game in the slightest I am having difficulty cleaning up the switch statement at all. Please explain what the player does here and I will see what I can do.
Was This Post Helpful? 0
  • +
  • -

#3 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 06:12 AM

It looks like the game 'NIM'
Was This Post Helpful? 0
  • +
  • -

#4 liquidninja  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 03-March 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 10:19 AM

Yes, its the game NIM.
I want to automatically generate a random number of stripes between 21 and 35.
Then, player one can choose to remove 1, 2 or 3 stripes. Next it's player 2's turn. He can also remove 1 2 or 3 stripes.
THey keep doing this until there's only one stripe left. The player that must remove the last stripe, loses.
So if there are only 4 stripes left and its my turn, i remove 3. You'll have to take the last one so you lose.
My friend made the game in C++, with a console application, but i want to remake it in C#. (he's ok with this)

If you want i can copy the C++ source code into an attachment, but its in dutch. I saw one of you was german.. i could post it anyway if you want, you'll understand it =)?

i tried the code but it says "( expected" and "{ expected". I just want the label to be filled when the window loads.
I tried window.loaded and stuff like that but nothing works.. cant find anything useful on google either.
Here's the C# part of what i have so far. Its driving me crazy lol

 
namespace NimSpel
{

    public partial class Spel : Window
    {

        public Spel()
        {
            String content;
            String sign = "|";

            Random randomNumber = new Random();
            int iRandom = randomNumber.Next(21, 35);

            for (int i = 0, i = iRandom, i++){
            content += sign;         
            label1.Text = content;      
            }
        }
    }
}


This post has been edited by liquidninja: 09 March 2010 - 12:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 12:26 PM

Syntax-wise, the code should look like this (instead of what you have):

namespace NimSpel
{

    public partial class Spel : Window
    {

        public Spel() 
        { 
            String content; 
            String sign = "|"; 
 
            Random randomNumber = new Random(); 
            int iRandom = randomNumber.Next(21, 35);

            for (int i = 0; i < iRandom; i++)
            {
                content += sign;
                label1.Text = content;
            } 
        }
    }
}



The separator in the for statement is the semicolon, not the colon. Also, you need to execute the for block while i < iRandom, not while both are equal.
Was This Post Helpful? 1
  • +
  • -

#6 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 12:28 PM

How big of a hurry are you in? I read up on Nim and I find myself having the urge to make it this weekend. Could be entertaining to create the logic for a computer player :P.

As for your code, what kind of controls do you have? How do the players articulate how many "sticks" they wanna pick up? The answers to these questions will help you to fully implement the proper method. I would say go with something like this.
private static void RemoveSticks(int removalVariable, Label control)
{
    control.Text = control.Text.Remove(control.Length - 1 - removalVariable, control.Length - 1);  // Seriously double check this line cause it doesn't look right to me, but the idea is there :P/>
}



Something along those lines should get you going. When I have more time I will try and do something a little prettier for you.
Was This Post Helpful? 0
  • +
  • -

#7 liquidninja  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 03-March 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 12:53 PM

View PostAdkins, on 09 March 2010 - 11:28 AM, said:

How big of a hurry are you in? I read up on Nim and I find myself having the urge to make it this weekend. Could be entertaining to create the logic for a computer player :P.

As for your code, what kind of controls do you have? How do the players articulate how many "sticks" they wanna pick up? The answers to these questions will help you to fully implement the proper method. I would say go with something like this.
private static void RemoveSticks(int removalVariable, Label control)
{
    control.Text = control.Text.Remove(control.Length - 1 - removalVariable, control.Length - 1);  // Seriously double check this line cause it doesn't look right to me, but the idea is there :P/>
}



Something along those lines should get you going. When I have more time I will try and do something a little prettier for you.

im in a very big hurry lol. I have to finish 3 projects this week for school. Vb.Net, C# and PHP. Sick =( i am dead!!!
check attachment for C++ code (its in dutch but i think you'll understand) i commented a little.Attached File  NIMGAME.txt (7.43K)
Number of downloads: 90

This post has been edited by liquidninja: 09 March 2010 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#8 liquidninja  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 03-March 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 01:17 PM

ok Core thanks alot. Stupid mistake. also label.Text doesnt work in C#, i had to be label.Content.
But now i get the error: "Use of unassigned local variable 'theLine'"

i placed the code to put the text inside the label outside the for section because i think its better if it first makes the string , and then places it.

        public Spel()
        {


            String theLine;
            String sign = "|";

            Random randomNumber = new Random();
            int iRandom = randomNumber.Next(21, 35);

            for (int i = 0; i < iRandom; i++)
            {
                theLine += sign; //error here, 
                
            }
            lblLucifer.Content = theLine;
        }


Was This Post Helpful? 0
  • +
  • -

#9 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 01:43 PM

I have always used label.Text. Not sure what the problem is with that.

To fix the variable problem simply use
string theLine = string.Empty;



I'll take a look at the code now and see what I can figure out for you.

EDIT:

It is going to take me a bit to get through this code. German is my second language so trying to interpret a third language based on similarities to it isn't exactly simple. I think what might be a good starting point for you would be to actually copy the code contained in the methods into your C# application. C# is a combination of C++, Java, and others so you might be able to reuse large amounts of that code by simply replacing the cin and cout statements.

This post has been edited by Adkins: 09 March 2010 - 01:49 PM

Was This Post Helpful? 0
  • +
  • -

#10 liquidninja  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 03-March 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 02:07 PM

yea i know, but its the replacing that doesnt work. I cant even display some stripes in a label...
adding that code DID fix the problem, but now i says the following:
I guess i should create an object or so.. but you cant put objects in labels. Adkins if you want i can translate the whole thing in english.. will take some time but it might be better for you..

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#11 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 02:28 PM

View PostAdkins, on 09 March 2010 - 07:28 PM, said:

How big of a hurry are you in? I read up on Nim and I find myself having the urge to make it this weekend. Could be entertaining to create the logic for a computer player :P.

I know the feeling! That's why I wrote an implementation that, once the human made a mistake, was unbeatable for a TI-57 programmable calculator in 1976 or 1977... It had 50 programming steps. One of my first attempts at game AI.
I must have the code somewhere in a box in the attic :-)

EDIT: being dutch myself, I had no trouble reading the code, but the way it is written is not very efficient or clear: lot's of repeated code and such...
for example the check for valid user input:
    while (!(iKeuze3 == 1 || iKeuze3 == 2 || iKeuze3 == 3))
    {
        cout << "Foute invoer: Hoeveel lucifers wilt u wegnemen [1], [2] of [3]?" << endl;
        cin >> iKeuze3;
        cout << endl;
    }


is all over the place, instead of in a function...
So using it as a starting point may not be very smart.

This post has been edited by Ferencn: 09 March 2010 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

#12 liquidninja  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 03-March 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 02:44 PM

Quote

I know the feeling! That's why I wrote an implementation that, once the human made a mistake, was unbeatable for a TI-57 programmable calculator in 1976 or 1977... It had 50 programming steps. One of my first attempts at game AI.
I must have the code somewhere in a box in the attic :-)

EDIT: being dutch myself, I had no trouble reading the code, but the way it is written is not very efficient or clear: lot's of repeated code and such...
for example the check for valid user input:

is all over the place, instead of in a function...
So using it as a starting point may not be very smart.


i know that its not very clear. My friend made it after he had only 10 lessons of C++ programming. But in C#, i will make a graphical user interface, with just the buttons 1,2 and 3, so no inputvalidation is needed. I also want to leave the "play vs computer" part behind due to lack of time. If i can get player vs player to work ill be VERY happy :)

heres the english version of the program:

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#13 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 02:45 PM

You try to assign a string to lblLucifer.Content
Try assigning it to lblLucifer.Text instead!
Was This Post Helpful? 0
  • +
  • -

#14 liquidninja  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 03-March 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 02:49 PM

View PostFerencn, on 09 March 2010 - 01:45 PM, said:

You try to assign a string to lblLucifer.Content
Try assigning it to lblLucifer.Text instead!


i already tried that

'System.Windows.Controls.Label' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Windows.Controls.Label' could be found (are you missing a using directive or an assembly reference?)
Was This Post Helpful? 0
  • +
  • -

#15 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: How to make a label with random generated vertical lines

Posted 09 March 2010 - 02:57 PM

When I add a label to a form in Visual Studio, it is generated as a
System.Windows.Forms.Label
Are you building a Forms-app?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2