-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
175 lines (155 loc) · 6.66 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spire.Xls;
namespace Battle
{
public partial class Form1 : Form
{
private List<Hero> _heroes = new List<Hero>();
public Form1()
{
InitializeComponent();
}
private Hero GetHeroFromString(string line)
{
var stats = line.Split(',');
var name = stats[0];
var isAgil = Convert.ToInt32(stats[1]) == 1;
var streng = Convert.ToInt32(stats[2]);
var agility = Convert.ToInt32(stats[3]);
var armor = Convert.ToInt32(stats[4]);
var damage = Convert.ToInt32(stats[5]);
var regen = Convert.ToInt32(stats[6]);
return new Hero(name,isAgil,streng,agility,armor, damage, regen);
}
private void load_Click(object sender, EventArgs e)
{
var workbook = new Workbook();
workbook.LoadFromFile(@"..\heroes.csv", ",");
var worksheet = workbook.Worksheets[0];
var t = worksheet.ExportDataTable();
dataGridView1.DataSource = t;
}
private void update_Click(object sender, EventArgs e)
{
var t = (DataTable) dataGridView1.DataSource;
var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.InsertDataTable(t, true, 1, 1);
workbook.SaveToFile(@"..\heroes.csv");
}
private void start_Click(object sender, EventArgs e)
{
int playerIndex;
try
{
playerIndex = dataGridView1.CurrentRow.Index;
}
catch (NullReferenceException r)
{
MessageBox.Show("Герой не выбран", " ", MessageBoxButtons.OK);
return;
}
var compIndex = new Random().Next(dataGridView1.Rows.Count);
while(compIndex==playerIndex) compIndex = new Random().Next(dataGridView1.Rows.Count);
var playerStr = (from DataGridViewCell obj in dataGridView1.CurrentRow.Cells
select obj.Value.ToString()).ToList();
var line = playerStr.Aggregate("", (current, str) => current + (str + ","));
var playerHero = GetHeroFromString(line);
var compStr = (from DataGridViewCell obj in dataGridView1.Rows[compIndex].Cells
select obj.Value.ToString()).ToList();
line = compStr.Aggregate("", (current, str) => current + (str + ","));
var compHero = GetHeroFromString(line);
new Game(playerHero, compHero).Show();
Hide();
}
private void loadGame_Click(object sender, EventArgs e)
{
var reader = new StreamReader(@"../save.txt");
var stats = reader.ReadLine().Split(',');
if (stats.Length == 0)
{
MessageBox.Show("Нет сохранения", " ", MessageBoxButtons.OK);
return;
}
var name = stats[0];
var isAgil = Convert.ToInt32(stats[1]) == 1;
var streng = Convert.ToInt32(stats[2]);
var agility = Convert.ToInt32(stats[3]);
var armor = Convert.ToInt32(stats[4]);
var damage = Convert.ToInt32(stats[5]);
var regen = Convert.ToInt32(stats[6]);
var currenth = Convert.ToDouble(stats[7]);
var playerHero = new Hero(name,isAgil,streng,agility,armor, damage, regen, currenth);
stats = reader.ReadLine().Split(',');
name = stats[0];
isAgil = Convert.ToInt32(stats[1]) == 1;
streng = Convert.ToInt32(stats[2]);
agility = Convert.ToInt32(stats[3]);
armor = Convert.ToInt32(stats[4]);
damage = Convert.ToInt32(stats[5]);
regen = Convert.ToInt32(stats[6]);
currenth = Convert.ToDouble(stats[7]);
var compHero = new Hero(name,isAgil,streng,agility,armor, damage, regen, currenth);
new Game(playerHero, compHero).Show();
Hide();
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
var s = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
switch (e.ColumnIndex)
{
case 0 : if (s.Length == 0 || s == null) {
MessageBox.Show("колонка не может быть пустой");
dataGridView1[e.ColumnIndex, e.RowIndex].Value = "";
}
break;
case 1:
try
{
var a = Convert.ToInt32(s);
if (a != 0 && a != 1)
{
MessageBox.Show("Неверный аттрибут", " ", MessageBoxButtons.OK);
dataGridView1[e.ColumnIndex, e.RowIndex].Value = "";
}
}
catch (Exception)
{
MessageBox.Show("Неверный аттрибут", " ", MessageBoxButtons.OK);
dataGridView1[e.ColumnIndex, e.RowIndex].Value = "";
}
break;
default:
if (s.Length == 0 || s == null)
{
MessageBox.Show("колонка не может быть пустой");
dataGridView1[e.ColumnIndex, e.RowIndex].Value = "";
}
try
{
var a = Convert.ToInt32(s);
if (a <= 0 || a > 1000)
{
MessageBox.Show("хар-ка может быть в пределах от 0 до 1000", " ", MessageBoxButtons.OK);
dataGridView1[e.ColumnIndex, e.RowIndex].Value = "";
}
}
catch (Exception)
{
MessageBox.Show("хар-ка может быть в пределах от 0 до 1000", " ", MessageBoxButtons.OK);
dataGridView1[e.ColumnIndex, e.RowIndex].Value = "";
}
break;
}
}
}
}