I've been trying to make a hangman game lately, I've almost gotten the main part of the game to work. But the part where the player guesses the letter is not working properly, I'll try and make this clearer:
When I press one correct letter it shows, but then when I press another *different, correct letter, that one shows but the previous one disappears. I also tested out using two or more of the same letters and when I typed a correct letter, it only shows the position of one of them. I don't understand what's going on, here's what I got so far:
**UPDATE June 15, 2014**
*I've almost got the letters working properly*, but the code still only adds one position of a correct letter even if there are two of the same letters in different positions of a word. Here's the updated code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Randell_Hangman
{
public partial class Form1 : Form
{
string[] brandnames = new string[1] { "abc" };
Random word_shuffle = new Random();
string word_select;
int life_count;
int i = 0;
int word_length;
string w, x, y, z;
int keypress_stage;
int a_key;
public Form1()
{
InitializeComponent();
word_select = brandnames[word_shuffle.Next(0, brandnames.Length)];
word_length = word_select.Length;
life_count = 5;
keypress_stage = 0;
a_key = 0;
}
private void Form1_Load(object sender, EventArgs e)
{
space_adder_timer.Start();
life_line.Text = "Lives = " + life_count.ToString();
w = word_select.Remove(0);
word_display.Text = x.ToString();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
life_line.Text = "Lives = " + life_count.ToString();
#region Pressing A
if (e.KeyData == Keys.A)
{
string letter_a = "a";
if (word_select.Contains(letter_a) == true)
{
int y1 = word_select.IndexOf("a");
if (keypress_stage == 0)
{
y = x.Insert(y1, "a");
keypress_stage = 1;
a_key = 1;
}
else if (keypress_stage == 1)
{
y = y.Insert(y1, "a");
keypress_stage = 1;
a_key = 1;
}
}
if (word_select.Contains(letter_a) == false)
{
life_count--;
}
if (life_count <= 0)
{
MessageBox.Show("Game Over");
life_count = 0;
life_line.Text = "Lives = 0";
}
}
if (e.KeyCode >= Keys.A)
{
if (a_key == 1)
{
e.SuppressKeyPress = true;
}
}
#endregion
#region Pressing B
if (e.KeyData == Keys.B)
{
string letter_b = "b";
if (word_select.Contains(letter_b) == true)
{
int y1 = word_select.IndexOf("b");
if (keypress_stage == 0)
{
y = x.Insert(y1, "b");
keypress_stage = 1;
}
if (keypress_stage == 1)
{
y = y.Insert(y1, "b");
keypress_stage = 1;
}
}
if (word_select.Contains(letter_b) == false)
{
life_count--;
}
if (life_count <= 0)
{
MessageBox.Show("Game Over");
life_count = 0;
life_line.Text = "Lives = 0";
}
}
#endregion
word_display.Text = y.ToString();
}
private void space_adder_timer_Tick(object sender, EventArgs e)
{
x += w.Insert(0, "_");
if (x.Length == word_length)
{
space_adder_timer.Stop();
word_display.Text = x.ToString();
}
}
}
}