From d7ef1c3fb022516a926b7f00ab57233385f54146 Mon Sep 17 00:00:00 2001 From: IgnatSergeev/laptop Date: Sat, 20 May 2023 09:50:16 +0300 Subject: [PATCH] Added realisation --- C#/forSpbu/FindPair/FindPair.csproj | 12 +++ C#/forSpbu/FindPair/FindPare.csproj.user | 8 ++ C#/forSpbu/FindPair/GameCore.cs | 100 +++++++++++++++++++++++ C#/forSpbu/FindPair/GameForm.Designer.cs | 67 +++++++++++++++ C#/forSpbu/FindPair/GameForm.cs | 32 ++++++++ C#/forSpbu/FindPair/Program.cs | 31 +++++++ C#/forSpbu/forSpbu.sln | 17 +++- 7 files changed, 263 insertions(+), 4 deletions(-) create mode 100644 C#/forSpbu/FindPair/FindPair.csproj create mode 100644 C#/forSpbu/FindPair/FindPare.csproj.user create mode 100644 C#/forSpbu/FindPair/GameCore.cs create mode 100644 C#/forSpbu/FindPair/GameForm.Designer.cs create mode 100644 C#/forSpbu/FindPair/GameForm.cs create mode 100644 C#/forSpbu/FindPair/Program.cs diff --git a/C#/forSpbu/FindPair/FindPair.csproj b/C#/forSpbu/FindPair/FindPair.csproj new file mode 100644 index 0000000..bdff398 --- /dev/null +++ b/C#/forSpbu/FindPair/FindPair.csproj @@ -0,0 +1,12 @@ + + + + WinExe + net7.0-windows + enable + true + enable + FindPare + + + \ No newline at end of file diff --git a/C#/forSpbu/FindPair/FindPare.csproj.user b/C#/forSpbu/FindPair/FindPare.csproj.user new file mode 100644 index 0000000..3a34caa --- /dev/null +++ b/C#/forSpbu/FindPair/FindPare.csproj.user @@ -0,0 +1,8 @@ + + + + + Form + + + \ No newline at end of file diff --git a/C#/forSpbu/FindPair/GameCore.cs b/C#/forSpbu/FindPair/GameCore.cs new file mode 100644 index 0000000..4a3cd61 --- /dev/null +++ b/C#/forSpbu/FindPair/GameCore.cs @@ -0,0 +1,100 @@ +namespace FindPare; + +public class GameCore +{ + enum State + { + firstButtonPressed, + secondButtonPressed + } + public GameCore(int fieldSize) + { + _field = new int[fieldSize][]; + for (int i = 0; i < fieldSize; i++) + { + _field[i] = new int[fieldSize]; + } + _isOpened = new bool[fieldSize][]; + for (int i = 0; i < fieldSize; i++) + { + _isOpened[i] = new bool[fieldSize]; + } + _dimensionSize = fieldSize; + _state = State.secondButtonPressed; + RandomiseField(); + } + + private void RandomiseField() + { + var cellValues = new List(Enumerable.Range(0, _dimensionSize * _dimensionSize / 2)); + cellValues.AddRange(Enumerable.Range(0, _dimensionSize * _dimensionSize / 2)); + var random = new Random(); + for (var i = 0; i < _dimensionSize; i++) + { + for (int j = 0; j < _dimensionSize; j++) + { + var nextValueIndex = random.Next(cellValues.Count); + _field[i][j] = cellValues[nextValueIndex]; + cellValues.RemoveAt(nextValueIndex); + } + } + + } + + public int GetValue(int firstIndex, int secondIndex) + { + if (firstIndex < 0 || firstIndex >= _field.Length) + { + throw new ArgumentOutOfRangeException(nameof(firstIndex)); + } + + if (secondIndex < 0 || secondIndex >= _field[0].Length) + { + throw new ArgumentOutOfRangeException(nameof(secondIndex)); + } + return _field[firstIndex][secondIndex]; + } + + public bool IsCellOpened(int firstIndex, int secondIndex) + { + if (firstIndex < 0 || firstIndex >= _isOpened.Length) + { + throw new ArgumentOutOfRangeException(nameof(firstIndex)); + } + + if (secondIndex < 0 || secondIndex >= _isOpened[0].Length) + { + throw new ArgumentOutOfRangeException(nameof(secondIndex)); + } + return _isOpened[firstIndex][secondIndex]; + } + + public bool OpenCell(int index) + { + switch (_state) + { + case State.firstButtonPressed: + int rowIndex = index % _dimensionSize; + var currentCell = (rowIndex, index - rowIndex * _dimensionSize); + if (_field[_lastOpened.Item1][_lastOpened.Item2] == _field[currentCell.Item1][currentCell.Item2]) + { + return true; + } + + return false; + case State.secondButtonPressed: + int firstIndex = index % _dimensionSize; + _lastOpened = (firstIndex, index - firstIndex * _dimensionSize); + return true; + default: + throw new ArgumentOutOfRangeException(nameof(_state)); + } + } + + private State _state; + + private (int, int) _lastOpened; + private readonly int[][] _field; + private readonly bool[][] _isOpened; + private readonly int _dimensionSize; +} \ No newline at end of file diff --git a/C#/forSpbu/FindPair/GameForm.Designer.cs b/C#/forSpbu/FindPair/GameForm.Designer.cs new file mode 100644 index 0000000..fd94e73 --- /dev/null +++ b/C#/forSpbu/FindPair/GameForm.Designer.cs @@ -0,0 +1,67 @@ +namespace FindPare; + +partial class GameForm +{ + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent(int fieldSize) + { + this.mainLayout = new TableLayoutPanel(); + this.fieldSize = fieldSize; + + this.Size = new Size(400, 400); + this.MinimumSize = new Size(300, 400); + this.Text = "FindPair"; + this.Controls.Add(this.mainLayout); + + this.mainLayout.Size = new Size(385, 362); + this.mainLayout.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; + this.mainLayout.ColumnCount = fieldSize; + this.mainLayout.RowCount = fieldSize; + for (int i = 0; i < fieldSize; i++) + { + mainLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100/fieldSize)); + mainLayout.RowStyles.Add(new ColumnStyle(SizeType.Percent, 100/fieldSize)); + for (int j = 0; j < fieldSize; j++) + { + var button = new Button(); + button.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; + button.BackColor = Color.Gray; + button.TextAlign = ContentAlignment.MiddleCenter; + button.Tag = (i * fieldSize + j).ToString(); + button.ForeColor = Color.Azure; + button.Font = new Font("Segoe UI", 15F, FontStyle.Regular, GraphicsUnit.Pixel); + button.Click += CellOnClick; + this.mainLayout.Controls.Add(button, i, j); + } + } + } + + #endregion + + private TableLayoutPanel mainLayout; + private int fieldSize; +} \ No newline at end of file diff --git a/C#/forSpbu/FindPair/GameForm.cs b/C#/forSpbu/FindPair/GameForm.cs new file mode 100644 index 0000000..eaabb3b --- /dev/null +++ b/C#/forSpbu/FindPair/GameForm.cs @@ -0,0 +1,32 @@ +namespace FindPare; + +public partial class GameForm : Form +{ + public GameForm(int fieldSize) + { + _gameCore = new GameCore(fieldSize); + InitializeComponent(fieldSize); + } + + private void CellOnClick(object sender, EventArgs eventArgs) + { + if (sender is not Button senderButton) + { + return; + } + if (senderButton.Tag is not string indexString) + { + return; + } + int index = int.Parse(indexString); + var coordinate = (index % fieldSize, index - (index % fieldSize) * fieldSize); + if (_gameCore.OpenCell(index)) + { + senderButton.Text = _gameCore.GetValue(coordinate.Item1, coordinate.Item2); + } + + } + + private Button lastOpenedButton; + private readonly GameCore _gameCore; +} \ No newline at end of file diff --git a/C#/forSpbu/FindPair/Program.cs b/C#/forSpbu/FindPair/Program.cs new file mode 100644 index 0000000..b08bc16 --- /dev/null +++ b/C#/forSpbu/FindPair/Program.cs @@ -0,0 +1,31 @@ +namespace FindPare; + +static class Program +{ + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main(string[] args) + { + if (args.Length != 1) + { + Console.WriteLine("Incorrect number of program args"); + return; + } + if (!int.TryParse(args[0], out var fieldSize)) + { + Console.WriteLine("Error in parsing fieldSize, incorrect program parameter"); + return; + } + + if (fieldSize <= 0 || fieldSize % 2 == 1) + { + Console.WriteLine("Incorrect program parameter, should be > 0 and % 2 == 0"); + return; + } + + ApplicationConfiguration.Initialize(); + Application.Run(new GameForm(fieldSize)); + } +} \ No newline at end of file diff --git a/C#/forSpbu/forSpbu.sln b/C#/forSpbu/forSpbu.sln index 527f400..33c4853 100644 --- a/C#/forSpbu/forSpbu.sln +++ b/C#/forSpbu/forSpbu.sln @@ -10,6 +10,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "03.03", "03.03", "{882A9B9C EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "10.03", "10.03", "{EA6FC7D9-BDFB-49CD-AC00-FC5DDC5274B0}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Exam", "Exam", "{75E71CEE-F374-4770-87E9-93D96DFBBED0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FindPair", "FindPair\FindPair.csproj", "{EC7B9564-86F4-4F50-BB4D-D2A1DA0F8430}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,12 +25,17 @@ Global {E007586F-9760-4744-BB25-EDEFD6BA860C}.Release|Any CPU.ActiveCfg = Release|Any CPU {E007586F-9760-4744-BB25-EDEFD6BA860C}.Release|Any CPU.Build.0 = Release|Any CPU {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.Build.0 = Release|Any CPU + {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.Build.0 = Release|Any CPU + {EC7B9564-86F4-4F50-BB4D-D2A1DA0F8430}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EC7B9564-86F4-4F50-BB4D-D2A1DA0F8430}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC7B9564-86F4-4F50-BB4D-D2A1DA0F8430}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EC7B9564-86F4-4F50-BB4D-D2A1DA0F8430}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution - {E007586F-9760-4744-BB25-EDEFD6BA860C} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F} + {E007586F-9760-4744-BB25-EDEFD6BA860C} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F} {A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F} + {EC7B9564-86F4-4F50-BB4D-D2A1DA0F8430} = {75E71CEE-F374-4770-87E9-93D96DFBBED0} EndGlobalSection EndGlobal