Skip to content
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

add week0002.cs #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
134 changes: 134 additions & 0 deletions week0002/hainnt/csharp/week0002.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Week0002
{
public static class Week0002
{
public static void Main(string[] args)
{
while (true)
{
Console.Write("Input M: ");
var m = Convert.ToInt32(Console.ReadLine());
Console.Write("Input N: ");
var n = Convert.ToInt32(Console.ReadLine());

var arr = Cal(m, n);

foreach (var i in arr)
{
foreach (var j in i)
{
Console.Write(Draw(j, m * n) + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
}

public static List<List<int>> Cal(double m, double n)
{
var array = new List<List<int>>();

//Allocate 2 dimension array
for (var i = 0; i < m; i++)
{
array.Add(new List<int>());
for (var j = 0; j < n; j++)
{
array[i].Add(0);
}
}

var pointer = new Cell(0, 0);

//Change direction points
var MinMin = new Cell(1, 0);
Copy link
Member

Choose a reason for hiding this comment

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

Rename:

MinMin => TopLeft
MinMax => TopRight
MaxMax => BottomRight
MaxMin => BottomLeft

var MinMax = new Cell(0, n - 1);
var MaxMax = new Cell(m - 1, n - 1);
var MaxMin = new Cell(m - 1, 0);

var move = new Cell(0, 1);

for (var i = 1; i <= m * n; i++)
{
array[(int)pointer.X][(int)pointer.Y] = i;
Copy link
Member

Choose a reason for hiding this comment

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

Here's confusion: In your case X represents row (vertical ordinate) and Y represents column (horizontal ordinate).

It is counter-intuitive and hard to read. I would suggest to swap X & Y so they carry default meaning: Y for row & X for column.


if (pointer.Equal(MinMin))
{
move = new Cell(0, 1);
Copy link
Member

Choose a reason for hiding this comment

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

You are moving Right.


if (MinMin.X + 1 <= (m - 1) / 2 && MinMin.Y + 1 < (n - 1) / 2)
{

MinMin.X++; MinMin.Y++;
}
}
else if (pointer.Equal(MinMax))
{
move = new Cell(1, 0);
Copy link
Member

Choose a reason for hiding this comment

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

You are moving Down.

if (MinMax.X + 1 <= (m - 1) / 2 && MinMax.Y - 1 >= (n - 1) / 2)
{
MinMax.X++; MinMax.Y--;
}
}
else if (pointer.Equal(MaxMax))
{
if (MaxMax.X - 1 >= (m - 1) / 2 && MaxMax.Y - 1 >= (n - 1) / 2)
{
MaxMax.X--; MaxMax.Y--;
}
move = new Cell(0, -1);
Copy link
Member

Choose a reason for hiding this comment

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

You are moving Left.

}
else if (pointer.Equal(MaxMin))
{
if (MaxMin.X - 1 > (m - 1) / 2 && MaxMin.Y + 1 <= (n - 1) / 2)
{
MaxMin.X--; MaxMin.Y++;
}
move = new Cell(-1, 0);
Copy link
Member

Choose a reason for hiding this comment

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

You are moving Up.

}


pointer += move;
Copy link
Member

Choose a reason for hiding this comment

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

You are mixing up Cell & Movement. Although the code works, this does not make much sense from design perspective. Maybe you can simplify your code.

}

return array;
}
public static bool Equal(this Cell cell, Cell obj)
{
return cell.X == obj.X && cell.Y == obj.Y;
}

public static string Draw(int num, int max)
{
var result = "";
var size = max.ToString().Count() - num.ToString().Count();
for (var i = 0; i < size; i++)
{
result += " ";
}
return result += num;
}
}

public class Cell
{
public Cell(double x, double y)
{
X = x;
Y = y;
}

public double X { get; set; }
public double Y { get; set; }

public static Cell operator +(Cell c1, Cell c2)
{
return new Cell(c1.X + c2.X, c1.Y + c2.Y);
}
}
}