Console animations - c#

I just want to know how to create simple animations like blinking, moving stuffs on C# console applications. Is there any special method for this?

Traditional Console Spinner:
static void Main(string[] args)
{
ConsoleSpiner spin = new ConsoleSpiner();
Console.Write("Working....");
while (true)
{
spin.Turn();
}
}
public class ConsoleSpiner
{
int counter;
public ConsoleSpiner()
{
counter = 0;
}
public void Turn()
{
counter++;
switch (counter % 4)
{
case 0: Console.Write("/"); break;
case 1: Console.Write("-"); break;
case 2: Console.Write("\\"); break;
case 3: Console.Write("|"); break;
}
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
}
}

Here is my spinner. The purpose of it is to have a program do some work while the spinner displays to the user that something is actually happening:
public class Spinner : IDisposable
{
private const string Sequence = #"/-\|";
private int counter = 0;
private readonly int left;
private readonly int top;
private readonly int delay;
private bool active;
private readonly Thread thread;
public Spinner(int left, int top, int delay = 100)
{
this.left = left;
this.top = top;
this.delay = delay;
thread = new Thread(Spin);
}
public void Start()
{
active = true;
if (!thread.IsAlive)
thread.Start();
}
public void Stop()
{
active = false;
Draw(' ');
}
private void Spin()
{
while (active)
{
Turn();
Thread.Sleep(delay);
}
}
private void Draw(char c)
{
Console.SetCursorPosition(left, top);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(c);
}
private void Turn()
{
Draw(Sequence[++counter % Sequence.Length]);
}
public void Dispose()
{
Stop();
}
}
And you use the class like this:
using (var spinner = new Spinner(10, 10))
{
spinner.Start();
// Do your work here instead of sleeping...
Thread.Sleep(10000);
}

Yes, there are quite a few methods for this.
In particular, you may want to look at the following Console methods:
SetCursorPosition (you can move the cursor around, and overwrite elements)
MoveBufferArea (copy/paste over the top of regions)
ForegroundColor and BackgroundColor (change coloring)

Great work with the ConsoleSpinner and the sequence implementation. Thanks for the code. I thought about sharing my customized approach.
public class ConsoleSpinner
{
static string[,] sequence = null;
public int Delay { get; set; } = 200;
int totalSequences = 0;
int counter;
public ConsoleSpinner()
{
counter = 0;
sequence = new string[,] {
{ "/", "-", "\\", "|" },
{ ".", "o", "0", "o" },
{ "+", "x","+","x" },
{ "V", "<", "^", ">" },
{ ". ", ".. ", "... ", "...." },
{ "=> ", "==> ", "===> ", "====>" },
// ADD YOUR OWN CREATIVE SEQUENCE HERE IF YOU LIKE
};
totalSequences = sequence.GetLength(0);
}
/// <summary>
///
/// </summary>
/// <param name="sequenceCode"> 0 | 1 | 2 |3 | 4 | 5 </param>
public void Turn(string displayMsg = "", int sequenceCode = 0)
{
counter++;
Thread.Sleep(Delay);
sequenceCode = sequenceCode > totalSequences - 1 ? 0 : sequenceCode;
int counterValue = counter % 4;
string fullMessage = displayMsg + sequence[sequenceCode, counterValue];
int msglength = fullMessage.Length;
Console.Write(fullMessage);
Console.SetCursorPosition(Console.CursorLeft - msglength, Console.CursorTop);
}
}
Implementation:
ConsoleSpinner spinner = new ConsoleSpinner();
spinner.Delay = 300;
while (true)
{
spinner.Turn(displayMsg: "Working ",sequenceCode:5);
}
Output:

You'll need to use Console.ForegroundColor, Console.BackgroundColor and Console.SetCursorPosition(int, int)
EDIT: For inspiration, Let's Dance

Just saw this and a few other threads about it and love this kind of stuff! I took Tuukka's nice piece of code and improved it a bit so the class can easily be set to just about any spin sequence. I'll probably add some accessors and an overloaded constructor to polish it off and put it in the ol' toolbox. Fun stuff!
class ConsoleSpinner
{
int counter;
string[] sequence;
public ConsoleSpinner()
{
counter = 0;
sequence = new string[] { "/", "-", "\\", "|" };
sequence = new string[] { ".", "o", "0", "o"};
sequence = new string[] { "+", "x" };
sequence = new string[] { "V", "<", "^", ">" };
sequence = new string[] { ". ", ".. ", "... ", "...." };
}
public void Turn()
{
counter++;
if (counter >= sequence.Length)
counter = 0;
Console.Write(sequence[counter]);
Console.SetCursorPosition(Console.CursorLeft - sequence[counter].Length, Console.CursorTop);
}
}

I took the anwser from #ThisGuy and modified it a bit, but I noticed, that sometimes the row is not cleared as it should.
static void Main(string[] args)
{
Console.WriteLine("1");
var tddf = XConsole.BusyIndicator("test 0 trtg fdfdfvdgd 4343", () =>
{
return "";
});
Console.WriteLine("2");
var tdd = XConsole.BusyIndicator("test 0 trtg fdfdfvdgd", () =>
{
Thread.Sleep(1);
return "";
});
Console.WriteLine("3");
var t = XConsole.BusyIndicator("test 1 trtg vdgd", () =>
{
Thread.Sleep(1000);
return "";
});
Console.WriteLine("4");
var xt = XConsole.BusyIndicator("test 2", () =>
{
Thread.Sleep(2000);
return "";
});
var xtx = XConsole.BusyIndicator("test 2 csds fsd fdsf ds s", () =>
{
Thread.Sleep(2000);
return "";
});
Console.WriteLine("5");
Thread.Sleep(4000);
}
Spinner class:
public class Spinner : IDisposable
{
private const string Sequence1 = #"/-\|";
private const string Sequence3 = #".o0o";
private const string Sequence2 = #"<^>v";
private const string Sequence4 = #"#■.";
private const string Sequence5 = #"▄▀";
private const string Sequence = #"└┘┐┌";
private string BusyMessage = "";
private int counter = 0;
private readonly int delay;
private bool active;
private readonly Thread thread;
public Spinner(int delay = 200)
{
this.delay = delay;
thread = new Thread(Spin);
}
public void Start()
{
active = true;
Console.CursorVisible = false;
if (!thread.IsAlive)
{
thread.Start();
}
}
public void Start(string busyMessage)
{
BusyMessage = busyMessage;
Start();
}
public void Stop()
{
active = false;
Console.CursorVisible = true;
ClearCurrentConsoleLine();
BusyMessage = "";
}
private static void ClearCurrentConsoleLine()
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
private void Spin()
{
while (active)
{
Turn();
Thread.Sleep(delay);
}
}
/// <summary>
/// Draws the busy indicator
/// </summary>
/// <param name="c">if empty char, then clear screen</param>
private void Draw(char c)
{
int left = Console.CursorLeft;
int top = Console.CursorTop;
Console.Write('[');
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(c);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(']');
if (!string.IsNullOrEmpty(BusyMessage))
{
Console.Write(" " + BusyMessage);
}
//reset cursor position
Console.SetCursorPosition(left, top);
}
private void Turn()
{
Draw(Sequence[++counter % Sequence.Length]);
}
public void Dispose()
{
Stop();
}
}
My console class:
public static class XConsole {
public static T BusyIndicator<T>(Func<T> action)
{
T result;
using (var spinner = new Spinner())
{
spinner.Start();
result = action();
spinner.Stop();
}
return result;
}
public static T BusyIndicator<T>(string content, Func<T> action)
{
T result;
using (var spinner = new Spinner())
{
spinner.Start(content);
result = action();
spinner.Stop();
}
return result;
}
}
Why do I see this result sometimes?
1
2
3 st 0 trtg fdfdfvdgd ]
4
[└] test 2 csds fsd fdsf ds s
Looks like the Dispose didn't trigger? Or a new Task alredy started?
It should look like this:
1
2
3
4
[└] test 2 csds fsd fdsf ds s
UPDATE:
I added ClearCurrentConsoleLine(); and changed the Draw method, this fixed the issue.

i'm not TOO sure i know exactly what you're on about. but i'll give it a shot.
I think the biggest and best way to cause the "blinking" affect (or what i think the blinking affect is) is to use a carriage return.
The best way to explain it to you is to show you the Foo Bar experiment.
start a new project, and in your Main function, try this.
Console.WriteLine("Foo/nBar");
The output will look like this
Foo
Bar
But if you use a carriage return.
Console.WriteLine("Foo/rBar");
The output will look like this
Bar
The reason is that Foo is written, then the carriage return takes you BACK to the start of the line, then Bar is written. All you ever see is Bar.
This can be helpful for "Moving" things on one line, instead of rewriting the same things again on multiple lines.
A way to do progression would be to use Console.Write();
Try this.
Console.Write("Loading");
for(int i = 0; i < 10; i++)
{
Thread.Sleep(1000);
Console.Write(".");
}
The output should be
Loading
Followed by a Fullstop every second for 10 seconds.
If you combine the Carriage return with the Console.Write(); function you can write multiple things on a single line, clear the line and write something else, or indeed, the same thing just moved slightly. (This would of course need more than i have shown you, like recording where the "object" you are controlling is situated. If you would like a short example i would be happy to do one, just comment and ask me for it :)
Edit:
I noticed people mentioning colour, which i forgot. If you were doing animation i guess colour would be a must.
ForegroundColor and BackgroundColor are where it's at.
note that ForegroundColor will apply to the next characters written to the console, it will not completely recolour the Console.
/Edit
I hope this helps,
Skintkingle ;)

I thought I'd chime in with my version of the previously listed code. Here it is:
class ConsoleSpinner
{
bool increment = true,
loop = false;
int counter = 0;
int delay;
string[] sequence;
public ConsoleSpinner(string sSequence = "dots", int iDelay = 100, bool bLoop = false)
{
delay = iDelay;
if (sSequence == "dots")
{
sequence = new string[] { ". ", ".. ", "... ", "...." };
loop = true;
}
else if (sSequence == "slashes")
sequence = new string[] { "/", "-", "\\", "|" };
else if (sSequence == "circles")
sequence = new string[] { ".", "o", "0", "o" };
else if (sSequence == "crosses")
sequence = new string[] { "+", "x" };
else if (sSequence == "arrows")
sequence = new string[] { "V", "<", "^", ">" };
}
public void Turn()
{
if (loop)
{
if (counter >= sequence.Length - 1)
increment = false;
if (counter <= 0)
increment = true;
if (increment)
counter++;
else if (!increment)
counter--;
}
else
{
counter++;
if (counter >= sequence.Length)
counter = 0;
}
Console.Write(sequence[counter]);
Console.SetCursorPosition(Console.CursorLeft - sequence[counter].Length, Console.CursorTop);
System.Threading.Thread.Sleep(delay);
}
}
Adds delay (unfortunately through Thread.Sleep() but, hey), the ability to loop the animation forwards and backwards (starts to reverse when it hits the end), and general improvements overall. Enjoy!

This would be my prefered method:
public sealed class Spinner
{
private static Lazy<Spinner> lazy =
new Lazy<Spinner>(()=> new Spinner());
public static void Reset()
{
lazy = new Lazy<Spinner>(()=> new Spinner());
}
public static Spinner Instance { get { return lazy.Value; }}
private readonly int _consoleX;
private readonly int _consoleY;
private readonly char[] _frames = { '|', '/', '-', '\\' };
private int _current;
private Spinner()
{
_current = 0;
_consoleX = Console.CursorLeft;
_consoleY = Console.CursorTop;
}
public void Update()
{
Console.Write(_frames[_current]);
Console.SetCursorPosition(_consoleX, _consoleY);
if (++_current >= _frames.Length)
_current = 0;
}
}
Call Spinner.Instance.Update() to start the spinner at the current position of the console. Any consecutive call will render the next frame at the same position.
Call Spinner.Reset() if you want to write more text and then add a new spinner at a new location.

This is the way i solved it, sure it could/can be shorter but, Im just not that smart yet :-)
void cc() { Console.Clear(); }
void cr() { Console.ReadKey(); }
byte Sleeptimer = 90;
void sleepy() { System.Threading.Thread.Sleep(Sleeptimer); }
string[] Loading = { #"-- ", #"\ ", #"| ", #"/ ", "Loading", " complete!" };
for (byte i = 0; i <= 15; i++)
{
cc();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(Loading[0] + Loading[4]);
sleepy();
cc();
Console.WriteLine(Loading[1] + Loading[4]);
sleepy();
cc();
Console.WriteLine(Loading[2] + Loading[4]);
sleepy();
cc();
Console.WriteLine(Loading[3] + Loading[4]);
sleepy();
cc();
if (i == 15) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(Loading[4] + Loading[5]);
cc();
Console.ForegroundColor = ConsoleColor.Cyan;
Next();
}
}
/*
Now i feel even more noob while reading your code.
I'm a newbie in programing.
Guess this way would work if i just incfement the index of each Loading down below right?
Learned too much today, forgot how this exactly works, to increment or to change the index i'd lile to acces
Console.WriteLine(Loading[2] + Loading[4]);
sleepy();
cc();
*/

This is animation with cancellationToken
private static void ClearCurrentConsoleLine()
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
public static async Task Start(CancellationToken ct)
{
await Task.Run(() =>
{
Thread.Sleep(2000);
Console.WriteLine();
string text = "Executing";
for (int i = 0; i <= 3; i++)
{
if (ct.IsCancellationRequested)
{
Console.SetCursorPosition(0, Console.CursorTop - 1);
ClearCurrentConsoleLine();
ct.ThrowIfCancellationRequested();
}
Thread.Sleep(1000);
Console.SetCursorPosition(0, Console.CursorTop - 1);
ClearCurrentConsoleLine();
Console.WriteLine(text);
if (i == 3)
{
text = "Executing";
i = 0;
}
text += ".";
}
});
}```

Related

How to avoid UI freezing while typing highlighting text?

I'm working on JSON text editor analog with highlighting syntax support.
I'm using DevExpress RichEditControl, where override ISyntaxHighlightService(using class, which implements this).
After getting pretty JSON text at the view highlighter starts working permanently, searching for different tokens, classifying them to highlight with concrete color. So, searching mechanism works too long if there are more than 300 lines of text, and it causes freezing UI while typing text.
I think I should run that things in other thread, but I get errors, related to "control is in another thread"...
The last thing I've tried, was invoking that method 1 or 2 per second, using DispatherTimer, to make the problem less enoying, but it still doesn't work as easy as in popular text editors because of delay.
So, what could you offer? May be there are alternative methods for coloring syntax? Or I can use side text editor control with such functions, instead of RichEditControl? May be it's easy to be reached, using converters and XAML?
ISyntaxHighLightService implemetation:
public class CustomSyntaxHighlightService : ISyntaxHighlightService
{
Document document;
Regex _numberString = new Regex(#"(\d+\W\d+)|[\d]+| (.*?)");
Regex _quotedString = new Regex("\"([^\"]*)\"[:]");
public CustomSyntaxHighlightService(Document document)
{
this.document = document;
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatherExecute);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
private void dispatherExecute(object sender, EventArgs e)
{
List<SyntaxHighlightToken> JSONTokens = ParseTokens();
document.ApplySyntaxHighlight(JSONTokens);
}
public void ForceExecute()
{
}
public void Execute()
{
}
private List<SyntaxHighlightToken> ParseTokens()
{
List<SyntaxHighlightToken> tokens = new List<SyntaxHighlightToken>();
DocumentRange[] ranges = null;
ranges = document.FindAll(_quotedString).GetAsFrozen() as DocumentRange[];
for (int i = 0; i < ranges.Length; i++)
{
tokens.Add(CreateToken(ranges[i].Start.ToInt(), ranges[i].End.ToInt(), Color.FromArgb(207, 101, 239)));
}
// When I try to add such code block with another token string I get an error...
//ranges = document.FindAll(_anotherString).GetAsFrozen() as DocumentRange[];
//for (int i = 0; i < ranges.Length; i++)
//{
// tokens.Add(CreateToken(ranges[i].Start.ToInt(), ranges[i].End.ToInt(), Color.FromArgb(207, 101, 239)));
//}
ranges = document.FindAll(_numberString).GetAsFrozen() as DocumentRange[];
for (int j = 0; j < ranges.Length; j++)
{
if (!IsRangeInTokens(ranges[j], tokens))
{
tokens.Add(CreateToken(ranges[j].Start.ToInt(), ranges[j].End.ToInt(), Color.DeepSkyBlue));
}
}
tokens.Sort(new SyntaxHighlightTokenComparer());
tokens = CombineWithPlainTextTokens(tokens);
return tokens;
}
List<SyntaxHighlightToken> CombineWithPlainTextTokens(List<SyntaxHighlightToken> tokens)
{
List<SyntaxHighlightToken> result = new List<SyntaxHighlightToken>(tokens.Count * 2 + 1);
int documentStart = document.Range.Start.ToInt();
int documentEnd = document.Range.End.ToInt();
if (tokens.Count == 0)
{
result.Add(CreateToken(documentStart, documentEnd, Color.Black));
}
else
{
SyntaxHighlightToken firstToken = tokens[0];
if (documentStart < firstToken.Start)
{
result.Add(CreateToken(documentStart, firstToken.Start, Color.Black));
}
result.Add(firstToken);
for (int i = 1; i < tokens.Count; i++)
{
SyntaxHighlightToken token = tokens[i];
SyntaxHighlightToken prevToken = tokens[i - 1];
if (prevToken.End != token.Start)
{
result.Add(CreateToken(prevToken.End, token.Start, Color.Black));
}
result.Add(token);
}
SyntaxHighlightToken lastToken = tokens[tokens.Count - 1];
if (documentEnd > lastToken.End)
{
result.Add(CreateToken(lastToken.End, documentEnd, Color.Black));
}
}
return result;
}
private bool IsRangeInTokens(DocumentRange range, List<SyntaxHighlightToken> tokens)
{
return tokens.Any(t => IsIntersect(range, t));
}
bool IsIntersect(DocumentRange range, SyntaxHighlightToken token)
{
int start = range.Start.ToInt();
if (start >= token.Start && start < token.End)
{
return true;
}
int end = range.End.ToInt() - 1;
if (end >= token.Start && end < token.End)
{
return true;
}
if (start < token.Start && end >= token.End)
{
return true;
}
return false;
}
SyntaxHighlightToken CreateToken(int start, int end, Color foreColor)
{
SyntaxHighlightProperties properties = new SyntaxHighlightProperties();
properties.ForeColor = foreColor;
return new SyntaxHighlightToken(start, end - start, properties);
}
public class SyntaxHighlightTokenComparer : IComparer<SyntaxHighlightToken>
{
public int Compare(SyntaxHighlightToken x, SyntaxHighlightToken y)
{
return x.Start - y.Start;
}
}
EDIT:
I've understood the idea about cancellation token, but I don't know how to realize it right. Now I have got static class for token:
public static class CTTest
{
public static CancellationTokenSource CancellationTokenSource = new CancellationTokenSource();
public static CancellationToken Token = CancellationTokenSource.Token;
public static void SetNewTokenValues()
{
CancellationTokenSource = new CancellationTokenSource();
Token = CancellationTokenSource.Token;
}
}
And I use it here:
void ParseAndHighLight(CancellationToken token)
{
if (!token.IsCancellationRequested)
{
List<SyntaxHighlightToken> JSONTokens = ParseTokens();
Dispatcher.CurrentDispatcher.BeginInvoke(new ThreadStart(delegate { document.ApplySyntaxHighlight(JSONTokens); }));
}
else
{
CTTest.SetNewTokenValues();
ForceExecute();
}
}
public void ForceExecute()
{
Execute();
}
public void Execute()
{
t = new Task(() => ParseAndHighLight(CTTest.Token));
t.Start();
}
Cancelling when text changes:
private void xParsedRichEditControl_TextChanged(object sender, EventArgs e)
{
CTTest.CancellationTokenSource.Cancel();
}

How can I use List.Contains

I have assignment to make Hangman game with methods, so far everything is going ok until I realized that the word that I input by char when has two consecutive characters it can't get the following if statement
if (correctGuesses.Count == randomWord.Length)
{
Console.WriteLine("You won the word is: {0}", randomWord);
break;
}
and thus I can never finish the game if the word is something like Green
I was trying to use List.Contains('*') if contains it to continue if not to break and to write the Word thus to win, but it fails if I put '!' in front or if I don't put it, it becomes a endless loop . Could you please help me if there is a way to use Contains in a way that will not search only for one symbol but will check for every single one until there is no more.
I will post the code here.
static string GeneratingRandomWords()
{
Random r = new Random();
List<string> words = new List<string>() { /*"Cat", "Dog", "Eagle", "Lion", "Shark",*/ "Green" };
string word = words[r.Next(0, words.Count)];
return word;
}
static char Input()
{
char inputt = char.Parse(Console.ReadLine());
return inputt;
}
static char[] TransformingCharToInvisible(string randomWord)
{
char[] charFromString = randomWord.ToCharArray();
for (int i = 0; i < randomWord.Length; i++)
{
charFromString[i] = '*';
}
Console.WriteLine(charFromString);
return charFromString;
}
static int CorrectGuesses(char input, string randomWord, int correct)
{
if (randomWord.Contains(input))
{
Console.WriteLine("Next");
correct++;
}
return correct;
}
static int Lives(string randomWord, char input, int lives)
{
if (!randomWord.Contains(input))
{
Console.WriteLine("Try another one");
lives--;
}
return lives;
}
static List<char> CorrectWord(List<char> correctGuesses, string randomWord, char input)
{
if (randomWord.Contains(input))
{
correctGuesses.Add(input);
char[] charFromString = randomWord.ToCharArray();
for (int i = 0; i < randomWord.Length; i++)
{
charFromString[i] = '*';
if (correctGuesses.Contains(randomWord[i]))
{
charFromString[i] = randomWord[i];
}
}
Console.WriteLine(charFromString);
}
return correctGuesses;
}
static void Main(string[] args)
{
string randomWord = GeneratingRandomWords();
TransformingCharToInvisible(randomWord);
List<char> correctGuesses = new List<char>();
int lives = 10;
int correct = 0;
//bool won = true;
while (true)
{
Console.WriteLine("Write a char");
char input = Input();
correct = CorrectGuesses(input, randomWord, correct);
lives = Lives(randomWord, input, lives);
if (correctGuesses.Contains(input))
{
Console.WriteLine("You've already tried '{0}', and it was correct!", input);
continue;
}
correctGuesses = CorrectWord(correctGuesses, randomWord, input);
if (lives == 0)
{
Console.WriteLine("You lose sorry, try againg next time ");
break;
}
if (correctGuesses.Count == randomWord.Length)
{
Console.WriteLine("You won the word is: {0}", randomWord);
break;
}
}
}
Here a simplified version of your code where i did not add all the error checking but the basics use the required Contains to check if letters are found
static void Main(string[] args)
{
var lives = 10;
var correctGuesses = new List<char>();
var word = "green";
while (true)
{
Console.WriteLine("Guess a letter? ");
// deliberatly just check for 1 character for simplicity reasons
var input = Console.ReadLine()[0];
// if already guessed give a chance to the user to retry
if (correctGuesses.Contains(input))
{
Console.WriteLine("Letter already guessed");
}
else
{
// if the word contains the letter
if (word.Contains(input))
{
// add as a correct guess
correctGuesses.Add(input);
Console.WriteLine("Letter found");
}
else
{
// letter dont exist remove a life
lives--;
Console.WriteLine("Letter not found");
}
}
// check if the user still have lives
if (lives == 0)
{
Console.WriteLine("You lost");
break;
}
// check if the amount of distinct character in the word match
// the amount found. This mean the word is completly guessed
else if (word.Distinct().Count() == correctGuesses.Count())
{
Console.WriteLine("You won you found the word");
break;
}
}
Console.ReadKey();
}
I didn't understand you very well
but I modified your code like this :
private static bool IsCorrectGuess(char input, string actualWord)
{
return actualWord.Contains(input);
}
private static void WriteCorrectGuesses(ICollection<char> correctGuesses, string randomWord)
{
char[] charFromString = randomWord.ToCharArray();
for (var i = 0; i < randomWord.Length; i++)
{
charFromString[i] = '*';
if (correctGuesses.Contains(randomWord[i]))
charFromString[i] = randomWord[i];
}
Console.WriteLine(charFromString);
}
private static string GeneratingRandomWords()
{
var r = new Random();
var words = new List<string>
{
/*"Cat", "Dog", "Eagle", "Lion", "Shark",*/ "Green"
};
return words[r.Next(0, words.Count)];
}
private static char Input()
{
return char.Parse(Console.ReadLine() ?? throw new InvalidOperationException());
}
private static void Main(string[] args)
{
string randomWord = GeneratingRandomWords();
var correctGuesses = new List<char>();
WriteCorrectGuesses(correctGuesses, randomWord);
var lives = 10;
var correct = 0;
//bool won = true;
while (true)
{
Console.WriteLine("Write a char");
char input = Input();
if (IsCorrectGuess(input, randomWord))
{
// correct letter
int score = randomWord.ToCharArray().Count(item => item == input);
for (var i = 0; i < score; i++)
{
correctGuesses.Add(input);
correct++;
}
WriteCorrectGuesses(correctGuesses, randomWord);
if (correctGuesses.Count == randomWord.Length)
{
Console.WriteLine("You won the word is: {0}", randomWord);
Console.Read();
break;
}
Console.WriteLine("Next");
}
else
{
// wrong letter
Console.WriteLine($"Try another one. You still have {lives} to try.");
lives--;
}
if (lives == 0)
{
Console.WriteLine("You lose sorry, try again next time ");
break;
}
}
}

Foreach won't return elements of char list

So im making this hangman game, as Im trying to learn C# but now im stuck with System.Collection.Generic.List'1[System.Char]. What im trying to do is to save wrong answers into List nepravilne, look into functions izpis and igra
class Program
{
static private int _sccore;
static void Main(string[] args)
{
string beseda;
int dolzina;
bool play=true;
char input;
do
{
beseda = izberi_besedo();
dolzina = beseda.Length;
igra(beseda, dolzina);
Console.WriteLine("Vnesite Y za nadaljevanje ali N za zakljucitev igre.");
input = char.Parse(Console.ReadLine());
if (input.Equals('y'))
{
play = true;
Console.WriteLine("play {0}",play);
}
if (input.Equals('n'))
{
play = false;
Console.WriteLine("play {0}", play);
}
} while (play == true);
}
static private string izberi_besedo() {
string[] besede = { "voda", "ladija", "letalo", "motor", "klavir", "harmonika", "saksofon", "oklep", "penkalo", "tiskalnik", "miza", "copat", "krogla", "klobuk", "gumb", "harfa", "kontrabas", "mandarina", "les", "knjiga", "vlak", "vijak", "struna", "kozarec" };
Random rnd = new Random();
int stevilka = rnd.Next(0, 23);
string beseda = besede[stevilka];
return beseda;
}
static private void igra (string beseda, int dolzina){
int i, poizkusi = 0;
int pravilne = 0;
bool endloop = false;
char crka;
List<char> nepravilne = new List<char>();//declaring char list for wrong words
string[] odkrite = new string[dolzina];
for(i=0; i<dolzina; i++) { odkrite[i] = "_"; }
do {
izpis(odkrite,nepravilne); //izpis - function which returns just text, we are inputing list nepravilne, which are wrong answers
vpis(out crka);
if (!(beseda.Contains(crka)))//if word doesen't contain letter
{
poizkusi++;
_sccore--;
nepravilne.Add(crka);//add that letter to list
}
for (i = 0; i<dolzina; i++)
{
if (crka.Equals(beseda[i]))
{
odkrite[i] = Convert.ToString(crka);
pravilne++;
_sccore++;
}
}
Console.Clear();
if (pravilne >= dolzina || poizkusi >= 4)endloop = true;
} while (endloop==false);
}
static private void vpis(out char crka)
{
string vpis;
bool stevilka=false, status;
Console.WriteLine("\nVnesite crko za ugibanje besede");
vpis = Console.ReadLine();
stevilka = IsNumeric(vpis);
if (vpis.Length == 1 && stevilka==false)
{
crka = Convert.ToChar(vpis);
}
else
{
do
{
status = false;
if (vpis.Length!=1) Console.WriteLine("Vnesli ste prevec crk, poizkusite ponovno");
if(stevilka==true) Console.WriteLine("Vnesli ste stevilko, poizkusite ponovno");
vpis = Console.ReadLine();
stevilka = IsNumeric(vpis);
if (vpis.Length == 1 && stevilka == false)
{
status = true;
}
} while (status==false);
crka = Convert.ToChar(vpis);
}
}
private static bool IsNumeric(string vpis)
{
int number;
return int.TryParse(vpis, out number);
}
private static void izpis(string[] odkrite, List<char> nepravilne)
{
Console.Write("Rezultat {0} | ", _sccore);
foreach (char element in nepravilne)//write out char elements which contain letter
{
Console.Write("{0} ", nepravilne);
}
Console.WriteLine();
foreach (string element in odkrite)
{
Console.Write("{0} ", element);
}
}
}
}
I think you have a typo in the following code, i.e I think you are intending to print the variable element in the loop and not nepravilne:
foreach (char element in nepravilne)
{
Console.Write("{0} ", nepravilne);
}
Should be as follows instead?
foreach (char element in nepravilne)
{
Console.Write("{0} ", element);
}

Pass variables between methods

I have 2 methods in my program.
1. Start Exam
2. Mark Exam
as you see,
in first method i calculated how many questions are correct or wrong.
in second, i want to show the result.
PROBLEM:
How can i pass the correctlyAnswers and wrongAnswers and LIST between methods?
this is what i have:
static void Main(string[] args)
{
int menuChoice = 99;
Question[] questions = new Question[30];
do
{
Console.Clear();
DisplayMenu();
menuChoice = InputOutput_v1.GetValidInt("Option");
switch (menuChoice)
{
case 1:
InputOutput_v1.DisplayTitle("Start Exam");
CreateQuestion(questions);
break;
case 2:
InputOutput_v1.DisplayTitle("Mark Exam");
DisplayAllQuestions(questions);
break;
}
} while (menuChoice != '0') ;
}
public static void StartExam(Question[] questions)
{
char[] studentAnswers = new char[30];
int[] wrongAnswers = new int[30];
int correctlyAnswered = 0;
int falselyAnswered = 0;
string list = "";
Console.Clear();
Console.WriteLine("== DO NOT CHEAT! ==\n");
Console.WriteLine("----------------");
for (int i = 0; i < studentAnswers.Length; i++)
{
Console.WriteLine("\nQuestion {0}", i + 1);
Console.WriteLine("------------");
questions[i].DisplayQuestion();
studentAnswers[i] = InputOutput_v1.GetValidChar("Your Answer (A, B, C, D)");
if (studentAnswers[i] == questions[i].correctAnswer)
{
correctlyAnswered = correctlyAnswered + 1;
}
else
{
falselyAnswered = falselyAnswered + 1;
wrongAnswers[i] = i + 1;
list += i + 1 + ", ";
}
}
}
public static void MarkExam(Question[] questions)
{
if (correctlyAnswered >= 15)
{
Console.WriteLine("Passed with {0} correct answers", correctlyAnswered);
}
else
{
Console.WriteLine("Failed with {0} incorrect answers. Incorrect answers are: {1} ", falselyAnswered, list.Remove(list.Length - 2));
}
Console.ReadLine();
}
Well, why can't you call your MarkExam() method passing those variables like
public static void StartExam(Question[] questions)
{
char[] studentAnswers = new char[30];
int[] wrongAnswers = new int[30];
int correctlyAnswered = 0;
int falselyAnswered = 0;
string list = "";
........
MarkExam(questions, wrongAnswers, correctlyAnswered);
In which case you will have to change your method signature accordingly
Make the correctlyAnswers, wrongAnswers, and LIST defined as global variables outside your methods, e.g.
private int correctlyAnswered;
private int falselyAnswered;
private Question[] questions;
First, why are you using an array instead of a list? And inside this class your methods don't really need to be static.
Second, create private variables to the class and they can be used in all methods.
public class ExamClass
{
private List<Question> questions = new List<Question>();
private List<Question> incorrect = new List<Question>();
private List<Question> correct = new List<Question>();
...
}

Replace characters with complete words depending on their position C#

I have a list of incomplete band names such as
string band1 = "ONE ...", string band2 = "... 5", string band3 = "30 ... ... ...", string band4 = "The ... Stones"
I need to replace the characters ... to form the full band's name so they become
ONE DIRECTION, MAROON 5, 30 SECONDS TO MARS, THE ROLLING STONES
I have the associated answer, for example the string DIRECTION that can be combined with string band1 = ONE ... to form ONE DIRECTION. My question is since the ... characters may be located before or after the string ONE, how can I make sure to create ONE DIRECTION instead of DIRECTION ONE and so on?
use a combination of string.StartsWith, string.EndsWith and string.Length to determine the correct replacement
Try this example with your sample strings. You will the get the idea.
static void Main(string[] args)
{
string question = string.Empty;
string answer = string.Empty;
string formattedString = string.Empty;
question = Console.ReadLine();
Console.WriteLine("Replace ... with:");
answer = Console.ReadLine();
formattedString = question.Replace("... ", answer);
Console.WriteLine(formattedString);
Console.ReadLine();
}
EDIT 2:
In this solution, i'm splitting your string by ... to an array of strings. Like this you know where ... are placed at, no matter how many of them are around and where they are and finally merge them.
Additionally i'm working with a Dictionary, with this it's dynamic.
Have a look at the code:
static void Main(string[] args)
{
Dictionary<string, string> bands = new Dictionary<string, string>();
bands.Add("30 ... ... ...", "SECONDS TO MARS");
bands.Add("... 5", "MAROON");
bands.Add("... STEPS ... ...", "TWO FROM HELL");
foreach (KeyValuePair<string, string> band in bands)
{
bool solved = false;
while (!solved)
{
Console.WriteLine("current band: " + band.Key);
string input = Console.ReadLine();
if (band.Value == input.ToUpper())
{
Console.WriteLine("correct");
string[] splittedQuestion = band.Key.Split(new string[] { "..." }, StringSplitOptions.None);
string[] splittedAnswer = band.Value.Split(' ');
// fill splittedQuestion string with answer values
for (int i = 0; i < splittedAnswer.Count(); i++)
{
int currentIndex = GetNextDotIndex(splittedQuestion);
if (currentIndex != -1)
{
splittedQuestion[currentIndex] = splittedAnswer[i];
}
}
// build result
string result = "";
for (int i = 0; i < splittedQuestion.Count(); i++)
{
result += splittedQuestion[i].Trim().ToUpper();
if (i < splittedQuestion.Count() - 1)
{
result += " ";
}
}
Console.WriteLine(result);
solved = true;
}
else
{
Console.WriteLine("wrong");
}
}
}
Console.WriteLine("finished");
Console.ReadLine();
}
private static int GetNextDotIndex(string[] splittedQuestion)
{
for (int j = 0; j < splittedQuestion.Count(); j++)
{
if (splittedQuestion[j] == "" || splittedQuestion[j] == " ")
{
return j;
}
}
// return -1, when no more ... are available
return -1;
}

Categories