Codeword NextCodeword(Codeword codeword, uint suffixBit) { var c = Codeword.Clone(codeword); c.AppendBit(suffixBit); return(c); }
/// <summary> /// Deep clone the codeword /// </summary> /// <param name="codeword"></param> /// <returns></returns> public static Codeword Clone(Codeword codeword) { return(new Codeword { BitLength = codeword.BitLength, value = codeword.value }); }
public void Ask() { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("\nPlease type a letter."); Console.ResetColor(); string input = Console.ReadLine(); // check and tell user to enter ONLY 1 letter if (input.Length > 1) { Console.WriteLine("Please only type ONE letter at a time!\nPress Enter to continue..."); Console.ReadLine(); return; } // check if the input is a letter and prompt the user if not if (!char.IsLetter(input[0])) { Console.WriteLine("Please only enter letters!\nPress Enter to continue..."); Console.ReadLine(); return; } // convert input to lower letter string letter = input[0].ToString().ToLower(); // if the letter has already been inclear if (letterList.Contains(letter)) { Console.WriteLine("'{0}' has already been found.\nPlease insert another letter\nPress Enter to continue...", letter); Console.ReadLine(); return; } letterList.Add(letter); // check if the word contains the inserted letter if (Codeword.Contains(letter)) { // prompt the user Console.WriteLine("Good guess. The codeword contains '{0}'.", letter); // loop thru codeword for inserted letter // if found replace '_' with letter in current word for (int i = 0; i < Codeword.Length; i++) { if (Codeword[i].ToString() == letter) { CurrentWord = CurrentWord.Remove(i, 1).Insert(i, letter); } } } // increase the wrong guesses by 1 and Update ufo image else { CurrentNumWrongGuesses += 1; ufo.AddPart(); } }
public bool didWin() { if (Codeword.Equals(word)) { return(true); } else { return(false); } }
public void Ask() { if (_guessedLetters.Count > 0) { Console.WriteLine($"Already typed letters in thi game session: "); _guessedLetters.ForEach(item => Console.Write($" {item} ")); Console.WriteLine(""); } Console.Write("Enter your guess letter: "); var input = Console.ReadLine()?.ToLower(); if (input == null) { return; } if (input.Length > 1) { Console.WriteLine("Only one letter at a time is valid."); return; } var contains = Codeword.Contains(input); var letter = input.ToCharArray()[0]; if (_guessedLetters.Contains(letter)) { Console.WriteLine($"You already typed the ({input}) letter, try another one..."); return; } if (contains) { var currentIndex = 0; while (true) { var index = Codeword.IndexOf(letter, currentIndex); if (index == -1) { break; } currentIndex = index + 1; CurrentWord = CurrentWord.Remove(index, 1).Insert(index, letter.ToString()); } } else { Ufo.AddPart(); WrongGuesses++; } _guessedLetters.Add(letter); }
// Method checks if the player won & prints a congrats message public bool DidWin() { // return true/false if (Codeword.Equals(CurrentWord)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\n Good job, you win!\nThe passcode was: {0}", Codeword); Console.ResetColor(); } //test only //Console.WriteLine(Codeword); //Console.WriteLine(CurrentWord); return(Codeword.Equals(CurrentWord)); }
/// <summary> /// 符号化領域のバイトデータを返します。 /// </summary> private byte[] GetEncodingRegionBytes() { byte[][] dataBlock = BuildDataBlock(); byte[][] ecBlock = BuildErrorCorrectionBlock(dataBlock); int numCodewords = Codeword.GetTotalNumber(_currVersion); int numDataCodewords = DataCodeword.GetTotalNumber( _parent.ErrorCorrectionLevel, _currVersion); byte[] ret = new byte[numCodewords]; int index = 0; int n; n = 0; while (index < numDataCodewords) { int r = n % dataBlock.Length; int c = n / dataBlock.Length; if (c <= dataBlock[r].Length - 1) { ret[index] = dataBlock[r][c]; index++; } n++; } n = 0; while (index < numCodewords) { int r = n % ecBlock.Length; int c = n / ecBlock.Length; if (c <= ecBlock[r].Length - 1) { ret[index] = ecBlock[r][c]; index++; } n++; } return(ret); }
// turn the codewords into canonical ordered ones // needed for make table later void MakeCanonical(List <Node> leaves1) { // see https://pineight.com/mw/index.php?title=Canonical_Huffman_code // http://www.compressconsult.com/huffman // https://en.wikipedia.org/wiki/Canonical_Huffman_code // sort by bit lengths to order symbol values. // on ties, sort by symbol leaves1.Sort((a, b) => { int result = a.Codeword.BitLength.CompareTo(b.Codeword.BitLength); if (result == 0) { return(a.Symbol.CompareTo(b.Symbol)); } return(result); }); // rewrite symbols in increasing order, maintaining code lengths if (Options.HasFlag(OptionFlags.DumpDictionary)) { WriteLine("Huffman dictionary: "); } var codeValue = new Codeword(); codeValue.AppendBit(0); // start at the 1-bit code of 0 foreach (var leaf in leaves1) { // lengthen code while (codeValue.BitLength < leaf.Codeword.BitLength) { codeValue.AppendBit(0); } leaf.Codeword = Codeword.Clone(codeValue); if (Options.HasFlag(OptionFlags.DumpDictionary)) { WriteLine($" x{leaf.Symbol:X2}->{leaf.Codeword}"); } codeValue.Increment(); } }
public void Ask() { Console.WriteLine("Enter a letter."); string stringGuess = Console.ReadLine(); if (stringGuess.Trim().Length != 1) { Console.WriteLine("One letter at a time!"); return; } char guess = stringGuess.Trim().ToCharArray()[0]; if (Codeword.Contains(guess)) { Console.WriteLine($"'{guess}' is in the word!"); for (int i = 0; i < Codeword.Length; i++) { if (guess == Codeword[i]) { word = word.Remove(i, 1).Insert(i, guess.ToString()); } } } }
public bool DidWin() { return(Codeword.Equals(CurrentWord)); }