Jump to content

so i am making a chess game but i need the knight(Paard in dutch), to move correctly. ill post my code below

and as always thnx in advance

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace opgave1{    class Program    {        static void Main(string[] args)        {            Schaakstuk[,] schaakbord = new Schaakstuk[8, 8];            InitSchaakbord(schaakbord);            ToonSchaakbord(schaakbord);            Positie van;            Positie naar;                        while (true)            {                try                {                    Console.Write("geef van positie: ");                    string vanPositie = Console.ReadLine();                    Console.WriteLine();                    if (!StringToPosition(vanPositie, out van))                    {                        Console.WriteLine("Deze positie bestaat niet");                        Console.WriteLine();                        continue;                    }                    Console.Write("geef naar positie: ");                    string naarPositie = Console.ReadLine();                    Console.WriteLine();                    if (!StringToPosition(naarPositie, out naar))                    {                        Console.WriteLine("Deze positie bestaat niet");                        Console.WriteLine();                        continue;                    }                                       Console.WriteLine("Er staat een schaakstuk op van-positie:" + schaakbord[van.rij, van.kolom].type);                    Console.WriteLine();                    MaakSprong(schaakbord, van, naar);                    GeldigePaardZet(van, naar);                    ToonSchaakbord(schaakbord);                }                catch                {                    Console.WriteLine("Geen geldige waarde");                }            }        }        public static bool GeldigePaardZet(Positie van, Positie naar)        {            if (true)            {                //TODO ######################################################################################################################                //TODO ######################################################################################################################                // this is where i have to say in the if statement when the horse is allowed to move.            }        }        static bool MaakSprong(Schaakstuk[,] schaakbord, Positie van, Positie naar)        {            schaakbord[naar.rij, naar.kolom] = schaakbord[van.rij, van.kolom];            schaakbord[van.rij, van.kolom] = null;            return true;        }        static bool StringToPosition(string txt, out Positie pos)        {            pos.kolom = txt[0] - 'A';            pos.rij = Int32.Parse(txt[1].ToString()) - 1; if (pos.rij < 8 && pos.kolom < 8 )                {                    return true;                }                else { return false; }                    }        static void InitSchaakbord(Schaakstuk[,] schaakbord)        {            // maak schaakbord leeg            for (int r = 0; r < schaakbord.GetLength(0); r++)            {                for (int k = 0; k < schaakbord.GetLength(1); k++)                {                    schaakbord[r, k] = null;                }            }            // plaats witte schaakstukken ('bovenaan')            schaakbord[0, 0] = new Schaakstuk(SchaakstukType.Toren, SchaakstukKleur.Wit);            schaakbord[0, 1] = new Schaakstuk(SchaakstukType.Paard, SchaakstukKleur.Wit);            schaakbord[0, 2] = new Schaakstuk(SchaakstukType.Loper, SchaakstukKleur.Wit);            schaakbord[0, 3] = new Schaakstuk(SchaakstukType.Koning, SchaakstukKleur.Wit);            schaakbord[0, 4] = new Schaakstuk(SchaakstukType.Koningin, SchaakstukKleur.Wit);            schaakbord[0, 5] = new Schaakstuk(SchaakstukType.Loper, SchaakstukKleur.Wit);            schaakbord[0, 6] = new Schaakstuk(SchaakstukType.Paard, SchaakstukKleur.Wit);            schaakbord[0, 7] = new Schaakstuk(SchaakstukType.Toren, SchaakstukKleur.Wit);            schaakbord[1, 0] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Wit);            schaakbord[1, 1] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Wit);            schaakbord[1, 2] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Wit);            schaakbord[1, 3] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Wit);            schaakbord[1, 4] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Wit);            schaakbord[1, 5] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Wit);            schaakbord[1, 6] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Wit);            schaakbord[1, 7] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Wit);            // plaats zwarte schaakstukken ('onderaan')            schaakbord[7, 0] = new Schaakstuk(SchaakstukType.Toren, SchaakstukKleur.Zwart);            schaakbord[7, 1] = new Schaakstuk(SchaakstukType.Paard, SchaakstukKleur.Zwart);            schaakbord[7, 2] = new Schaakstuk(SchaakstukType.Loper, SchaakstukKleur.Zwart);            schaakbord[7, 3] = new Schaakstuk(SchaakstukType.Koning, SchaakstukKleur.Zwart);            schaakbord[7, 4] = new Schaakstuk(SchaakstukType.Koningin, SchaakstukKleur.Zwart);            schaakbord[7, 5] = new Schaakstuk(SchaakstukType.Loper, SchaakstukKleur.Zwart);            schaakbord[7, 6] = new Schaakstuk(SchaakstukType.Paard, SchaakstukKleur.Zwart);            schaakbord[7, 7] = new Schaakstuk(SchaakstukType.Toren, SchaakstukKleur.Zwart);            schaakbord[6, 0] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Zwart);            schaakbord[6, 1] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Zwart);            schaakbord[6, 2] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Zwart);            schaakbord[6, 3] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Zwart);            schaakbord[6, 4] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Zwart);            schaakbord[6, 5] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Zwart);            schaakbord[6, 6] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Zwart);            schaakbord[6, 7] = new Schaakstuk(SchaakstukType.Pion, SchaakstukKleur.Zwart);        }        static void ToonSchaakbord(Schaakstuk[,] schaakbord)        {            // save colors            ConsoleColor backgroundBackup = Console.BackgroundColor;            ConsoleColor foregroundBackup = Console.ForegroundColor;            // print letters bovenaan schaakbord            Console.WriteLine("   A  B  C  D  E  F  G  H ");            for (int r = 0; r < schaakbord.GetLength(0); r++)            {                Console.BackgroundColor = ConsoleColor.Black;                Console.ForegroundColor = ConsoleColor.Gray;                Console.Write("{0}", r + 1);                for (int k = 0; k < schaakbord.GetLength(1); k++)                {                    // toggle background color                    if ((r + k) % 2 == 0)                        Console.BackgroundColor = ConsoleColor.DarkYellow;                    else                        Console.BackgroundColor = ConsoleColor.Gray;                    Schaakstuk schaakstuk = schaakbord[r, k];                    if (schaakstuk == null)                    {                        Console.Write("   ");                        continue;                    }                    // stel de fontkleur in, afhankelijk van de kleur van het schaakstuk                    if (schaakstuk.kleur == SchaakstukKleur.Wit)                        Console.ForegroundColor = ConsoleColor.White;                    else if (schaakstuk.kleur == SchaakstukKleur.Zwart)                        Console.ForegroundColor = ConsoleColor.Black;                    else                        Console.ForegroundColor = ConsoleColor.Cyan;                    // print het schaakstuk                    switch (schaakstuk.type)                    { case SchaakstukType.Pion:                            Console.Write(" p ");                            break;                        case SchaakstukType.Toren:                            Console.Write(" T ");                            break;                        case SchaakstukType.Paard:                            Console.Write(" P ");                            break;                        case SchaakstukType.Loper:                            Console.Write(" L ");                            break;                        case SchaakstukType.Koning:                            Console.Write(" K ");                            break;                        case SchaakstukType.Koningin:                            Console.Write(" Q ");                            break;                    }                }                Console.WriteLine();                         }            Console.BackgroundColor = backgroundBackup;            Console.ForegroundColor = foregroundBackup;                               }       }}
Link to comment
https://linustechtips.com/topic/460095-chess-game-c/
Share on other sites

Link to post
Share on other sites

many # such wow

 

 

 

 

 

 

 

 

 

 

 

sorry cant help ya

We've now got three different subjects going on, an Asian fox and motorbike fetish, two guys talking about Norway invasions and then some other people talking about body building... This thread is turning into a free for all fetish infested Norwegian circle jerk.

Link to comment
https://linustechtips.com/topic/460095-chess-game-c/#findComment-6174688
Share on other sites

Link to post
Share on other sites

public static bool GeldigePaardZet(Positie van, Positie naar){	int rowDiff = Math.Abs(van.rij-naar.rij);     // Get the absolute row movement 	int colDiff = Math.Abs(van.kolom-naar.kolom); // Get the absolute column movement 		// The Knight needs to move 2 steps in either column or row plus 1 step in the other		return (rowDiff == 1 && colDiff == 2) || (rowDiff == 2 && colDiff == 1);}

The code assumes that the both positions are valid and within the board it self.

 

Also a tip: I saw you were using Int32.Parse() try to avoid that one stick to something similar to:

int myInt;string myText = "";if (!int.TryParse(myText, out myInt))   myInt = 0; // or what you want the value to be when the text isn't a valid number
Link to comment
https://linustechtips.com/topic/460095-chess-game-c/#findComment-6176640
Share on other sites

Link to post
Share on other sites

Also a tip: I saw you were using Int32.Parse() try to avoid that one

 

Just to expand and give some explanation as to why you would want to use TryParse over Parse in most situations.

 

TryParse and Parse are almost identical in how they are implemented. The main difference is that TryParse uses booleans instead of throwing exceptions which gives it a slight performance improvement when handling invalid data. While I can't say for sure, there's probably no real difference when they succeed.

 

With that said, there's a couple situations where you might choose Parse over TryParse

  • If you know for sure the string is the required type (this basically only comes down to syntax preference)
  • If you need to handle the specific exceptions (OverflowException, FormatException, etc)
Link to comment
https://linustechtips.com/topic/460095-chess-game-c/#findComment-6177757
Share on other sites

Link to post
Share on other sites

You might want to think about your design a bit more.

 

For starters:

  • Get rid of the statics
  • Ditch the huge hard coded array
  • Loose the horrible switch statements and branching logic
  • Break things up into classes
  • Think about single responsibility

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/460095-chess-game-c/#findComment-6178189
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×