How can I condense this code in C# -


i want know how condense working code. simple game of go fish made partner our third day in c# class. seems ton of code im sure can condensed few lines.

c# code part need on

        int count1 = 0;             int count2 = 0;             int count3 = 0;             int count4 = 0;             int count5 = 0;             int count6 = 0;             int count7 = 0;             int count8 = 0;             int count9 = 0;             int count10 = 0;             int count11 = 0;             int count12 = 0;             int count13 = 0;         // system.console.writeline("hi there @ me " + player1.numofcardsinhand());         for(int y = player1.numofcardsinhand()-1; y >= 0 ; y--)         {              if(player1.getlistobject()[y].val == 1)             {                 count1++;             }             if(player1.getlistobject()[y].val == 2)             {                 count2++;             }             if(player1.getlistobject()[y].val == 3)             {                 count3++;             }             if(player1.getlistobject()[y].val == 4)             {                 count4++;             }             if(player1.getlistobject()[y].val == 5)             {                 count5++;             }             if(player1.getlistobject()[y].val == 6)             {                 count6++;             }             if(player1.getlistobject()[y].val == 7)             {                 count7++;             }             if(player1.getlistobject()[y].val == 8)             {                 count8++;             }             if(player1.getlistobject()[y].val == 9)             {                 count9++;             }             if(player1.getlistobject()[y].val == 10)             {                 count10++;             }             if(player1.getlistobject()[y].val == 11)             {                 count11++;             }             if(player1.getlistobject()[y].val == 12)             {                 count12++;             }             if(player1.getlistobject()[y].val == 13)             {                 count13++;             }             if(count1 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("1"));             }             if(count2 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("2"));             }             if(count3 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("3"));             }             if(count4 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("4"));             }             if(count5 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("5"));             }             if(count6 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("6"));             }             if(count7 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("7"));             }             if(count8== 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("8"));             }             if(count9 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("9"));             }             if(count10 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("10"));             }             if(count11 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("11"));             }             if(count12 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("12"));             }if(count13 == 4)             {                 player1points++;                 player1.getlistobject().removeall(u => u.equals("13"));                 }               } 

store cards count in array element each card type. use card type index in cards array increment counter or current cards count of given type:

int[] cards = new int[13]; for(int = player1.numofcardsinhand() - 1; >= 0 ; i--) {     int cardtype = player1.getlistobject()[i].val;     cards[cardtype - 1]++;      if (cards[cardtype - 1] == 4)     {         player1points++;         player1.getlistobject().removeall(u => u.equals(cardtype.tostring()));     } } 

though if want count of points (i.e. 4 cards of given type) , see left in hand, can simplified to:

var fouroftype = player1.getlistobject()                         .groupby(c => c.val)                         .where(g => g.count() == 4)                         .select(g => g.key)                         .tolist();  player1points = fouroftype.count; foreach(var cardtype in fouroftype)     player1.getlistobject().removeall(c => c.equals(cardtype.tostring())) 

Comments