-
Notifications
You must be signed in to change notification settings - Fork 0
Added realisation #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>WinExe</OutputType> | ||
| <TargetFramework>net7.0-windows</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <UseWindowsForms>true</UseWindowsForms> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <RootNamespace>FindPare</RootNamespace> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <ItemGroup> | ||
| <Compile Update="Form1.cs"> | ||
| <SubType>Form</SubType> | ||
| </Compile> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,100 @@ | ||||||||||||
| namespace FindPare; | ||||||||||||
|
|
||||||||||||
| public class GameCore | ||||||||||||
| { | ||||||||||||
| enum State | ||||||||||||
| { | ||||||||||||
| firstButtonPressed, | ||||||||||||
| secondButtonPressed | ||||||||||||
| } | ||||||||||||
| public GameCore(int fieldSize) | ||||||||||||
|
Comment on lines
+9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| { | ||||||||||||
| _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<int>(Enumerable.Range(0, _dimensionSize * _dimensionSize / 2)); | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Более того, _dimensionSize * _dimensionSize / 2 - 1 стоило вынести в отдельную переменную, несколько раз это выражение в методе используется |
||||||||||||
| 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) | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Метод публичный, в него могут передать отрицательный или другой индекс, который вы бы посчитали некорректным. Нужно такие случаи обрабатывать |
||||||||||||
| { | ||||||||||||
| switch (_state) | ||||||||||||
| { | ||||||||||||
| case State.firstButtonPressed: | ||||||||||||
| int rowIndex = index % _dimensionSize; | ||||||||||||
| var currentCell = (rowIndex, index - rowIndex * _dimensionSize); | ||||||||||||
|
Comment on lines
+77
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отображение из числа в пару у вас написано неправильно :) |
||||||||||||
| 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)); | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут стоило исключение другого типа выбросить. Попадание в default statement этого оператора switch не зависит от аргумента метода |
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| private State _state; | ||||||||||||
|
|
||||||||||||
| private (int, int) _lastOpened; | ||||||||||||
| private readonly int[][] _field; | ||||||||||||
| private readonly bool[][] _isOpened; | ||||||||||||
| private readonly int _dimensionSize; | ||||||||||||
| } | ||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| namespace FindPare; | ||
|
|
||
| static class Program | ||
| { | ||
| /// <summary> | ||
| /// The main entry point for the application. | ||
| /// </summary> | ||
| [STAThread] | ||
| static void Main(string[] args) | ||
| { | ||
| if (args.Length != 1) | ||
| { | ||
| Console.WriteLine("Incorrect number of program args"); | ||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Мы уже давно не печатаем в консоль в случае некорректного ввода :) В таких случаях нужно выкинуть исключение |
||
| 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)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.