1

I think I found what the problem is. And I think my code fails when i try to replace a value in an array with another value from another array. I uses a for loop and then find out "i", the index and I try to replace one value in one array. There is some mixup in the indexes, but I can't figure it out!

What's below is as far as I have come.

There are some Norwegian words in there but don't let them confuse you. And I should tell you that I try to make a hangman game.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
char [] arrayOrd;
char[] bokstav;
char[] byggeOrd; 

protected void Page_Load(object sender, EventArgs e)
{
    //Session for remembering what word is gonna be guessed
    if (Session["arrayOrd"] != null)
    {
       arrayOrd = (char[])Session["arrayOrd"];
    }
    //This session contains the underscores
    if (Session["byggeOrd"] != null) {
        byggeOrd = (char[])Session["byggeOrd"];
    }
}
protected void Page_Unload(object sender, EventArgs e) 
{
    Session["arrayOrd"] = arrayOrd;
    Session["byggeOrd"] = byggeOrd;

}

protected void btnStart_Click(object sender, EventArgs e)
{
    string ord = txtOrd.Text.ToLower();
  arrayOrd = ord.ToCharArray();//Puts text from a textbox to an array called arrayOrd
  List<char> list = new List<char>();
    for (int i = 0; i < arrayOrd.Length; i++) {//Put underscores for as long arrayOrd is in a list.
      labRiktigBokstav.Text += "_ ";
      list.Add('_');
  }
  byggeOrd = list.ToArray(); //Put's the list that contains underscores in an array

}
//Checking if a letter is in the word
protected void btnSjekkOrd_Click(object sender, EventArgs e)
{
    string BSjekk = txtBokstavSjekk.Text.ToLower();
    bokstav = BSjekk.ToCharArray();
    if (arrayOrd.Contains(bokstav[0]))
    {
        for (int i = 0; i < arrayOrd.Length; i++)
        {
            if (arrayOrd[i] == bokstav[0])
            {
                byggeOrd[Array.IndexOf(byggeOrd, byggeOrd[i])] = arrayOrd[i]; //I think there is something wrong here!!!!!
                string resultat = new string(byggeOrd);
                labRiktigBokstav.Text = resultat;
            }
        }

    }
    else
    {
        //Print out that the guessed letter is wrong
    }
}
}
5
  • This line is redundant: byggeOrd[Array.IndexOf(byggeOrd, byggeOrd[i])]. You are getting the index of a value through its very index. You can replace it with byggeOrd[i]. What results exactly are you getting and what do you want to accomplish? Commented Feb 21, 2014 at 21:01
  • @rla4 Sorry, that may very well be the answer upon further inspection. Your answer was worded like it was just a code review suggestion and not the answer to the issue. Commented Feb 21, 2014 at 21:11
  • I have an array with underscores: '', ''. And if the user guess a letter right then the index of that underscore and the index of the word the user are guessing will be replaced. So if the word are "car". And the user guess a, I want to change '', '', '_' with "a". My results are that the indexes doens't match up. The guessed letters isn't put in the right index and are then dispayed wrong. If the user guesses "c" then "r" and then "a". It will be displayed as: cra and not car. Commented Feb 21, 2014 at 21:12
  • @TyCobb in fact I think you were right. I was not sure what the OP was asking, and should have asked for clarifications before trying to answer. :) Commented Feb 21, 2014 at 21:14
  • @rla4 Thank you! I replaced byggeOrd[Array.IndexOf(byggeOrd, byggeOrd[i])] with byggeord[i] as you suggested and it works perfectly now!! Thank you! Commented Feb 21, 2014 at 21:19

1 Answer 1

1

The following line is wrong:

byggeOrd[Array.IndexOf(byggeOrd, byggeOrd[i])] 

You are getting the index of a value through its very index.

int ind = Array.IndexOf(byggeOrd, byggeOrd[i]); 

The code above can retrieve any index. That's why it can modify an unintended value. You should replace it with

if (arrayOrd[i] == bokstav[0]){
    byggeOrd[i]= arrayOrd[i]; 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.