Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions C#/forSpbu/FindPair/FindPair.csproj
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>
8 changes: 8 additions & 0 deletions C#/forSpbu/FindPair/FindPare.csproj.user
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>
100 changes: 100 additions & 0 deletions C#/forSpbu/FindPair/GameCore.cs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Решение не собирается
  2. Есть warning от nullability-анализа
  3. Нет формы и логики с обработчиками элементов управления

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
public GameCore(int fieldSize)
}
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<int>(Enumerable.Range(0, _dimensionSize * _dimensionSize / 2));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var cellValues = new List<int>(Enumerable.Range(0, _dimensionSize * _dimensionSize / 2));
var cellValues = new List<int>(Enumerable.Range(0, _dimensionSize * _dimensionSize / 2 - 1));

Более того, _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)

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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));

Choose a reason for hiding this comment

The 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;
}
67 changes: 67 additions & 0 deletions C#/forSpbu/FindPair/GameForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions C#/forSpbu/FindPair/GameForm.cs
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;
}
31 changes: 31 additions & 0 deletions C#/forSpbu/FindPair/Program.cs
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

Choose a reason for hiding this comment

The 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));
}
}
17 changes: 13 additions & 4 deletions C#/forSpbu/forSpbu.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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