i have it generate a 4 diget random number and save it into diffrent arrays then trying to let the user try and guess the number
Generate a random four digit number. The player has to keep inputting four digit numbers until they guess the randomly generated number. After each unsuccessful try it should say how many numbers they got correct, but not which position they got right. At the end of the game should congratulate the user and say how many tries it took.
this is the breif im trying to follow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace _4_Number_Guess_Game
{
class Program
{
static void Main(string[] args)
{
int[] RanNums = new int[4];
for (int i = 0; i < 4; i++)
{
Random ran = new Random();
RanNums[i] = ran.Next(1, 9);
Thread.Sleep(11);
}
int[] Input = new int[4];
bool guess = false;
while (guess == false)
{
Console.WriteLine("Input a 4 digit number: ");
Console.
Console.ReadLine();
}
Console.ReadLine();
}
}
}
Related
I am a beginner in Unity and I am currently making a simple game. I have a problem managing the flow of my minigame. The minigame is simply finding an object, when I found and tap on the item name below will be shaded or marked or there can be animation just to indicate that the item is found.
What I want to do is to get the name of the objects that need to be found and set them randomly in the three (3) item names below. like every time this minigame opens the names of the items are randomly placed in the 3 texts. And when the item is found the name below will be marked or shaded or anything that will indicate it is found, but for now I will just set it inactive for easier indication. How can I properly do this whole process?
The objects inside the scene are button for them to have onCLick() events
Correction: the term choices are wrong because they just display the name of the items that you need to find, just in case you get confused with the term choice in my minigame. I will fix it.
Here is the visuals for the minigame:
The script I currently have for when the objects was clicked:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using TMPro;
public class ClickObject : MonoBehaviour
{
[SerializeField] Button pillowBtn, pcBtn, lampBtn;
// [SerializeField] TextMeshProUGUI choice1, choice2, choice3;
// FROM THE SOLUTION
[SerializeField] List<GameObject> gameObjectWanted;
[SerializeField] List<TextMeshProUGUI> textBoxes;
// METHOD NAMES IS TEMPORARY
public void pillowClicked()
{
Debug.Log("you found the " + EventSystem.current.currentSelectedGameObject.name);
}
public void desktopClicked()
{
Debug.Log("you found the " + EventSystem.current.currentSelectedGameObject.name);
}
public void lampClicked()
{
Debug.Log("you found the " + EventSystem.current.currentSelectedGameObject.name);
}
}
You asked for a lot and I hope I understood your intention.
first, if you want to randomly choose an amount of game objects from your game, I think the best way to do it is by adding the refernece of all the game objects you want to choose from radomly inside a list of game objects and then randomly take a game object of the list and make it a child of another game object I call "fatherGoTranform" on my code like that:
[SerializeField] List<GameObject> gameObjectWanted;
[SerializeField] float numOfGO = 4;
[SerializeField] Transform fatherGoTranform;
void Start()
{
for(int i=0;i<numOfGO;i++)
{
int index = Random.Range(0, gameObjectWanted.Count-1);
GameObject currentGO = gameObjectWanted[index ];
currentGO.transform.parent = fatherGoTranform;
gameObjectWanted.RemoveAt(index);
}
}
and then to click on a game object and the do with what you want try this:
void Update()
{
//Check for mouse click
if (Input.GetMouseButtonDown(0))
{
RaycastHit raycastHit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out raycastHit, 100f))
{
if (raycastHit.transform != null)
{
//Our custom method.
CurrentClickedGameObject(raycastHit.transform.gameObject);
}
}
}
}
I have not checked the code so if there is an error tell me and I will fix it
This is the solution that worked for my problem. I randomly placed numbers to the list according to childCount and every index indicate the index of the text that I want to put them on which I get as a Transform child.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using TMPro;
public class ClickObject : MonoBehaviour
{
// [SerializeField] Button pillowBtn, pcBtn, lampBtn;
[SerializeField] GameObject choices, v1, finishPanel;
// RANDOM NUMBER HOLDER FOR CHECKING
private int randomNumber, foundCount;
// RANDOMIZED NUMBER LIST HOLDER
public List<int> RndmList = new List<int>();
private void Awake()
{
foundCount = 0;
RndmList = new List<int>(new int[v1.transform.childCount]);
for (int i = 0; i < v1.transform.childCount; i++)
{
randomNumber = UnityEngine.Random.Range(0, (v1.transform.childCount) + 1);
while (RndmList.Contains(randomNumber))
{
randomNumber = UnityEngine.Random.Range(0, (v1.transform.childCount) + 1);
}
RndmList[i] = randomNumber;
// Debug.Log(v1.transform.GetChild(randomNumber-1).name);
choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().text = v1.transform.GetChild(randomNumber - 1).name;
}
}
public void objectFound()
{
string objectName = EventSystem.current.currentSelectedGameObject.name;
Debug.Log("you found the " + objectName);
for (int i = 0; i < choices.transform.childCount; i++)
{
if (objectName == choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().text)
{
// Debug.Log(i);
// choices.transform.GetChild(i).gameObject.SetActive(false);
// choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().color = Color.gray;
if(RndmList.Contains(i+1))
{
// Debug.Log(i);
// Debug.Log(v1.transform.GetChild(RndmList[i]-1).name);
choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().color = Color.gray;
v1.transform.GetChild(RndmList[i]-1).GetComponent<Button>().enabled = false;
foundCount++;
}
}
}
if(foundCount == v1.transform.childCount)
{
finishPanel.SetActive(true);
}
}
}
I've made a little app for printing Dymo labels and it works as intended, but when I run it on my Surface tablet it disables scalling af I've pushed the print button. If reopened the scalling is correct till print button is pushed again.
Can anybody tell me why?
The print button only runs my LabelPrinter class which has the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dymo;
using System.Windows.Forms;
namespace LabelPrinter
{
class LabelPrinter
{
private DymoAddInClass dymoAddIn;
private DymoLabelsClass dymoLabels;
private string labelPath, classRoom;
private int labelCount;
public LabelPrinter(string labelPath, string classRoom, int labelCount)
{
dymoAddIn = new DymoAddInClass();
dymoLabels = new DymoLabelsClass();
this.labelPath = labelPath;
this.classRoom = classRoom;
this.labelCount = labelCount;
}
public void printLabels()
{
dymoAddIn.SelectPrinter(dymoAddIn.GetDymoPrinters().Split('|')[0]);
if (dymoAddIn.Open(labelPath))
{
int firstNo = StaticMethods.getNextCodeEntryWithString(classRoom), lastNo = firstNo + labelCount;
dymoAddIn.StartPrintJob();
for (int i = firstNo; i < lastNo; i++)
{
foreach (string objName in dymoLabels.GetObjectNames(true).Split('|'))
{
dymoLabels.SetField(objName, classRoom + "_" + i);
}
dymoAddIn.Print(1, false);
}
dymoAddIn.EndPrintJob();
}
}
}
}
I need to extract the amplitudes of a wav file and want to do it as a simple commandline app. What's an easy way to do that? Cross-platform would be great. Needs to at least work on windows.
You can download an audio file for testing from soundcloud (or from https://dl.dropboxusercontent.com/u/313647/hosted/one_pos_to_neg_crossing.wav until soundcloud processes it).
Some libraries for potential use:
.NET
NAudio
Python
scipy.io.wavfile
pydub
PySoundFile
friture
snack
Here is a solution I came up with using Accord.NET:
public IEnumerable<float> AmplitudesFromFile(string fileName)
{
Accord.Audio.Formats.WaveDecoder sourceDecoder = new Accord.Audio.Formats.WaveDecoder(fileName);
Signal sourceSignal = sourceDecoder.Decode();
for (int i = 0; i < sourceSignal.Samples; i++) yield return sourceSignal.GetSample(1, i);
}
Project Page: http://accord-framework.net/
Documentation: http://accord-framework.net/docs/Index.html
Here's the first way I figured out how to do it with NAudio.
Output
>SoundToAmplitudes.exe w:\materials\audio\one_pos_to_neg_crossing.wav
0.04284668
-0.005615234
-0.1177368
Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave;
namespace SoundToAmplitudes {
class Program {
private static int Main(string[] args) {
#if DEBUG
Console.WriteLine(String.Join(", ", args));
args = new[] { #"W:\materials\audio\one_pos_to_neg_crossing.wav" };
#endif
return Cli(args);
}
static int Cli(string[] args) {
string fileName = args[0];
var soundFile = new FileInfo(fileName);
foreach (float s in AmplitudesFromFile(soundFile)) {
Console.WriteLine(s);
}
//Console.WriteLine();
#if DEBUG
Console.Read();
#endif
return 0;
}
public static IEnumerable<float> AmplitudesFromFile(FileInfo soundFile) {
var reader = new AudioFileReader(soundFile.FullName);
int count = 4096; // arbitrary
float[] buffer = new float[count];
int offset = 0;
int numRead = 0;
while ((numRead = reader.Read(buffer, offset, count)) > 0) {
foreach (float amp in buffer.Take(numRead)) {
yield return amp;
}
}
}
}
}
Here's one way to do it in python (using scipy), although I assume that the wav file is 16-bit.
Output
W:\>python amplitudes.py w:\materials\audio\one_pos_to_neg_crossing.wav
0.0428479873043
-0.00561540574358
-0.117740409558
Code
"""Usage:
amplitudes WAV_FILE
Returns the (linear) amplitude of the signal inside the wav file, sample by sample.
"""
from __future__ import division
import docopt
import scipy.io.wavfile
MAX_WAV16_AMP = 32767 # = 2**15-1 # because wav16 is signed (wav8 isn't)
def main():
args = docopt.docopt(__doc__)
one_pos_to_neg_crossing_path = r"W:\materials\audio\one_pos_to_neg_crossing.wav"
if False: args['WAV_FILE'] = one_pos_to_neg_crossing_path
rate, amp_arr = scipy.io.wavfile.read(args['WAV_FILE'])
for amp in (amp_arr / MAX_WAV16_AMP):
print amp
if __name__ == '__main__':
main()
See also
Reading *.wav files in Python
Extracting an amplitude list from *.wav file for use in Python
Using C# and Visual Studio 2010, how can I make a grayscaled emf from a colored one? Should I enumerate records and change color settings somehow?
As a result I want to have a new emf image.
Here is an open source emf read/write utility. I don't think it can be done with GDI+ and preserve the vector content. So this hopefully will help you, but I haven't remotely tested all possible emf cases.
https://wmf.codeplex.com/
this works for both wmf and emf formats. We can combine the Oxage.Wmf strategy with the strategy from the other post which essentially re-colors a pen or brush when it is created so that anything drawn with that pen or brush should be gray.
using Oxage.Wmf;
using Oxage.Wmf.Records;
using Oxage.Wmf.Objects;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MetafileTest
{
public class GrayMap
{
public void GrayFile(string sourceFile, string destFile){
WmfDocument wmf = new WmfDocument();
wmf.Load(sourceFile);
foreach (var record in wmf.Records)
{
if (record is WmfCreateBrushIndirectRecord)
{
var brush = record as WmfCreateBrushIndirectRecord;
brush.Color = Gray(brush.Color);
}
else if (record is WmfCreatePenIndirectRecord)
{
var pen = record as WmfCreatePenIndirectRecord;
pen.Color = Gray(pen.Color);
}
else if (record is WmfCreatePalette) {
var pal = record as WmfCreatePalette;
foreach (PaletteEntry entry in pal.Palette.PaletteEntries) {
Color test = Color.FromArgb(entry.Red, entry.Green, entry.Blue);
Color grayTest = Gray(test);
entry.Red = grayTest.R;
entry.Green = grayTest.G;
entry.Blue = grayTest.B;
}
}
}
wmf.Save(destFile);
}
public Color Gray(Color original) {
int r = (int)(original.R*0.2989);
int g = (int)(original.G*0.5870);
int b = (int)(original.B*0.1140);
Color result = Color.FromArgb(r, g, b);
return result;
}
}
}
This was tested with the following super simple case of filling a blue rectangle, so for at least the simple cases this will work. It may not be able to handle all cases, but in such a case you can probably extend the original source to suite your needs since it is open source.
private void button1_Click(object sender, EventArgs e)
{
var wmf = new WmfDocument();
wmf.Width = 1000;
wmf.Height = 1000;
wmf.Format.Unit = 288;
wmf.AddPolyFillMode(PolyFillMode.WINDING);
wmf.AddCreateBrushIndirect(Color.Blue, BrushStyle.BS_SOLID);
wmf.AddSelectObject(0);
wmf.AddCreatePenIndirect(Color.Black, PenStyle.PS_SOLID, 1);
wmf.AddSelectObject(1);
wmf.AddRectangle(100, 100, 800, 800);
wmf.AddDeleteObject(0);
wmf.AddDeleteObject(1);
wmf.Save("D:\\test.emf");
}
private void button2_Click(object sender, EventArgs e)
{
GrayMap map = new GrayMap();
map.GrayFile("D:\\test.emf", "D:\\test2.emf");
}
I have come across a situation where Tasks I am creating only seem to work when I am debugging the code.
As you will see below I keep getting a index out of range exception which should not be possible as the loop should never get to 5.
If I add a break point to the loop and then keep pressing F10 through to the end of the program all works as expected.
Any ideas what I am doing wrong?
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
List<int> nums = new List<int>();
var tasks = new Task[5];
for (int i = 0; i < numbers.Length; i++)
{
tasks[i] = Task.Factory.StartNew(() =>
{
nums.Add(numbers[i]);
},
TaskCreationOptions.None);
}
Task.WaitAll(tasks);
for (int i = 0; i < nums.Count; i++)
{
Console.WriteLine(nums[i]);
}
Console.ReadLine();
}
}
}
I would expect to see 1, 2, 3, 4, 5 but not in any particular order as running async
Strangely this does work but I don't see the difference other than the extra typing.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
List<int> nums = new List<int>();
var tasks = new Task[5]
{
Task.Factory.StartNew(() => {nums.Add(numbers[0]);}, TaskCreationOptions.None),
Task.Factory.StartNew(() => {nums.Add(numbers[1]);}, TaskCreationOptions.None),
Task.Factory.StartNew(() => {nums.Add(numbers[2]);}, TaskCreationOptions.None),
Task.Factory.StartNew(() => {nums.Add(numbers[3]);}, TaskCreationOptions.None),
Task.Factory.StartNew(() => {nums.Add(numbers[4]);}, TaskCreationOptions.None)
};
Task.WaitAll(tasks);
for (int i = 0; i < nums.Count; i++)
{
Console.WriteLine(nums[i]);
}
Console.ReadLine();
}
}
}
Thanks
Mike
Since the task happens outside the loop, which has finished by the time the task executes, i at this time is 5 (loop exit condition).
In your second example, you dont use i, but instead hard code the values so the problem disappears.
In the debugger, the timing is different and the tasks can execute before the loop completes: again, the problem disappears.
I think you could fix it like this:
var ii=i;
tasks[i] = Task.Factory.StartNew(() =>
{
nums.Add(numbers[ii]);
},
TaskCreationOptions.None);
UPDATE: In C#5 this is no longer a problem. A breaking change was made so that the loop variable of a foreach is logically inside the loop.
This is yet another example of one of the most common mistakes you can make in C# code: capturing the loop variable in an anonymous delegate. Eric Lippert has a good explanation of exactly what goes wrong.
Actually, your situation is even worse because in theory it could go right or wrong randomly. Suppose you made the following change:
for (int i = 0; i < numbers.Length; i++)
{
tasks[i] = ...;
tasks[i].Wait();
}
then your program suddenly works as expected. The reason is, that your task takes the value of i (the loop variable) when the task executes, not the value of i when that task was created. So the following sequence is possible in your original program:
i = 0
Create task 0
i = 1
Run task 0: This task sees i = 1
Create task 1
i = 2
Create task 2
i = 3
Create task 3
i = 4
Create task 4
i = 5
Run task 1: This task sees i = 5 and throws an exception
Tom's answer fixes this problem by introducing a new variable ii inside the loop. When each task is created, it captures this variable, which has a fixed value for each iteration.
The problem here is that the Task access the i variable, and by the time the task is running, this variable will be changed, and the reason you are getting the exception in the last iteration the variable i will be 5, and although the loop will stop after this increment, but the task body still reference it, so this line will throw
nums.Add(numbers[i]);
because obviously 5 is out of range value.
To fix this you need to pass i to the StartNew method as a state parameter
tasks[i] = Task.Factory.StartNew((obj) =>
{
int index = (int) obj;
nums.Add(numbers[index]);
},
i);