trouble with team ID's in list<>'s
I'm having trouble with a particular segment of my program. The following code sends the variable p to the AddPlayer method of the Teams class (which is part of the league class). The AddPlayer method calculates whether player p's salaryCapFigure exceeds the team's salary cap; if it does, the method returns false, and the player is supposed to be sent to the team "free agency" (FA), which is what the code inside the brackets is supposed to accomplish. If the method returns true, the player is simply added to that team's list, and the code inside the brackets is skipped.
if (!league.Teams[league.Teams.Count - 1].AddPlayer(p)) { Team FA = league.AllTeams[2]; FA.AddPlayer(p); }For testing purposes, there are 4 teams: two regular teams (1, 2) and two free agency teams (3,4).
My problem is that this code only works when the statement league.AllTeams[2]; has a 0 or 1 in the brackets (representing teams 0 and 1, which are the two regular teams). When it is set to 2 or 3, I get an error in the console: "Index out of range." And the error points to this line: Team FA = league.AllTeams[2];. Now, I've tried different things to get team 2 (0 index) to be assigned to FA:
Team FA = league.AllTeams.Find(t => t.Nickname == "FreeAgency"); Team FA = league.Teams[2];
None have solved it. The code is in a nested If/Else design in a LoadFile method.
I've tested the code with league.AllTeams[0] and it works just fine. I set the salary cap to 32 so that the accumulation of team 1's players' cap figures exceed 32 and team 0's is far below. Using 0, code successfully sent a player whose salaryCapFigure exceeded team 1's salary cap to team 0.
The file contains 4 teams, 9 players: 2 on team 0, 6 on team 1, 1 on team 2 (free agency), and 0 on team 3 (test team)
Here is the AddPlayer method that's in the Team class:
public bool AddPlayer(Player player) { if (SalaryCap - Payroll >= player.salaryCapFigure) { Players.Add(player); Payroll += player.salaryCapFigure; return true; } return false; }And here is my complete LoadFile method code:
public static void Load(League league, string filePath) //load default rosters { using (StreamReader rd = new StreamReader(File.OpenRead(filePath))) { while (!rd.EndOfStream) { string line = rd.ReadLine(); if (!line.StartsWith("*")) { if (line.StartsWith("$")) { string[] team = line.Split(new char[] { ' ' }); Team tm = new Team(); league.Teams.Add(tm); league.Teams[league.Teams.Count - 1].Name = line.Substring(line.IndexOf("$") + 1, (line.LastIndexOf(" ") - line.IndexOf(" ")) - 3); tm.Conference = Convert.ToInt32(team[2]); tm.Division = Convert.ToInt32(team[3]); tm.SalaryCap = 32.00; //Salary cap } else { string[] player = line.Split(new char[] { ' ' }); Player p = new Player(); //Hidden 1-10 p.contractBase = Convert.ToInt32(player[11]); p.contractBonus = Convert.ToInt32(player[12]); p.contractLength = Convert.ToInt32(player[13]); p.yearOfContract = Convert.ToInt32(player[14]); p.salaryCapFigure = Convert.ToDouble(player[15]); p.CalculateContract(); if (!league.Teams[league.Teams.Count - 1].AddPlayer(p)) { Team FA = league.AllTeams[2]; FA.AddPlayer(p); } } } } rd.Close(); }
답변
- Bump
EDIT: I think I know what's wrong. Team 1 players that don't fit under its salary cap tries to send those players to team 2 before team 2 is actually created.- 편집됨dhalberger 2009년 11월 9일 월요일 오전 12:35.
- 답변으로 표시됨dhalberger 2009년 11월 9일 월요일 오전 12:57
모든 응답
- Without seeing your input file it's hard to tell. I'd suspect you aren't getting more than 2 teams parsed in when you try to use the index for the 3rd. You also do odd things in your team adding code, why not use the object directly (tm) since you have it, rather than index into the list to get the object to access the properties of it?
Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit Regarding your tm question, do you mean why I simply didn't code
if (!tm.AddPlayer(p)) { //code to send player to free agency here }According to Visual Studio, tm doesn't exist in that context.
Here's my input file: (Note: team line layout = name nickname conference division)
*firstname lastname height weight position age yearsPro draftRd draftPk personality morale contract(base) (bonus) (length) (year of) capfigure ...
$Team01 Sharks 1 1
Timothy Stevens 602 202 QB 24 4 1 3 None 98 30 10 5 1 1 28 64 62 90 64 64 62 64 77 62 55 90 90 18 18 92 79 92
Timbo McCord 602 202 CB 24 4 1 3 None 98 42 18 4 4 1 28 94 62 68 88 91 84 64 98 67 60 22 21 18 18 92 79 92
$Team02 Devils 1 1
Johnny McCall 602 202 FS 24 4 1 3 None 98 31 21 5 1 1 28 88 62 70 85 90 76 64 89 62 60 22 21 18 18 92 79 92
Lee Applewhite 602 202 DT 24 4 1 3 None 98 10 2 4 1 1 28 62 96 68 66 77 62 64 66 67 72 22 21 18 18 92 79 92
James Madison 510 200 HB 27 6 2 42 None 77 40 22 5 3 1 28 96 60 76 96 98 84 79 91 76 22 22 22 30 30 91 75 78
Fred Gosselin 509 180 CB 34 12 0 0 None 27 1 0 1 1 1 28 82 40 51 78 82 58 0 79 20 41 1 1 1 1 61 55 41
Ash Gosselin 604 355 LT 36 16 0 0 None 92 15 4 3 3 1 28 68 97 77 72 82 2 2 44 0 22 0 0 80 80 99 44 99
Brian Pinopoulos 604 220 WR 21 0 0 0 TeamLeader 98 2 1 1 1 1 28 96 70 51 98 98 78 68 99 77 22 88 81 41 41 90 79 71
$Team03 UFreeAgency 0 0
Cam Peterson 604 220 WR 21 0 0 0 TeamLeader 98 2 1 1 1 1 28 96 70 51 98 98 78 68 99 77 22 88 81 41 41 90 79 71
$Team04 RFreeAgency 0 0- 편집됨dhalberger 2009년 11월 8일 일요일 오전 6:05.
- 편집됨dhalberger 2009년 11월 8일 일요일 오전 6:10.
- No, these lines:
Team tm = new Team(); league.Teams.Add(tm); league.Teams[league.Teams.Count - 1].Name = line.Substring(line.IndexOf("$") + 1, (line.LastIndexOf(" ") - line.IndexOf(" ")) - 3);
And let me take a look at the rest of it now that I have the input :)
Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit - Looking at your code I don't see where you assign anything to AllTeams. What is AllTeams supposed to contain? How did it get there?
Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit - To your first response since mine: I must have forgotten to change it when I decided to use tm. My mistake. It doesn't help any but it's good for consistency.
To your second response: Here's the AllTeams method, located in the Team class:
public List<Team> AllTeams { get { var list = from at in Teams select at; return list.ToList(); } }This method is supposed to return a list of all of the teams in the league. - If Teams is already a list, why not just return it?
Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit I use it as a reminder that it's simply a list of all teams. It also makes it easier to read the code (I say to myself, "ok, of 'AllTeams' in 'league',... "). But mostly because someone recommended it and I've yet to find a more logical purpose for it.
- Bump
EDIT: I think I know what's wrong. Team 1 players that don't fit under its salary cap tries to send those players to team 2 before team 2 is actually created.- 편집됨dhalberger 2009년 11월 9일 월요일 오전 12:35.
- 답변으로 표시됨dhalberger 2009년 11월 9일 월요일 오전 12:57

