I have to create a game

using arrays and such

Page 1 of 1

2 Replies - 551 Views - Last Post: 14 November 2009 - 06:15 PM Rate Topic: -----

#1 jmartin51  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 25-September 09

I have to create a game

Posted 14 November 2009 - 03:29 PM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LA4_2
{
	public partial class Form1 : Form
	{
		private StreamReader inFile;
		
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			int[] PresidentArray = new int[44];
			string PresidentValue = "";
			inFile = new StreamReader("presidents.txt");
			
			if (File.Exists("presidents.txt"))
			{
				try
				{
					
					inFile = new StreamReader("presidents.txt");
					while ((PresidentValue = inFile.ReadLine()) != null)
					{
						this.listBox1.Items.Add(PresidentValue);
					}  // while
				}  // try
				catch (System.IO.IOException exc)
				{
					listBox1.Text = exc.Message;
				}  // catch SystemIO
			}  //  if
			else
			{
				listBox1.Text = "File unavailable";
			}  // else
		}
		}


		

		
	}
 


ok this is what I have so far, I created the design basically its a list on the left and then a little output on the right with two radio buttons and a label on the bottom

now what I have to do it turn the list into a random array, I can do that

but I have to make it to where if someone clicks on one of the presidents a random president appears on the little output box to the right and then the radio buttons are pushed depending if the president came before or after the highlighted president on the right

im stuck at how to make appear on the right after it's been clicked

This post has been edited by jmartin51: 14 November 2009 - 05:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I have to create a game

#2 jmartin51  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 25-September 09

Re: I have to create a game

Posted 14 November 2009 - 04:54 PM

ok I got the array going, but how do I calculate a value for each input like washington is array[0]

Im sorry I keep bugging you guys but with this coding I figure something out and then a huge road block

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LA4_2
{
	public partial class Form1 : Form
	{
		private StreamReader inFile;
		
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			string[] PresidentArray = new string[44];
			string PresidentValue = "";
			inFile = new StreamReader("presidents.txt");
			int traversePresidentArray = 0;
			if (File.Exists("presidents.txt"))
			{
				try
				{
					
					inFile = new StreamReader("presidents.txt");
					
					while ((PresidentValue = inFile.ReadLine()) != null)
					{
						PresidentArray[traversePresidentArray] = Convert.ToString(PresidentValue);
						traversePresidentArray++;
						this.listBox1.Items.Add(PresidentValue);
					}  // while
				}  // try
				catch (System.IO.IOException exc)
				{
					listBox1.Text = exc.Message;
				}  // catch SystemIO
			}  //  if
			else
			{
				listBox1.Text = "File unavailable";
			}  // else

			
			
		}

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{

		}
	  


		}


Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: I have to create a game

Posted 14 November 2009 - 06:15 PM

What you could do is in the SelectedIndexChanged event for the listbox is create a random number using the Random class. You would use the Next method passing in 0 for the first parameter and the length of your array for the last parameter. You might want to do it inside of a do-while loop in case the random number selected is the selected index in the list box, it could happen. This is how you would use the Random class to generate a random number between 0 and the length of your array. It is okay to use the Length property of the array because this overload of the Random class returns a value between the first paramater and the second parameter excluding the number.

Random random = new Random();

int value = random.Next(0, PresidentArray.Length);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1