How do I remove the input halt to automate the input? - c#

So basically the program does what it is supposed to do. The two patterns have to be split with an open line. But I did something wrong now the input wont go in smoothly. I have to press enter a couple of times. The Expected input is:
...........*........
....*.....*.........
.........*..*...*...
*..*..*......***....
..*.....*...........
.*..................
.......*.........*.*
....................
.....*............*.
..........
.*.**.*...
*....*.*.*
..........
..*.....*.
and Output should be
...................*
.................**.
..............***...
........******......
......**............
.....*..............
..***...............
....................
**..................
..........
......****
..****....
..........
**........
But mine looks like this
...........*........
....*.....*.........
.........*..*...*...
*..*..*......***....
..*.....*...........
.*..................
.......*.........*.*
....................
.....*............*.
...................*
.................**.
..............***...
........******......
......**............
.....*..............
..***...............
....................
**..................
..........
.*.**.*...
*....*.*.*
..........
..*.....*.
......****
..****....
..........
**........
I have to press enter a couple of times to get this.
My Code looks like this
using System;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System.Linq;
public class Program
{
static void defragDisk(Queue<string> diskLine) // Method that will move all the Starsto the right
{
int lnLength = 0;
int iIndex = 0;
int iStars = 0;
int iTotalStars = 0;
while (diskLine.Count > 0)
{
var line = diskLine.Dequeue();
lnLength = line.Length;
iIndex = lnLength - 1 - iTotalStars;
iStars = line.Count(x => x == '*');
iTotalStars += iStars;
var rangeFrom = iIndex - iStars + 1;
var availableIndexes = Enumerable.Range(rangeFrom, iStars).ToDictionary(x => x);
for (int i = 0; i < lnLength; i++)
{
if (availableIndexes.ContainsKey(i))
Console.Write("*");
else
Console.Write(".");
}
Console.Write("\n");
}
}
static void populateQueue(Queue<string> diskLine) // I am using a queue as the sizes can vary without indication
{
bool bCompleted = false;
while (!bCompleted)
{
var line = Console.ReadLine();
if (line == "")
{
bCompleted = true;
break;
}
else
{
diskLine.Enqueue(line);
}
}
}
public static void Main()
{
bool bCompleted = false;
Queue<string> diskLine = new Queue<string>();
while (!bCompleted)
{
populateQueue(diskLine);
defragDisk(diskLine);
diskLine.Clear();
if (Console.ReadLine() == "")
{
bCompleted = true;
break;
}
}
}
}

Related

Save and load a game by using StreamWriter and ReadWriter (C#)

I'm creating a small program which can save and load char array values. Then, I got stuck with two problems.
I have no idea how to make the program end after saving the data.
After loading the char array, it looks the game starts where I saved last time. However, when I put "#" on the place where is already marked, it is accepted. (It is supposed to display error message)
This is when I start new game.
It displays error message properly.
Here is class which includes streamWriter and streamReader.
public class History
{
public char QUIT = 'Y';
public char CONTINUE = 'N';
public char inputGameContinue;
public void WriteFile(char []arr)
{
FileStream sb = new FileStream("MyFile.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(sb);
WriteLine("If you want to save the data, enter" + QUIT +"| To continue, enter "+CONTINUE );
inputGameContinue = char.Parse(ReadLine());
if(inputGameContinue=='Y')
{
sw.Write(arr);
WriteLine("The data is saved!");
}
sw.Close();
}
public void ReadFile()
{
string path = "MyFile.txt";
WriteLine("New game? >> 1 | Load saved data? >>2 ");
int command = int.Parse(ReadLine());
if (command == 2)
{
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
WriteLine(sr.ReadLine());
}
}
}
}
}
Class2
using System;
using static System.Console;
using System.IO;
namespace createSample
{
public class writeRead
{
public static void Main(string[] args)
{
InputClass inputClass = new InputClass();
ArrayValue arrayValue = new ArrayValue();
History history = new History();
WriteLine("Welcome to game!");
WriteLine("");
history.ReadFile();
do
{
inputClass.inputNumber();
while (true)
{
if (arrayValue.arr[inputClass.input] == '#')
{
WriteLine("{0} already marked '#'. Try another.", inputClass.input);
inputClass.inputNumber();
}
else
{
arrayValue.arr[inputClass.input] = '#';
arrayValue.printArray();
history.WriteFile(arrayValue.arr);
break;
}
}
}
while (checkWhenFinish(arrayValue)!=1 );
WriteLine("All letters are marked with '#'");
Read();
}
public static int checkWhenFinish(ArrayValue a)
{
if(a.arr[0] != '0' && a.arr[1] != '1' && a.arr[2] != '2' && a.arr[3] != '3' && a.arr[4] != '4')
{
return 1;
}
else
{
return 0;
}
}
}
}
Class3
using System;
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace createSample
{
public class ArrayValue
{
public char []arr = { '0','1', '2', '3', '4' };
public void printArray()
{
WriteLine("{0},{1},{2},{3},{4}", arr[0], arr[1], arr[2], arr[3], arr[4]);
}
}
}
Class 4
using System;
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace createSample
{
public class InputClass
{
public int input;
public void inputNumber()
{
while (true)
{
Write("Enter number ? (0 to 4) >> ");
if (!int.TryParse(ReadLine(), out input))
{
input = -1;
}
if (input == 0|| input == 1 || input == 2 || input == 3 || input == 4 )
{
break;
}
else
{
WriteLine("Error! Try again!");
}
}
}
}
}
The problem is when you call history.ReadFile() it can read the file and display its contents, but it never updates arrayValue.arr so when the check is done later, arrayValue.arr[inputClass.input] is still 2.
You might want to pass in arrayValue by reference to ReadFile() to have it updated:
history.ReadFile(ref arrayValue);
then in History
public void ReadFile(ref ArrayValue arrayValue)
{
...
while (sr.Peek() >= 0)
{
var line = sr.ReadLine();
WriteLine(line);
for (int i = 0; i < arrayValue.arr.Length; i++)
{
arrayValue.arr[i] = line[i];
}
}
...
}

How to unit test code that requires user input c#

its my first time doing unit tests/integration tests and I have a question. So I started doing unit tests for my code, but I have a method, which is actually the logic of the whole application, where multiple methods are called, and user input is required. How can I test that method? Here is the method:
public async Task RunAsync()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var playAgain = 'y';
do
{
var attempts = 1;
var foundNumber = false;
while (attempts < 10 && foundNumber == false)
{
try
{
var inputNumber = int.Parse(GetInput());
if (inputNumber == _randomNumber)
{
foundNumber = true;
OnSuccesfulGuess(watch, attempts);
}
else if (attempts < 10)
{
OnWrongGuessWithinAttempts(inputNumber);
}
else
{
Console.WriteLine("Oops, maybe next time.");
}
}
catch (Exception ex)
{
Console.WriteLine("Please enter a number");
}
attempts++;
}
playAgain = PlayAgain(playAgain);
_randomNumber = await GetRandomNumber(1, 100);
Log.Information("User wants to play again");
}
while (playAgain == 'y' || playAgain == 'Y');
}
This is the method where i run in my Program class in order to start the application.
Your code is essentially untestable.
The method does too much work. It should be splitted into several smaller ones that can be tested separately.
You should get rid of static methods. Because you can't get them fake.
Getting data over the network (I see using WebSocket), as well as from the database or file system, should be brought out. You should pass ready-made data to this method.
Here is the modified code, broken down into small methods. Logging and events are removed from the code so as not to complicate the explanation.
public class App
{
private readonly Random _random = new Random();
private Task<int> GetRandomNumber(int min, int max)
{
return Task.FromResult(_random.Next(min, max));
}
internal int GetInput()
{
Console.WriteLine("Please guess a number between 1 and 100");
int value;
while (true)
{
string input = Console.ReadLine();
bool result = int.TryParse(input, out value);
if (!result)
Console.WriteLine("Not a number");
else if (value < 1 || value > 100)
Console.WriteLine("Must be between 1 and 100");
else
break;
}
return value;
}
internal bool PlayAgain()
{
Console.WriteLine("Do you want to play again?");
string input = Console.ReadLine();
return input == "Y" || input == "y";
}
internal void Guessing(int randomNumber)
{
int attempts = 1;
while (attempts < 10)
{
var inputNumber = GetInput();
// logging
if (inputNumber == randomNumber)
{
// OnSuccesfulGuess
return;
}
else
{
// OnWrongGuessWithinAttempts
}
attempts++;
}
Console.WriteLine("Oops, maybe next time.");
// logging
}
public async Task RunAsync()
{
do
{
int randomNumber = await GetRandomNumber(1, 100);
Guessing(randomNumber);
}
while (PlayAgain());
}
}
Now we have the ability to test individual methods.
I use MSTest.
[DataTestMethod]
[DataRow("Y")]
[DataRow("y")]
public void PlayAgain_InputY_ReturnsTrue(string value)
{
using (var reader = new StringReader(value))
{
Console.SetIn(reader);
var app = new App();
bool result = app.PlayAgain();
Assert.IsTrue(result);
}
}
[DataTestMethod]
[DataRow("N")]
[DataRow("boo")]
[DataRow("")]
public void PlayAgain_InputNotY_ReturnsFalse(string value)
{
using (var reader = new StringReader(value))
{
Console.SetIn(reader);
var app = new App();
bool result = app.PlayAgain();
Assert.IsFalse(result);
}
}
We do the same with the other methods.
Here are the tests for the GetInput method.
Since there is a loop inside that runs indefinitely when incorrect values are entered, we must interrupt it by entering the correct value. This is done by passing two values via a line feed: "0\n50". Entering an incorrect value is a test of the output string, then interrupting the loop with the correct value.
[DataTestMethod]
[DataRow("1")]
[DataRow("50")]
[DataRow("100")]
public void GetInput_InputCorrectString_ReturnsNumber(string value)
{
using (var reader = new StringReader(value))
{
Console.SetIn(reader);
var app = new App();
int actual = app.GetInput();
int expected = int.Parse(value);
Assert.AreEqual(expected, actual);
}
}
[DataTestMethod]
[DataRow("0\n50")]
[DataRow("101\n50")]
public void GetInput_InputSmallerOrGreaterValue_WritesMessage(string value)
{
using (var reader = new StringReader(value))
using (var writer = new StringWriter())
{
Console.SetIn(reader);
Console.SetOut(writer);
var app = new App();
_ = app.GetInput();
string actualMessage = writer.ToString();
string expectedMessage = "Must be between 1 and 100";
Assert.IsTrue(actualMessage.Contains(expectedMessage));
}
}
[DataTestMethod]
[DataRow("x\n50")]
[DataRow("qwerty\n50")]
public void GetInput_InputNotNumber_WritesMessage(string value)
{
using (var reader = new StringReader(value))
using (var writer = new StringWriter())
{
Console.SetIn(reader);
Console.SetOut(writer);
var app = new App();
_ = app.GetInput();
string actualMessage = writer.ToString();
string expectedMessage = "Not a number";
Assert.IsTrue(actualMessage.Contains(expectedMessage));
}
}
Normally the unit testing methods are made regarding the different results that may return. You can create an interface to handle this method and communicate giving values depending of the output expected (Mocking). Check this post maybe would help!:
How do I apply unit testing to C# function which requires user input dynamically?

SimpleAudioPlayer CurrentPosition and Duration are always 0

I'm trying to make a playlist in Xamarin Forms using a list of audios that can be added in a ListView through other ListViews.
I'm using the SimpleAudioPlayer plugin in order to reach this.
I would like to get multiple audios played consecutively.
The play function is working fine and the Stream doesn't throw any Exception.
Right now, the code plays always the same audio multiple times.
To reach the consecutive effect, I tried to make a while loop using CurrentPosition and Duration properties inside a for loop for each track in the playlist, but I didn't find any self esplicative documentation about these two properties.
using G.Models;
using Plugin.SimpleAudioPlayer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Input;
using Xamarin.Forms;
namespace G.ViewModels
{
class ABSViewModel : BaseViewModel{
public ObservableCollection<PlayListItem> Playlist { get; set; }
public ICommand PlayPlaylistCommand { get; set; }
private PlayListItem playlistSelectedItem;
public PlayListItem PlaylistSelectedItem
{
get {
return playlistSelectedItem;
}
set {
playlistSelectedItem = value;
}
}
public ABSViewModel()
{
audio = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
PlayButtonText = "PLAY";
PlayButtonImage = "play_icon.png";
PlayButtonFlag = "1";
PlayPlaylistCommand = new Command(PlayPlaylist);
Playlist = new ObservableCollection<PlayListItem>();
}
private async void PlayPlaylist()
{
if (PlayButtonFlag == "1")
{
PlayButtonText = "PAUSE";
PlayButtonImage = "pause_icon.png";
PlayButtonFlag = "0";
int startPosition;
if(PlaylistSelectedItem != null)
{
startPosition = 0;
}
else
{
startPosition = Playlist.IndexOf(playlistSelectedItem);
}
for(int i = 0; i < Playlist.Count; i++)
{
if (audio.IsPlaying) audio.Stop();
PlaylistSelectedItem = Playlist[i];
OnPropertyChanged(nameof(Playlist));
audio.Load(GetStreamFromFile(Playlist[i].Name + ".wav"));
audio.Play();
while (audio.CurrentPosition != audio.Duration) {
Debug.WriteLine("Test");
}
}
}
else
{
PlayButtonText = "PLAY";
PlayButtonImage = "play_icon.png";
PlayButtonFlag = "1";
if (audio.IsPlaying) audio.Pause();
}
NotifyButtonChange();
}
Stream GetStreamFromFile(string filename)
{
var assembly = typeof(App).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("G2019." + "RSNZBile.wav");
return stream;
}
private void NotifyButtonChange()
{
OnPropertyChanged(nameof(PlayButtonText));
OnPropertyChanged(nameof(PlayButtonImage));
OnPropertyChanged(nameof(PlayButtonFlag));
}
}
}
I omit the Commands to add elements to the playlist or to stop the playlist from playing because I think they are out of context.
Now, when I press the "Play" button, I see in the debugger that my while cycle did not start at all. So, the player plays all the elements of the playlist in few milliseconds and the only one the human can hear is the last one. That is because my CurrentPosition and Duration properties are both set to 0. Why?
Did anyone try to do something like this?
If anyone is interested, I solved the problem using PlaybackEnded event.
Every piece of code that was inside the for loop was moved inside the PlaybackEnded handler function with no need of a while. The Duration value set to 0 is still a mystery.
private async void PlayPlaylist(string value)
{
if (PlayButtonMode == 1)
{
int startPosition;
//Playing playlist from selected item if user selected one. (first item if he didn't)
if (PlaylistSelectedItem == null) startPosition = 0;
else startPosition = Playlist.IndexOf(playlistSelectedItem);
if (audio.IsPlaying) audio.Stop();
PlaylistSelectedItem = Playlist[startPosition];
audio.Load(GetStreamFromFile(PlaylistSelectedItem.Name + ".wav"));
audio.Play();
audio.PlaybackEnded += Audio_PlaybackEnded;
}
else if (PlayButtonMode == 2)
audio.Play();
else
{
if (audio.IsPlaying) audio.Pause();
}
ChangeButtonMode(PlayButtonMode);
}
private void ChangeButtonMode(int senderMode)
{
if (senderMode == 1 || senderMode == 2)
{
PlayButtonText = "PAUSE";
PlayButtonImage = "pause_icon.png";
PlayButtonMode = 0;
}
else
{
PlayButtonText = "PLAY";
PlayButtonImage = "play_icon.png";
if (senderMode == 3) PlayButtonMode = 1;
else PlayButtonMode = 2;
}
NotifyButtonChange();
}
private void Audio_PlaybackEnded(object sender, EventArgs e)
{
//Check if next playlist element exists
if(Playlist.IndexOf(PlaylistSelectedItem) + 1 < Playlist.Count)
{
PlaylistSelectedItem = Playlist[Playlist.IndexOf(PlaylistSelectedItem) + 1];
if (audio.IsPlaying) audio.Stop();
audio.Load(GetStreamFromFile(PlaylistSelectedItem.Name + ".wav"));
audio.Play();
audio.PlaybackEnded += Audio_PlaybackEnded;
}else if(audio.IsPlaying) audio.Stop();
}
private void StopAudio()
{
if (audio.IsPlaying)
audio.Stop();
ChangeButtonMode(3);
}

Check ReadKey only for letters (alphabet) in string

i am doing game "Hangman" but i have a problem. I am using ReadKey to find the guess of player, but he can use even numbers or buttons like "Enter" etc. I want to stop this, but i don't know how :/ Can you help me please? I want control, if in result of ReadKey is letter from alphabet or no (but it must be in "string" format, cuz i am working later with string). Thank you very much guys, here is my code :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hangman
{
class MainClass
{
/// <summary>
/// Hra Oběšenec
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.WriteLine("Vítejte ve hře Oběšenec!");
//List slov, které bude hráč hádat
string[] words = new string[5];
words[0] = "car";
words[1] = "school";
words[2] = "apple";
words[3] = "orange";
words[4] = "xamarin";
//Získávám délku slova (hádaného slova)
for (int X = 0; X < words.Length; X++)
{
string guessWord = words[X];
short lifes = 5;
short guessedLetters = 0;
char GuessingLetter;
char[] lettersOfWord = new char[guessWord.Length];
//
for (int I = 0; I < guessWord.Length; I++)
{
//Length of word is changing to *
lettersOfWord[I] = '*';
}
bool InGame = true;
//When this while is true, the game is still going, when it will be false, the game will end
while (InGame)
{
//Ochrana proti špatnému inputu
try
{
Console.WriteLine(lettersOfWord);
Console.WriteLine("Stiskněte klávesu. Životy:" + lifes);
ConsoleKeyInfo result_guess = Console.ReadKey(true);
string result = result_guess.KeyChar.ToString();
if (result.Length == 0)
{
continue;
}
//Hráč hádá slovo
GuessingLetter = result[0];
lifes--;
for (int I = 0; I < lettersOfWord.Length; I++)
{
if (lettersOfWord[I] == '*')
{
//Pokud hráč uhádne písmenko ve slově
if (guessWord[I] == GuessingLetter)
{
lettersOfWord[I] = GuessingLetter;
guessedLetters++;
lifes++;
Console.WriteLine("Správně!");
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Nastala neočekávaná chyba!" + e);
}
//Pokud prohraje(promrhá všechny pokusy) hra skončí, avšak pokud slovo uhádne (a zůstane alespoň jeden život) vyhrál hru
if (lifes== 0 || guessedLetters == guessWord.Length)
{
//Konec hry
InGame = false;
if (lifes == 0)
{
Console.WriteLine("Smůla prohrál jsi!");
Console.ReadKey();
break;
}
else
{
//Vítězství nad levlem
Console.WriteLine("Uhodnul jsi celé slovo! Gratuluji!!");
}
}
}
}
}
}
}
Check if the keychar is a letter with the IsLetter method and keep asking for letters if it doesn't. Something like this:
private char GetLetter()
{
while (true)
{
Console.WriteLine("Please input a letter.");
var character = Console.ReadKey().KeyChar;
if (char.IsLetter(character))
return character;
}
}

How to count the number of "correct" and "incorrect" responses

I'm new to coding and am creating a windows form application in C#. I was having difficulties figuring out how to track the number of correct and incorrect responses. When a button is clicked, if the response is correct a label says correct. I want a different label to count the amount of times it says correct and incorrect. Here's what I was thinking but didn't work.
int add = 0;
add = int.Parse(correct.Text);
if (label1.Text == "Correct")
{
correct.Text = add++;
}
else
incorrect.text = add++;
correct and incorrect are names of my label.
Here's my button click event, there are many alike.
private void G_Click(object sender, EventArgs e)
{
if (go.Visible == true)
{
go.Visible = false;
Random rnd = new Random();
int y = rnd.Next(1, 7);
if (y == 1)
{
eo.Visible = true;
}
if (y == 2)
{
ao.Visible = true;
}
if (y == 4)
{
dd.Visible = true;
}
if (y == 5)
{
go.Visible = true;
}
if (y == 6)
{
eeo.Visible = true;
}
timer1.Start();
label1.Text = "Correct";
}
else
{
label1.Text = "Incorrect";
}
private int correctCount = 0;
private int incorrectCount = 0;
if (label1.Text = "Correct";)
{
correctCount++;
correct.Text = correctCount.ToString();
}
else
{
incorrectCount++;
incorrect.Text = incorrectCount.ToString();
}
The immediate problem is that the type of the Text property is string, not int - you have to set it to text, not a number. Fortunately, you can just call ToString on an int to get a string representation. However, I'd suggest there are other things to change too.
I would strongly suggest that you keep separate counters as variables. While you could keep parsing your labels, it's simpler for them to be purely output. So you might have:
// Fields
private int correctCount = 0;
private int incorrectCount = 0;
...
// Wherever your code is
if (correctAnswer)
{
correctCount++;
correct.Text = correctCount.ToString();
}
else
{
incorrectCount++;
incorrect.Text = incorrectCount.ToString();
}
Note the correctAnswer condition here - we don't know what's setting the text of label1, but I'd suggest that that's the right place to also change the correct/incorrect counters, from the same condition. Again, treat label1 just as output, rather than as a feedback system.
class fields = variables that are declared outside of a method and directly inside a class
local variables = variables that are declared inside of a method
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace deleteMe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int correctCount = 0; // field
private int incorrectCount = 0; // field
private void G_Click(object sender, EventArgs e)
{
if (go.Visible == true)
{
go.Visible = false;
Random rnd = new Random();
int y = rnd.Next(1, 7); // local variable
if (y == 1)
{
eo.Visible = true;
}
if (y == 2)
{
ao.Visible = true;
}
if (y == 4)
{
dd.Visible = true;
}
if (y == 5)
{
go.Visible = true;
}
if (y == 6)
{
eeo.Visible = true;
}
timer1.Start();
incorrect.Text = "Correct";
}
else
{
incorrect.Text = "Incorrect";
}
if (label1.Text = "Correct")
{
correctCount++;
correct.Text = correctCount.ToString();
}
else
{
incorrectCount++;
incorrect.Text = incorrectCount.ToString();
}
}
}
}
== equality. (for comparing 2 values)
= assign a value. (for initialize a variable or field)

Categories

Resources