-
February 22nd, 2012, 04:58 PM
#1
High card project
Well I'm back again to pick brains! We've written this code in class and i was wondering if im stuck in an endless loop somewhere. i cant get the application to load, when it does i just find it sitting in my task manager doing nothing but taking up CPU %. can some one help me here? its also due tonight by 11:59 pm i hate my timing its crummy i say!
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace HighCard
{
public partial class FrmHighCard : Form
{
//fields
private int rankPlayer; //2-14 w/ A high
private int suitPlayer; //1-4 c,d,h,s
private Random r;
public FrmHighCard()
{
InitializeComponent();
rankPlayer = 0;
suitPlayer = 0;
r = new Random();
}
private void btnDealCards_Click(object sender, EventArgs e)
{
// declarations
string rankStr, suitStr;
string card1 = "", card2 = "";
int i = 0;
int player1Rank = 0;
bool cardsDelt = false;
StreamWriter writer;
while (!cardsDelt)
{
// input
rankPlayer = r.Next(2, 4); // generates value of 2-14 since max is exclusive***
suitPlayer = r.Next(1, 2);
// process
switch (rankPlayer)
{
case 11:
rankStr = "Jack";
break;
case 12:
rankStr = "Queen";
break;
case 13:
rankStr = "King";
break;
case 14:
rankStr = "Ace";
break;
default:
rankStr = rankPlayer.ToString();
break;
}
switch (suitPlayer)
{
case 1:
suitStr = "Clubs";
break;
case 2:
suitStr = "Diamonds";
break;
case 3:
suitStr = "Hearts";
break;
default:
suitStr = "Spades";
break;
}//end of switch suit
if (i == 0) //first iteration
{
i++;
player1Rank = rankPlayer;
card1 = rankStr + " of " + suitStr;
}
else// second itteration
{
card2 = rankStr + " of " + suitStr;
if (card1 != card2)
cardsDelt = true;
else if (card1 == card2)
i = 0;
}
}//end of for loop
//output
lstplayer1.Items.Add(card1);
lstplayer2.Items.Add(card2);
lstplayer1.SelectedIndex = lstplayer1.Items.Count - 1;
lstplayer2.SelectedIndex = lstplayer2.Items.Count - 1;
if (player1Rank > rankPlayer)
MessageBox.Show(cboxPlayer1.SelectedItem + " Wins High Card");
else if (rankPlayer > player1Rank)
MessageBox.Show(cboxPlayer1.SelectedItem + " Wins High Card");
else
//MessageBox.Show(cboxPlayer1.SelectedItem + " and " + cboxPlayer2.SelectedItem + " Tie Highcard");
try
{
writer = new StreamWriter("carddeck.dat", true);
writer.WriteLine(card1);
writer.WriteLine(card2);
writer.Close();
}
catch
{
MessageBox.Show("file output error");
}
}
private void FrmHighCard_Load(object sender, EventArgs e)
{
StreamReader reader;
string player;
try
{
reader = new StreamReader("players.dat");
player = reader.ReadLine();
while (player != null)
{
cboxPlayer1.Items.Add(player);
}
reader.Close();
}
catch
{
MessageBox.Show("File Read Error");
}
}
private void cboxPlayer1_SelectedIndexChanged(object sender, EventArgs e)
{
StreamReader reader;
string player;
cboxPlayer2.Items.Clear();
try
{
reader = new StreamReader("players.dat");
player = reader.ReadLine();
while (player != null)
{
if (cboxPlayer1.SelectedItem.ToString() != player)
cboxPlayer2.Items.Add(player);
player = reader.ReadLine();
}
reader.Close();
}
catch
{
MessageBox.Show("File Read Error");
}
}
}
}
-
February 22nd, 2012, 05:36 PM
#2
Re: High card project
By not loading I am guessing you mean the window does not pop up showing you any of the UI? Have you tried simply creating a new project and importing the code into it? It seems like it is not a big project so doing it would take no more than a few minutes. This has not happened to me so I am not sure how to fix it the right way but I am sure someone will tell you the right way of doing it.
As for the code itself nothing seems to be running in an infinite loop
-
February 22nd, 2012, 05:55 PM
#3
Re: High card project
the thing you may not expect is that each time you add an item to cboxPlayer1 it triggers the SelectedIndexChanged which creates a reader, opens another copy of the data, reads all of the lines and adds them to cboxPlayer2. This may take a considerable amount of time if the list is large.
You can put a break in SelectedIndexChanged to confirm this is happening. I usually use a global flag that indicates if initial data is loading. If it is then all other events (that could trigger) will ignore a call.
-
February 23rd, 2012, 02:26 PM
#4
Re: High card project
thank you ubiquitous, i tried that and I'm still getting an issue of it not loading. its really odd to be honest its still there sitting away in my task manager. and tha'ts correct the GUI fails to load for some odd reason, theres no error being displayed. ill try it once more and see if there's something going wrong when i copy the code over.
thanks as well mur16, i looked on MSDN added one and implemented it and still to no avail the GUI dosn't load :/ this is evil!
-
February 23rd, 2012, 02:43 PM
#5
Re: High card project
When i change the file name from players to player i get the file read error and the GUI loads. the files only like 10 lines long of students names in the class thats it.
-
February 23rd, 2012, 03:07 PM
#6
Re: High card project
Okay after about 3 hours of poking around i found the problem out
Code:
private void FrmHighCard_Load(object sender, EventArgs e)
{
StreamReader reader;
string player;
try
{
reader = new StreamReader("Names.dat");
player = reader.ReadLine();
while (player != null)
{
cboxPlayer1.Items.Add(player);
player = reader.ReadLine(); //*******MISSING THIS READLINE COMMAND******
}
reader.Close();
}
catch
{
MessageBox.Show("File Read Error");
}
}
-
February 23rd, 2012, 03:57 PM
#7
Re: High card project
Originally Posted by Doc618
Okay after about 3 hours of poking around i found the problem out
Code:
private void FrmHighCard_Load(object sender, EventArgs e)
{
StreamReader reader;
string player;
try
{
reader = new StreamReader("Names.dat");
player = reader.ReadLine();
while (player != null)
{
cboxPlayer1.Items.Add(player);
player = reader.ReadLine(); //*******MISSING THIS READLINE COMMAND******
}
reader.Close();
}
catch
{
MessageBox.Show("File Read Error");
}
}
Sorry for missing that I skimmed through the code and everything looked correct.
From my understanding you have now resolved your issue but you do not need string player as that is really not doing anything.
You could shorten your code and have the same functions with
Code:
// Create reader
StreamReader reader = new StreamReader("Names.dat");
// Loop until end of file is reached
while (reader.Peek() > -1)
{
// Add player names to combo box
cboxPlayer1.Items.Add(reader.ReadLine());
}
// Close reader
reader.Close();
What Mur16 was saying in his post is that according to your code every time you choose a new player in combo box 1 it opens up the Names.dat file again and reads of the names to add to cboxPlayer2. In a small program like this there is no real performance issue since data is miniscule. You mentioned it was a class assignment so from my guess is that the instructor is drilling the code into you guys by making you repeatedly type the same code over and over but in reality all the data for your application can be loaded at the start of the application.
Since both combo boxes effectively have the same names and none of them change you could simply add the names to both combo boxes in one go like this.
Code:
// Create reader
StreamReader reader = new StreamReader("Names.dat");
string playerName = "";
// Loop until end of file is reached
while (reader.Peek() > -1)
{
// Read line and save it to string so it can be added to players combo box
playerName = reader.ReadLine();
// Add player names to combo box
cboxPlayer1.Items.Add(playerName);
cboxPlayer2.Items.Add(playerName);
}
// Close reader
reader.Close();
This way it adds the names to both combo boxes and you don't have to keep opening the file as you are now with
Code:
private void cboxPlayer1_SelectedIndexChanged(object sender, EventArgs e)
{
StreamReader reader;
string player;
cboxPlayer2.Items.Clear();
try
{
reader = new StreamReader("players.dat");
player = reader.ReadLine();
while (player != null)
{
if (cboxPlayer1.SelectedItem.ToString() != player)
cboxPlayer2.Items.Add(player);
player = reader.ReadLine();
}
reader.Close();
}
catch
{
MessageBox.Show("File Read Error");
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|