I've been given the task to make a hangman like program, and it seemed simple to me, but I just can't make the code behave like I want it to. I need the code to check if the array "placement" is a session variable, and if not it needs to set up an array of equal length to the array "arr".
Code:
// Default.aspx.cs created with MonoDevelop
// User: Lynx at 14:08*02/16/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Web;
using System.Web.UI;
namespace Finntalliarray
{
public partial class Default : System.Web.UI.Page
{
int[] placement={0,0,0,0,0,0};
protected void page_Load(object sender, EventArgs e)
{
if(Session["placement"]!=null)
{
int[] placement=(int[])Session["placement"];
}
}
protected void guessbutton_Click(object sender, EventArgs e)
{
// Empty existing labels
answer.Text="";
result.Text="";
check.Text="";
/*
This is where I set up my variables.
arr contains the word the user is supposed to guess.
y is used to count how many times the chosen sign appears.
*/
string[] arr={"h","u","r","r","r","r"};
int y=0;
// Code which checks if the guessed sign exists in arr, and then saves its position in placement.
for (int x=0; x<arr.Length; x++)
{
if (arr[x]==guessbox.Text.ToLower())
{
y++;
placement[x]=1;
}
}
result.Text+="The sign " + guessbox.Text + " appears " + y + " time(s).<br/>";
result.Text+=guessbox.Text + " is at index(es): ";
// Code which prints the index of the guessed sign, if it exists.
for (int x=0; x<placement.Length;x++)
{
if (placement[x]==1)
{
result.Text+=x + ",";
}
}
// Code which fetches the signs that have been guessed, and prints them in the answer label. If it hasn't been guessed, an underscore takes its place.
for(int item=0;item<placement.Length;item++)
{
if (placement[item]!=0)
{
answer.Text+=arr[item];
}
else
{
answer.Text+="_";
}
}
// This is just here to print the placement array values for me to see that they are stored properly, which they aren't.
for(int t=0;t<placement.Length;t++)
{
check.Text+=placement[t] + " ";
}
}
protected void page_Unload(object sender,EventArgs e)
{
Session["placement"]=placement;
}
}
}
Comment