In my project that i'm working on I have to match 5 numbers for yahtzee. So all these numbers have to be the same. Now I have thought about how to do this but i'm not sure about what the best and easiest way is. Sure I can write it all out but there has to be a shorter way.
I haven't written the code for the part that checks if yahtzee has been thrown. This is because I only can come up with one way and that is to write it all out.
Here's my code so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Opdr3
{
class Program
{
struct YahtzeeGame
{
public int[] dobbelstenen;
public Random rnd;
public void Gooi()
{
for (int i = 0; i < 5; i++)
{
dobbelstenen[i] = Int32.Parse(rnd + "");
}
}
public bool Yahtzee()
{
Here it has to check if all dobbelstenen[int]
are the same
}
}
static void Main(string[] args)
{
// maak YahtzeeGame (struct) aan
YahtzeeGame yahtzeeGame;
// initialiseer struct-members
yahtzeeGame.rnd = new Random();
yahtzeeGame.dobbelstenen = new int[5];
// probeer yahtzee te gooien
int aantalPogingen = 0;
do
{
// gooi alle dobbelstenen
yahtzeeGame.Gooi();
aantalPogingen++;
} while (!yahtzeeGame.Yahtzee());
// vermeld aantal pogingen voor yahtzee
Console.WriteLine("Aantal pogingen nodig: {0}", aantalPogingen);
// wacht op gebruiker
Console.ReadKey();
}
}
}
You'll need a little loop:
public bool Yahtzee()
{
// check if all dobbelstenen[int] are the same
for(int i = 1; i < 5; i++) // start with second dobbelstenen
{
if(dobbelstenen[i] != dobbelstenen[0]) return false;
}
return true;
}
It simply compares second, third, ... against the first.
Related
I was trying to make a lucky dip program where 6 random numbers,1-59, are chosen then printed out in an array. I managed to get this to work, however you needed to use an IndexOf method so that no same number was printed twice by checking if the new number is already in the array.
using System;
namespace LuckyDip
{
class Program
{
static void Main(string[] args)
{
int[] luckyNumbers = new int[6];
Random random = new Random();
for (int x = 0; x<6; x++)
{
num[x] = random.Next(1,59);
Console.WriteLine(num[x]);
}
Console.ReadLine();
}
}
}
It prints out numbers, but sometimes they are the same.
You state that you want to use IndexOf, but that method is used for strings (see docs). Your example has an int array so the solution below uses Contains. This solution adds a check within your loop and generates a new number if this number already exists within your array. If you really need to use IndexOf, create a string array and convert the numbers using String.valueOf(randomNumber)
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] luckyNumbers = new int[6];
Random random = new Random();
for (int x = 0; x<6; x++)
{
int randomNumber = random.Next(1,59);
while (luckyNumbers.Contains(randomNumber))
{
randomNumber = random.Next(1,59);
}
luckyNumbers[x] = randomNumber;
Console.WriteLine(luckyNumbers[x]);
}
}
}
Another possible solution would be:
using System;
using System.Collections;
using System.Linq;
public class Program
{
public static void Main()
{
int arraySize = 6;
int[] luckyNumbers = Enumerable.Range(1,59).OrderBy(g => Guid.NewGuid()).Take(arraySize).ToArray();
for (int x = 0; x < luckyNumbers.Length; x++)
{
Console.WriteLine(luckyNumbers[x]);
}
}
}
I need to design a program that reads in an ASCII text file and creates an output file that contains each unique ASCII character and the number of times it appears in the file. Each unique character in the file must be represented by a character frequency class instance. The character frequency objects must be stored in an array list. My code is below:
using System.IO;
using System;
using System.Collections;
namespace ASCII
{
class CharacterFrequency
{
char ch;
int frequency;
public char getCharacter()
{
return ch;
}
public void setCharacter(char ch)
{
this.ch = ch;
}
public int getfrequency()
{
return frequency;
}
public void setfrequency(int frequency)
{
this.frequency = frequency;
}
static void Main()
{
string OutputFileName;
string InputFileName;
Console.WriteLine("Enter the file path");
InputFileName = Console.ReadLine();
Console.WriteLine("Enter the outputfile name");
OutputFileName = Console.ReadLine();
StreamWriter streamWriter = new StreamWriter(OutputFileName);
string data = File.ReadAllText(InputFileName);
ArrayList al = new ArrayList();
//create two for loops to traverse through the arraylist and compare
for (int i = 0; i < data.Length; i++)
{
int k = 0;
int f = 0;
for (int j = 0; j < data.Length; j++)
{
if (data[i].Equals(data[j]))
{
f++;
if (i > j) { k++; }
}
}
al.Add(data[i] + "(" + (int)data[i] + ")" + f + " ");
foreach (var item in al)
{
streamWriter.WriteLine(item);
}
}
streamWriter.Close();
}
}
}
When I run the program, the program does not stop running and the output file keeps getting larger until it eventually runs out memory and I get an error stating that. I am not seeing where the error is or why the loop won't terminate. It should just count the characters but it seems to keep looping and repeating counting the characters. Any help?
Try this approach :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace yourNamespace
{
class Char_frrequency
{
Dictionary<Char, int> countMap = new Dictionary<char, int>();
public String getStringWithUniqueCharacters(String input)
{
List<Char> uniqueList = new List<Char>();
foreach (Char x in input)
{
if (countMap.ContainsKey(x))
{
countMap[x]++;
}
else
{
countMap.Add(x, 1);
}
if (!uniqueList.Contains(x))
{
uniqueList.Add(x);
}
}
Char[] uniqueArray = uniqueList.ToArray();
return new String(uniqueArray);
}
public int getFrequency(Char x)
{
return countMap[x];
}
}
}
This might not be the ideal solution. But you can use these methods
This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 4 years ago.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DieRoller
{
public class Program
{
public static void Main()
{
for (int a = 0; a < 20; a = a + 1)
{
Console.WriteLine(RollDie());
}
Console.ReadLine();
}
public static int RollDie()
{
Random roll = new Random();
int test = roll.Next(1, 6 + 1);
return test;
}
}
}
When I execute this code I get the number 4 multiple times or the number 2 multiple times...etc.
Isn't it supposed to execute the RollDie function for each iteration of the loop? and isn't that supposed to yield a different value each time? pls halp!
EDIT: The thing is guys, I need to generate the randomness only inside the RollDie method, and I can't have any arguments for the RollDie method (Basically I have to generate the randomness only using the random method inside the RollDie method), other questions don't address that.
See comments for explanation of why it doesn't work. Here a possible way to make it work:
public static void Main()
{
Random roll = new Random();
for (int a = 0; a < 20; a = a + 1)
{
Console.WriteLine(RollDie(roll));
}
Console.ReadLine();
}
public static int RollDie(Random roll)
{
int test = roll.Next(1, 6 + 1);
return test;
}
Or, for simplicity, just:
public static void Main()
{
Random roll = new Random();
for (int a = 0; a < 20; a = a + 1)
{
Console.WriteLine(roll.Next(1, 6 + 1));
}
Console.ReadLine();
}
I want to create console app which read userinput value and create multiple instances of one class and then run void say() in all class instances.
Code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int botsnumber = 0;
Console.WriteLine("Type number: ");
botsnumber = Int32.Parse(Console.ReadLine());
for (int i = 0; i < botsnumber; i++)
{
//Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot();
// Bot b = new Bot();
}
b.say();// b1.say(); b2.say(); b3.say(); ...
}
}
class Bot
{
public static void say()
{
Console.WriteLine("Hello!");
}
}
}
Put the instances into a list, fill it, after that, loop the list and call say
botsnumber = Int32.Parse(Console.ReadLine());
var list = new List<Bot>();
for (int i = 0; i < botsnumber; i++)
{
Bot b = new Bot();
list.Add(b);
}
list.ForEach(b => b.say());
And you don't need to put static in your method say
class Bot
{
public void say()
{
Console.WriteLine("Hello!");
}
}
Like this i guess
public static List<Bot> bots = new List<Bot>();
...
for (int i = 0; i < botsnumber; i++)
{
bots.Add(new Bot);
}
...
bots[0].Say();
bots[1].Say();
I think you want to call say() inside the loop, and make it a non-static method:
for (int i = 0; i < botsnumber; i++)
{
Bot b = new Bot();
b.Say();
}
class Bot
{
public void Say()
{
Console.WriteLine("Hello!");
}
}
Although note that in many ways there's no point in creating a stateless object instance that you immediately discard, so ... in real usage I would either expect some state, or I'd expect it to be a static method.
Alternatively, you could create all the bots first, accumulate them in a list, then loop over that - again, whether this is sensible depends on context, but:
var bots = new List<Bot>(botsnumber);
for (int i = 0; i < botsnumber; i++)
{
bots.Add(new Bot());
}
// ...
foreach(var bot in bots)
{
bot.Say();
}
You could either immediately call say, or call them all after creating all the bots
for (int i = 0; i < botsnumber; i++)
{
//Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot();
Bot b = new Bot();
b.say();
}
// or
var bots = new List<Bot>();
for (int i = 0; i < botsnumber; i++)
{
//Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot();
bots.Add(new Bot());
}
foreach (var bot in bots)
{
bot.say();
}
The code is to generate random numbers in 100 files numbered from 0..99.
What I couldn't get was why this code ended up creating a file called 100.txt and I even got an exception saying that 100.txt was being written by another process.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RandomNumbersFileGenerator
{
class Program
{
static Random Random = new Random();
static void Main(string[] args)
{
List<Task> tasks = new List<Task>();
for(int fileNumber = 0; fileNumber < 100; ++fileNumber)
{
tasks.Add(Task.Run(()=>GenerateFileWithRandomNumbers(Path.Combine($"c:\\FilesWithRandomNumbers\\{fileNumber}.txt"), 10000000)));
}
Task.WaitAll(tasks.ToArray());
}
static void GenerateFileWithRandomNumbers(string path, int numberOfNumbers)
{
List<string> listOfNumbers = new List<string>();
for(;numberOfNumbers > 0; --numberOfNumbers)
{
listOfNumbers.Add(Random.Next().ToString());
}
File.WriteAllLines(path, listOfNumbers);
}
}
}
It is related to closures and captured variables. Change
for(int fileNumber = 0; fileNumber < 100; ++fileNumber)
{
tasks.Add(Task.Run(()=>GenerateFileWithRandomNumbers(Path.Combine($"c:\\FilesWithRandomNumbers\\{fileNumber}.txt"), 10000000)));
}
To
for(int fileNumber = 0; fileNumber < 100; ++fileNumber)
{
int tmp = fileNumber;
tasks.Add(Task.Run(()=>GenerateFileWithRandomNumbers(Path.Combine($"c:\\FilesWithRandomNumbers\\{tmp}.txt"), 10000000)));
}
See also http://csharpindepth.com/articles/chapter5/closures.aspx