mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 15:50:20 -08:00
Add move logic
This commit is contained in:
@@ -2,17 +2,43 @@ namespace OneCheck;
|
||||
|
||||
internal class Board
|
||||
{
|
||||
private readonly int[][] _checkers;
|
||||
private readonly bool[][] _checkers;
|
||||
private int _count;
|
||||
|
||||
public Board()
|
||||
{
|
||||
_checkers =
|
||||
Enumerable.Range(0, 8)
|
||||
.Select(r => Enumerable.Range(0, 8)
|
||||
.Select(c => r > 1 && r < 6 && c > 1 && c < 6 ? 0 : 1).ToArray())
|
||||
.Select(c => r <= 1 || r >= 6 || c <= 1 || c >= 6).ToArray())
|
||||
.ToArray();
|
||||
_count = 48;
|
||||
}
|
||||
|
||||
private bool this[int index]
|
||||
{
|
||||
get => _checkers[(index - 1) / 8][(index-1) % 8];
|
||||
set => _checkers[(index - 1) / 8][(index-1) % 8] = value;
|
||||
}
|
||||
|
||||
public int Count => _count;
|
||||
|
||||
public bool TryMove(Move move)
|
||||
{
|
||||
if (move.IsInRange && move.IsTwoSpacesDiagonally && IsPieceJumpingPieceToEmptySpace(move))
|
||||
{
|
||||
this[move.From] = false;
|
||||
this[move.Jumped] = false;
|
||||
this[move.To] = true;
|
||||
_count--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool IsPieceJumpingPieceToEmptySpace(Move move) => this[move.From] && this[move.Jumped] && !this[move.To];
|
||||
|
||||
public override string ToString() =>
|
||||
string.Join(Environment.NewLine, _checkers.Select(r => string.Join(" ", r.Select(c => $" {c}"))));
|
||||
}
|
||||
string.Join(Environment.NewLine, _checkers.Select(r => string.Join(" ", r.Select(c => c ? " 1" : " 0"))));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ internal class Game
|
||||
{
|
||||
private readonly IReadWrite _io;
|
||||
private readonly Board _board;
|
||||
private int _moveCount;
|
||||
|
||||
public Game(IReadWrite io)
|
||||
{
|
||||
@@ -15,6 +16,31 @@ internal class Game
|
||||
{
|
||||
_io.Write(Streams.Introduction);
|
||||
|
||||
_io.WriteLine(_board);
|
||||
do
|
||||
{
|
||||
_io.WriteLine(_board);
|
||||
_io.WriteLine();
|
||||
} while (PlayMove());
|
||||
|
||||
_io.WriteLine(Formats.Results, _moveCount, _board.Count);
|
||||
}
|
||||
|
||||
private bool PlayMove()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var from = (int)_io.ReadNumber(Prompts.From);
|
||||
if (from == 0) { return false; }
|
||||
|
||||
var move = new Move { From = from, To = (int)_io.ReadNumber(Prompts.To) };
|
||||
|
||||
if (_board.TryMove(move))
|
||||
{
|
||||
_moveCount++;
|
||||
return true;
|
||||
}
|
||||
|
||||
_io.Write(Streams.IllegalMove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
13
67_One_Check/csharp/Move.cs
Normal file
13
67_One_Check/csharp/Move.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace OneCheck;
|
||||
|
||||
internal class Move
|
||||
{
|
||||
public int From { get; init; }
|
||||
public int To { get; init; }
|
||||
public int Jumped => (From + To) / 2;
|
||||
|
||||
public bool IsInRange => From >= 1 && From <= 64 && To >= 1 && To <= 64;
|
||||
public bool IsTwoSpacesDiagonally => RowDelta == 2 && ColumnDelta == 2;
|
||||
private int RowDelta => Math.Abs(From / 8 - To / 8);
|
||||
private int ColumnDelta => Math.Abs(From % 8 - To % 8);
|
||||
}
|
||||
Reference in New Issue
Block a user