patterncsharpMinor
3-axis coordinate system for a Chinese Checkers board
Viewed 0 times
axissystemchinesecoordinateforboardcheckers
Problem
About a year ago, my brother and I came up with this cool idea for how to solve the problem of determining if any given "cell" on a Chinese Checkers board was adjacent to the cell in question.
Recently we started digging our teeth back into the idea and how we could work on variations of the game in which more than directly adjacent cells are of interest. I took up the task of figuring out how to adjust the grid system.
We'd previously developed some concept code in both C++ and C#, but decided for this project to work in C# (which I'm a little rusty in; been working with C and C++ mostly lately). To start, I merged the concepts of coordinates and direction into a more mathematical "vector" concept. However, it quickly became apparent that it is valuable to have a type-enforced way of separating unit vectors from vectors of non-unit magnitude.
For instance, suppose I want
That said, I like the idea of keeping the
I'm looking for suggestions on the cleanest, lightest (is that an oxymoron?), or in other words most preferred C# way of enforcing the use of "unit vectors". Additionally, I'm open to any comments about the interfaces publicly accessible outside of the "Grid" class as far as "good practice" for C# goes.
```
using System;
using System.Collections.Generic;
namespace Cs_Console_Test {
public class Program {
const string TableHeader = " X \t YL \t YR ";
static string TableRow(Grid.Vector vec) {
Recently we started digging our teeth back into the idea and how we could work on variations of the game in which more than directly adjacent cells are of interest. I took up the task of figuring out how to adjust the grid system.
We'd previously developed some concept code in both C++ and C#, but decided for this project to work in C# (which I'm a little rusty in; been working with C and C++ mostly lately). To start, I merged the concepts of coordinates and direction into a more mathematical "vector" concept. However, it quickly became apparent that it is valuable to have a type-enforced way of separating unit vectors from vectors of non-unit magnitude.
For instance, suppose I want
Grid.Cell.Neighbor to require a unit vector as a parameter. I could include conditional code, and either return null or throw exceptions, but that kind of overhead seems excessive for this simple task (not to mention forcing calling code to add potential run-time error checking for something that should be able to be validated at compile-time).That said, I like the idea of keeping the
Vector struct/class as simple as possible because it's not a terribly complex structure (and wouldn't warrant a polymorphic class in my typical C++ projects; but instead a simple typedef).I'm looking for suggestions on the cleanest, lightest (is that an oxymoron?), or in other words most preferred C# way of enforcing the use of "unit vectors". Additionally, I'm open to any comments about the interfaces publicly accessible outside of the "Grid" class as far as "good practice" for C# goes.
```
using System;
using System.Collections.Generic;
namespace Cs_Console_Test {
public class Program {
const string TableHeader = " X \t YL \t YR ";
static string TableRow(Grid.Vector vec) {
Solution
Only scratching the surface of
A struct should be immutable if possible. You have
and in addition if you need a comment for a variable/property then it should be named better.
Please don't declare and/or initialize multiple variables on the same line. This becomes just unreadable. Nevertheless this variables
Although I place simple properties which are having only
As a little side note, almost every C# developer would expect to see the opening brace
struct Vector A struct should be immutable if possible. You have
public setters public sbyte X { get; set; } // Horizontal Axis
public sbyte YL { get; set; } // Left-slanted Vertical Axis
public sbyte YR { get; set; } // Right-slanted Vertical Axisand in addition if you need a comment for a variable/property then it should be named better.
public static Vector? operator+(Vector lhs, Vector rhs) {
int x = lhs.X + rhs.X,
yl = lhs.YL + rhs.YL,
yr = lhs.YR + rhs.YR;
try {
Vector shifted = new Vector(
checked((sbyte)(lhs.X + rhs.X)),
checked((sbyte)(lhs.YL + rhs.YL)),
checked((sbyte)(lhs.YR + rhs.YR)));
return shifted;
} catch {
return null;
}
}Please don't declare and/or initialize multiple variables on the same line. This becomes just unreadable. Nevertheless this variables
x,yl and yr are never used so get rid of them. Although I place simple properties which are having only
get; and/or set; on the same line I wouldn't do it for public Vector(sbyte x, sbyte yl, sbyte yr) { X = x; YL = yl; YR = yr; } for the sake of readability. As a little side note, almost every C# developer would expect to see the opening brace
{ at a new line.Code Snippets
public sbyte X { get; set; } // Horizontal Axis
public sbyte YL { get; set; } // Left-slanted Vertical Axis
public sbyte YR { get; set; } // Right-slanted Vertical Axispublic static Vector? operator+(Vector lhs, Vector rhs) {
int x = lhs.X + rhs.X,
yl = lhs.YL + rhs.YL,
yr = lhs.YR + rhs.YR;
try {
Vector shifted = new Vector(
checked((sbyte)(lhs.X + rhs.X)),
checked((sbyte)(lhs.YL + rhs.YL)),
checked((sbyte)(lhs.YR + rhs.YR)));
return shifted;
} catch {
return null;
}
}Context
StackExchange Code Review Q#135960, answer score: 5
Revisions (0)
No revisions yet.