0

ok, after i got thelp with keypresses it is time to go one step further. i am ,making a very simple hangman game. the user will guess letters like you mormally do in hangman. what i want to be able to do is if he cant remember what he has guessed he can press a button. with that presss of the buttion all the letters/ keys that were captured would show up in a text box. but they would only show up when he pressed them.

i dont know if this is possible so any help would be great

Tayspen

2
Contributors
16
Replies
17
Views
11 Years
Discussion Span
Last Post by tayspen
0

couple of different ways you can handle it
I would probably do something like the following.
When the game starts instantiate a string called lettersUsed to be an empty string

string lettersUsed = "";

then, when someone hits a letter, add it to the string if it isn't already there. (adding to the code from your previous post)

case(Keys.A):
 if(lettersUsed.IndexOf("a") = -1)  lettersUsed+= "a";
 break;

then when they hit space

case(Keys.Space):
 MessageBox.Show("Letters used: " + lettersUsed);
 break;
0

ug im still consused though it always says lettersUsed is not defined. would any body mind giving sample code.

0

you probably need to do something like. This is all pseudocode

class Hangman{
string usedLetters= "";;
...
function startGame{
usedLetters = "";
}

}

0

call me stupid but i cant get it it always gives me erros index of... does not exist

0

heres that part of it keep in mind im new :D

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        string keyData = "";

        public Form1()
        {
            InitializeComponent();
            string keyData = "";

        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool result = false;
            switch (keyData)
            {
               case(Keys.A):
              if(lettersUsed.IndexOf("a") = -1)  lettersUsed+= "a";
              break;

           case(Keys.Space):
           MessageBox.Show("Letters used: " + lettersUsed);
           break;
            break;





            }
            return result;
        }


        [DllImport("winmm.dll")]
        private static extern bool PlaySound(string lpszName, int hModule, int dwFlags);


    }
}

Edited by pyTony: fixed formating

0

here it is closer i think

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace WindowsApplication1
{
    public partial class Form1 : Form{
   string lettersUsed = "";


        public Form1()
        {
            InitializeComponent();


        }

        protected override bool ProcessCmdKey(ref Message msg, Keys lettersUsed)
        {
            bool result = false;
            switch (lettersUsed)
            {
               case(Keys.A):
              if(lettersUsed.IndexOf("a") = -1)  lettersUsed+= "a";
              break;

           case(Keys.Space):
           MessageBox.Show("Letters used: " + lettersUsed);
           break;
            break;





            }
            return result;
        }


        [DllImport("winmm.dll")]
        private static extern bool PlaySound(string lpszName, int hModule, int dwFlags);


    }
}

Edited by pyTony: fixed formating

0

i see the problem. you are using the same variable name twice.
you have it as the name of the string and as the name of the parameter in ProcessCmdKey(ref Message msg, Keys lettersUsed).


try this instead

ProcessCmdKey(ref Message msg, Keys pressedKey){
....
switch(pressedKey){...

0

so this

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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {

        string lettersUsed = "";

        public Form1()
        {
            InitializeComponent();
        }


        protected override bool ProcessCmdKey(ref Message msg, Keys pressedKey)
        {
            {
                bool result = false;
                switch (pressedKey)
                {
                    case (Keys.A):
                        if (lettersUsed.IndexOf("a") = -1) lettersUsed += "a";
                        break;



                    case (Keys.Space):
                        MessageBox.Show("Letters used: " + lettersUsed);
                        break;
                }
                return result;
            }

        }


    }
}

if that is right i still get errors.... :(

Edited by pyTony: fixed formating

0

compiling

they are

cannot implicitly convert 'int' to 'bool'

and

the left hand side of an assigment must be a varible, propery, or idexer

0

made the most basic of mistakes while I was in a hurry to give you the first code that I gave you. in the if statements, change the = to ==

0

thank you hank you thank you thank you. i really aprciate your help

thank you

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.