מיני פרויקט חלונות תרגיל 2 – משחק קלפים (מלחמה)

benny#C, NET.Leave a Comment

המחלקה Card


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

namespace dotNet5773_02_8802_8814
{
    enum E_color
    {
        red, black
    }

    class Card : IComparable
    {
        private E_color color;                //card color

        public E_color co                   // set get for color
        {
            set { color = value; }
            get { return color; }
        }

       

        private int number;

        public int num
        {
            get { return number; }
            set
            {
                if (value >= 2 && value <= 14) // the value is in a range
                    number = value;
                else
                    Console.Write("the number us not correct "); // the value is not in a range

            }
        }

        public Card()  // a default constructor
        {
            
        }

        public Card(int num, E_color c)  //constructor 
        {
            number = num;
            color = c;
        }

        public String CardName
        {

            get
            {

                int x = number;
                if (x >= 2 && x <= 10) 
                {
                    return x.ToString();

                }
                else    //if the card number is mor then 10 so...
                {
                    switch (x)
                    {
                        case 11:
                            return "Jack";

                        case 12:
                            return "Queen";

                        case 13:
                            return "King";

                        case 14:
                            return "Ace";

                    }
                    return null;
                }

            }
        }


        public override String ToString()   // function that return the card name & color
        {

            return "card: "+CardName + ". color: " + color;
        }


        public int CompareTo(object obj)   //if the tow card are even the function return 0
        {                                  //if first card biger  function return 1
                                           //if first card smaller  function return -1  
            return (num.CompareTo(((Card)obj).num));
        }



    }
}


המחלקה CardStock

using System;
using System.Collections.Generic;
using System.Collections;

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

namespace dotNet5773_02_8802_8814
{
    class CardStock : IEnumerable
    {

        public List<Card> Cards = new List<Card>();   //  list that contain Card elemant


        public CardStock()
        {
            for (int i = 2, j = 2; i < 15 && j < 15; i++, j++)
            {

                Card a = new Card(i, E_color.black);     // initialize the list with 13 black cards
                Card b = new Card(j, E_color.red);       // initialize the list with 13 red cards
                Cards.Add(a); Cards.Add(b);   // add the card to the card list

            }
        }

        public void mix(ref CardStock a)   // to mix the card list
        {

            Random r = new Random();

            for (int k = 0; k < 30; k++)
            {
                int i = r.Next(0, 25);  // i is a random number between 0 to 25
                int j = r.Next(0, 25);  // j is a random number between 0 to 25

                Card tmp = a.Cards[i];

                a.Cards[i] = a.Cards[j];   //mix the card index
                a.Cards[j] = tmp;
            }

        }

        public override String ToString()
        {
            String str = null;
            foreach (Card item in Cards)
                str += item.ToString() + "\n";   // [ item.ToString() --->  return CardName + " " + color;]
            return str;
        }

        public IEnumerator GetEnumerator()  // for IEnumerable
        {
            foreach (var item in Cards)
            {
                yield return Cards;
            }
        }


        public void distribute(params Player[] players)  // to split the card between the plyers (unknown numbr of players)
        {

            while (true)
            {
                foreach (Player item in players)
                {
                    if (Cards.Count() != 0)  // there is no more card to split
                    {
                        item.addCard(Cards[Cards.Count() - 1]); // add card to player
                        Cards.RemoveAt(Cards.Count() - 1); // remove the card from card list
                    }
                    else
                        return;
                }
            }
        }
      

        public void removeCard(Card c)
        {
            Cards.Remove(c);     // remove the card from list
        }

        public Card Indexer(string card)
        {
            int x;
            bool b = Int32.TryParse(card, out x);
            if (!b)
            {
                switch (card)
                {
                    case "Jack":
                        { x = 11; }
                        break;

                    case "Queen":
                        { x = 12; }
                        break;

                    case "King":
                        { x = 13; }
                        break;

                    case "Ace":
                        { x = 14; }
                        break;
                }
            }
            foreach (var item in Cards)
            {
                if (item.num == x)
                    return item;
            }
            return null;
        }

        public Card this[string cardName]   // indexer
        {
            get
            {
                for (int i = 0; i < Cards.Count; i++)
                {
                    if (Cards[i].CardName == cardName)
                        return Cards[i];
                }
                return null;
            }
        }

        public void Sort()     // sort function
        {
            Card minimum = new Card();
            int index;
            for (int i = 0; i < Cards.Count; i++)
            {
                minimum = Cards[i];
                index = i;
                for (int j = i; j < Cards.Count; j++)
                    if (Cards[j].CompareTo(minimum) > 0)
                    {
                        minimum = Cards[j];
                        index = j;
                    }
                Cards[index] = Cards[i];
                Cards[i] = minimum;
            }
        }
    }

}

המחלקה Player

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

namespace dotNet5773_02_8802_8814

{
    class Player : IEnumerable
    {
        private String name;

        public String playerName
        {
            get { return name; }
            set { name = value; }

        }

        private Queue<Card> pack = new Queue<Card>();  // player cards stord in  Queue

        public Queue<Card> Ppack
        {
            get { return pack; }
        }

        public IEnumerator GetEnumerator()
        {
            foreach (var item in pack)
            {
                yield return pack;
            }
        }

        public void addCard(params Card[] c)    // add a unknown number of cards
        {
            foreach (var item in c)
            {
                pack.Enqueue(item);
            }
        }

        public override String ToString()
        {
            int x = pack.Count();
            String str = name + " has " + x + " " + "cards\n\n";
            foreach (Card item in pack)
            {
                str += item.ToString() + "\n";
            }
            return str;
        }
        public bool lose()   // chack if player is losing
        {
            return this.pack.Count() == 0;    // if the count value is 0 --> the player is out of cards
        }


       // public Card pop()
       // {
         //   return this.pack.Dequeue();  // take card out
      //  }
    
    }
}

המחלקה Game

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

namespace dotNet5773_02_8802_8814
{
    class Game
    {

        private CardStock GamePack;

        public Player player1 = new Player();
        public Player player2 = new Player();



        public Game(string name1, string name2)
        {
            GamePack = new CardStock();
            player1.playerName = name1;
            player2.playerName = name2;
            this.GamePack.mix(ref GamePack);
            this.GamePack.distribute(player1, player2);


        }
        public String win()
        {
            if (this.player1.lose())
                return this.player2.playerName;
            if (this.player2.lose())
                return this.player1.playerName;
            else
                return null;

            //return winner.lose();
        }

        public bool endGame()
        {
            if (this.player1.lose() || this.player2.lose())  // if some player is out of card
                return true;
            else
                return false;

        }

        public override String ToString()
        {

            //string str = player1.playerName + " " + player1.Ppack.Count + " "  + player2.playerName + " " + player2.Ppack.Count ;
            string str = player1.ToString() +"\n\n"+ player2.ToString() ;
            return str;
        }

        public void step()
        {
            
            Card[] tmp = new Card[26];    // create temp array to put the cards if thay even
            int i = 0;
            int x;
            do
            {
               

                if (!endGame())    
                {
                    tmp[i++] = player1.Ppack.Dequeue();       // take the first card from player1 to temp array
                    tmp[i] = player2.Ppack.Dequeue();       // take the first card from player2 to temp array

                    x = tmp[i - 1].CompareTo(tmp[i]);     // compare the tow cards

                    if (x == 1)           // if the first one is bigger
                    {
                        int j = 0;
                        while (tmp[j] != null)  
                        {    
                            j++;   // to insert the card to player1 in a diffrent awy (from the end)
                        }           // it is more similar to reality (mor randomaly) game
                        while (j>0)
                        {
                            player1.Ppack.Enqueue(tmp[j-1]);
                            j--;
                        }
                    }
                    if (x == -1)            // if the first one is smaller
                    {
                        int j = 0;
                        while (tmp[j] != null)
                        {
                            player2.Ppack.Enqueue(tmp[j++]);   // to insert the card to player2 in a diffrent way from player1)
                        }    
                    }
                    if (x == 0)      // if the are even put again 2 cards
                        i++;
                    }
                else
                {
                  
                    break;
                }
              
            } while (x == 0&&tmp[25]==null);
            
        }


    }

}

תכנית ראשית


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

namespace dotNet5773_02_8802_8814
{
    class Program
    {
       
        static void Main(string[] args)
        {      
            Console.WriteLine("please enter the names of the players");
            string name1, name2;
            name1 = Console.ReadLine();
            name2 = Console.ReadLine();
            Game game = new Game(name1, name2);

            Console.WriteLine(game);   
         
            do
            {
                Console.WriteLine("to one step press Enter\nto runing the game press '0'");

                string input = Console.ReadLine();

                switch (input)
                {
                    case "0":   
                        {
                            while (!game.endGame())
                            {
                                game.step();
                                Console.WriteLine(game);
                            }
                        }
                        
                        break;
                    case "":
                        {
                            game.step();
                            Console.WriteLine(game);
                        }
                        break;

                    
                }
            }
            while (!game.endGame());

            Console.WriteLine("game over\n");
            Console.WriteLine("the winner is: " + game.win()+"\n\n\n\nprees any key to exit");
            Console.ReadKey();



        }
    }
}

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *