Difference between revisions of "CheckWordIsInTiles"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 1: Line 1:
        private static bool CheckWordIsInTiles(string Word, string PlayerTiles)
+
      <syntaxhighlight lang="c#">
 +
 
 +
private static bool CheckWordIsInTiles(string Word, string PlayerTiles)
 
         {
 
         {
 
             bool InTiles = true;
 
             bool InTiles = true;
Line 16: Line 18:
 
             return InTiles;
 
             return InTiles;
 
         }
 
         }
 +
</syntaxhighlight>
  
The input PlayerTiles is a string representing the all the tiles a player currently possesses.
+
The input PlayerTiles is a string representing the all the tiles a player currently possesses. <br>
CopyOfTiles is a local copy of PlayerTiles created so that PlayerTiles is not permanently affected, as the purpose of CheckWordIsInTiles is only to check whether the player has sufficient tiles to spell a word rather than actually taking the player's tiles away.
+
CopyOfTiles is a local copy of PlayerTiles created so that PlayerTiles is not permanently affected, as the purpose of CheckWordIsInTiles is only to check whether the player has sufficient tiles to spell a word rather than actually spelling it. <br>
The other input, Word, is a string representing the word the player is trying to spell.
+
The other input, Word, is a string representing the word the player is trying to spell. <br>
The for loop increments from the Word's first to last letter inclusive. For each letter of Word, the if statement checks whether that letter is contained in CopyOfTiles.  
+
The for loop increments from the Word's first to last letter inclusive. For each letter of Word, the if statement checks whether that letter is contained in CopyOfTiles. <br>
If true, one tile representing that letter is removed from CopyOfTiles as each tile can only be used once.
+
If true, one tile representing that letter is removed from CopyOfTiles as each tile can only be used once. <br>
 
If false, the Word cannot be spelled with the tiles the player currently possesses, so InTiles is set to false which will later be used to invalidate the player's turn.
 
If false, the Word cannot be spelled with the tiles the player currently possesses, so InTiles is set to false which will later be used to invalidate the player's turn.

Revision as of 12:25, 14 November 2017

 private static bool CheckWordIsInTiles(string Word, string PlayerTiles)
        {
            bool InTiles = true;
            string CopyOfTiles = PlayerTiles;
            for (int Count = 0; Count < Word.Length; Count++)
            {
                if (CopyOfTiles.Contains(Word[Count]))
                {
                    CopyOfTiles = CopyOfTiles.Remove(CopyOfTiles.IndexOf(Word[Count].ToString()), 1);
                }
                else
                {
                    InTiles = false;
                }
            }
            return InTiles;
        }

The input PlayerTiles is a string representing the all the tiles a player currently possesses.
CopyOfTiles is a local copy of PlayerTiles created so that PlayerTiles is not permanently affected, as the purpose of CheckWordIsInTiles is only to check whether the player has sufficient tiles to spell a word rather than actually spelling it.
The other input, Word, is a string representing the word the player is trying to spell.
The for loop increments from the Word's first to last letter inclusive. For each letter of Word, the if statement checks whether that letter is contained in CopyOfTiles.
If true, one tile representing that letter is removed from CopyOfTiles as each tile can only be used once.
If false, the Word cannot be spelled with the tiles the player currently possesses, so InTiles is set to false which will later be used to invalidate the player's turn.