Advertisement
AssoAndrea

memo2

Dec 5th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Aiv.Draw;
  7.  
  8. namespace MemoryGame
  9. {
  10.     enum Color { Rosso, Verde, Giallo, Blu, Viola, Bianco, Marrone, Celeste, Arancione, VerdeOliva, RosaScuro, Oro, last }
  11.  
  12.     class Program
  13.     {
  14.         struct CardColor
  15.         {
  16.             public byte R;
  17.             public byte G;
  18.             public byte B;
  19.         }
  20.         struct Card
  21.         {
  22.             public int X;
  23.             public int Y;
  24.             public float Width;
  25.             public int Height;
  26.             public int LeftPadding;
  27.             public int UpPadding;
  28.             public CardColor color;
  29.             public CardColor cover1;
  30.             public CardColor cover2;
  31.         }
  32.         static void Main(string[] args)
  33.         {
  34.             int row;
  35.             int col;
  36.             int leftPadding = 20;
  37.             int upPadding = 20;
  38.             int cardWidth = 70;
  39.             int cardHeight = 100;
  40.             int windowWidth = 1000;
  41.             int windowHeight = 1000;
  42.             int maxRow = (windowWidth / (cardHeight + upPadding));
  43.             int maxCol = (windowHeight / (cardWidth + leftPadding));
  44.             Console.WriteLine("Righe massime: " + maxRow);
  45.             Console.WriteLine("Colonne massime: " + maxCol);
  46.  
  47.             AskRowCol(out row, out col, maxRow, maxCol);
  48.             Window window = new Window(windowWidth, windowHeight, "Memory", PixelFormat.RGB);
  49.             Card[] deck = new Card[row * col];
  50.             CardColor[] color = new CardColor[(int)Color.last];
  51.             for (int i = 0; i < (int)Color.last; i++)
  52.             {
  53.                 CreateColor(color, (Color)i);
  54.             }
  55.             CreateDeck(window, deck, leftPadding, upPadding, col, row, cardWidth, cardHeight, color);
  56.             Card[] coveredDeck = new Card[deck.Length + 2];
  57.             CopyDeck(coveredDeck, deck);
  58.             int mouseX1 = -1;
  59.             int mouseY1 = -1;
  60.             bool firstClick = true;
  61.  
  62.             int index1 = -1;
  63.             int saveindex1 = -1;
  64.             int saveindex2 = -1;
  65.             int uncoverCards = 0;
  66.             int cardSelected = 0;
  67.             float resetTime = 80;
  68.             int[] uncoverIndex = new int[deck.Length + 2];
  69.             bool swap = true;
  70.             float time = 50;
  71.             bool reset = false;
  72.             bool fine = false;
  73.             float uncoverSpeed = 250;
  74.             int coverTime = 0;
  75.             bool completed = false;
  76.             bool uncover=false;
  77.             bool uncover2 = false;
  78.             bool waitAnimation = false;
  79.             bool swapColor = false;
  80.  
  81.             while (window.opened)
  82.             {
  83.                 Clear(window);
  84.                 DrawCoverDeck(window, coveredDeck);
  85.                 SelectTwoCards(window, deck, coveredDeck, ref mouseX1, ref mouseY1, ref firstClick, ref index1, ref saveindex1, ref saveindex2, ref cardSelected, ref swap, completed, ref uncover, ref uncover2, ref waitAnimation,ref cardWidth, uncoverSpeed, ref uncover2);
  86.                 Animation(cardWidth, window, coveredDeck, saveindex1, uncoverSpeed, ref swapColor, ref uncover, ref uncover2,ref completed);
  87.  
  88.                 if (CompareCard(window, deck, saveindex1, saveindex2, uncoverIndex, ref uncoverCards) && cardSelected == 2)
  89.                 {
  90.                     swap = true;
  91.                     cardSelected = 0;
  92.                     saveindex1 = -1;
  93.                     saveindex2 = -1;
  94.                     index1 = -1;
  95.                 }
  96.                 else if (cardSelected >= 2)
  97.                 {
  98.                     swap = true;
  99.                     reset = true;
  100.                     Reset(coveredDeck, deck, ref index1, ref saveindex1, ref saveindex2, ref cardSelected, ref time, ref reset);
  101.                 }
  102.  
  103.                 WaitTime(resetTime, window, reset, ref time);
  104.                 if (uncoverCards >= deck.Length)
  105.                 {
  106.                     DrawDeck(window, deck);
  107.                     if (fine)
  108.                     {
  109.                         Console.WriteLine("Hai vinto");
  110.                         break;
  111.                     }
  112.                     fine = true;
  113.                 }
  114.  
  115.                 Console.WriteLine(coveredDeck[1].Width);
  116.                 window.Blit();
  117.             }
  118.             Console.ReadLine();
  119.         }
  120.  
  121.         private static void Animation(int cardWidth, Window window, Card[] coveredDeck, int saveindex1, float uncoverSpeed, ref bool swapColor, ref bool uncover, ref bool uncover2, ref bool completed)
  122.         {
  123.             if (uncover && saveindex1 >= 0)
  124.             {
  125.                 coveredDeck[saveindex1].Width -= uncoverSpeed * window.deltaTime;
  126.                 if (coveredDeck[saveindex1].Width <= 0)
  127.                 {
  128.                     swapColor = true;
  129.                     uncover = false;
  130.                     uncover2 = true;
  131.                 }
  132.  
  133.             }
  134.             if (uncover2)
  135.             {
  136.                 Console.WriteLine(saveindex1);
  137.                 coveredDeck[saveindex1].Width += uncoverSpeed * window.deltaTime;
  138.                 if (coveredDeck[saveindex1].Width >= cardWidth)
  139.                 {
  140.                     swapColor = false;
  141.                     uncover2 = false;
  142.                     completed = true;
  143.                 }
  144.             }
  145.         }
  146.  
  147.         static void DrawCoverDeck(Window window, Card[] deck)
  148.         {
  149.             for (int i = 0; i < deck.Length; i++)
  150.             {
  151.                 DrawCoverRect(window, deck[i]);
  152.             }
  153.         }
  154.  
  155.         static void DrawCoverRect(Window window, Card card)
  156.         {
  157.             bool swap = true;
  158.             int c = 0;
  159.             for (int i = card.Y; i < card.Y + card.Height; i++)
  160.             {
  161.                 if (swap)
  162.                 {
  163.                     DrawHorizontalLine(window, card.X, i, (int)card.Width,card.cover1);
  164.                 }
  165.                 else
  166.                 {
  167.                     DrawHorizontalLine(window, card.X, i, (int)card.Width, card.cover2);
  168.  
  169.                 }
  170.                 c++;
  171.                 if (c>=6)
  172.                 {
  173.                     c = 0;
  174.                     swap = !swap;
  175.                 }
  176.             }
  177.         }
  178.  
  179.         static void Reset(Card[] coveredDeck,Card[] deck, ref int index1, ref int saveindex1, ref int saveindex2, ref int cardSelected, ref float time, ref bool reset)
  180.         {
  181.             if (time >= 100 && saveindex1 >= 0 && saveindex2 >= 0)
  182.             {
  183.                 coveredDeck[saveindex1].cover1 = deck[saveindex2].cover1;
  184.                 coveredDeck[saveindex1].cover2 = deck[saveindex1].cover2;
  185.                 coveredDeck[saveindex2].cover1 = deck[saveindex2].cover1;
  186.                 coveredDeck[saveindex2].cover2 = deck[saveindex2].cover2;
  187.                 cardSelected = 0;
  188.                 saveindex1 = -1;
  189.                 saveindex2 = -2;
  190.                 index1 = -3;
  191.                 time = 0;
  192.                 reset = false;
  193.             }
  194.         }
  195.  
  196.         static void SelectTwoCards(Window window, Card[] deck, Card[] coveredDeck, ref int mouseX1, ref int mouseY1, ref bool firstClick, ref int index1, ref int saveindex1, ref int saveindex2, ref int uncoverCards, ref bool swap,bool swapColor,ref bool uncover, ref bool coverAnimation, ref bool waitAnimation,
  197.             ref int cardWidth, float uncoverSpeed, ref bool uncover2)
  198.         {
  199.             bool completed = false;
  200.             if (uncoverCards < 2)
  201.             {
  202.                 if (UncoverFirstCard(window, ref mouseX1, ref mouseY1, ref firstClick, ref index1, deck, ref uncoverCards)|| waitAnimation)
  203.                 {
  204.                     waitAnimation = true;
  205.                     if (swap)
  206.                     {
  207.                         uncover = true;
  208.                         saveindex1 = index1;
  209.  
  210.                         if (swapColor)
  211.                         {
  212.                             coveredDeck[saveindex1].cover1 = deck[saveindex1].color;
  213.                             coveredDeck[saveindex1].cover2 = deck[saveindex1].color;
  214.                         }
  215.                         if(completed)
  216.                         {
  217.  
  218.                             swap = false;
  219.                             waitAnimation = false;
  220.                         }
  221.                        
  222.                        
  223.  
  224.                     }
  225.                     else
  226.                     {
  227.                         uncover = true;
  228.                         saveindex2 = index1;
  229.                         //Animation(cardWidth, window, coveredDeck, saveindex2, uncoverSpeed, ref completed, ref uncover, ref uncover2);
  230.  
  231.                         if (swapColor)
  232.                         {
  233.                             coveredDeck[saveindex2].cover1 = deck[saveindex2].color;
  234.                             coveredDeck[saveindex2].cover2 = deck[saveindex2].color;
  235.                         }
  236.                     }
  237.                 }
  238.             }
  239.  
  240.         }
  241.  
  242.  
  243.         static bool UncoverFirstCard(Window window, ref int mouseX1, ref int mouseY1, ref bool firstClick, ref int index1, Card[] deck, ref int uncoverCards)
  244.         {
  245.  
  246.             if (Click(window, ref mouseX1, ref mouseY1, ref firstClick))
  247.             {
  248.                 if (CheckClick(ref index1, mouseX1, mouseY1, deck))
  249.                 {
  250.                     uncoverCards++;
  251.                     return true;
  252.                 }
  253.             }
  254.             return false;
  255.         }
  256.  
  257.         static void DrawUncoverCard(Window window, int[] uncoverIndex, int uncoverCard, Card[] deck)
  258.         {
  259.             for (int i = 0; i < uncoverCard; i++)
  260.             {
  261.                 DrawRect(window, deck[i]);
  262.             }
  263.         }
  264.  
  265.         static bool CompareCard(Window window, Card[] deck, int index1, int index2, int[] uncoverIndex, ref int uncoverCards)
  266.         {
  267.             if (CompareColor(deck, index1, index2))
  268.             {
  269.                 uncoverCards += 2;
  270.                 uncoverIndex[uncoverCards] = index1;
  271.                 uncoverIndex[uncoverCards + 1] = index2;
  272.                 return true;
  273.             }
  274.             else
  275.             {
  276.                 return false;
  277.             }
  278.         }
  279.  
  280.         static void WaitTime(float resetTime, Window window, bool reset, ref float time)
  281.         {
  282.             if (reset)
  283.             {
  284.                 time += resetTime * window.deltaTime;
  285.             }
  286.         }
  287.  
  288.         static bool CompareColor(Card[] deck, int index1, int index2)
  289.         {
  290.             if (index1 >= 0 && index2 >= 0)
  291.             {
  292.                 if ((deck[index1].color.R == deck[index2].color.R) && (deck[index1].color.G == deck[index2].color.G) && (deck[index1].color.B == deck[index2].color.B))
  293.                 {
  294.                     return true;
  295.                 }
  296.             }
  297.  
  298.             return false;
  299.         }
  300.  
  301.         static bool CheckClick(ref int index, int mouseX, int mouseY, Card[] deck)
  302.         {
  303.             for (int i = 0; i < deck.Length; i++)
  304.             {
  305.                 if ((mouseX >= deck[i].X && mouseX <= deck[i].X + deck[i].Width) && (mouseY >= deck[i].Y && mouseY <= deck[i].Y + deck[i].Height))
  306.                 {
  307.                     index = i;
  308.                     return true;
  309.                 }
  310.             }
  311.             return false;
  312.         }
  313.  
  314.         static bool Click(Window window, ref int mouseX, ref int mouseY, ref bool firstClick)
  315.         {
  316.             if (window.mouseLeft && firstClick)
  317.             {
  318.                 mouseX = window.mouseX;
  319.                 mouseY = window.mouseY;
  320.                 firstClick = false;
  321.                 return true;
  322.             }
  323.             else
  324.             {
  325.                 if (!window.mouseLeft) firstClick = true;
  326.                 return false;
  327.             }
  328.         }
  329.  
  330.         static void CopyDeck(Card[] coveredDeck, Card[] deck)
  331.         {
  332.             for (int i = 0; i < deck.Length; i++)
  333.             {
  334.                 coveredDeck[i] = deck[i];
  335.             }
  336.         }
  337.  
  338.  
  339.  
  340.         static void CreateColor(CardColor[] color, Color enumColor)
  341.         {
  342.             switch (enumColor)
  343.             {
  344.                 case Color.Rosso:
  345.                     color[(int)enumColor].R = 255;
  346.                     color[(int)enumColor].G = 0;
  347.                     color[(int)enumColor].B = 0;
  348.                     break;
  349.                 case Color.Verde:
  350.                     color[(int)enumColor].R = 0;
  351.                     color[(int)enumColor].G = 255;
  352.                     color[(int)enumColor].B = 0;
  353.                     break;
  354.                 case Color.Giallo:
  355.                     color[(int)enumColor].R = 255;
  356.                     color[(int)enumColor].G = 255;
  357.                     color[(int)enumColor].B = 102;
  358.                     break;
  359.                 case Color.Blu:
  360.                     color[(int)enumColor].R = 0;
  361.                     color[(int)enumColor].G = 0;
  362.                     color[(int)enumColor].B = 255;
  363.  
  364.                     break;
  365.                 case Color.Viola:
  366.                     color[(int)enumColor].R = 128;
  367.                     color[(int)enumColor].G = 0;
  368.                     color[(int)enumColor].B = 128;
  369.                     break;
  370.                 case Color.Bianco:
  371.                     color[(int)enumColor].R = 255;
  372.                     color[(int)enumColor].G = 255;
  373.                     color[(int)enumColor].B = 255;
  374.                     break;
  375.                 case Color.Marrone:
  376.                     color[(int)enumColor].R = 101;
  377.                     color[(int)enumColor].G = 67;
  378.                     color[(int)enumColor].B = 33;
  379.                     break;
  380.                 case Color.Celeste:
  381.                     color[(int)enumColor].R = 10;
  382.                     color[(int)enumColor].G = 186;
  383.                     color[(int)enumColor].B = 181;
  384.                     break;
  385.                 case Color.Arancione:
  386.                     color[(int)enumColor].R = 255;
  387.                     color[(int)enumColor].G = 117;
  388.                     color[(int)enumColor].B = 20;
  389.                     break;
  390.                 case Color.VerdeOliva:
  391.                     color[(int)enumColor].R = 128;
  392.                     color[(int)enumColor].G = 128;
  393.                     color[(int)enumColor].B = 0;
  394.                     break;
  395.                 case Color.RosaScuro:
  396.                     color[(int)enumColor].R = 231;
  397.                     color[(int)enumColor].G = 84;
  398.                     color[(int)enumColor].B = 128;
  399.                     break;
  400.                 case Color.Oro:
  401.                     color[(int)enumColor].R = 255;
  402.                     color[(int)enumColor].G = 215;
  403.                     color[(int)enumColor].B = 0;
  404.                     break;
  405.                 default:
  406.                     break;
  407.             }
  408.         }
  409.  
  410.         static void DrawDeck(Window window, Card[] deck)
  411.         {
  412.             for (int i = 0; i < deck.Length; i++)
  413.             {
  414.                 DrawRect(window, deck[i]);
  415.             }
  416.         }
  417.         static void CreateDeck(Window window, Card[] deck, int leftPadding, int upPadding, int col, int row, int width, int height, CardColor[] color)
  418.         {
  419.             Random r = new Random();
  420.             int cardWidth = width;
  421.             int cardHeight = height;
  422.             int tempX = 0;
  423.             int tempY = 0 - deck[0].Height;
  424.             InitCard(tempX, tempY, deck, leftPadding, upPadding, cardWidth, cardHeight, color);
  425.             PlaceCard(window, deck, col, row, ref tempX, ref tempY);
  426.             ShuffleCard(deck, r);
  427.         }
  428.  
  429.         static void ShuffleCard(Card[] deck, Random r)
  430.         {
  431.             Card card;
  432.             for (int i = 0; i < deck.Length; i++)
  433.             {
  434.                 int index = r.Next(0, deck.Length);
  435.                 card = deck[i];
  436.                 deck[i].color = deck[index].color;
  437.                 deck[index].color = card.color;
  438.             }
  439.         }
  440.  
  441.         static void PlaceCard(Window window, Card[] deck, int col, int row, ref int tempX, ref int tempY)
  442.         {
  443.             bool firstCard = true;
  444.  
  445.             int rowCount = 0;
  446.             for (int i = 0; i < deck.Length; i++)
  447.             {
  448.                 if (rowCount == 0)
  449.                 {
  450.                     tempX = 0;
  451.                     deck[i].X = deck[i].LeftPadding;
  452.                     tempX = deck[i].X;
  453.                     if (firstCard)
  454.                     {
  455.                         deck[i].Y = deck[i].UpPadding;
  456.                         firstCard = false;
  457.                     }
  458.                     else
  459.                     {
  460.                         deck[i].Y += tempY + deck[i].UpPadding + deck[i].Height;
  461.                     }
  462.                     tempY = deck[i].Y;
  463.                     rowCount = col;
  464.                 }
  465.                 else
  466.                 {
  467.                     deck[i].X = tempX + deck[i].LeftPadding + (int)deck[i].Width;
  468.                     deck[i].Y = tempY;
  469.                     tempX = deck[i].X;
  470.                 }
  471.                 rowCount--;
  472.             }
  473.  
  474.         }
  475.  
  476.         static void DrawRect(Window window, Card card)
  477.         {
  478.             for (int i = card.Y; i < card.Y + card.Height; i++)
  479.             {
  480.                 DrawHorizontalLine(window, card.X, i, (int)card.Width, card.color);
  481.             }
  482.         }
  483.         static void Clear(Window window)
  484.         {
  485.             for (int i = 0; i < window.bitmap.Length; i++)
  486.             {
  487.                 window.bitmap[i] = 0;
  488.             }
  489.         }
  490.  
  491.         static void InitCard(int newX, int newY, Card[] card, int leftPadding, int upPadding, int width, int height, CardColor[] color)
  492.         {
  493.             int c = 0;
  494.             for (int i = 0; i < card.Length; i++)
  495.             {
  496.                 card[i].Width = width;
  497.                 card[i].Height = height;
  498.                 card[i].UpPadding = upPadding;
  499.                 card[i].LeftPadding = leftPadding;
  500.                 card[i].X = newX;
  501.                 card[i].Y = newY;
  502.                 card[i].cover1 = color[(int)Color.Bianco];
  503.                 card[i].cover2 = color[(int)Color.Rosso];
  504.             }
  505.             for (int i = 0; i < card.Length; i += 2)
  506.             {
  507.                 if (c >= color.Length)
  508.                 {
  509.                     c = 0;
  510.                 }
  511.                 card[i].color = color[c];
  512.                 card[i + 1].color = color[c];
  513.                 c++;
  514.             }
  515.         }
  516.  
  517.         static void DrawHorizontalLine(Window win, int x, int y, int width, CardColor color)
  518.         {
  519.             for (int i = x; i < x + width; i++)
  520.             {
  521.                 PutPixel(win, i, y, color);
  522.             }
  523.         }
  524.         static void PutPixel(Window win, int x, int y, CardColor color)
  525.         {
  526.             if (x >= 0 && y >= 0 && x < win.width && y < win.height)
  527.             {
  528.                 int index = (y * win.width + x) * 3;
  529.  
  530.                 win.bitmap[index] = color.R;
  531.                 win.bitmap[index + 1] = color.G;
  532.                 win.bitmap[index + 2] = color.B;
  533.             }
  534.         }
  535.  
  536.         static void AskRowCol(out int row, out int col, int maxRow, int maxCol)
  537.         {
  538.             row = 1;
  539.             col = 1;
  540.             bool check = false;
  541.             bool checkCol = false;
  542.             bool checkRow = false;
  543.             while (!check)
  544.             {
  545.                 Console.Write("Quante righe vuoi?: ");
  546.                 row = int.Parse(Console.ReadLine());
  547.                 Console.Write("Quante colonne vuoi?: ");
  548.                 col = int.Parse(Console.ReadLine());
  549.                 if (row <= maxRow)
  550.                 {
  551.                     checkRow = true;
  552.                 }
  553.                 if (col <= maxCol)
  554.                 {
  555.                     checkCol = true;
  556.                 }
  557.                 if (col % 2 == 0 && checkRow && checkCol)
  558.                 {
  559.                     check = true;
  560.                 }
  561.                 else check = false;
  562.  
  563.             }
  564.         }
  565.     }
  566. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement