kulpot / STUDY.VS2022.CSharp.HeroMaker

C# avatar maker app 01 How to use checkboxes tutorial
1 stars 0 forks source link

C# Chess Board 04 program flow #5

Open kulpot opened 8 months ago

kulpot commented 8 months ago

######################### Program.cs #########################################

using ClassBoardModel; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

//-------------------- C# Chess Board 02 board cell classes ------------------------------- //ref link:https://www.youtube.com/watch?v=SFMVyiJ2S6g&list=PLhPyEFL5u-i0YDRW6FLMd1PavZp9RcYdF&index=3

// addNewItemToSolution:class library(name:ClassBoardModel), addNewClassTo:ClassBoardModel(name:Cell.cs/Board.cs),

//-------------------- C# Chess Board 03 next legal moves ------------------------------------------------- //ref link:https://www.youtube.com/watch?v=iV9hBTi-QB8&list=PLhPyEFL5u-i0YDRW6FLMd1PavZp9RcYdF&index=5&t=20s

//-------------------- C# Chess Board 04 program flow ------------------------------------------------- //ref link:https://www.youtube.com/watch?v=xc6C2I_wAxI&list=PLhPyEFL5u-i0YDRW6FLMd1PavZp9RcYdF&index=4

// Add ref to:C# Chess Board 04->Add->Reference...->Projects->ChessBoardModel,

namespace C__Chess_Board_02 { class Program { //--------START---------- C# Chess Board 04 program flow ------------------------------------------------- Board myBoard = new Board(8); //--------END---------- C# Chess Board 04 program flow -------------------------------------------------

    static void Main(string[] args)
    {
        //-------START----------- C# Chess Board 04 program flow -------------------------------------------------

        // show the empty chess board

        // ask the user for an x and y coordinate where we will place a piece

        // calculate all legal moves for that piece

        // print the chess board. Use an X for occupied square. Use a + for legal move. Use . for empty cell.

        // wait for another enter key press before ending the program.

        //--------END---------- C# Chess Board 04 program flow -------------------------------------------------

    }
}

}

kulpot commented 8 months ago

########################### Board.cs #########################################

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace ClassBoardModel { public class Board { //---------START--------- C# Chess Board 02 board cell classes ------------------------------- // the size of the board usually 8x8 public int Size { get; set; }

    // 2d array of type cell.
    public Cell[,] theGrid { get; set; }

    // constructor
    public Board(int s)
    {
        // initial size of the board is defined by parameter s.
        Size = s;

        // create a new 2D array of type cell.
        theGrid = new Cell[Size, Size];

        // fill the 2D array with new Cells, each with unique x and y coordinates.
        for (int i = 0; i < Size; i++)
        {
            for (int j = 0; j < Size; j++)
            {
                theGrid[i, j] = new Cell(i, j);
            }
        }
    }
    //---------END--------- C# Chess Board 02 board cell classes -------------------------------

    //-------START----------- C# Chess Board 03 next legal moves -------------------------------------------------
    public void MarkNextLegalMoves(Cell currentCell, string chessPiece)
    {
        // step 1 - clear all previous legal moves
        for (int i = 0; i < Size; i++)
        {
            for (int j = 0; j < Size; j++)
            {
                theGrid[i, j].LegalNextMove = false;
                theGrid[i, j].CurrentlyOccupied = false;
            }
        }

        // step 2 - find all legal moves and mark the cells as "legal"

        switch (chessPiece)
        {
            case "Knight":
                theGrid[currentCell.RowNumber + 2, currentCell.ColumnNumber + 1].LegalNextMove = true;
                theGrid[currentCell.RowNumber + 2, currentCell.ColumnNumber - 1].LegalNextMove = true;
                theGrid[currentCell.RowNumber - 2, currentCell.ColumnNumber + 1].LegalNextMove = true;
                theGrid[currentCell.RowNumber - 2, currentCell.ColumnNumber - 1].LegalNextMove = true;
                theGrid[currentCell.RowNumber + 1, currentCell.ColumnNumber + 2].LegalNextMove = true;
                theGrid[currentCell.RowNumber + 1, currentCell.ColumnNumber - 2].LegalNextMove = true;
                theGrid[currentCell.RowNumber - 1, currentCell.ColumnNumber + 2].LegalNextMove = true;
                theGrid[currentCell.RowNumber - 1, currentCell.ColumnNumber - 2].LegalNextMove = true;
                break;

            case "King":
                break;

            case "Rook":
                break;

            case "Bishop":
                break;

            case "Queen":
                break;

            default:
                break;
        }
    }
    //--------END---------- C# Chess Board 03 next legal moves -------------------------------------------------

}

}

kulpot commented 8 months ago

############################### Cell.cs ##############################################

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace ClassBoardModel { public class Cell { //---------START--------- C# Chess Board 02 board cell classes ------------------------------- public int RowNumber { get; set; } public int ColumnNumber { get; set; } public bool CurrentlyOccupied { get; set; } public bool LegalNextMove { get; set; }

    public Cell(int x, int y)
    {
        RowNumber = x;
        ColumnNumber = y;
    }
    //---------END--------- C# Chess Board 02 board cell classes -------------------------------

}

}