I have a word guessing game I am working on for class.
Thank you for your honesty; for me, it is good not to have to wonder whether answering a question is actually doing someone's homework for them.
Also, thank you for sharing your code, it's easier to help you when you share the relevant parts of your code.
I'm going to teach you a different game; you'll need a reasonable size piece of blank paper and a pen or pencil.
"PLAYING COMPUTER"
Playing computer is debugging without the debugger*.
let me get you started ... look at this section of your code that follows ... ask yourself questions like the ones that i will show you below ... when you discover a "bug", modify your code to fix the bug, then start the game again with your modified code.
After you win the game of "PLAYING COMPUTER" because you've fixing all of the bugs, see whether you can rewrite your program a different way to make it better.
Notice how i've used comments below to make it easier to find the }
the matches each correspondiing {.
Here are some generic questions that you should ask yourself while you are playing computer:
(1) what would happen if the word was "deep", or "ieee", or "guessing", or "appreciated"?
(2) how can i make the program more generalized? (what good is a guessing game program where the answer is always "hello"?)
(a) String secretWord = "hello";
(b) String showAnswer = "*****";
(c) for (int i = 0; i < 100; i++)
{
(d) if (userGuess.Text == "h")
(e) {
(f) showAnswer = "h****";
(g) theWord.Text = showAnswer;
(h) if (userGuess.Text == "e")
{
(i) showAnswer = "he***";
(j) theWord.Text = showAnswer;
(k) if (userGuess.Text == "l")
{
(l) showAnswer = "hell*";
(m) theWord.Text = showAnswer;
(n) if (userGuess.Text == "o")
{
(o) showAnswer = "hello";
(p) theWord.Text = showAnswer;
} // if (userGuess.Text == "o")
} // if (userGuess.Text == "l")
} // if (userGuess.Text == "e")
} // if (userGuess.Text == "h")
(q) else
(u) theWord.Text = showAnswer;
(v) } // for (int i = 0; i < 100; i++)
in the code above, start by writing the value of secretword and
showanswer; each time the value changes, cross it out and write the new value; ask yourself whether the new value is what you expected it to be.
at (c), ask yourself whether the for statement makes sense.
at (d), ask yourself where you go in your code if the first guess is "e" and the second guess is "h".
at (d), if the guess is "h", do you still need to execute statements (h) through (p).
ru336, when you are reading any code, and especially your own code, read the code as if your are studying for a very important examination the requires your to get a perfect score to pass.
search videos at Google with debug visual studio 2010
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Well first of all I would like to thank those of you who offered some help to me. I have taken this approach and I think I'm very close, but I'm not sure. I'm still very much stressed as this assignment is due tomorrow. Are you able to see what I am doing
wrong? I will post the code I have and then the only errors I have left.
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
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Page.Validate();
if (Page.IsValid)
{
String secretWord = "hello";
for (int i = 0; i < userGuess.Text.Length; i++)
{
if (userGuess.Text == "h")
{
String showAnswer = "*****";
string[] showAnswerArray = showAnswer.Split();
if (userGuess.Text == "h")
{
showAnswerArray[0] = "h";
theWord.Text = showAnswerArray;
}
else if (userGuess.Text == "e")
{
showAnswerArray[1] = "e";
theWord.Text = showAnswerArray;
}
else if (userGuess.Text == "l")
{
showAnswerArray[2] = "l";
showAnswerArray[3] = "l";
theWord.Text = showAnswerArray;
}
else if (userGuess.Text == "o")
{
showAnswerArray[4] = "o";
theWord.Text = showAnswerArray;
}
else
theWord.Text = showAnswerArray;
}
}
}
}
}
}
And now the onlly errors I get are as follows:
"Cannot implicitly convert type 'string[]' to 'string'".
Those errors are in regards to the statements that go: theWord.Text = showAnswerArray;
I tried doing this:
showAnswerArray = showAnswerArray.ToString;
theWord.Text = showAnswerArray;
That also doesn't work. Can someone please push me in the right direction?
what happens when you walk your code after doing this?:
change:
theWord.Text= showAnswerArray;
to: theWord.Text= showAnswerArray.ToString();
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
if i remove the parentheses, i get the same error as you get.
ru336 .... you must be more careful when you copy code; being a programmer means being careful because the c# compiler is not that forgiving.
ru336, what happens after you add the parentheses?
N.B.: please remember to "play computer" ... you will learn a lot by playing computer.
g,
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Hey, Gerry, you're absolutely right. I was forgetting to put the parenthesis at the end of the ".ToString();" portion of my code. My program compiles and runs w/o errors now, but still not working correctly.
When I run the website now and enter a letter such as "h", "e", "l" or "o", the answer is "System.String[]".
If I guess a letter that is none of them then nothing happens at all.
Ugh this is so frustrating. I really appreciate the pointers that you've been giving me. I just don't know what else to try at this point. I've examined my code and I don't see how else to make this work correctly.
no offence meant, but if this were a job interview, you be falling it for not playing computer; let me explain.
A. your for statement implies that a person may be typing several letters like "abcdefgh".
B. you if statements look at only one letter at a time.
C. look at your first if ..... you will only get inside your
if block when userGuess.Text == "h"; however, when
userGuess.Text is "h", then userGuess.Text can
not be anything else ... simple logic, if something is white then it is not
black.
ru336, you really need to play computer and/or use the debugger ... otherwise, unless some member here does your homework for you, the method that you are using to solve your problem is
for all intents and purposes random.
FWIW, assuming you've been paying attention in class, then it's possible that your teacher has failed at helping you learn the basics.
ru336, let me guide in the processing logic, using pseudo-code ....
you probably want the end user to guess one letter at a time ...
so i would do away with the for and use a hidden field.
initially the hidden field, let's call it guesses is
String.Empty.
you need to check your target word against the currect guess, and also against the preceding guesses.
for each letter in the target word,
check whether it matches the single letter that the end user typed,
if it is, allow it to be displayed, otherwise, replace it with an asterisk.
also, append the most recent guess to guesses
Example: target word for end user to guess is year
the user types 'a', you display **a*;
guesses <=== a
the user types 'e', you display *ea*;
guesses <=== ae
the user types 'i', you display *ea*;
guesses <=== aei
the user types 'o', you display *ea*;
guesses <=== aeio
the user types 'b', you display *ea*;
guesses <=== aeiob
the user types 'r', you display *ear;
guesses <=== aeiobr
et cetera ...
????????? how many guesses will you allow?
g.
TIMTOWTDI =. there is more than one way to do it
Note:
ru336, it's very important that you understand how your game will be played; it's also very important to play computer with a piece of paper like i suggested earlier ... í've been programming for 45+ years and i still play computer whenever i think it necessary
... also, i use the debugger a lot ...
i even use the debugger on programs that appear to be working because doing that can help one uncover bugs that have not yet happened.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
ru336
0 Points
5 Posts
Word Guessing Game - Please Help
Apr 05, 2012 06:59 PM|LINK
Hey everyone, I have a word guessing game I am working on for class.
Example: Secret word = "hello"
Lets say the user enters "e". When they click the button it should pop up "*e***". And so on and so forth until they have figured out the words.
Can someone please tell me what I'm doing wrong here?
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h1> Word Guessing Game</h1> <hr /> <p> Guess A Letter: <asp:TextBox ID="userGuess" runat="server" /> The Word: <asp:Label ID="theWord" runat="server" /> </p> <p> <asp:Button ID="guess" runat="server" Text="Guess" /> </p> </div> </form> </body> </html>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 { protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { Page.Validate(); if (Page.IsValid) { String secretWord = "hello"; String showAnswer = "*****"; for (int i = 0; i < 100; i++) { if (userGuess.Text == "h") { showAnswer = "h****"; theWord.Text = showAnswer; if (userGuess.Text == "e") { showAnswer = "he***"; theWord.Text = showAnswer; if (userGuess.Text == "l") { showAnswer = "hell*"; theWord.Text = showAnswer; if (userGuess.Text == "o") { showAnswer = "hello"; theWord.Text = showAnswer; } } } } else theWord.Text = showAnswer; } } } } }cool.asp
Member
545 Points
190 Posts
Re: Word Guessing Game - Please Help
Apr 05, 2012 07:37 PM|LINK
You might read about using IndexOf
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Word Guessing Game - Please Help
Apr 06, 2012 02:55 PM|LINK
@ ru336
Thank you for your honesty; for me, it is good not to have to wonder whether answering a question is actually doing someone's homework for them.
Also, thank you for sharing your code, it's easier to help you when you share the relevant parts of your code.
I'm going to teach you a different game; you'll need a reasonable size piece of blank paper and a pen or pencil.
"PLAYING COMPUTER"
Playing computer is debugging without the debugger*.
let me get you started ... look at this section of your code that follows ... ask yourself questions like the ones that i will show you below ... when you discover a "bug", modify your code to fix the bug, then start the game again with your modified code.
After you win the game of "PLAYING COMPUTER" because you've fixing all of the bugs, see whether you can rewrite your program a different way to make it better.
Notice how i've used comments below to make it easier to find the } the matches each correspondiing {.
Here are some generic questions that you should ask yourself while you are playing computer:
(1) what would happen if the word was "deep", or "ieee", or "guessing", or "appreciated"?
(2) how can i make the program more generalized? (what good is a guessing game program where the answer is always "hello"?)
(a) String secretWord = "hello"; (b) String showAnswer = "*****"; (c) for (int i = 0; i < 100; i++) { (d) if (userGuess.Text == "h") (e) { (f) showAnswer = "h****"; (g) theWord.Text = showAnswer; (h) if (userGuess.Text == "e") { (i) showAnswer = "he***"; (j) theWord.Text = showAnswer; (k) if (userGuess.Text == "l") { (l) showAnswer = "hell*"; (m) theWord.Text = showAnswer; (n) if (userGuess.Text == "o") { (o) showAnswer = "hello"; (p) theWord.Text = showAnswer; } // if (userGuess.Text == "o") } // if (userGuess.Text == "l") } // if (userGuess.Text == "e") } // if (userGuess.Text == "h") (q) else (u) theWord.Text = showAnswer; (v) } // for (int i = 0; i < 100; i++)in the code above, start by writing the value of secretword and showanswer; each time the value changes, cross it out and write the new value; ask yourself whether the new value is what you expected it to be.
at (c), ask yourself whether the for statement makes sense.
at (d), ask yourself where you go in your code if the first guess is "e" and the second guess is "h".
at (d), if the guess is "h", do you still need to execute statements (h) through (p).
ru336, when you are reading any code, and especially your own code, read the code as if your are studying for a very important examination the requires your to get a perfect score to pass.
g.
P.S.: learn how to use the debugger:
* walking your code with your debugger:
video, c. 8 minutes: http://msdn.microsoft.com/en-ca/vstudio/ee672313.aspx
"How Do I: Step with The Debugger in Visual Studio?"
Debugging: http://lmgtfy.com/?q=debug+visual+studio+2010
example: http://www.codeproject.com/KB/cs/MasteringInDebugging.aspx
videos: http://www.youtube.com/watch?v=z5gBIizwsY0
search videos at Google with debug visual studio 2010
ru336
0 Points
5 Posts
Re: Word Guessing Game - Please Help
Apr 07, 2012 10:09 PM|LINK
Well first of all I would like to thank those of you who offered some help to me. I have taken this approach and I think I'm very close, but I'm not sure. I'm still very much stressed as this assignment is due tomorrow. Are you able to see what I am doing wrong? I will post the code I have and then the only errors I have left.
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 { protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { Page.Validate(); if (Page.IsValid) { String secretWord = "hello"; for (int i = 0; i < userGuess.Text.Length; i++) { if (userGuess.Text == "h") { String showAnswer = "*****"; string[] showAnswerArray = showAnswer.Split(); if (userGuess.Text == "h") { showAnswerArray[0] = "h"; theWord.Text = showAnswerArray; } else if (userGuess.Text == "e") { showAnswerArray[1] = "e"; theWord.Text = showAnswerArray; } else if (userGuess.Text == "l") { showAnswerArray[2] = "l"; showAnswerArray[3] = "l"; theWord.Text = showAnswerArray; } else if (userGuess.Text == "o") { showAnswerArray[4] = "o"; theWord.Text = showAnswerArray; } else theWord.Text = showAnswerArray; } } } } } }And now the onlly errors I get are as follows:
"Cannot implicitly convert type 'string[]' to 'string'".
Those errors are in regards to the statements that go: theWord.Text = showAnswerArray;
I tried doing this:
showAnswerArray = showAnswerArray.ToString;
theWord.Text = showAnswerArray;
That also doesn't work. Can someone please push me in the right direction?
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Word Guessing Game - Please Help
Apr 07, 2012 10:41 PM|LINK
@ ru336
what happens when you walk your code after doing this?:
change:
theWord.Text = showAnswerArray;
to:
theWord.Text = showAnswerArray.ToString();
g.
ru336
0 Points
5 Posts
Re: Word Guessing Game - Please Help
Apr 08, 2012 12:11 PM|LINK
Hi, Gerry, when I try that I get the following error:
"Error: Cannot convert method gropu 'ToString' to non-delegate type string. Did you intend to invoke the method?"
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Word Guessing Game - Please Help
Apr 08, 2012 02:01 PM|LINK
@ ru336
did you write .ToString() with the parentheses? or just .ToString without the parentheses?
The code below compiles 100% clean in LINQPad 4 and therefore should compile clean in vs2010:
String secretWord = "hello"; string theWord =""; string userGuess = "w"; for (int i = 0; i < 2; i++) { if (userGuess == "h") { String showAnswer = "*****"; string[] showAnswerArray = showAnswer.Split(); if (userGuess == "h") { showAnswerArray[0] = "h"; theWord = showAnswerArray.ToString(); } else if (userGuess == "e") { showAnswerArray[1] = "e"; theWord = showAnswerArray.ToString(); } else if (userGuess == "l") { showAnswerArray[2] = "l"; showAnswerArray[3] = "l"; theWord = showAnswerArray.ToString(); } else if (userGuess == "o") { showAnswerArray[4] = "o"; theWord = showAnswerArray.ToString(); } else theWord = showAnswerArray.ToString(); } }if i remove the parentheses, i get the same error as you get.
ru336 .... you must be more careful when you copy code; being a programmer means being careful because the c# compiler is not that forgiving.
ru336, what happens after you add the parentheses?
N.B.: please remember to "play computer" ... you will learn a lot by playing computer.
g,
ru336
0 Points
5 Posts
Re: Word Guessing Game - Please Help
Apr 08, 2012 04:20 PM|LINK
Hey, Gerry, you're absolutely right. I was forgetting to put the parenthesis at the end of the ".ToString();" portion of my code. My program compiles and runs w/o errors now, but still not working correctly.
When I run the website now and enter a letter such as "h", "e", "l" or "o", the answer is "System.String[]".
If I guess a letter that is none of them then nothing happens at all.
Ugh this is so frustrating. I really appreciate the pointers that you've been giving me. I just don't know what else to try at this point. I've examined my code and I don't see how else to make this work correctly.
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Word Guessing Game - Please Help
Apr 08, 2012 10:16 PM|LINK
@ ru336
no offence meant, but if this were a job interview, you be falling it for not playing computer; let me explain.
A. your for statement implies that a person may be typing several letters like "abcdefgh".
B. you if statements look at only one letter at a time.
C. look at your first if ..... you will only get inside your if block when userGuess.Text == "h"; however, when userGuess.Text is "h", then userGuess.Text can not be anything else ... simple logic, if something is white then it is not black.
ru336, you really need to play computer and/or use the debugger ... otherwise, unless some member here does your homework for you, the method that you are using to solve your problem is for all intents and purposes random.
FWIW, assuming you've been paying attention in class, then it's possible that your teacher has failed at helping you learn the basics.
ru336, let me guide in the processing logic, using pseudo-code ....
you probably want the end user to guess one letter at a time ...
so i would do away with the for and use a hidden field.
initially the hidden field, let's call it guesses is String.Empty.
you need to check your target word against the currect guess, and also against the preceding guesses.
for each letter in the target word,
check whether it matches the single letter that the end user typed,
if it is, allow it to be displayed, otherwise, replace it with an asterisk.
also, append the most recent guess to guesses
Example: target word for end user to guess is year
the user types 'a', you display **a*; guesses <=== a
the user types 'e', you display *ea*; guesses <=== ae
the user types 'i', you display *ea*; guesses <=== aei
the user types 'o', you display *ea*; guesses <=== aeio
the user types 'b', you display *ea*; guesses <=== aeiob
the user types 'r', you display *ear; guesses <=== aeiobr
et cetera ...
????????? how many guesses will you allow?
g.
TIMTOWTDI =. there is more than one way to do it
Note: ru336, it's very important that you understand how your game will be played; it's also very important to play computer with a piece of paper like i suggested earlier ... í've been programming for 45+ years and i still play computer whenever i think it necessary ... also, i use the debugger a lot ...
i even use the debugger on programs that appear to be working because doing that can help one uncover bugs that have not yet happened.