using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Collections; using System.IO; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } ArrayList teamList = new ArrayList(); ArrayList playerList = new ArrayList(); ateam myteam = new ateam(); scoreboard myscore = new scoreboard(); scoreboard imprvscore = new scoreboard(); float gameplayed; float gameleft; int orankDiff; float FGM, FGA, FTM, FTA; private void Form1_Load(object sender, EventArgs e) { // TODO: 這行程式碼會將資料載入 'nBA_DataSet.Players' 資料表。您可以視需要進行移動或移除。 this.playersTableAdapter.Fill(this.nBA_DataSet.Players); // TODO: 這行程式碼會將資料載入 'nBA_DataSet.Players' 資料表。您可以視需要進行移動或移除。 this.playersTableAdapter.Fill(this.nBA_DataSet.Players); } public void FileToTeams() { string[] lineOfTeams = File.ReadAllLines("team.csv"); foreach (var roughline in lineOfTeams) { string line = roughline.Replace("*", ""); string[] tokens = line.Split(','); team newTeam = new team(); newTeam.Rank = int.Parse(tokens[0]); newTeam.teamName = tokens[1].ToString(); newTeam.GP = float.Parse(tokens[2]); newTeam.FG = float.Parse(tokens[3]); newTeam.FT = float.Parse(tokens[4]); newTeam.THREEPTM = float.Parse(tokens[5]); newTeam.PTS = float.Parse(tokens[6]); newTeam.REB = float.Parse(tokens[7]); newTeam.AST = float.Parse(tokens[8]); newTeam.ST = float.Parse(tokens[9]); newTeam.BLK = float.Parse(tokens[10]); newTeam.TO = float.Parse(tokens[11]); teamList.Add(newTeam); if (newTeam.teamName == "The Money Ball") { myteam.myteamscore = newTeam; } } } public void FileToPlayers(string filename) { string[] lineOfPlayers = File.ReadAllLines(filename); foreach (var roughline in lineOfPlayers) { string line = roughline.Replace("*", ""); string[] tokens = line.Split(','); player newPlayer = new player(); newPlayer.ORANK = int.Parse(tokens[0]); newPlayer.GP = float.Parse(tokens[3]); string[] FG = tokens[5].Split('/'); newPlayer.FGM = float.Parse(FG[0]); newPlayer.FGA = float.Parse(FG[1]); string[] FT = tokens[7].Split('/'); newPlayer.FTM = float.Parse(FT[0]); newPlayer.FTA = float.Parse(FT[1]); newPlayer.THREEPTM = float.Parse(tokens[9]); newPlayer.PTS = float.Parse(tokens[10]); newPlayer.REB = float.Parse(tokens[11]); newPlayer.AST = float.Parse(tokens[12]); newPlayer.ST = float.Parse(tokens[13]); newPlayer.BLK = float.Parse(tokens[14]); newPlayer.TO = float.Parse(tokens[15]); playerList.Add(newPlayer); for (int i = 0; i < myteam.oranklist.Count(); i++) { if (newPlayer.ORANK == myteam.oranklist[i]) { myteam.myplayers.Add(newPlayer); } } } } public void FileToMyPlayers() { string[] lineOfMyPlayers = File.ReadAllLines("myplayer.csv"); foreach (var roughline in lineOfMyPlayers) { string line = roughline.Replace("*", ""); string[] tokens = line.Split(','); myteam.oranklist.Add(int.Parse(tokens[0])); } } public void FileToMyScore() { string[] lineOfMyScore = File.ReadAllLines("myscore.csv"); foreach (var roughline in lineOfMyScore) { string line = roughline.Replace("*", ""); string[] tokens = line.Split(','); if (tokens[0] == "The Money Ball") { myscore.TeamName = tokens[0]; myscore.FGRank = float.Parse(tokens[1]); myscore.FTRank = float.Parse(tokens[2]); myscore.ThreeRank = float.Parse(tokens[3]); myscore.PTSRank = float.Parse(tokens[4]); myscore.REBRank = float.Parse(tokens[5]); myscore.ASTRank = float.Parse(tokens[6]); myscore.STRank = float.Parse(tokens[7]); myscore.BLKRank = float.Parse(tokens[8]); myscore.TORank = float.Parse(tokens[9]); myscore.Score = float.Parse(tokens[10]); } } } public void inputname() { string[] lineofnames = File.ReadAllLines("playersnames.csv"); foreach(var line in lineofnames){ string[] tokens = line.Split(','); for(int q = 0; q < playerList.Count; q++) { if (((player)playerList[q]).ORANK == int.Parse(tokens[1])) { ((player)playerList[q]).name = tokens[0]; if (tokens[2] == "out") { ((player)playerList[q]).status = "out"; } } } } } public void chk() { listBox1.Items.Clear(); scoreboard newscore = myscore; team improvedteam = new team(); //印出標題 string r = "Deal,FG%,FT%,3PTM,PTS,REB,AST,ST,BLK,TO,Rank,FG%,FT%,3PTM,PTS,REB,AST,ST,BLK,TO"; using (FileStream fs = new FileStream("result.csv", FileMode.Append, FileAccess.Write)) using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(r); } for (int i = 0; i < myteam.myplayers.Count; i++) { for (int j = 0; j < playerList.Count; j++) { if (((player)playerList[j]).status == "out" || ((player)playerList[j]).status == "mine") { continue; } if (checkBox1.Checked == true && (myteam.myplayers[i].ORANK < ((player)playerList[j]).ORANK + orankDiff)) { continue; } float allFGM = 820 * FGM / gameplayed + ((player)playerList[j]).FGM * gameleft - myteam.myplayers[i].FGM * gameleft; float allFGA = 820 * FGA / gameplayed + ((player)playerList[j]).FGA * gameleft - myteam.myplayers[i].FGA * gameleft; improvedteam.FG = allFGM / allFGA; float allFTM = 820 * FTM / gameplayed + ((player)playerList[j]).FTM * gameleft - myteam.myplayers[i].FTM * gameleft; float allFTA = 820 * FTA / gameplayed + ((player)playerList[j]).FTA * gameleft - myteam.myplayers[i].FTA * gameleft; improvedteam.FT = allFTM / allFTA; improvedteam.THREEPTM = 820 * myteam.myteamscore.THREEPTM / gameplayed + ((player)playerList[j]).THREEPTM * gameleft - myteam.myplayers[i].THREEPTM * gameleft; improvedteam.PTS = 820 * myteam.myteamscore.PTS / gameplayed + ((player)playerList[j]).PTS * gameleft - myteam.myplayers[i].PTS * gameleft; improvedteam.REB = 820 * myteam.myteamscore.REB / gameplayed + ((player)playerList[j]).REB * gameleft - myteam.myplayers[i].REB * gameleft; improvedteam.AST = 820 * myteam.myteamscore.AST / gameplayed + ((player)playerList[j]).AST * gameleft - myteam.myplayers[i].AST * gameleft; improvedteam.ST = 820 * myteam.myteamscore.ST / gameplayed + ((player)playerList[j]).ST * gameleft - myteam.myplayers[i].ST * gameleft; improvedteam.BLK = 820 * myteam.myteamscore.BLK / gameplayed + ((player)playerList[j]).BLK * gameleft - myteam.myplayers[i].BLK * gameleft; improvedteam.TO = 820 * myteam.myteamscore.TO / gameplayed + ((player)playerList[j]).TO * gameleft - myteam.myplayers[i].TO * gameleft; //算新的FG排名 float passed = 1; float even = 0; for (int k = 0; k < teamList.Count; k++) { float eventcount = 0; if (improvedteam.FG > ((team)teamList[k]).FG) { passed++; } if (improvedteam.FG == ((team)teamList[k]).FG) { eventcount++; } if (eventcount != 0) { even += 1 / eventcount; } } if (even != 0) { imprvscore.FGRank = passed + 1 / even; } else{ imprvscore.FGRank = passed; } //算新的FT排名 passed = 1; even = 0; for (int k = 0; k < teamList.Count; k++) { float eventcount = 0; if (improvedteam.FT > ((team)teamList[k]).FT) { passed++; } if (improvedteam.FT == ((team)teamList[k]).FT) { eventcount++; } if (eventcount != 0) { even += 1 / eventcount; } } if (even != 0) { imprvscore.FTRank = passed + 1 / even; } else{ imprvscore.FTRank = passed; } //算新的3PTM排名 passed = 1; even = 0; for (int k = 0; k < teamList.Count; k++) { float eventcount = 0; if (improvedteam.THREEPTM/820 > ((team)teamList[k]).THREEPTM/((team)teamList[k]).GP) { passed++; } if (improvedteam.THREEPTM/820 == ((team)teamList[k]).THREEPTM / ((team)teamList[k]).GP) { eventcount++; } if (eventcount != 0) { even += 1 / eventcount; } } if (even != 0) { imprvscore.ThreeRank = passed + 1 / even; } else{ imprvscore.ThreeRank = passed; } //算新的PTS排名 passed = 1; even = 0; for (int k = 0; k < teamList.Count; k++) { float eventcount = 0; if (improvedteam.PTS / 820 > ((team)teamList[k]).PTS / ((team)teamList[k]).GP) { passed++; } if (improvedteam.PTS / 820 == ((team)teamList[k]).PTS / ((team)teamList[k]).GP) { eventcount++; } if (eventcount != 0) { even += 1 / eventcount; } } if (even != 0) { imprvscore.PTSRank = passed + 1 / even; } else { imprvscore.PTSRank = passed; } //算新的REB排名 passed = 1; even = 0; for (int k = 0; k < teamList.Count; k++) { float eventcount = 0; if (improvedteam.REB / 820 > ((team)teamList[k]).REB / ((team)teamList[k]).GP) { passed++; } if (improvedteam.REB / 820 == ((team)teamList[k]).REB / ((team)teamList[k]).GP) { eventcount++; } if (eventcount != 0) { even += 1 / eventcount; } } if (even != 0) { imprvscore.REBRank = passed + 1 / even; } else { imprvscore.REBRank = passed; } //算新的AST排名 passed = 1; even = 0; for (int k = 0; k < teamList.Count; k++) { float eventcount = 0; if (improvedteam.AST / 820 > ((team)teamList[k]).AST / ((team)teamList[k]).GP) { passed++; } if (improvedteam.AST / 820 == ((team)teamList[k]).AST / ((team)teamList[k]).GP) { eventcount++; } if (eventcount != 0) { even += 1 / eventcount; } } if (even != 0) { imprvscore.ASTRank = passed + 1 / even; } else { imprvscore.ASTRank = passed; } //算新的ST排名 passed = 1; even = 0; for (int k = 0; k < teamList.Count; k++) { float eventcount = 0; if (improvedteam.ST / 820 > ((team)teamList[k]).ST / ((team)teamList[k]).GP) { passed++; } if (improvedteam.ST / 820 == ((team)teamList[k]).ST / ((team)teamList[k]).GP) { eventcount++; } if (eventcount != 0) { even += 1 / eventcount; } } if (even != 0) { imprvscore.STRank = passed + 1 / even; } else { imprvscore.STRank = passed; } //算新的BLK排名 passed = 1; even = 0; for (int k = 0; k < teamList.Count; k++) { float eventcount = 0; if (improvedteam.BLK / 820 > ((team)teamList[k]).BLK / ((team)teamList[k]).GP) { passed++; } if (improvedteam.BLK / 820 == ((team)teamList[k]).BLK / ((team)teamList[k]).GP) { eventcount++; } if (eventcount != 0) { even += 1 / eventcount; } } if (even != 0) { imprvscore.BLKRank = passed + 1 / even; } else { imprvscore.BLKRank = passed; } //算新的TO排名 passed = 1; even = 0; for (int k = 0; k < teamList.Count; k++) { float eventcount = 0; if (improvedteam.TO / 820 < ((team)teamList[k]).TO / ((team)teamList[k]).GP) { passed++; } if (improvedteam.TO / 820 == ((team)teamList[k]).TO / ((team)teamList[k]).GP) { eventcount++; } if (eventcount != 0) { even += 1 / eventcount; } } if (even != 0) { imprvscore.TORank = passed + 1 / even; } else { imprvscore.TORank = passed; } imprvscore.Score = imprvscore.FGRank + imprvscore.FTRank + imprvscore.ThreeRank + imprvscore.PTSRank + imprvscore.REBRank + imprvscore.ASTRank + imprvscore.STRank + imprvscore.BLKRank + imprvscore.TORank; //印出結果 if (imprvscore.Score >= myscore.Score + float.Parse(textBox7.Text)) if (((player)playerList[j]).status != "mine") { string s = ((player)playerList[j]).ORANK.ToString() + " " +((player)playerList[j]).name + " vs " + myteam.myplayers[i].ORANK.ToString() + " " + myteam.myplayers[i].name + "," + imprvscore.FGRank.ToString() + "," + imprvscore.FTRank.ToString() + "," + imprvscore.ThreeRank.ToString() + "," + imprvscore.PTSRank.ToString() + "," + imprvscore.REBRank.ToString() + "," + imprvscore.ASTRank.ToString() + "," + imprvscore.STRank.ToString() + "," + imprvscore.BLKRank.ToString() + "," + imprvscore.TORank.ToString() + "," + imprvscore.Score.ToString() + "," + improvedteam.FG.ToString() + "," + improvedteam.FT.ToString() + "," + improvedteam.THREEPTM.ToString() + "," + improvedteam.PTS.ToString() + "," + improvedteam.REB.ToString() + "," + improvedteam.AST.ToString() + "," + improvedteam.ST.ToString() + "," + improvedteam.BLK.ToString() + "," + improvedteam.TO.ToString(); s.Replace("?", " "); using (FileStream fm = new FileStream("result.csv", FileMode.Append, FileAccess.Write)) using (StreamWriter sm = new StreamWriter(fm)) { sm.WriteLine(s); } listBox1.Items.Add(s); } } } listBox1.Items.Add("End"); } class team { public int Rank; public string teamName; public float GP; public float FG; public float FT; public float THREEPTM; public float PTS; public float REB; public float AST; public float ST; public float BLK; public float TO; } class player { public int ORANK; public string name = "none"; public float GP; public float FGM; public float FGA; public float FTM; public float FTA; public float THREEPTM; public float PTS; public float REB; public float AST; public float ST; public float BLK; public float TO; public string status = "in"; } class ateam { public List oranklist = new List(); public List myplayers = new List(); public team myteamscore = new team(); } class scoreboard { public string TeamName; public float FGRank; public float FTRank; public float ThreeRank; public float PTSRank; public float REBRank; public float ASTRank; public float STRank; public float BLKRank; public float TORank; public float Score; } private void button1_Click(object sender, EventArgs e) { if (File.Exists("Result.csv")) { File.Delete("Result.csv"); } FileToMyPlayers(); FileToTeams(); gameplayed = float.Parse(textBox1.Text); gameleft = (820-gameplayed) / 13; orankDiff = int.Parse(textBox2.Text); FGM = float.Parse(textBox3.Text); FGA = float.Parse(textBox4.Text); FTM = float.Parse(textBox5.Text); FTA = float.Parse(textBox6.Text); FileToPlayers(textBox8.Text); FileToMyScore(); inputname(); chk(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { OpenFileDialog file = new OpenFileDialog(); file.ShowDialog(); textBox8.Text = file.SafeFileName; } private void checkBox1_CheckedChanged(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { try { DataRow dr = nBA_DataSet.Tables["Players"].NewRow(); dr["ORANK"] = 1; dr["NAME"] = "Michael Jordan"; dr["GP"] = 52; dr["FGM"] = 18.6; dr["FGA"] = 25.4; dr["FTM"] = 6.3; dr["FTA"] = 8.0; dr["THREEPTM"] = 5.2; dr["PTS"] = 38.6; dr["REB"] = 10.6; dr["AST"] = 7.6; dr["ST"] = 4.6; dr["BLK"] = 3.6; dr["TO"] = 2.6; dr["STATUS"] = "in"; nBA_DataSet.Tables["Players"].Rows.Add(dr); playersTableAdapter.Update(nBA_DataSet); MessageBox.Show("insert success"); } catch (InvalidCastException error) { MessageBox.Show(error.Message.ToString()); } } } }