Source

Banker / src / Banker / Banker / Form1.cs

The default branch has multiple heads

  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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
 Программа реализующая алгоритм Банкира
 Куликов Александр ист-08
 
 */

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;


 

namespace Banker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        #region Глобальные переменные
        Stack<int> blockProc=new Stack<int>(); // стек содержащий индексы выполненых процессов
        Stack<int> blockRes = new Stack<int>();  // стек содержащий индексы ресурсов выполненых процессов
        Stack<int> endStack = new Stack<int>(); 

        int proc = 0; // индекс процесса
        int res = 0; // индекса ресурса

        int[] rezerv = new int[3];

        Var cur;

        int[,] temp = new int[3, 5]; // массив занимаемых ресурсов процессов
        int[,] max = new int[3, 5]; // массив максимальных значений процессов
        int[,] nul = new int[3, 5] { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }; 

        Random rnd = new Random(); // генератор случайных чисел

    static  public  int level=1;
     static  public int variant=1;

        string family="";
        string name = "";
        string group = "";

        #endregion



        class Var
        {
           public int[,] t;
           public int[] m;
            
          public  Var(int[,] t, int[] m)
          {
              this.t = t; 
              this.m = m; 
          }

            
        }
        #region Варианты заданий
        Var[] var1 = new Var[9] 
        {   new Var(new int[3,5] { {3, 4, 5,0,0},{0,0,0,0,0},{0,0,0,0,0}},new int[]{ 5,0,0}),
            new Var(new int[3,5] { {4, 4, 5,0,0},{0,0,0,0,0},{0,0,0,0,0} }, new int[]{ 5,0,0}),
            new Var(new int[3,5] { {5, 4, 5,0,0} ,{0,0,0,0,0},{0,0,0,0,0}}, new int[]{ 5,0,0}),
            new Var(new int[3,5] { {4, 5, 4,0,0},{0,0,0,0,0},{0,0,0,0,0} }, new int[]{ 5,0,0}),
            new Var(new int[3,5] { {5, 4, 5,0,0},{0,0,0,0,0},{0,0,0,0,0} }, new int[]{ 5,0,0}),
            new Var(new int[3,5] { {4, 5, 5,0,0},{0,0,0,0,0},{0,0,0,0,0} }, new int[]{ 5,0,0}),
            new Var(new int[3,5] { {5, 4, 5,0,0},{0,0,0,0,0},{0,0,0,0,0} }, new int[]{ 5,0,0}),
            new Var(new int[3,5] { {5, 5, 5,0,0},{0,0,0,0,0},{0,0,0,0,0} }, new int[]{ 5,0,0}),
            new Var(new int[3,5] { {5, 5, 4,0,0},{0,0,0,0,0},{0,0,0,0,0} }, new int[]{ 5,0,0}),
            
        };

        Var[] var2 = new Var[9] 
        {   new Var(new int[3,5] { {3, 6, 4, 5,0},{2, 5, 3, 4,0},{0,0,0,0,0} }, new int[]{6, 5,0}),
            new Var(new int[3,5] { {5, 6, 4, 5,0},{3, 5, 4, 5,0},{0,0,0,0,0} }, new int[]{ 6,5,0}),
            new Var(new int[3,5] { {3, 6, 4, 5,0},{5, 1, 5, 5,0},{0,0,0,0,0} }, new int[]{ 6,5,0}),
            new Var(new int[3,5] { {4, 6, 5, 3,0},{5, 2, 4, 5,0} ,{0,0,0,0,0}}, new int[]{ 6,5,0}),
            new Var(new int[3,5] { {2, 6, 5, 5,0},{4, 1, 4, 5,0},{0,0,0,0,0} }, new int[]{ 6,5,0}),
            new Var(new int[3,5] { {2, 6, 5, 6,0},{5, 1, 4, 5,0} ,{0,0,0,0,0}}, new int[]{ 6,5,0}),
            new Var(new int[3,5] { {2, 6, 5, 5,0},{5, 0 ,4, 5,0},{0,0,0,0,0} }, new int[]{ 6,5,0}),
            new Var(new int[3,5] { {3, 5, 4, 5,0},{5, 2, 4, 5,0} ,{0,0,0,0,0}}, new int[]{ 6,5,0}),
            new Var(new int[3,5] { {3, 5, 4, 5,0},{5, 2, 4, 5,0} ,{0,0,0,0,0}}, new int[]{ 6,5,0}),
            
        };

        Var[] var3 = new Var[9] 
        {   new Var(new int[3,5] { {5, 0, 5, 6, 5},{6, 4, 2, 4, 5},{1, 6, 3, 5, 5} }, new int[]{7,6,6}),
            new Var(new int[,] { {6, 4, 2, 4, 5},{3, 6, 3, 5, 5},{5, 0, 4, 6, 5} }, new int[]{ 7,6,6}),
            new Var(new int[,] { {4, 2, 5, 6, 5},{6, 5, 1, 4, 5},{1, 6, 3, 5, 5} }, new int[]{ 7,6,6}),
            new Var(new int[,] { {6, 4, 2, 4, 5},{1, 6, 3, 5, 5} ,{6, 1, 5, 6, 5}}, new int[]{ 7,6,6}),
            new Var(new int[,] { {5, 3, 6, 1, 6},{6, 4, 2, 4, 5} ,{2, 6, 3, 5, 5}}, new int[]{ 7,6,6}),
            new Var(new int[,] { {5, 0, 5, 6, 5},{6, 3, 5, 3, 6 } ,{1, 6, 3, 5, 5}}, new int[]{ 7,6,6}),
            new Var(new int[,] { {4, 2, 6, 6, 5 },{6, 4, 2, 4, 5},{2, 6, 3, 4, 6} }, new int[]{ 7,6,6}),
            new Var(new int[,] { {5, 0, 7, 6, 5},{6, 4, 3, 4, 5},{2, 6, 3, 5, 5 } }, new int[]{ 7,6,6}),
            new Var(new int[,] { {5, 0, 5, 6, 5},{6, 4, 2, 4, 5},{1, 6, 3, 5, 5 } }, new int[]{ 7,6,6}),
            
        };
        #endregion

        void level1(Var var) // Первый уровень, var - вариант задания
        {
            cur = var;
            table1.Rows[3].Visible = false;
            table1.Rows[4].Visible = false;

            table1.Columns[3].Visible = false;
            table1.Columns[4].Visible = false;
            table1.Columns[5].Visible = false;
            table1.Columns[6].Visible = false;

            table2.Columns[2].Visible = false;
            table2.Columns[3].Visible = false;
            temp = new int[3, 5];
          copy( max, var.t);
            var.m.CopyTo(rezerv,0);
            reSet();
            genQ();

        } 
        void level2(Var var) // Второй уровень, var - вариант задания
        {
            cur = var;
            table1.Rows[3].Visible = true;
            table1.Rows[4].Visible = false;

            table1.Columns[3].Visible = true;
            table1.Columns[4].Visible = true;
            table1.Columns[5].Visible = false;
            table1.Columns[6].Visible = false;

            table2.Columns[2].Visible = true;
            table2.Columns[3].Visible = false;
            temp = new int[3, 5];
            copy(max, var.t);
            var.m.CopyTo(rezerv, 0);
            reSet();
            genQ();
        }
        void level3(Var var) // Третий уровень, var - вариант задания
        {
            cur = var;
            table1.Rows[3].Visible = true;
            table1.Rows[4].Visible = true;

            table1.Columns[3].Visible = true;
            table1.Columns[4].Visible = true;
            table1.Columns[5].Visible = true;
            table1.Columns[6].Visible = true;

            table2.Columns[2].Visible = true;
            table2.Columns[3].Visible = true;
            temp = new int[3, 5];
            copy(max, var.t);
            var.m.CopyTo(rezerv, 0);
            reSet();
            genQ();
        }

        void genOut() // генератор выходного файла
        {
            StreamWriter of = new StreamWriter("banker.out");
            string s = "Фамилия: " + family + "\nИмя: " + name + "\nГруппа: " + group + "\nИмя ПК: " + Environment.MachineName + "\nДата и время: " + DateTime.Now+"\n";
            
            s += MD5(s);
            of.Write(s);
            of.Close();
        
        }

        void copy(int[,] x, int[,] y)
        {
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 5; j++)
                    x[i, j] = y[i, j];
        }

        public string MD5(string instr)
        {
            string strHash = string.Empty;

            foreach (byte b in new MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.Default.GetBytes(instr)))
            {
                strHash += b.ToString("X2");
            }
            return strHash;
        } 

        void genQ() 
        {
            switch (level) 
            {
                case 1:  do { proc = rnd.Next(1, 4); res = 1; } while (isBlock()&&!isOver()); label1.Text = "Процесс " + proc + " просит ресурс " + res; break;
                case 2: do { proc = rnd.Next(1, 5); res = rnd.Next(1, 3); } while (isBad() && !isOver()); label1.Text = "Процесс " + proc + " просит ресурс " + res; break;
                case 3: do { proc = rnd.Next(1, 6); res = rnd.Next(1, 4); } while (isBlock() && !isOver()); label1.Text = "Процесс " + proc + " просит ресурс " + res; break;
            }
            if (radioButton2.Checked) if (isExist()) label8.Text = "Дать"; else label8.Text = "Не дать";
            if (isOver()) { label1.Text = ""; label8.Text = ""; groupBox1.Hide();  }
        }

        #region Логические функции

        bool isBlock() 
        {
            return isBlock(this.proc, this.res);
        }

        bool isBlock(int proc, int res)
        {
            bool k = false;
            int[] k1=blockProc.ToArray();
            int[] k2=blockRes.ToArray();
            for (int i = 0; i < k1.Length; i++)
            {
                if (k1[i] == proc && k2[i] == res) k = true;
            }
                return k;
        }

        bool isExist()
        {
            bool k = false;
            switch (level)
            {
                case 1:
                    {
                        if (temp[res - 1, proc - 1] + rezerv[res - 1] >= max[res - 1, proc - 1])return true ;
                        for(int i=0;i<3;i++)if(!isBlock(i+1,1)) if (temp[0, i] + rezerv[0]-1 >= max[0, i])return true ;
                        if (rezerv[0] == 0) return false;
                    } break;
                case 2:
                    {
                        if ((temp[0, proc - 1] + rezerv[0] >= max[0, proc - 1]) && (temp[1, proc - 1] + rezerv[1] >= max[1, proc - 1])) return true;
                        for (int i = 0; i < 4; i++)
                            if (!isBlock(i + 1, 1))
                            {
                                if (res == 1)
                                {
                                    if ((temp[0, i] + rezerv[0] - 1 >= max[0, i]) && (temp[1, i] + rezerv[1] >= max[1, i])) return true;
                                }
                                else
                                {
                                    if ((temp[0, i] + rezerv[0] >= max[0, i]) && (temp[1, i] + rezerv[1] - 1 >= max[1, i])) return true;
                                }
                            }
                        if ((rezerv[0] == 0) || (rezerv[1] == 0)) return false;
                        
                    } break;
                case 3: 
                    {

                    } break;
            }
            


                return k;
        }

        bool isBad()
        {
            if (isBlock()) return true;
            int max1=rnd.Next(0,4), max2=0, max3=0;

            switch(level){
                case 2:
                    //for (int j = 0; j < 4; j++)
                    //{
                    //    if (!isBlock(j + 1, 1)) if (max[0, j] - temp[0, j] < max[0, max1] - temp[0, max1]) max1 = j;
                    //    if (!isBlock(j + 1, 2)) if (max[1, j] - temp[1, j] < max[1, max2] - temp[1, max2]) max2 = j;
                    //}
                    //if (res == 1) { if (proc == max1 + 1)return false; } else { if (proc == max2 + 1)return false; }
                    //if (res == 1) { if (temp[0, proc - 1] + max[0, max1] >= max[0, proc - 1])return false; } else { if (temp[1, proc - 1] + max[1, max2] >= max[1, proc - 1])return false; }

                    for (int j = 0; j < 4; j++)
                    {
                      if(!endStack.Contains(j))  if (max[0, j] - temp[0, j] <= max[0, max1] - temp[0, max1]&&max[1, j] - temp[1, j] <= max[1, max1] - temp[1, max1])  max1 = j;
                         
                    }
                    if (proc - 1 == max1 || (temp[0, proc - 1] + max[0, max1]+1 >= max[0, proc - 1]) && (temp[1, proc - 1] + max[1, max1] >= max[1, proc - 1])) return false;
                    break;
        }
                return true;
        }

        bool isOver()
        {
            switch (level)
            {
                case 1: if (max[0, 0] == 0 && max[0, 1] == 0 && max[0, 2] == 0) return true; break;
                case 2: if (max[0, 0] == 0 && max[0, 1] == 0 && max[0, 2] == 0 && max[1, 0] == 0 && max[1, 1] == 0 && max[1, 2] == 0 && max[0, 3] == 0 && max[1, 3] == 0) {  unblck(); reSet(); return true; } break;
                case 3: return true; break;

            }
            return false;
        }

        #endregion

        void blck() 
        {
            groupBox2.Enabled = false;
            groupBox3.Enabled = false;
        }
        void unblck() 
        {
            groupBox2.Enabled = true;
            groupBox3.Enabled = true;
        }
        void reSet()
        {
           
            for(int i=0;i<3;i++)
                for (int j = 0; j < 5; j++)
                {
                    table1[ i * 2 + 1,j].Value = temp[ i,j];
                    table1[ i * 2 + 2,j].Value = max[ i,j];
                }
            for (int i = 1; i <= 3; i++) table2[ i,0].Value = rezerv[i - 1];
        }

        #region Обработка событий

        private void Form1_Load(object sender, EventArgs e)
        {
           table1.Rows.Add("Процесс1", 0, 0, 0, 0, 0, 0);
           table1.Rows.Add("Процесс2", 0, 0, 0, 0, 0, 0);
           table1.Rows.Add("Процесс3", 0, 0, 0, 0, 0, 0);
           table1.Rows.Add("Процесс4", 0, 0, 0, 0, 0, 0);
           table1.Rows.Add("Процесс5", 0, 0, 0, 0, 0, 0);
           table2.Rows.Add("Резерв", 5, 5, 5);

           groupBox1.Hide();

           
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (isExist())
            {
                rezerv[res - 1] -= 1;
                temp[res - 1, proc - 1] += 1;
                cmp();
                if(isOver())
                {  MessageBox.Show(level +" уровень закончен"); }
                reSet();
                if (isOver()) if (radioButton1.Checked) nextLevel(); else { unblck(); }
            }
            else 
            {
                MessageBox.Show("Ресурс не выделен");
                temp = new int[3, 5] { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } };
                max=cur.t;
                cur.m.CopyTo(rezerv, 0);
                blockProc.Clear(); blockRes.Clear(); endStack.Clear();
                reSet();
            }
            genQ();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!isExist()) { }
            else
            {
                MessageBox.Show("Ресурс выделен");
                temp = new int[3, 5] { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } };
                copy(max, cur.t);
                cur.m.CopyTo(rezerv, 0);
                blockProc.Clear(); blockRes.Clear(); endStack.Clear();
                reSet();
            }
            genQ();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            blockProc.Clear(); blockRes.Clear();

            groupBox1.Show();
            groupBox1.Enabled = true;




            if (radioButton1.Checked)
            {
                if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
                {
                    family = textBox1.Text; textBox1.ReadOnly = true;
                    name = textBox2.Text; textBox2.ReadOnly = true;
                    group = textBox3.Text; textBox3.ReadOnly = true;
                }

                else { MessageBox.Show("Введите регистрационную информацию", "Ошибка"); return; }
                blck();
                level1(var1[rnd.Next(1, 4)]);

            }
            else
            {
                Form2 f2 = new Form2();

                f2.ShowDialog();
                blck();
                switch (level)
                {
                    case 1: level1(var1[variant]); break;
                    case 2: level2(var2[variant]); break;
                    case 3: level3(var3[variant]); break;
                }

            }

        }

        #endregion

        private void nextLevel()
        {
            switch (level) 
            {
                case 1: level += 1; level2(var2[rnd.Next(9)]); break;
                case 2: level += 1; level3(var3[rnd.Next(9)]); break;
                case 3: genOut(); break;
            }
        }

        
       

        private void cmp()
        {
            switch(level){
                case 1:
            for(int i=0;i<1;i++)
                for (int j = 0; j < 3; j++)
                {
                    if (temp[i, j] != max[i, j] || isBlock(j + 1, i + 1)) continue;
                    blockProc.Push(j + 1);
                    blockRes.Push(i + 1);
                    rezerv[res - 1] += max[i, j];
                    temp[i, j] = max[i, j] = 0;

                    
                    reSet();
                }break;
                case 2:
                    for (int i = 0; i < 2; i++)
                        for (int j = 0; j < 4; j++) 
                        {
                            if (temp[i, j] != max[i, j] || isBlock(j + 1, i + 1)) continue;
                            blockProc.Push(j + 1);
                            blockRes.Push(i + 1);
                        }
                    for (int j = 0; j < 4; j++) 
                    {
                        if (isBlock(j+1, 1) && isBlock(j+1, 2)) 
                        {
                            rezerv[0] += max[0, j];
                            rezerv[1] += max[1, j];
                            temp[0, j] = max[0, j] = temp[1, j] = max[1, j] = 0;
                            reSet();
                            endStack.Push(j);

                        }
                    }

                    break;
                case 3:
                    break;
        
        }
            
        }

        

    }
}