這裏的事情的清單給你做:
- 請你幫個忙:理解代碼,而不是簡單地複製/粘貼
- 實現對角線檢查
- 調整有效性謂詞以適合您的需求
- 修復所有潛在的錯誤
- 如果您可以將其延長到每列/行/對角線有效性的報告積分
- 測試,測試,測試! (僅測試的基本情況)
- 拋出一個黨:)
顏色
public enum CoinColor
{
Red,
Green,
Blue
}
信
public enum CoinLetter
{
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z
}
硬幣
public sealed class Coin
{
internal Coin(CoinColor color, CoinLetter letter)
{
Color = color;
Letter = letter;
}
public CoinColor Color { get; }
public CoinLetter Letter { get; }
private bool Equals(Coin other)
{
return Color == other.Color && Letter == other.Letter;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Coin) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((int) Color * 397)^(int) Letter;
}
}
public static bool operator ==(Coin left, Coin right)
{
return Equals(left, right);
}
public static bool operator !=(Coin left, Coin right)
{
return !Equals(left, right);
}
public override string ToString()
{
return $"{nameof(Color)}: {Color}, {nameof(Letter)}: {Letter}";
}
}
助手
public static class Coins
{
public static Coin[][] All => new[]
{
AllRed,
AllGreen,
AllBlue
};
public static Coin[] AllRed { get; } =
{
RedS, RedD, RedA, RedC, RedY, RedU, RedL, RedJ, RedO
};
public static Coin[] AllGreen { get; } =
{
GreenG, GreenX, GreenB, GreenH, GreenE, GreenZ, GreenW, GreenP, GreenQ, GreenR, GreenN, GreenI
};
public static Coin[] AllBlue { get; } =
{
BlueF, BlueT, BlueV, BlueM, BlueK
};
public static Coin RedA => new Coin(CoinColor.Red, CoinLetter.A);
public static Coin RedC => new Coin(CoinColor.Red, CoinLetter.C);
public static Coin RedD => new Coin(CoinColor.Red, CoinLetter.D);
public static Coin RedJ => new Coin(CoinColor.Red, CoinLetter.J);
public static Coin RedL => new Coin(CoinColor.Red, CoinLetter.L);
public static Coin RedO => new Coin(CoinColor.Red, CoinLetter.O);
public static Coin RedS => new Coin(CoinColor.Red, CoinLetter.S);
public static Coin RedU => new Coin(CoinColor.Red, CoinLetter.U);
public static Coin RedY => new Coin(CoinColor.Red, CoinLetter.Y);
public static Coin GreenB => new Coin(CoinColor.Green, CoinLetter.B);
public static Coin GreenE => new Coin(CoinColor.Green, CoinLetter.E);
public static Coin GreenG => new Coin(CoinColor.Green, CoinLetter.G);
public static Coin GreenH => new Coin(CoinColor.Green, CoinLetter.H);
public static Coin GreenI => new Coin(CoinColor.Green, CoinLetter.I);
public static Coin GreenN => new Coin(CoinColor.Green, CoinLetter.N);
public static Coin GreenP => new Coin(CoinColor.Green, CoinLetter.P);
public static Coin GreenQ => new Coin(CoinColor.Green, CoinLetter.Q);
public static Coin GreenR => new Coin(CoinColor.Green, CoinLetter.R);
public static Coin GreenW => new Coin(CoinColor.Green, CoinLetter.W);
public static Coin GreenX => new Coin(CoinColor.Green, CoinLetter.X);
public static Coin GreenZ => new Coin(CoinColor.Green, CoinLetter.Z);
public static Coin BlueF => new Coin(CoinColor.Blue, CoinLetter.F);
public static Coin BlueK => new Coin(CoinColor.Blue, CoinLetter.K);
public static Coin BlueM => new Coin(CoinColor.Blue, CoinLetter.M);
public static Coin BlueT => new Coin(CoinColor.Blue, CoinLetter.T);
public static Coin BlueV => new Coin(CoinColor.Blue, CoinLetter.V);
}
遊戲
public class Game
{
public Game() : this(new Coin[GridWidth, GridHeight])
{
}
public Game(Coin[,] grid)
{
if (grid == null)
throw new ArgumentNullException(nameof(grid));
if (grid.GetUpperBound(0) != GridWidth - 1)
throw new ArgumentOutOfRangeException(nameof(grid));
if (grid.GetUpperBound(1) != GridHeight - 1)
throw new ArgumentOutOfRangeException(nameof(grid));
Grid = grid;
}
private static int GridWidth { get; } = 3;
private static int GridHeight { get; } = 3;
private static int GridExtent => 3;
private Coin[,] Grid { get; }
public Coin GetCoin(int col, int row)
{
Check(col, row);
var coin = Grid[col, row];
return coin;
}
public void SetCoin(Coin coin, int col, int row)
{
Check(col, row);
Grid[col, row] = coin;
}
public bool Check()
{
for (var col = 0; col < GridWidth; col++)
if (CheckCol(col))
return true;
for (var row = 0; row < GridHeight; row++)
if (CheckRow(row))
return true;
return false;
}
private bool CheckCol(int col)
{
CheckColIndex(col);
var coins = new Coin[GridHeight];
for (var row = 0; row < GridHeight; row++)
coins[row] = GetCoin(col, row);
var check = Check(coins);
return check;
}
private bool CheckRow(int row)
{
CheckRowIndex(row);
var coins = new Coin[GridWidth];
for (var column = 0; column < GridWidth; column++)
coins[column] = GetCoin(column, row);
var check = Check(coins);
return check;
}
private bool Check(Coin[] coins)
{
if (coins == null)
throw new ArgumentNullException(nameof(coins));
var any = Coins.All.Any(s => s.Where(coins.Contains).Distinct().Count() == GridExtent);
return any;
}
private static void Check(int col, int row)
{
CheckColIndex(col);
CheckRowIndex(row);
}
private static void CheckColIndex(int col)
{
if (col < 0 || col >= GridWidth)
throw new ArgumentOutOfRangeException(nameof(col));
}
private static void CheckRowIndex(int row)
{
if (row < 0 || row >= GridHeight)
throw new ArgumentOutOfRangeException(nameof(row));
}
}
測試
internal class Test
{
public Test()
{
var allGood = new[,]
{
{Coins.RedS, Coins.RedD, Coins.RedA},
{Coins.GreenG, Coins.GreenX, Coins.GreenB},
{Coins.BlueF, Coins.BlueT, Coins.BlueV}
};
var allFalse = new[,]
{
{Coins.RedS, Coins.RedS, Coins.RedA},
{Coins.GreenG, Coins.GreenG, Coins.GreenB},
{Coins.BlueF, Coins.BlueF, Coins.BlueV}
};
foreach (var grid in new[] {allGood, allFalse})
{
var game = new Game(grid);
var check = game.Check();
Console.WriteLine(check);
}
}
}
這聽起來很像功課問題....所以,你有什麼嘗試? Stackoverlow不是一個代碼寫入服務。 – mituw16
@ mituw16好吧,我只是不知道如何開始,我不是要求完整的解決方案。 –
如果你想讓別人爲你寫出解決方案,你必須將其標記爲代碼高爾夫球;) –