|
As a suggestion, firstly don't use 5 seperate integers, use an array with 5 elements.
You could do the test a couple of ways. Firstly, sort the array, then check that two neighbouring elements do not contain the same number. That guarantees uniqueness.
Or, create five booleans, all false originally, then set the appopriate one on each iteration of a loop over the array. All booleans being set would signal uniqueness too.
Regards,
Rob Philpott.
|
|
|
|
|
Changed my code, can I make it easier? TO read and write? I don't know much about C#...
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
<br />
namespace W1<br />
{<br />
public partial class w1 : Form<br />
{<br />
public w1()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
private void w1_Load(object sender, EventArgs e)<br />
{<br />
<br />
}<br />
<br />
private void exitToolStripMenuItem_Click(object sender, EventArgs e)<br />
{<br />
Application.Exit();<br />
}<br />
<br />
private void btnDrop_Click(object sender, EventArgs e)<br />
{<br />
Random random = new Random();<br />
<br />
int randomNumber1C = random.Next(1, 7);<br />
int randomNumber2C = random.Next(1, 7);<br />
int randomNumber3C = random.Next(1, 7);<br />
int randomNumber4C = random.Next(1, 7);<br />
int randomNumber5C = random.Next(1, 7);<br />
<br />
int randomNumber1 = random.Next(1, 7);<br />
int randomNumber2 = random.Next(1, 7);<br />
int randomNumber3 = random.Next(1, 7);<br />
int randomNumber4 = random.Next(1, 7);<br />
int randomNumber5 = random.Next(1, 7);<br />
<br />
List<int> randomList = new List<int>();<br />
randomList.Add(randomNumber1);<br />
randomList.Add(randomNumber2);<br />
randomList.Add(randomNumber3);<br />
randomList.Add(randomNumber4);<br />
randomList.Add(randomNumber5);<br />
randomList.Sort();<br />
<br />
int last = 0;<br />
foreach (int itemInRandomList in randomList)<br />
{<br />
if (itemInRandomList == last || itemInRandomList - last != 1)<br />
{<br />
last = 0;<br />
break;<br />
}<br />
last = itemInRandomList;<br />
}<br />
<br />
if (last == 0)<br />
{<br />
lblDropped.ForeColor = System.Drawing.Color.Red;<br />
lblDropped.Text = "LOST";<br />
}<br />
else<br />
{<br />
lblDropped.ForeColor = System.Drawing.Color.Green;<br />
lblDropped.Text = "WON";<br />
}<br />
<br />
List<int> randomListC = new List<int>();<br />
randomListC.Add(randomNumber1C);<br />
randomListC.Add(randomNumber2C);<br />
randomListC.Add(randomNumber3C);<br />
randomListC.Add(randomNumber4C);<br />
randomListC.Add(randomNumber5C);<br />
randomListC.Sort();<br />
<br />
int lastC = 0;<br />
foreach (int itemInRandomList in randomListC)<br />
{<br />
if (itemInRandomList == lastC || itemInRandomList - lastC != 1)<br />
{<br />
lastC = 0;<br />
break;<br />
}<br />
lastC = itemInRandomList;<br />
}<br />
<br />
if (lastC == 0)<br />
{<br />
lblDroppedC.ForeColor = System.Drawing.Color.Red;<br />
lblDroppedC.Text = "LOST";<br />
}<br />
else<br />
{<br />
lblDroppedC.ForeColor = System.Drawing.Color.Green;<br />
lblDroppedC.Text = "WON";<br />
}<br />
<br />
lblDice1C.Text = "Dropped: " + randomNumber1C.ToString();<br />
lblDice2C.Text = "Dropped: " + randomNumber2C.ToString();<br />
lblDice3C.Text = "Dropped: " + randomNumber3C.ToString();<br />
lblDice4C.Text = "Dropped: " + randomNumber4C.ToString();<br />
lblDice5C.Text = "Dropped: " + randomNumber5C.ToString();<br />
<br />
lblDice1.Text = "Dropped: " + randomNumber1.ToString();<br />
lblDice2.Text = "Dropped: " + randomNumber2.ToString();<br />
lblDice3.Text = "Dropped: " + randomNumber3.ToString();<br />
lblDice4.Text = "Dropped: " + randomNumber4.ToString();<br />
lblDice5.Text = "Dropped: " + randomNumber5.ToString();<br />
<br />
lblDice1.Visible = true;<br />
lblDice2.Visible = true;<br />
lblDice3.Visible = true;<br />
lblDice4.Visible = true;<br />
lblDice5.Visible = true;<br />
lblDice1C.Visible = true;<br />
lblDice2C.Visible = true;<br />
lblDice3C.Visible = true;<br />
lblDice4C.Visible = true;<br />
lblDice5C.Visible = true;<br />
lblDropped.Visible = true;<br />
lblDroppedC.Visible = true;<br />
w1.ActiveForm.Size = new Size(316, 256);<br />
}<br />
<br />
private void rulesToolStripMenuItem_Click(object sender, EventArgs e)<br />
{<br />
MessageBox.Show("Rules");<br />
}<br />
}<br />
}<br />
<br />
|
|
|
|
|
First as suggested previously you need a 5 long array of ints. The method described above (checking for uniqueness) won't work as (2,3,4,5,6) is unique but not winning.
You will also find your code improves vastly if you study OO and work out how to refactor. The two guidlines I try to live by [and often fail] are "Don't Repeat Yourself" "Keep It Simple Stupid" (DRY-KISS).
For example
int randomNumber1 = random.Next(1, 7);<br />
int randomNumber2 = random.Next(1, 7);<br />
int randomNumber3 = random.Next(1, 7);<br />
int randomNumber4 = random.Next(1, 7);<br />
int randomNumber5 = random.Next(1, 7);
Fails the DRY test.
I've separated out an example Yahtzee Roller class for you, this is one implementation of many possible and is not meant to be perfect. It contains an array of roll results, abd an array of winning values, these are compared. The amin program creates an instance of the roller, and proceeds to roll until a winning set is found.
The roller:
public class YahtzeeRoller
{
int[] rollResults = new int[]{0, 0, 0, 0, 0};
int[] WinningValues = new int[] { 1, 2, 3, 4, 5 };
Random random = new Random();
public bool IsWinning
{
get
{
return WinningValues.Except(rollResults).Count() == 0;
}
}
public string CurrentRoll
{
get
{
string result = "Roll =";
foreach (int rollResult in rollResults)
result += " " + rollResult;
return result;
}
}
public void Roll()
{
for (int i = 0; i < rollResults.Length; i++)
rollResults[i] = random.Next(1, 7);
}
}
Calling Program:
class Program
{
static void Main(string[] args)
{
YahtzeeRoller yahtzeeRoller = new YahtzeeRoller();
while (!yahtzeeRoller.IsWinning)
{
yahtzeeRoller.Roll();
Console.Write(yahtzeeRoller.CurrentRoll);
if (yahtzeeRoller.IsWinning)
Console.WriteLine(" Yahtzee!");
else
Console.WriteLine(" Not Yahtzee !");
}
Console.ReadKey();
}
}
Notice a few things. The re-factored code is shorter (and hopefully clearer) than the original. Additionally, it separates out the rolling functionality from the display [mostly] the use of CurrentRole property is debatable. You can now call Roll as many times as needed (it is not hard-coded), and if desired, easily change what constitutes a winning set of rolls.
I hope this helps!
CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks
|
|
|
|
|
Hi,
- you have a single random generator, that is good.
- you don't need five or ten Labels, its much easier to show all five numbers in a single Label.
- you want to recognize not only "12345" but also all permutations thereof. Sorting the numbers is one way, but is unnecessary. The smart way to do this is by using a bit collection. Have a look at this:
int all=0;
all = all | (1<<number);
...
if (all==goodValue) output("WON");
Now try to use that to your advantage; the end result will be less than half the code you have now.
BTW: please use PRE tags, not CODE tags.
|
|
|
|
|
But this isn't working at any computer?
|
|
|
|
|
dennis_max27 wrote: this
what?
dennis_max27 wrote: isn't working
what gives?
dennis_max27 wrote: any computer?
huh?
if you can't communicate what it is you want or try or experience, nobody can help you.
|
|
|
|
|
Ha! Use a System.Collections.Generic.HashSet!
Are you only looking for a large straight?
Clear the HashSet.
Generate the numbers (into an array).
Iterate the array, putting each value in the HashSet. (Or you could eliminate the array if you're only interested in large straights.)
Check how many values are in the HashSet -- if there are five and 1 or 6 is not present, then you have a large straight!
To also check for small straights, you can track the min and max values.
Large straight := Five values, max - min == 4
Small straight := Four values, max - min == 3
Also, if the HashSet contains only one value, you have a Yahtzee.
If only two, you might have a full house.
|
|
|
|
|
I have this problem with a SQL query that is executed through a c# program.
it occurs in the following query:
string strSql = "SELECT 2010Forms.Task_ID_PK, 2010Forms.isMainOutcome, 2010Forms.Outcome, ";
strSql += " 2010Outcome.InProg, 2010Forms.OutcomeType, ";
strSql += " 2010Outcome.TraineeStatus, 2010Outcome.ReviewerStatus ";
strSql += " FROM 2010Outcome INNER JOIN 2010Forms ON ";
strSql += " 2010Outcome.2010Outcome_ID_FK = 2010Forms.Task_ID_PK";
strSql += " where (2010Outcome.Review_ID_FK = '" + StringRevID "')";
strSql += " and (2010Outcome.Trainee_ID_FK = '" + StringTraineeID + "')";
strSql += " and (2010Forms.isFinal = 0)";
strSql += " order by 2010Forms.OutcomeOrder asc ";
I cannot see any missing operators, but is anyone can assist me, I will be very greatful.
|
|
|
|
|
Two things:
1) Shouldn't you have a space between ".Outcome" and "Order"?
AlucardCode wrote: strSql += " order by 2010Forms.OutcomeOrder asc ";
2) You realy should use Parameters.AddWithValue rather than encoding IDs directly. Even if they are numeric it is good practice - avoid SQl injection problems!
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Maybe using table names starting with a number can be a problem: identifiers starting with a number has a special meaning in SQL server (documentation here[^]).
Try enclosing the identifiers inside square brakets (eg [2010Outcome].InProg ). I'm not sure this is your problem through.
|
|
|
|
|
Cool Thanx, I will give it a try
|
|
|
|
|
missing plus sign
strSql += " where (2010Outcome.Review_ID_FK = '" + StringRevID "')";
strSql += " where (2010Outcome.Review_ID_FK = '" + StringRevID + "')";
|
|
|
|
|
when can we use begin invoke
suresh
|
|
|
|
|
|
sureshhi wrote: when can we use begin invoke
Any time you like!
|
|
|
|
|
Hi
I have a program that needs Microsoft Sql Server for my database .
Now i want to create an installer for my program with Installshield Software and i want to know what should i add to my installer for using sql database?
|
|
|
|
|
SQL Server, SQL Express or MSDE? I assume that you're referring to the free version, SQL Express. There's no merge-module anymore, but an executable that you can run from your own setup;MSDN states: SQL Server Express no longer uses merge modules to embed the SQL Server Express Database Engine. Instead, it contains a single-setup executable that can be bundled with your custom application. This executable accepts command-line parameters for a smooth installation experience.
More information can be obtained here[^].
Good luck
I are Troll
|
|
|
|
|
The best thing is to have them separately. Your setup will be fairly large if you can try to add it to your setup. Install SQL separately.
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
Just put a statement in the documentation that they need to have some version of SQL Server available somewhere on there network. That's the best you can do. Likewise for .net or any other product that you didn't write.
|
|
|
|
|
If we have many unused namespaces in a class, does it affect performance in any way?
|
|
|
|
|
In visual studio you can:
Main Menu - On the Edit menu, point to IntelliSense, point to Organize Usings, and then click Remove Unused Usings.
Context Menu - Right-click anywhere inside the code editor, point to Organize Usings, and then click Remove Unused Usings.
My feeling is that, it will be removed during compilation time (JIT) so the performance will be impacted during the load of the file, evaluation of it. But not during the runtime.
|
|
|
|
|
|
jdkulkarni wrote: if you add extra unused references then yes
They may affect compile time, but not run time.
|
|
|
|
|
Only during the compile phase when the compiler builds its internal reference tables. The final object code should not be affected.
|
|
|
|
|
That question makes no sense.
0) Namespaces aren't in classes.
1) If it's unused, it's not in the class.
|
|
|
|
|