예제 #1
0
    public ConnectFourView(ConnectFourBoard myboard, int centerx = 300, int centery = 300, int targetwidth = 480)
    {
        _myboard = myboard;

        // prepare graphics:
        cell = new AnimationSprite[myboard._height, myboard._width];
        for (int row = 0; row < _myboard._height; row++)
        {
            for (int col = 0; col < _myboard._width; col++)
            {
                AnimationSprite newcell = new AnimationSprite("../../assets/tileset.png", 3, 1);
                newcell.x = col * newcell.width;
                newcell.y = row * newcell.width;
                AddChild(newcell);
                cell [row, col] = newcell;
            }
        }
        float realwidth = cell [0, 0].width * _myboard._width;

        scaleX = targetwidth / realwidth;
        scaleY = targetwidth / realwidth;
        x      = centerx - targetwidth / 2;
        y      = centery - targetwidth / 2;    // assuming #rows<=#columns

        // set callbacks:
        _myboard.OnCellChange += CellChangeHandler;

        _myboard.OnWin += WinHandler;

        wincells = new List <AnimationSprite> ();
    }
예제 #2
0
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");

        var positions = Enumerable.Range(1, ConnectFourBoard.CellAmount).Select(_ => (ConnectFourPiece)ConnectFourPiece.Empty).ToArray();
        var board     = new ConnectFourBoard(positions, (ConnectFourPiece)ConnectFourPiece.Black);

        var(human, computer) = SelectSide();

        var moveNumber = 0;

        while (board.GetLegalMoves().Any())
        {
            Console.Clear();
            Console.WriteLine($"step#{moveNumber}");
            Console.WriteLine(board.ToString());

            var location = board.GetTurn() == human
                ? DoHumanMove(board, human)
                : DoComputerMove(board);

            Console.WriteLine($"the move is to put {board.GetTurn()} into column {location}");

            moveNumber++;
            board = board.Move(location) as ConnectFourBoard;
        }

        PrintResult(moveNumber, board, human);
    }
        /// <summary>
        /// Creates a control that allows a user to play a game of Connect Four. This constructor sets the first player chip and the opponent type to specified values via
        /// the parameters.
        /// </summary>
        /// <param name="firstPlayerChip">The chip that represents the first player.</param>
        /// <param name="isOpponentComputer">States whether the opponent is a computer or not.</param>
        public ConnectFourContainer(Chip firstPlayerChip, bool isOpponentComputer)
        {
            Cursor         = Cursors.Default;
            DoubleBuffered = true;
            Font           = new Font("Arial", 18, FontStyle.Bold);
            GameBoard      = new ConnectFourBoard(7, 6, firstPlayerChip, isOpponentComputer);

            SubscribeToEvents();
            OnResize(null);
        }
예제 #4
0
 public ConnectFourBoard(ConnectFourBoard orig) : base(orig._width, orig._height)
 {
     for (int i = 0; i < _height; i++)
     {
         for (int j = 0; j < _width; j++)
         {
             board[i, j] = orig.board[i, j];
         }
     }
     activeplayer = orig.activeplayer;
     movesmade    = orig.movesmade;
     winchecked   = orig.winchecked;
     terminal     = orig.terminal;
 }
예제 #5
0
    private static int DoHumanMove(ConnectFourBoard board, ConnectFourPiece human)
    {
        var location       = -1;
        var legalMoves     = new HashSet <int>(board.GetLegalMoves());
        var availableMoves = string.Join(",", board.GetLegalMoves().Select(m => $"{m}"));

        while (!legalMoves.Contains(location))
        {
            Console.WriteLine($"please, enter where to put next {human} [{availableMoves}]");
            location = int.TryParse(new string(Console.ReadKey().KeyChar, 1), out var loc) ? loc : -1;
        }

        return(location);
    }
예제 #6
0
 private static void PrintResult(int moveNumber, ConnectFourBoard board, ConnectFourPiece human)
 {
     Console.Clear();
     Console.WriteLine($"After {moveNumber} steps");
     Console.WriteLine(board.ToString());
     if (board.IsWin())
     {
         var winnerName = board.GetTurn() == human ? "computer" : "human";
         Console.WriteLine($"{winnerName} wins!");
     }
     else
     {
         Console.WriteLine("It's a draw!");
     }
 }
예제 #7
0
    // --- UNITY CALLBACKS ---

    void Awake()
    {
        Instance   = this;
        boardSetup = FindObjectOfType <ConnectFourBoardSetup>();
    }
예제 #8
0
 private static int DoComputerMove(ConnectFourBoard board) => Solver <int> .FindBestMoveAlphaBeta(board, 6);