Zufallszahl

Slater

Erfahrenes Mitglied
Hy Leute,
Habe schon wider ein Problem (Spiel). Ich erstelle Zufallszahlen von 0 bis 99. Diese speichere ich in einem Array. beim nächsten Spielzug wird wider eine neue Zufallszahl ermittelt. Wenn es Sie aber schon gibt (Im Array gespeichert), soll eine neue definiert werden (Solange bis es eine trifft, die es noch nicht gibt....
Jedoch erhalte ich mit meinem untenstehenden Code mit der Zeit die Meldung : "System.StackOverflowException" :rolleyes: (Kein Wunder...)

Wie kann ich das problem sonst noch lösen?

Bin für Anregungen Dankbar.

Code:
private void Computer()
		{
			Random();
			Kombination = zahl;
			Prüfen();
		}
private void Random()
		{
			Random R =new Random();
			int I;
			int MaxLimit = 99;
			int MinLimit = 0;
			for(I = 0; I < 1; I++)
			{
				zahl = R.Next(MinLimit,MaxLimit);
			}
		}
private void Prüfen()
		{
			for (int i=0; i<100; i++)
			{
				if (Kombinationen[i] == Kombination)
				{
					Computer();
				}
			}
                   }
 
warum machst du das ganze nicht viel kürzer?

Code:
private void Computer()
{
     Random z  = new Random();
     Kombination = z.Next(0,99);
     foreach (int elem in Kombinationen)
     {
          if (elem  == zahl)
		{
			Computer();
		}
                else
                {
                        break;
                }
     }
      ...
}

Wo speicherst du denn die Zahl in dem Array? in einer anderen Methode?
 
Das habe ich gelöscht, als ich den Code "Sauber" ins Forum schrieb... Hier noch mal der Code:
(Funktioniert immer noch nicht :( )

Code:
int[] Kombinationen = new int[100];
private void Computer()
{
	 Random z  = new Random();
	Kombination = z.Next(0,99);
	foreach (int elem in Kombinationen)
	{
		if (elem  == zahl)
		{
			Computer();
		}
		else
		 {
			break;
	}
	Kombinationen[Index] = Kombination;
	Index++;
}
 
Bei mir klappt der Code einwandfrei, mal ein paar Fragen:

Sehe ich richtig, dass der Array "nacheinander" gefüllt wird. Wenn also die Methode Computer() aufgerufen wird, wird mithilfe der Variable Index, dem Array an der Stelle von Index ein Wert zugewiesen? Danach wird Index erhöht und neim nächsten Aufruf von Computer() wird wieder an der Stelle von Index eine Zahl hinzugefügt.

Code:
for (int i = 0; i < index; i++)
	{
		if (Kombinationen[i]  == zahl)
		{
			Computer();
		}
		else
		 {
			break;
                 }
	}

Damit wird dann wenigstens nicht 100mal geprüft, sondern nur so oft, wie Computer() schon aufgerufen wurde. Der Rest vom Array sollte dann ja sowieso noch nicht gefüllt sein.

Aber wiegesagt: Bei mir klappt es einwandfrei.

Kommt bei dem 2. Code den du versucht hattest die gleiche Fehlermeldung?
An welcher Stelle im Code tritt den der Fehler genau auf?
 
So, jetzt habe ich den Sinn des Programmes neu "gecodet".
Wenn man auf den Button1 clickt zeichnet es aus eine Zufallsrechteck (Wenn es schon "Gezeichnet" wurde, wird eine neue Zufallszahl ermittelt...)
Jedoch hat es eine "Fehler" drin. Kann ihn aber nicht finden :(
Irgdenwie geht er plötzlich in eine Endlosschleife....
Hänge mal den NEUEN Code an:


Edit: Code gelöscht (falscher Code :( )
 
Zuletzt bearbeitet:
Hm, ich sehe in dem Programm keine Zufallszahl oder etwas ähnliches und deinen 'alten'l Code auch nicht :)

Es gibt lediglich 10 Sätze mit einer for und 10 Sätze mit einer while Schleife aus, eine Endlosschleife gibt es auch nicht.

Vielleicht den falschen Code angehängt? :)
 
ops, ja, das ist ein grosser Fehler ;)
Hier noch der richtige Code.
1. Button1 -> Alle Felder leer zeichnen
2. Button2 -> Feld berechnen & zeichnen

Jedoch zeichnet es manchmal nichts :(
Warum?

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication8
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(360, 24);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(136, 24);
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Location = new System.Drawing.Point(16, 24);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(301, 301);
			this.pictureBox1.TabIndex = 1;
			this.pictureBox1.TabStop = false;
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(360, 152);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(144, 173);
			this.listBox1.TabIndex = 2;
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(360, 344);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(72, 20);
			this.textBox1.TabIndex = 3;
			this.textBox1.Text = "textBox1";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(360, 64);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(136, 24);
			this.button2.TabIndex = 0;
			this.button2.Text = "button1";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(328, 424);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(80, 20);
			this.textBox2.TabIndex = 4;
			this.textBox2.Text = "textBox2";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(328, 456);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(80, 20);
			this.textBox3.TabIndex = 4;
			this.textBox3.Text = "textBox2";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(328, 488);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(80, 20);
			this.textBox4.TabIndex = 4;
			this.textBox4.Text = "textBox2";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 542);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox4);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		ArrayList a = new ArrayList();
		int Index=100;
		int Kombination;
		int Zahl;
		int Split1;
		int Split2;
		private void Form1_Load(object sender, System.EventArgs e)
		{
			for (int i=0; i<100; i++)
			{
				a.Add(i);
			}
			for (int i=0; i<100; i++)
			{
				listBox1.Items.Add(a[i]);
			}
			
		}
		private void button1_Click(object sender, System.EventArgs e)
		{
			Graphics g = pictureBox1.CreateGraphics();
			for (int c=0; c<10; c++)
			{
				for (int i=0; i<10; i++)
				{
					g.DrawRectangle(new Pen(Color.Black),i*30,c*30,30,30);
				}
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			Berechnen();
			Zeichnen();
		}
		private void Berechnen()
		{
			Random z  = new Random();
			Kombination = z.Next(0,Index);
			Zahl = Convert.ToInt32(a[Kombination]);
			a.Remove(Kombination);
			Index--;
		}
		private void Zeichnen()
		{
			textBox1.Text = Convert.ToString(Zahl);
			listBox1.Items.Clear();
			for (int i=0; i<Index; i++)
			{
				listBox1.Items.Add(a[i]);
			}
			if (Zahl > 9)
			{
				Split1 = Convert.ToInt32(Convert.ToString(Zahl).Substring(0,1));
				Split2 = Convert.ToInt32(Convert.ToString(Zahl).Substring(1));
			}
			else
			{
				Split1 = 0;
				Split2 = Convert.ToInt32(Convert.ToString(Zahl).Substring(0,1));
			}
			textBox2.Text = Convert.ToString(Split1);
			textBox3.Text = Convert.ToString(Split2);
			textBox4.Text = Convert.ToString(Index);
			Graphics g = pictureBox1.CreateGraphics();
			g.FillRectangle(new SolidBrush(Color.Coral),Split2*30,Split1*30,30,30);
		}
	}
}
 
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication8
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(360, 24);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(136, 24);
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Location = new System.Drawing.Point(16, 24);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(301, 301);
			this.pictureBox1.TabIndex = 1;
			this.pictureBox1.TabStop = false;
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(360, 152);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(144, 173);
			this.listBox1.TabIndex = 2;
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(360, 344);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(72, 20);
			this.textBox1.TabIndex = 3;
			this.textBox1.Text = "textBox1";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(360, 64);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(136, 24);
			this.button2.TabIndex = 0;
			this.button2.Text = "button1";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(328, 424);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(80, 20);
			this.textBox2.TabIndex = 4;
			this.textBox2.Text = "textBox2";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(328, 456);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(80, 20);
			this.textBox3.TabIndex = 4;
			this.textBox3.Text = "textBox2";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(328, 488);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(80, 20);
			this.textBox4.TabIndex = 4;
			this.textBox4.Text = "textBox2";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 542);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox4);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		ArrayList a = new ArrayList();
		int Index=100;
		int Kombination;
		int Zahl;
		int Split1;
		int Split2;
		private void Form1_Load(object sender, System.EventArgs e)
		{
			for (int i=0; i<100; i++)
			{
				a.Add(i);
			}
			for (int i=0; i<100; i++)
			{
				listBox1.Items.Add(a[i]);
			}
			
		}
		private void button1_Click(object sender, System.EventArgs e)
		{
			Graphics g = pictureBox1.CreateGraphics();
			for (int c=0; c<10; c++)
			{
				for (int i=0; i<10; i++)
				{
					g.DrawRectangle(new Pen(Color.Black),i*30,c*30,30,30);
				}
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			Berechnen();
			Zeichnen();
		}
		private void Berechnen()
		{
			Random z  = new Random();
			Kombination = z.Next(0,Index);
			Zahl = Convert.ToInt32(a[Kombination]);
			a.RemoveAt(Kombination);
			Index--;
		}
		private void Zeichnen()
		{
			textBox1.Text = Convert.ToString(Zahl);
			listBox1.Items.Clear();
			for (int i=0; i<Index; i++)
			{
				listBox1.Items.Add(a[i]);
			}
			if (Zahl > 9)
			{
				Split1 = Convert.ToInt32(Convert.ToString(Zahl).Substring(0,1));
				Split2 = Convert.ToInt32(Convert.ToString(Zahl).Substring(1));
			}
			else
			{
				Split1 = 0;
				Split2 = Convert.ToInt32(Convert.ToString(Zahl).Substring(0,1));
			}
			textBox2.Text = Convert.ToString(Split1);
			textBox3.Text = Convert.ToString(Split2);
			textBox4.Text = Convert.ToString(Index);
			Graphics g = pictureBox1.CreateGraphics();
			g.FillRectangle(new SolidBrush(Color.Coral),Split2*30,Split1*30,30,30);
		}
	}
}

RemoveAt, weil wenn du eine Zahl mit Remove aus dem Array entfernst, verschieben sich die darüber liegenden eben um eine Stelle. Wenn 56 enfernt wurde ist Zahl 57 an Platz 56, usw :)
 

Neue Beiträge

Zurück