Untitled

mail@pastecode.io avatar
unknown
csharp
7 months ago
3.5 kB
1
Indexable
Never
namespace domino;

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {

        Game game = new Game();
    }
}

public class DominoPiece
{
    public int End1 { get; private set; }
    public int End2 { get; private set; }

    public DominoPiece(int end1, int end2)
    {
        End1 = end1;
        End2 = end2;
    }

    public void Flip()
    {
        int temp = End1;
        End1 = End2;
        End2 = temp;
    }
}


public class Bazar
{
    private List<DominoPiece> bazar;

    public Bazar()
    {
        bazar = new List<DominoPiece>();
        for (int i = 0; i <= 6; i++)
        {
            for (int j = i; j <= 6; j++)
            {
                bazar.Add(new DominoPiece(i, j));
            }
        }
    }

    public void Shuffle()
    {
        for (int i = bazar.Count() - 1; i >= 1; i--)
        {
            Random random = new Random();
            int j = random.Next(i + 1);
            var temp = bazar[j];
            bazar[j] = bazar[i];
            bazar[i] = temp;
        }
    }

    public List<DominoPiece> DealPieces(int numberOfPieces)
    {
        List<DominoPiece> dealtPieces = new List<DominoPiece>();
        for (int i = 0; i < numberOfPieces; i++)
        {
            if (bazar.Count > 0)
            {
                dealtPieces.Add(bazar[0]);
                bazar.RemoveAt(0);
            }
        }
        return dealtPieces;
    }
}


public class Player
{
    public List<DominoPiece> Hand { get; private set; }

    public Player()
    {
        Hand = new List<DominoPiece>();
    }

    public void AddPiece(DominoPiece piece)
    {
        Hand.Add(piece);
    }

    public bool CanPlayPiece(DominoPiece piece, List<DominoPiece> board)
    {
        if (!Hand.Contains(piece)) return false;

        if (board.Count == 0) return true;

        DominoPiece leftEndPiece = board.First();
        DominoPiece rightEndPiece = board.Last();

        return piece.End1 == leftEndPiece.End1 || piece.End1 == rightEndPiece.End2 ||
               piece.End2 == leftEndPiece.End1 || piece.End2 == rightEndPiece.End2;
    }

    public bool MakeMove(DominoPiece piece, bool isLeft, List<DominoPiece> board)
    {
        if (CanPlayPiece(piece, board))
        {
            Hand.Remove(piece);
            if (isLeft)
            {
                board.Insert(0, piece);
            }
            else
            {
                board.Add(piece);
            }
            return true;
        }
        return false;
    }
}


public class Game
{
    private List<Player> players;
    private Bazar dominoSet;
    private List<DominoPiece> board;
    private int currentPlayerIndex;

    public Game()
    {
        players = new List<Player> { new Player(), new Player() };
        dominoSet = new Bazar();
        board = new List<DominoPiece>();
        dominoSet.Shuffle();
        foreach (var player in players)
        {
            player.Hand.AddRange(dominoSet.DealPieces(6));
        }
        currentPlayerIndex = 0; 
    }

    public void MakeMove(DominoPiece piece, bool isLeft)
    {
        Player currentPlayer = players[currentPlayerIndex];
        if (currentPlayer.CanPlayPiece(piece, board) && currentPlayer.MakeMove(piece, isLeft, board))
        {
            currentPlayerIndex = (currentPlayerIndex + 1) % players.Count;
        }
        else
        {
            Console.WriteLine("Ход невозможен");
        }
    }
}
Leave a Comment