-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (91 loc) · 3.4 KB
/
Program.cs
File metadata and controls
96 lines (91 loc) · 3.4 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Task3
{
// class RandomNumberGeneratorRealization : RandomNumberGenerator
// {
// public void GetBytes(byte[] bytes){
// }
// }
class Program
{
const int byteBufferSize = 16;
static void Main(string[] playables)
{
if (playables.Length < 3 || playables.Length % 2 != 1)
{
Console.WriteLine("Invalid parameters count!");
return;
}
else if (playables.Distinct().ToArray().Length != playables.Length)
{
Console.WriteLine("Invalid parameters! Do not use similar variants!");
return;
}
byte[] key = new byte[byteBufferSize];
RandomNumberGenerator keyGenerator;
(key, keyGenerator) = GenerateKey();
int pcStep = GenerateAns(keyGenerator, playables.Length);
byte[] HMAC = GenerateHMAC(key, pcStep);
Console.WriteLine("HMAC = " + BitConverter.ToString(HMAC).Replace("-", ""));
int playerStep = -1;
do
{
PrintAbles(playables);
Console.Write("Enter your move: ");
playerStep = Convert.ToInt32(Console.ReadLine()) - 1;
}
while(playerStep >= playables.Length || playerStep < 0);
Console.WriteLine("Your move = " + playables[Convert.ToInt32(playerStep)]);
Console.WriteLine("PC move = " + playables[pcStep]);
if (pcStep == playerStep)
{
Console.WriteLine("Tie!");
}
else if ((pcStep - playerStep <= Convert.ToInt32(playables.Length/2) && pcStep - playerStep > 0) ||
(pcStep - playerStep < 0 - Convert.ToInt32(playables.Length/2)))
{
Console.WriteLine("You lose!");
Console.WriteLine("Key = " + BitConverter.ToString(key).Replace("-", ""));
}
else
{
Console.WriteLine("You win!");
}
Console.Read();
}
static (byte[], RandomNumberGenerator) GenerateKey()
{
var keyGenerator = RandomNumberGenerator.Create();
byte[] key = new byte[byteBufferSize];
keyGenerator.GetNonZeroBytes(key);
var result = BitConverter.ToString(key);
return (key, keyGenerator);
}
static int GenerateAns(RandomNumberGenerator keyGenerator, int length)
{
byte[] compplayerStep = new byte[sizeof(int)];
keyGenerator.GetBytes(compplayerStep, 0, compplayerStep.Length);
return (Math.Abs(BitConverter.ToInt32(compplayerStep, 0)) % length);
}
static byte[] GenerateHMAC(byte[] key, int pcStep)
{
using (var hmac = new HMACSHA256(key))
{
byte[] hmacBytes = new byte[key.Length];
hmacBytes = hmac.ComputeHash(Encoding.Default.GetBytes(pcStep.ToString()));
return hmacBytes;
}
}
static void PrintAbles(string[] playables)
{
int counter = 1;
foreach (string playable in playables)
Console.WriteLine((counter++).ToString() + ". " + playable);
}
}
}