2016-10-06 17 views
-1

私は現在、複数のプレーヤーの手を比較し、勝者が誰であるかを決定するポーカー評価者を作成しています。しかし、実際の比較とタイブレークの方法は、はるかに多くの作業が必要です。私のコードを単純化する方法はありますか?私はすべてのソースコードを投稿していないので、以下で呼び出されるメソッドのいくつかの機能について議論する予定です。ポーカー評価のコードを簡素化する助けが必要

int HandEval(Card [] hand)は、配列の配列をとり、

int GetHighCard(Card [] hand)は、カードの配列をとり、タイブレークに使用されたハイカードのランクを返します(つまり、1つの種類のカードよりも高いスコアを持ちます)。 。種類の3の場合には、これらの3種類のいずれかのランクが返されます)

public void FindWinner() 
    { 
     int maxScore = 0; 
     int maxRank = 0; 
     List<String> potentialWinners = new List<string>(); 
     List<Card[]> candHand = new List<Card[]>(); 

     Dictionary<string, Card[]> deckTable = new Dictionary<string, Card[]>(); 
     Dictionary<string, int> scoreTable = new Dictionary<string, int>(); 
     Dictionary<string, int> highTable = new Dictionary<string, int>(); 

     //place each player to deckTable which holds the player name and their hand 
     //place each player to scoreTable which holds the player name and the score of their hand 
     for (int i = 0; i < players.Count; i++) 
     { 
      deckTable.Add(players[i].GetName(), players[i].GetSorted()); 
      scoreTable.Add(players[i].GetName(), HandEval(players[i].GetSorted())); 

     } 

     //display the player name and the score of their hand 
     foreach (KeyValuePair<string, int> pair in scoreTable) 
     { 
      Console.WriteLine("{0}, {1}", pair.Key, +pair.Value); 
     } 

     //find the max score of the current game 
     foreach (var kvp in scoreTable) 
     { 
      if (kvp.Value > maxScore) 
      { 
       maxScore = kvp.Value; 
      } 
     } 

     //display the max score 
     Console.WriteLine("The maximum score is " + maxScore); 

     //for all players with the max score, add them to the potential winners list 
     foreach (var kvp in scoreTable) 
     { 
      if (kvp.Value == maxScore) 
      { 
       potentialWinners.Add(kvp.Key); 
      } 
     } 

     //if there are more than one potential winner, run the tie-break checks 
     if (potentialWinners.Count > 1) 
     { 
      Console.WriteLine("Potential winners include: "); 
      for (int i = 0; i < potentialWinners.Count(); i++) 
      { 
       Console.WriteLine("" + potentialWinners[i] + ""); 
      } 

      //add the name of the potential winners and the rank of the high card as a key value pair to highTable 
      for (int i = 0; i < potentialWinners.Count(); i++) 
      { 
       if (deckTable.ContainsKey(potentialWinners[i])) 
       { 
        Card[] cHand = deckTable[potentialWinners[i]]; 
        highTable.Add(potentialWinners[i], GetHighCard(cHand)); 
       } 
      } 

      Console.WriteLine("Displaying potential winners with their high card rank."); 
      foreach (KeyValuePair<string, int> pair in highTable) 
      { 
       Console.WriteLine("{0}, {1}", pair.Key, pair.Value); 
      } 

      //find the max rank of high card from all potential winners 
      foreach (var kvp in highTable) 
      { 
       if (kvp.Value > maxRank) 
       { 
        maxRank = kvp.Value; 
       } 
      } 

      Console.WriteLine("The final winner after tie-breaking is"); 

      //display the winner(s) with the highest rank of card 
      foreach (var kvp in highTable) 
      { 
       if (kvp.Value == maxRank) 
       { 
        Console.WriteLine("" + kvp.Key + ""); 
       } 
      } 
     } 
     //if there is only one potential winner, display the name 
     else 
     { 
      Console.WriteLine("The final winner is"); 
      Console.WriteLine(potentialWinners[0]); 
     } 
    } 
} 

すべてのヘルプやヒントを大幅に高く評価されます!あなたは基本的にループのために2回同じことを呼び出している

+0

これはCodeReviewの方が良いでしょう –

+0

スタックオーバーフローから質問を削除するのは珍しいことです。この考えは、トピックが検索エンジンによって容易に見つけられる何らかの種類のデータベースを作成することです。いくつかの古いコードでは恥ずかしいやり方ではない、経験豊富なコーダーとして誰も生まれていない。私が古いコードを見るたびに、たぶん1歳になるかもしれません。 – Aziuth

答えて

0
for (int i = 0; i < potentialWinners.Count(); i++) 
{ 
    Console.WriteLine("" + potentialWinners[i] + ""); 
} 

//add the name of the potential winners and the rank of the high card as a key value pair to highTable 


for (int i = 0; i < potentialWinners.Count(); i++) 
{ 
    if (deckTable.ContainsKey(potentialWinners[i])) 
    { 
     Card[] cHand = deckTable[potentialWinners[i]]; 
     highTable.Add(potentialWinners[i], GetHighCard(cHand)); 
    } 
} 

それは...また、plsはあなたのコンソールWriteLineメソッドのうち、("")を削除した後、あなたは、forループで

Console.WriteLine("" + potentialWinners[i] + ""); 

をもたらす可能性がありますこれらのループのために言われて、あなたのコードが遅く

同じ作りますが、その基本的にやっては何もないのです

foreach (var kvp in highTable) 
あなたのコードがsmaler、より効率的にするの

foreach (var kvp in scoreTable) 

シンプルなもの

挨拶

+0

お返事ありがとうございます、私は今すぐ近くに見ていきます。私は何かが私のコードで間違っていたことを知っていた:) – nvidia123

+0

あなたは大歓迎です。 –

2

あなたの最大の過ちは、使用している言語の種類を無視することです。あなたのスタイルは、オブジェクト指向ではなく不可欠です。

あなたがしたいのは、cardHandやscoreTableのようなものを扱うためのクラスを作成することです。次に、getBestHandのようなメソッドを与えます。 この擬似コードのようなものになるはずです、あなたがものをプリントアウトしたコードの一部:

プリントごとに任意のせずに、ある+ table.getBestHand.getPlayer.name

「勝者があります」ループのようなものであってもよい。ロジックと出力を分離したいとします。通常は、単一の関数またはメソッドが画面の半分以下のサイズを持つ場合(例外はもちろん発生します)、良い兆候です。 1つの単語で説明できるすべてのものを取り上げますが、5行以上のコードがあり、その中からメソッドを作ります。

これはもちろん、より多くのコードを生成しますが、私はポーカーゲームであっても、適切なクラスを持っていれば、すでに支払いを受けることになります。コードの量ではない場合は、少なくとも可読性と保守容易性で。

関連する問題