A-level Computing/AQA/Paper 1/Skeleton program/2018

This is for the new Computer Science specification (2018)

Possible Section C Questions

edit

Section C:

State the name of a user defined subroutine - CreateTileDictionary

State the name of an identifier for an array variable - Tiles, Contents, TStringArray, TIntegerArray

Give an example of instantiation - TileDictionary As New Dictionary(Of Char, Integer)

Give an example of a subroutine that returns - (1) - An integer - TTileDictionary (2) - A boolean value - QueueOfTiles.IsEmpty (3) - A string - QueueOfTiles.Remove, GetStarttingHand

Give the name of a built in function - print(), open()

Give an example of an assignment statement - MaxHandSize = 20

Give a name of a subclass that uses iteration and give an example - AddEndOfTurnTiles (For Count := 1 to NoOfEndOfTurnTiles) DO CheckWordIsValid(While...Do)

Give the name of a subclass that uses selection - GetScoreForWord(If Length(word)>7 then Score := Score+20 etc..)

Give the name of a subclass that uses exception handling - LoadALlowedWords (Try...Finally) Why has Try...Finally been used -

State the steps you would have to take in order to add 4 players to the game

Name the subroutine that uses a linear search algorithm - CheckWordIsValid (Follow up) Give the time complexity of a linear search- O(n), Disadvantage of Linear search - Has to look through each word step by step to find if the word is valid therefore inefficient

Recursion - N/A

Draw a class diagram for QueueOfTiles / a hierarchy chart for the program

Give the name of a method and describe its purpose-

Give an example of an abstract data structure used- a linear queue, dictionary

State the name of an identifier for:

A user-defined data type

A global variable used to store a whole number

A function that returns a string.

A user-defined subroutine that has only one parameter

Explain what happens to the other items in the tile queue when one item is removed.

Why is this inefficient?

Outline how the workings of the tile queue could be improved to avoid this.

The CheckWordIsValid function uses a linear search to check whether Word is in AllowedWords.

What is the big(O) of a linear search?

What more efficient search could be used instead?

Outline how this more efficient search works.

What is its big(O)?

Explain what is meant by exception handling and how exception handling could be used in the GetChoice subroutine.

Explain what is meant by a structured programming approach.

There is a variable called Item in the method QueueOfTiles.Remove. There is also a different variable called Item in the method QueueOfTiles.Show. Explain why these two different variables can have the same identifier.

In the game, the list of allowed words is loaded from a text file. A binary file could have been used instead. Describe a difference between the way in which data are stored in a binary file and the way data are stored in a text file.

What is the difference between a value parameter and a reference parameter? Use examples from the skeleton program to explain your answer.

What is the difference between a procedure and a function?

The subroutines in the Skeleton Program avoid the use of global variables – they use local variables and parameter passing instead. State two reasons why subroutines should, ideally, not use global variables.

Describe the use of the TileDictionary and why a dictionary is appropriate

-Feel free to answer and add questions-


Change the distribution of letters

edit

Change the distribution of letters so that more common letters like 'E' are more likely to be put into a hand than 'Z'

C#:

            public void Add()
            {
                int RandNo = 0;
                if (Rear < MaxSize - 1)
                {
                    int[] Value1 = { 0, 4, 8, 13, 14, 17, 18, 19 };
                    int[] Value2 = { 1, 2, 3, 6, 11, 12, 15, 20 };
                    int[] Value3 = { 5, 7, 10, 21, 22, 24 };
                    int[] Value5 = { 9, 16, 23, 25 };

                    RandNo = Rnd.Next(0, 10); 
                    if (RandNo < 4) // 40% chance
                    {
                        RandNo = Value1[Rnd.Next(0, Value1.Length)]; // Pick a random Value1 character
                    }
                    else if (RandNo < 7) // 30% chance
                    {
                        RandNo = Value2[Rnd.Next(0, Value2.Length)]; // Pick a random Value2 character
                    }
                    else if (RandNo < 9) // 20% chance
                    {
                        RandNo = Value3[Rnd.Next(0, Value3.Length)]; // Pick a random Value3 character
                    }
                    else // 10% chance
                    {
                        RandNo = Value5[Rnd.Next(0, Value5.Length)]; // Pick a random Value5 character

                    }
                    Rear++;
                    Contents[Rear] = Convert.ToChar(65 + RandNo).ToString();
                }
            }


Delphi/Pascal:

const
  WeightedLetters : string = 'AAAAAAAABBCCCDDDDEEEEEEEEEEEEFFFFGGGHHHHHIIIIIIJJKKLLMMNNOOOOOOOOOPPQRRRRRSSSSSTTTTTTTTTUUUVVWWXYYZ';
//Could alternatively be a local variable, but this seemed likely to be marginally kinder to memory

procedure QueueOfTiles.Add();
  begin
    if Rear < MaxSize - 1 then
      begin
        Rear := Rear + 1;
        Contents[Rear] := WeightedLetters[1+Random(Length(WeightedLetters))]; //Pick a random letter from the 'weighted' array
      end;
  end;


Java:

void add()
        {
            Random rnd = new Random();
            int randNo = 0;
            randNo = rnd.nextInt(101);
            int[] letter1 = {0, 4, 8, 13, 14, 17, 18, 19};
            int[] letter2 = {1, 2, 3, 6, 11, 12, 15, 20};
            int[] letter3 = {5, 7, 10, 21, 22, 24};
            int[] letter4 = {9, 16, 23, 25};
            if(randNo <= 49)
            {
                randNo = letter1[rnd.nextInt(8)];
            } else if(randNo <= 79){
                randNo = letter2[rnd.nextInt(8)];
            } else if(randNo <= 89){
                randNo = letter3[rnd.nextInt(6)];
            }else {
                randNo = letter4[rnd.nextInt(4)];
            }
            rear += 1;
            contents[rear] = (char)(65 + randNo);
        }
//BY YA BOI JOSHUA MEMESON


Java: Alternative solution:

         String letters = "aaaaaaaabbcccddddeeeeeeeeeeeefffggghhhhhhiiiiiijkllllmmnnnnnnooooooopqrrrrrsssssstttttttttuuvvwwxyyz";     
  	 ArrayList<Character> Letter_Weighted = new ArrayList<>();

        void add()
        {
            Random rnd = new Random();
            if (rear < maxSize - 1) 
            {
                int randNo = rnd.nextInt(Letter_Weighted.size());
                rear += 1;
                contents[rear] = Letter_Weighted.get(randNo);
                Letter_Weighted.remove(randNo);
            }
        }
        QueueOfTiles(int maxSize)
        {
        	for (int i = 0; i< letters.length(); i++) {
      	    	Letter_Weighted.add(i, letters.charAt(i));
      	    	Letter_Weighted.get(i);
      	    	
      	    }
            contents = new char[maxSize];
            rear = -1;
            this.maxSize = maxSize;
            for (int count = 0; count < maxSize; count++)
            { 
                add();
            }
        }

// Done by Nima


Python:

import string
def Add(self):
    if self._Rear < self._MaxSize - 1:
      char_amounts = { 'A': 8, 'B': 2, 'C': 3, 'D': 3, 'E': 12, 'F':3,
                       'G': 3, 'H': 5, 'I': 6, 'J': 1, 'K': 1, 'L': 4,
                       'M': 2, 'N': 6, 'O': 7, 'P': 1, 'Q': 1, 'R': 5,
                       'S': 6, 'T': 9, 'U': 2, 'V': 2, 'W': 2, 'X': 1,
                       'Y': 2, 'Z': 1
                      }
      letters = "".join([key * char_amounts[key] for key in char_amounts])
      self._Rear += 1
      self._Contents[self._Rear] = random.choice(letters)


VB.NET:

    'Solution by Matthew Woods | Havant College

    Public Sub Add()
        Dim RandNo As Integer
        Dim Letters As String = "aaaaaaaabbcccddddeeeeeeeeeeeefffggghhhhhhiiiiiijkllllmmnnnnnnooooooopqrrrrrsssssstttttttttuuvvwwxyyz"
        If Rear < MaxSize - 1 Then
            RandNo = Int(Rnd() * Letters.Count)
            Rear += 1
            Contents(Rear) = Letters.Chars(RandNo)
        End If
    End Sub

    'Solution by Matthew Woods | Havant College


VB.NET (Alternate):

'Solution by Matthew Woods | Havant College

      Public Sub Add()
        Dim RandNo As Integer
        Dim Set1() As Integer = {0, 4, 8, 14, 18}
        Dim Set2() As Integer = {2, 11, 13, 18, 19}
        Dim Set3() As Integer = {3, 7, 12, 15, 20}
        Dim Set4() As Integer = {1, 5, 6, 7, 24}
        Dim Set5() As Integer = {10, 21, 23, 25, 9, 16}
        If Rear < MaxSize - 1 Then
            RandNo = Int(Rnd() * 15)
            Select Case RandNo
                Case RandNo >= 0 And RandNo <= 4
                    RandNo = Int(Rnd() * 5)
                    Rear += 1
                    Contents(Rear) = Chr(65 + Set1(RandNo))
                Case RandNo >= 5 And RandNo <= 8
                    RandNo = Int(Rnd() * 5)
                    Rear += 1
                    Contents(Rear) = Chr(65 + Set2(RandNo))
                Case RandNo >= 9 And RandNo <= 11
                    RandNo = Int(Rnd() * 5)
                    Rear += 1
                    Contents(Rear) = Chr(65 + Set3(RandNo))
                Case RandNo >= 12 And RandNo <= 14
                    RandNo = Int(Rnd() * 5)
                    Rear += 1
                    Contents(Rear) = Chr(65 + Set4(RandNo))
                Case RandNo = 15
                    RandNo = Int(Rnd() * 6)
                    Rear += 1
                    Contents(Rear) = Chr(65 + Set5(RandNo))
            End Select
        End If
    End Sub

'Solution by Matthew Woods | Havant College


Allow players to skip and end their turn

edit

Allow players to skip a turn and end their turn by drawing three or more letters

C#:

static int RandomNumber = 3;
private static void AddEndOfTurnTiles(ref QueueOfTiles TileQueue, ref string PlayerTiles, string NewTileChoice, string Choice)
{
    int NoOfEndOfTurnTiles = 0;
    if (NewTileChoice == "1")
    {
        NoOfEndOfTurnTiles = Choice.Length;
    }
    else if (NewTileChoice == "2")
    {
        NoOfEndOfTurnTiles = 3;
    }
    else if (NewTileChoice == "99")
    {
        NoOfEndOfTurnTiles = RandomNumber;
    }
    else
    {
        NoOfEndOfTurnTiles = Choice.Length + 3;
    }
    for (int Count = 0; Count < NoOfEndOfTurnTiles; Count++)
    {
        PlayerTiles = PlayerTiles + TileQueue.Remove();
        TileQueue.Add();
    }
}

private static string GetChoice()
{
    string Choice;
    Console.WriteLine();
    Console.WriteLine("Either:");
    Console.WriteLine("     enter the word you would like to play OR");
    Console.WriteLine("     press 1 to display the letter values OR");
    Console.WriteLine("     press 4 to view the tile queue OR");
    Console.WriteLine("     press 7 to view your tiles again OR");
    Console.WriteLine("     press 8 to skip a turn OR");
    Console.WriteLine("     press 0 to fill hand and stop the game.");
    Console.Write("> ");
    Choice = Console.ReadLine();
    Console.WriteLine();
    Choice = Choice.ToUpper();
    return Choice;
}

private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles)
{
    Console.WriteLine();
    Console.WriteLine(PlayerName + " it is your turn.");
    DisplayTilesInHand(PlayerTiles);
    string NewTileChoice = "2";
    bool ValidChoice = false;
    bool ValidWord = false;
    string Choice = "";
    while (!ValidChoice)
    {
        Choice = GetChoice();
        if (Choice == "1")
        {
            DisplayTileValues(TileDictionary, AllowedWords);
        }
        else if (Choice == "4")
        {
            TileQueue.Show();
        }
        else if (Choice == "7")
        {
            DisplayTilesInHand(PlayerTiles);
        }
        else if (Choice == "8")
        {
            ValidChoice = true;
            Random rand = new Random();
            RandomNumber = rand.Next(0, 5) + 3;
            Console.WriteLine("Skip Turn - You will get " + RandomNumber + " Extra Letters ");
            AddEndOfTurnTiles(ref TileQueue, ref PlayerTiles, "99", Choice);
        }
        else if (Choice == "0")
        {
            ValidChoice = true;
            FillHandWithTiles(ref TileQueue, ref PlayerTiles, MaxHandSize);
        }
        else
        {
            ValidChoice = true;
            if (Choice.Length == 0)
            {
                ValidWord = false;
            }
            else
            {
                ValidWord = CheckWordIsInTiles(Choice, PlayerTiles);
            }
            if (ValidWord)
            {
                ValidWord = CheckWordIsValid(Choice, AllowedWords);
                if (ValidWord)
                {
                    Console.WriteLine();
                    Console.WriteLine("Valid word");
                    Console.WriteLine();
                    UpdateAfterAllowedWord(Choice, ref PlayerTiles, ref PlayerScore, ref PlayerTilesPlayed, TileDictionary, AllowedWords);
                    NewTileChoice = GetNewTileChoice();
                }
            }
            if (!ValidWord)
            {
                Console.WriteLine();
                Console.WriteLine("Not a valid attempt, you lose your turn.");
                Console.WriteLine();
            }
            if (NewTileChoice != "4")
            {
                AddEndOfTurnTiles(ref TileQueue, ref PlayerTiles, NewTileChoice, Choice);
            }
            Console.WriteLine();
            Console.WriteLine("Your word was:" + Choice);
            Console.WriteLine("Your new score is:" + PlayerScore);
            Console.WriteLine("You have played " + PlayerTilesPlayed + " tiles so far in this game.");
        }
    }
}


Delphi/Pascal:

procedure AddEndOfTurnTiles(var TileQueue : QueueOfTiles; var PlayerTiles : string; NewTileChoice : string; Choice : string; AdditionalLetters : integer = 3);
//Implemented a 'default parameter', it's not required for the other calls (defaults to 3), but I can use it to specify otherwise
//http://wiki.freepascal.org/Default_parameter
  var
    NoOfEndOfTurnTiles : integer;
    Count : integer;
  begin
    if NewTileChoice = '1' then
      NoOfEndOfTurnTiles := length(Choice)
    else if NewTileChoice = '2' then
      NoOfEndOfTurnTiles := AdditionalLetters //Replaced 3 with AdditionalLetters
    else
      NoOfEndOfTurnTiles := length(Choice) + 3;
    for Count := 1 to NoOfEndOfTurnTiles do
      begin
        PlayerTiles := PlayerTiles + TileQueue.Remove();
        TileQueue.Add();
      end;
  end; 

function GetChoice() : string;
//...
    writeln('     press 1 to display the letter values OR');
    writeLn('     press 2 to skip turn and pick up at least 3 letters (random)');
    writeln('     press 4 to view the tile queue OR');
//...

procedure HaveTurn(PlayerName : string; var PlayerTiles : string; var PlayerTilesPlayed : integer; 

//...

        if Choice = '1' then
          DisplayTileValues(TileDictionary, AllowedWords)
        else if Choice = '2' then
          begin
            ValidChoice := True;
            AddEndOfTurnTiles(TileQueue, PlayerTiles, '2', '', 3 + Random(5));
            writeLn('Skipped turn, added new letters');
            DisplayTilesInHand(PlayerTiles);
          end
        else if Choice = '4' then
          TileQueue.Show()

//...


Java:

	int RandomNumber = 3;

    void addEndOfTurnTiles(QueueOfTiles tileQueue, Tiles playerTiles,
            String newTileChoice, String choice)
    {
        int noOfEndOfTurnTiles;
        if(newTileChoice.equals("1"))
        {
            noOfEndOfTurnTiles = choice.length();
        }
        else if(newTileChoice.equals("2"))
        {
            noOfEndOfTurnTiles = 3;
        }
        else if (newTileChoice.equals("99"))
        {
        	int NumberOfTiles = Integer.parseInt(newTileChoice);
        	noOfEndOfTurnTiles = RandomNumber;
        }
        else
        {
            noOfEndOfTurnTiles = choice.length()+3;
        }
        for (int count = 0; count < noOfEndOfTurnTiles; count++) 
        {
            playerTiles.playerTiles += tileQueue.remove();
            tileQueue.add();
        }
    }

    String getChoice()
    {
        Console.println();
        Console.println("Either:");
        Console.println("     enter the word you would like to play OR");
        Console.println("     press 1 to display the letter values OR");
        Console.println("     press 4 to view the tile queue OR");
        Console.println("     press 7 to view your tiles again OR");
        Console.println("     press 8 to skip a turn OR");
        Console.println("     press 0 to fill hand and stop the game.");
        Console.print("> ");
        String choice = Console.readLine();
        Console.println();
        choice = choice.toUpperCase(); 
        return choice;
    }
    
    void haveTurn(String playerName, Tiles playerTiles, 
            TileCount playerTilesPlayed, Score playerScore, Map tileDictionary, 
            QueueOfTiles tileQueue, String[] allowedWords, int maxHandSize, 
            int noOfEndOfTurnTiles)
    {
      Console.println();
      Console.println(playerName + " it is your turn.");
      displayTilesInHand(playerTiles.playerTiles);
      String newTileChoice = "2";
      boolean validChoice = false;
      boolean validWord;
      while (!validChoice)
      {
        String choice = getChoice();
        if (choice.equals("1"))
        {
          displayTileValues(tileDictionary, allowedWords);
        }
        else if (choice.equals("4"))
        {
          tileQueue.show();
        }
        else if (choice.equals("7"))
        {
          displayTilesInHand(playerTiles.playerTiles);
        }
        else if (choice.equals("0"))
        {
          validChoice = true;
          fillHandWithTiles(tileQueue, playerTiles, maxHandSize);
        }
        else if (choice.equals("8")) {
        	validChoice = true;
        	Random rand = new Random();
        	int RN = rand.nextInt(5) + 3;
        	RandomNumber = RN;
            Console.println("Skip Turn - You will get " + RandomNumber + " Extra Letters ");
            addEndOfTurnTiles(tileQueue, playerTiles, "99", choice);
        }
        else
        {
          validChoice = true;
          if (choice.length() == 0)
          {
            validWord = false;
          }
          else
          {
            validWord = checkWordIsInTiles(choice, playerTiles.playerTiles);
          }
          if (validWord)
          {
            validWord = checkWordIsValid(choice, allowedWords);
            if (validWord)
            {
              Console.println();
              Console.println("Valid word");
              Console.println();
              updateAfterAllowedWord(choice, playerTiles, playerScore, 
                      playerTilesPlayed, tileDictionary, allowedWords);
              newTileChoice = getNewTileChoice();
            }
          }
          if (!validWord)
          {
            Console.println();
            Console.println("Not a valid attempt, you lose your turn.");
            Console.println();
          }
          if (!newTileChoice.equals("4"))
          {
            addEndOfTurnTiles(tileQueue, playerTiles, newTileChoice, choice);
          }
          Console.println();
          Console.println("Your word was:" + choice);
          Console.println("Your new score is:" + playerScore.score);
          Console.println("You have played " + playerTilesPlayed.numberOfTiles + " tiles so far in this game.");
        }
      }
    }


Python:

def AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice):
  if NewTileChoice == "1":
    NoOfEndOfTurnTiles = len(Choice)
  elif NewTileChoice == "2":
    NoOfEndOfTurnTiles = 3    
  elif NewTileChoice == "99":
    NoOfEndOfTurnTiles = 3 + random.randint(0,5)
    print("You have drawn {} tiles".format(NoOfEndOfTurnTiles))
  else:
    NoOfEndOfTurnTiles = len(Choice) + 3
  for Count in range(NoOfEndOfTurnTiles):
    PlayerTiles += TileQueue.Remove()
    TileQueue.Add()
  return TileQueue, PlayerTiles

def GetChoice():
  print()
  print("Either:")
  print("     enter the word you would like to play OR")
  print("     press 1 to display the letter values OR")
  print("     press 4 to view the tile queue OR")
  print("     press 7 to view your tiles again OR")
  print("     press 8 to skip your turn and draw 3 or more tiles OR")
  print("     press 0 to fill hand and stop the game.")
  Choice = input(">")
  print()
  Choice = Choice.upper()
  return Choice

def HaveTurn(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles):
  print()
  print(PlayerName, "it is your turn.")
  DisplayTilesInHand(PlayerTiles)
  NewTileChoice = "2"
  ValidChoice = False
  while not ValidChoice:
    Choice = GetChoice()
    if Choice == "1":
      DisplayTileValues(TileDictionary, AllowedWords)
    elif Choice == "4":
      TileQueue.Show()
    elif Choice == "7":
      DisplayTilesInHand(PlayerTiles)      
    elif Choice == "0":
      ValidChoice = True
      TileQueue, PlayerTiles = FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
    elif Choice == "8":
      TileQueue, PlayerTiles = AddEndOfTurnTiles(TileQueue, PlayerTiles, "99", Choice)  # call AddEndOfTurnTiles and pass '99' as NewTileChoice 
      DisplayTilesInHand(PlayerTiles)
      ValidChoice = True  # set the flag to True to break out of loop
    else:
      ValidChoice = True
      if len(Choice) == 0:
        ValidWord = False
      else:
        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
      if ValidWord:
        ValidWord = CheckWordIsValid(Choice, AllowedWords)
        if ValidWord:
          print()
          print("Valid word")
          print()
          PlayerTiles, PlayerScore, PlayerTilesPlayed = UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
          NewTileChoice = GetNewTileChoice()
      if not ValidWord:
        print()
        print("Not a valid attempt, you lose your turn.")
        print()
      if NewTileChoice != "4":
        TileQueue, PlayerTiles = AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
      print()
      print("Your word was:", Choice)
      print("Your new score is:", PlayerScore)
      print("You have played", PlayerTilesPlayed, "tiles so far in this game.")
  return PlayerTiles, PlayerTilesPlayed, PlayerScore, TileQueue


VB.NET:

Sub AddEndOfTurnTiles(ByRef TileQueue As QueueOfTiles, ByRef PlayerTiles As String, ByVal NewTileChoice As String, ByVal Choice As String)

        Dim NoOfEndOfTurnTiles As Integer
        If NewTileChoice = "1" Then
            NoOfEndOfTurnTiles = Len(Choice)
        ElseIf NewTileChoice = "2" Then
            NoOfEndOfTurnTiles = 3

            ' new else if created. deals three penalty card.
        ElseIf NewTileChoice = "9" Then

            NoOfEndOfTurnTiles = 3

        Else
            NoOfEndOfTurnTiles = Len(Choice) + 3
        End If
        For Count = 0 To NoOfEndOfTurnTiles - 1
            PlayerTiles += TileQueue.Remove()
            TileQueue.Add()
        Next
    End Sub

    Function GetChoice()
        Dim Choice As String
        Console.WriteLine()
        Console.WriteLine("Either:")
        Console.WriteLine("     enter the word you would like to play OR")
        Console.WriteLine("     press 1 to display the letter values OR")
        Console.WriteLine("     press 4 to view the tile queue OR")
        Console.WriteLine("     press 7 to view your tiles again OR")

        ' this is new command line created.
        Console.WriteLine("     press 9 to skip your turn and draw penalty cards OR")

        Console.WriteLine("     press 0 to fill hand and stop the game.")
        Console.Write("> ")
        Choice = Console.ReadLine()
        Console.WriteLine()
        Choice = Choice.ToUpper()
        Return Choice
    End Function

    Sub HaveTurn(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer, ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef TileQueue As QueueOfTiles, ByRef AllowedWords As List(Of String), ByVal MaxHandSize As Integer, ByVal NoOfEndOfTurnTiles As Integer)
        Dim NewTileChoice As String
        Dim Choice As String
        Dim ValidChoice As Boolean
        Dim ValidWord As Boolean
        Console.WriteLine()
        Console.WriteLine(PlayerName & " it is your turn.")
        DisplayTilesInHand(PlayerTiles)
        NewTileChoice = "2"
        ValidChoice = False
        While Not ValidChoice
            Choice = GetChoice()
            If Choice = "1" Then
                DisplayTileValues(TileDictionary, AllowedWords)
            ElseIf Choice = "4" Then
                TileQueue.Show()
            ElseIf Choice = "7" Then
                DisplayTilesInHand(PlayerTiles)

                ' new statement for the skip.
            ElseIf Choice = "9" Then

                ' Allows the code to continue running
                ValidChoice = True

                ' called function which draws three penalty card 
                AddEndOfTurnTiles(TileQueue, PlayerTiles, "9", Choice)

                ' text output to alert the player of the penalty
                Console.WriteLine("You have skipped your turn. Penalty tiles have been added")
                Console.WriteLine("Number of new tiles added is 3")

            ElseIf Choice = "0" Then
                ValidChoice = True
                FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
            Else
                ValidChoice = True
                If Len(Choice) = 0 Then
                    ValidWord = False
                Else
                    ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
                End If
                If ValidWord Then
                    ValidWord = CheckWordIsValid(Choice, AllowedWords)
                    If ValidWord Then
                        Console.WriteLine()
                        Console.WriteLine("Valid word")
                        Console.WriteLine()
                        UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
                        NewTileChoice = GetNewTileChoice()
                    End If
                End If
                If Not ValidWord Then
                    Console.WriteLine()
                    Console.WriteLine("Not a valid attempt, you lose your turn.")
                    Console.WriteLine()
                End If
                If NewTileChoice <> "4" Then
                    AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
                End If
                Console.WriteLine()
                Console.WriteLine("Your word was: " & Choice)
                Console.WriteLine("Your new score is: " & PlayerScore)
                Console.WriteLine("You have played " & PlayerTilesPlayed & " tiles so far in this game.")
            End If
        End While
    End Sub


When a number other than 1, 4, 7 or 0 is entered, the program shouldn't use that as a word attempt

edit

When a number other than 1, 4, 7 or 0 is entered the program shouldn't use that as a word attempt (adding validation to GetChoice)

C#:

        private static string GetChoice()
        {
            string Choice = "";
            string[] ValidChoice = { "1", "4", "7", "0" };
            bool bLoop = true;
            while (bLoop)
            {
                Console.WriteLine();
                Console.WriteLine("Either:");
                Console.WriteLine("     enter the word you would like to play OR");
                Console.WriteLine("     press 1 to display the letter values OR");
                Console.WriteLine("     press 4 to view the tile queue OR");
                Console.WriteLine("     press 7 to view your tiles again OR");
                Console.WriteLine("     press 0 to fill hand and stop the game.");
                Console.Write("> ");
                Choice = Console.ReadLine();
                Console.WriteLine();
                Choice = Choice.ToUpper();

                if (Char.IsDigit(Choice[0]) && Array.IndexOf(ValidChoice, Choice) == -1)
                    Console.WriteLine("Noooooooo! Invalid choice");
                else
                    bLoop = false;
            }
            return Choice;
        }


Delphi/Pascal:

procedure HaveTurn(PlayerName : string; var PlayerTiles : string; var PlayerTilesPlayed : integer;
var PlayerScore : integer; TileDictionary : TTileDictionary; var TileQueue : QueueOfTiles;
var AllowedWords : TStringArray; MaxHandSize : integer; NoOfEndOfTurnTiles : integer);
  var
    NewTileChoice : string;
    ValidChoice : boolean;
    ValidWord : boolean;
    Choice : string;
    float:extended;
    error:integer;
  begin
    writeln;
    writeln(PlayerName, ' it is your turn.');
    DisplayTilesInHand(PlayerTiles);
    NewTileChoice := '2';
    ValidChoice := False;
    while not ValidChoice do
      begin
        Choice := GetChoice();
        val(choice,float,error);
        if (error=0) then validchoice:=false else
        if Choice = '1' then
          DisplayTileValues(TileDictionary, AllowedWords)


Java:

      void haveTurn(String playerName, Tiles playerTiles, 
            TileCount playerTilesPlayed, Score playerScore, Map tileDictionary, 
            QueueOfTiles tileQueue, String[] allowedWords, int maxHandSize, 
            int noOfEndOfTurnTiles)
    {
      Console.println();
      Console.println(playerName + " it is your turn.");
      displayTilesInHand(playerTiles.playerTiles);
      String newTileChoice = "2";
      boolean validChoice = false;
      boolean validWord;
      while (!validChoice)
      {
        String choice = getChoice();
        	if (choice.equals("1"))
        	{
        		displayTileValues(tileDictionary, allowedWords);
        	}
        	else if (choice.equals("4"))
        	{
        		tileQueue.show();
        	}
        	else if (choice.equals("7"))
        	{
        		displayTilesInHand(playerTiles.playerTiles);
        	}
        	else if (choice.equals("0"))
        	{
        		validChoice = true;
        		fillHandWithTiles(tileQueue, playerTiles, maxHandSize);
        	}
        	else if (choice.charAt(0) == '0' || choice.charAt(0) == '1' || choice.charAt(0) == '2' || choice.charAt(0) == '3' || choice.charAt(0) == '4' 
        			|| choice.charAt(0) == '5' || choice.charAt(0) == '6' || choice.charAt(0) == '7' || choice.charAt(0) == '8' || choice.charAt(0) == '9') {
        		System.out.println("That is not an option. Please chose between 0, 1, 4 and 7");
        	}
        	else
        	{        		
        		validChoice = true;
        		if (choice.length() == 0)
        		{
        			validWord = false;
        		}
        		else
        		{
        			validWord = checkWordIsInTiles(choice, playerTiles.playerTiles);
        		}
        		if (validWord)
        		{
        			validWord = checkWordIsValid(choice, allowedWords);
        			if (validWord)
        			{
        				Console.println();
        				Console.println("Valid word");
        				Console.println();
        				updateAfterAllowedWord(choice, playerTiles, playerScore, 
        						playerTilesPlayed, tileDictionary, allowedWords);
        				newTileChoice = getNewTileChoice();
        			}
        		}
          if (!validWord)
          {
            Console.println();
            Console.println("Not a valid attempt, you lose your turn.");
            Console.println();
          }
          if (!newTileChoice.equals("4"))
          {
            addEndOfTurnTiles(tileQueue, playerTiles, newTileChoice, choice);
          }
          Console.println();
          Console.println("Your word was:" + choice);
          Console.println("Your new score is:" + playerScore.score);
          Console.println("You have played " + playerTilesPlayed.numberOfTiles + " tiles so far in this game.");
        }
      }
      }
    //BY THE NIGHTSHAFTER


Python:

#Insert into the 'GetChoice' function

if Choice.lstrip('-').isdigit():  
    ValidNum = False  
    while ValidNum == False:  
      if Choice in ["1","4","7","0"]:  
        ValidNum = True  
      else:  
        raw_input = input("Please enter a valid number")
        Choice = raw_input(">")


def GetChoice():
  print()
  print("Either:")
  print("     enter the word you would like to play OR")
  print("     press 1 to display the letter values OR")
  print("     press 4 to view the tile queue OR")
  print("     press 7 to view your tiles again OR")
  print("     press 0 to fill hand and stop the game.")
  Choice = input(">")
  try:########################
    if int(Choice) not in ["1", "4", "7", "0"]:
      print("Invalid choice")
      Choice = input(">")
  except:
    print()
    Choice = Choice.upper()
  return Choice
#By Anas Baig Brentside

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

def GetChoice():
  Choice = "2"  #Temporary value
  while Choice in ["2", "3", "5", "6", "8", "9"]:   #Loops if any of the above numbers are entered, breaks out of the loop if a valid word is entered
    print()
    print("Either:")
    print("     enter the word you would like to play OR")
    print("     press 1 to display the letter values OR")
    print("     press 4 to view the tile queue OR")
    print("     press 7 to view your tiles again OR")
    print("     press 0 to fill hand and stop the game.")
    Choice = input(">")
    print()
    Choice = Choice.upper()
  return Choice
#By Ryan.D.S


VB.NET:

Function GetChoice()
        Dim Choice As String

        ' Set as False so whenever this function is called, the value is 
        ' reset.
        Dim Check As Boolean = False

        ' Added do loop which will continue this section of the code until  
        ' a specific condition is met. In this case we created boolean variable
        ' which shows if the check function has passed or failed.
        Do

            Console.WriteLine()
            Console.WriteLine("Either:")
            Console.WriteLine("     enter the word you would like to play OR")
            Console.WriteLine("     press 1 to display the letter values OR")
            Console.WriteLine("     press 4 to view the tile queue OR")
            Console.WriteLine("     press 7 to view your tiles again OR")
            Console.WriteLine("     press 0 to fill hand and stop the game.")
            Console.Write("> ")
            Choice = Console.ReadLine()
            Console.WriteLine()

            ' ChoiceCheck is new function created for this purpose.
            Check = ChoiceCheck(Choice)

            ' If statement which output a message when function returns false.
            If Check = False Then
                Console.WriteLine("Invalid Input. Please enter correct option")

            End If

            ' Loops until the check clears.
        Loop Until Check = True
        Choice = Choice.ToUpper()
        Return Choice
    End Function

    ' New function written for the check.
    Function ChoiceCheck(ByVal Choice As String) As Boolean

        ' To be fair, this could be more streamlined. This is only way i could
        ' immediately think of.
        If Choice = "1" Or Choice = "4" Or Choice = "7" Or Choice = "0" Then
            Return True

            ' not in the spec but this will prevent return key being registered as valid attempt.
        ElseIf Choice = "" Then
            Return False

        Else
            Return False

        End If

    End Function

A solution that actually works by Joe - KES Bath

Function GetChoice()

        Dim Choice As String

        'The function can only exit when ValidChoice is set to true. This only happens when it is found to not contain any of the digits in InvalidDigits
        Dim ValidChoice As Boolean = False

        'Choice will be checked to see if it contains any of the digits below
        Dim InvalidDigits() As String = {"0", "2", "3", "5", "6", "8", "9"}

        'Loop means that the menu is constantly displayed until 'ValidChoice' finally returns true. 
        Do

            'Initially, ValidChoice is true. If no InvalidDigits are found, then it will stay as true and the loop will exit.
            ValidChoice = True
            Console.WriteLine()
            Console.WriteLine("Either:")
            Console.WriteLine("     enter the word you would like to play OR")
            Console.WriteLine("     press 1 to display the letter values OR")
            Console.WriteLine("     press 4 to view the tile queue OR")
            Console.WriteLine("     press 7 to view your tiles again OR")
            Console.WriteLine("     press 9 to skip your go and get 3 penalty cards OR")
            Console.WriteLine("     press 0 to fill hand and stop the game.")
            Console.Write("> ")

            Choice = Console.ReadLine()

            'A For Loop goes through every value in InvalidDigits and the 'contains' method is used on Choice to see if the current InvalidDigit is present in Choice
            For L = 0 To InvalidDigits.Count - 1
                If Choice.Contains(InvalidDigits(L)) Then
                    'If any InvalidDigit is present, then ValidChoice is set to false and the loop will not exit.
                    ValidChoice = False
                Else
                End If
            Next
        Loop Until ValidChoice = True

        Console.WriteLine()
        Choice = Choice.ToUpper()
        Return Choice
    End Function

Alternative Solution By Big Heny Jnes

Choice = GetChoice()
            Checker = RemoveInvalidCharacters(Choice)
            If Checker = False Then
                Console.WriteLine("Invalid Entry. Please Try Again")
            Else
                Try
                    Number = CInt(Choice)
                Catch
                    Checker = True
                End Try
                If Number < 0 Or Number = 2 Or Number = 3 Or Number = 5 Or Number = 6 Or Number > 7 Then
                    Checker = False
                End If
                If Choice = "1" Then
                    DisplayTileValues(TileDictionary, AllowedWords)
                ElseIf Choice = "4" Then
                    TileQueue.Show()
                ElseIf Choice = "7" Then
                    DisplayTilesInHand(PlayerTiles)
                ElseIf Choice = "0" Then
                    ValidChoice = True
                    FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
                ElseIf Checker = False Then
                    Console.WriteLine("Invalid Entry. Please Try Again")
                Else
                    ValidChoice = True
                    If Len(Choice) = 0 Then
                        ValidWord = False
                    Else
                        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
                    End If
                    If ValidWord Then
                        ValidWord = CheckWordIsValid(Choice, AllowedWords)
                        If ValidWord Then
                            Console.WriteLine()
                            Console.WriteLine("Valid word")
                            Console.WriteLine()
                            UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
                            NewTileChoice = GetNewTileChoice()
                        End If
                    End If
                    If Not ValidWord Then
                        Console.WriteLine()
                        Console.WriteLine("Not a valid attempt, you lose your turn.")
                        Console.WriteLine()
                    End If
                    If NewTileChoice <> "4" Then
                        AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
                    End If
                    Console.WriteLine()
                    Console.WriteLine("Your word was: " & Choice)
                    Console.WriteLine("Your new score is: " & PlayerScore)
                    Console.WriteLine("You have played " & PlayerTilesPlayed & " tiles so far in this game.")
                End If
            End If
        End While
    End Sub

    Function RemoveInvalidCharacters(ByVal Choice As String) As Boolean
        Dim FinalOutput As String = ""
        Dim Check As Boolean
        Dim Value As Integer
        Dim x As Integer = -1
        Do
            x += 1
            Value = Asc(Choice.Substring(x, 1))
            If Value > 96 And Value < 123 Then
                Value -= 32
            End If
            If (Value >= 65 And Value <= 90) Or (Value >= 48 And Value <= 57) Then
                Check = True
            Else
                Check = False
            End If
        Loop Until Check = False Or x = Choice.Length - 1
        Return Check
    End Function

Alternative Solution by FatMatt

Sub HaveTurn(ByVal PlayerName As String, ByRef Turd As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer, ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef TileQueue As QueueOfTiles, ByRef AllowedWords As List(Of String), ByVal MaxHandSize As Integer, ByVal NoOfEndOfTurnTiles As Integer)
        Dim NewTileChoice As String
        Dim Choice As String
        Dim ValidChoice As Boolean
        Dim ValidWord As Boolean
        Console.WriteLine()
        Console.WriteLine(PlayerName & " it is your turn.")
        DisplayTilesInHand(PlayerTiles)
        NewTileChoice = "2"
        ValidChoice = False
        While Not ValidChoice
            Choice = GetChoice()
            If Choice = "1" Then
                DisplayTileValues(TileDictionary, AllowedWords)
            ElseIf Choice = "4" Then
                TileQueue.Show()
            ElseIf Choice = "7" Then
                DisplayTilesInHand(PlayerTiles)
            ElseIf Choice = "0" Then
                ValidChoice = True
                FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
            ElseIf IsNumeric(Choice) Then
                Console.WriteLine("Invalid input")
                Console.ReadKey()

Alternative Solution By Hamza Shahid

 Function GetChoice()

        Dim Choice As String

        Console.WriteLine()
        Console.WriteLine("Either:")
        Console.WriteLine("     enter the word you would like to play OR")
        Console.WriteLine("     press 1 to display the letter values OR")
        Console.WriteLine("     press 2 to swap your letters OR")
        Console.WriteLine("     press 4 to view the tile queue OR")
        Console.WriteLine("     press 7 to view your tiles again OR")
        Console.WriteLine("     press 8 to add extra words to the Valid words list OR")
        Console.WriteLine("     press 9 to view score table")
        Console.WriteLine("     press 0 to fill hand and stop the game.")

        Console.Write("> ")
        Choice = Console.ReadLine()
        Dim i = 1
        Dim legalnums As String = "1247890"
        Dim digits = "0123456789"
        legalnums = legalnums.ToCharArray()
        While i > 0
            For Each item As String In digits
                If Choice.Contains(item) And digits.Contains(Choice) = False Then
                    Console.WriteLine("INVALID INPUT")
                    Choice = Console.ReadLine
                Else
                    Choice = Choice
                    i = 0
                End If
            Next
        End While
        Console.WriteLine()
        Choice = Choice.ToUpper()

        Return Choice

    End Function

Alternative Solution by Anon tiger (using RegEx)

' chuck the following in the HaveTurn subroutine after the line "FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)"
ElseIf Text.RegularExpressions.Regex.IsMatch(Choice, "([0-9])+") Then
                Console.WriteLine()
                Console.WriteLine("Please enter a valid option.")
                Console.WriteLine()


Save Game Feature

edit

A feature that allows the user to save the game

C#:

 private static void SaveGame
            (int PlayerOneScore, int PlayerTwoScore, string PlayerOneTiles, string PlayerTwoTiles
            ,int PlayerOneTilesPlayed, int PlayerTwoTilesPlayed)
        {
            Console.WriteLine("Please enter your chosen file name:");
            string fName = Console.ReadLine();
            using(StreamWriter sw = new StreamWriter(fName))
            {
                /* Writes game details to file in one go using a formatted string. */
                sw.Write(
                    String.Format("{0},{1},{2}{6}{3},{4},{5}{6}"
                    ,PlayerOneScore,PlayerOneTilesPlayed,PlayerOneTiles
                    ,PlayerTwoScore,PlayerTwoTilesPlayed,PlayerTwoTiles
                    ,Environment.NewLine) /* Environment.NewLine is used because the characters for a new line are platform dependent*/
                    );
            }
            Console.WriteLine("Game saved as {0}!", fName);
        }
private static void PlayGame(List<string> AllowedWords, Dictionary<char, int> TileDictionary, bool RandomStart, int StartHandSize, int MaxHandSize, int MaxTilesPlayed, int NoOfEndOfTurnTiles)
        {
            /* Unchanged AQA code has been omitted from the top of this method */
            while (PlayerOneTilesPlayed <= MaxTilesPlayed && PlayerTwoTilesPlayed <= MaxTilesPlayed && PlayerOneTiles.Length < MaxHandSize && PlayerTwoTiles.Length < MaxHandSize)
            {
                HaveTurn("Player One", ref PlayerOneTiles, ref PlayerOneTilesPlayed, ref PlayerOneScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles);
                Console.WriteLine();
                Console.WriteLine("Press Enter to continue");
                Console.ReadLine();
                Console.WriteLine();
                HaveTurn("Player Two", ref PlayerTwoTiles, ref PlayerTwoTilesPlayed, ref PlayerTwoScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles);
                Console.WriteLine("Press 1 to save the game.");
                if (Console.ReadLine().Equals("1")) /* Save the game */
                {
                    SaveGame(PlayerOneScore, PlayerTwoScore, PlayerOneTiles, PlayerTwoTiles, PlayerOneTilesPlayed, PlayerTwoTilesPlayed);
                }
            }
            /* Unchanged AQUA code has been omitted from the bottom of this method */
        }


Delphi/Pascal:

//Definitely not the most efficient way of doing it, but it works

procedure SaveGame(PlayerOneScore:integer;PlayerTwoScore:integer;PlayerOneTilesPlayed:integer;PlayerTwoTilesPlayed:integer;PlayerOneTiles:string;PlayerTwoTiles:string);
var
  TFin:TextFile;
  FileName:string;
begin
 WriteLn('Please enter a file name for save:');
 ReadLn(FileName);
 FileName:=FileName+'.txt';
 AssignFile(TFin,FileName);
 Rewrite(TFin);
 writeln(TFin,PlayerOneScore);
 writeln(TFin,PlayerTwoScore);
 writeln(TFin,PlayerOneTilesPlayed);
 writeln(TFin,PlayerTwoTilesPlayed);
 writeln(TFin,PlayerOneTiles);
 writeln(TFin,PlayerTwoTiles);
 CloseFile(TFin);
end;


Java:

    void playGame(String[] allowedWords, Map tileDictionary, boolean randomStart, 
            int startHandSize, int maxHandSize, int maxTilesPlayed, 
            int noOfEndOfTurnTiles)
    {
    	
      Score playerOneScore = new Score();
      playerOneScore.score = 50;
      Score playerTwoScore = new Score();
      playerTwoScore.score = 50;
      TileCount playerOneTilesPlayed = new TileCount();
      playerOneTilesPlayed.numberOfTiles = 0;
      TileCount playerTwoTilesPlayed = new TileCount();
      playerTwoTilesPlayed.numberOfTiles = 0;
      Tiles playerOneTiles = new Tiles();
      Tiles playerTwoTiles = new Tiles();
      QueueOfTiles tileQueue  = new QueueOfTiles(20); 
      if(randomStart)
      {
        playerOneTiles.playerTiles = getStartingHand(tileQueue, startHandSize);
        playerTwoTiles.playerTiles = getStartingHand(tileQueue, startHandSize);
      }
      else
      {
        playerOneTiles.playerTiles = "BTAHANDENONSARJ";
        playerTwoTiles.playerTiles = "CELZXIOTNESMUAA";
      }
      while (playerOneTilesPlayed.numberOfTiles <= maxTilesPlayed && 
              playerTwoTilesPlayed.numberOfTiles <= maxTilesPlayed && 
              playerOneTiles.playerTiles.length() < maxHandSize && 
              playerTwoTiles.playerTiles.length() < maxHandSize)
      {
        haveTurn("Player One", playerOneTiles, playerOneTilesPlayed, playerOneScore, 
                tileDictionary, tileQueue, allowedWords, maxHandSize, 
                noOfEndOfTurnTiles);
        Console.println();
        Console.println("Press Enter to continue");
        Console.readLine();
        Console.println();
        haveTurn("Player Two", playerTwoTiles, playerTwoTilesPlayed, playerTwoScore, 
                tileDictionary, tileQueue, allowedWords, maxHandSize, 
                noOfEndOfTurnTiles);
        Console.println();
        Console.println("Press Enter to continue");
        Console.readLine();
        Console.println();
        //prompt save
        Console.println("Do you want to save?");
        String Option =Console.readLine();
        if(Option.equalsIgnoreCase("Y")|| Option.equalsIgnoreCase("yes")) {
        	saveGame(playerOneScore.score,playerTwoScore.score, playerOneTiles.playerTiles,playerTwoTiles.playerTiles,
        			playerOneTilesPlayed.numberOfTiles, playerTwoTilesPlayed.numberOfTiles);
        }
      }
      playerOneScore.score = updateScoreWithPenalty(playerOneScore.score, 
              playerOneTiles.playerTiles, tileDictionary);
      playerTwoScore.score = updateScoreWithPenalty(playerTwoScore.score, 
              playerTwoTiles.playerTiles, tileDictionary);
      displayWinner(playerOneScore.score, playerTwoScore.score);   
    }

    private static void saveGame
    (int PlayerOneScore, int PlayerTwoScore, String PlayerOneTiles, String PlayerTwoTiles
    ,int PlayerOneTilesPlayed, int PlayerTwoTilesPlayed)
{
    Writer file = null;
	try {
		file = new FileWriter("saveGame.txt");
	} catch (IOException e1) {
		e1.printStackTrace();
	}
    BufferedWriter bw = new BufferedWriter(file);
        try {
        	//writes all game data into file
			bw.write(
			    +PlayerOneScore+" "+PlayerOneTilesPlayed+" "+PlayerOneTiles+" "
			    +PlayerTwoScore+" "+PlayerTwoTilesPlayed+" "+PlayerTwoTiles
			    +" /n"
			    );
		} catch (IOException e) {
			e.printStackTrace();
		}
        try {
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
        Console.println("Game saved");
    }
/* H.Khawaja */


Python:

def LoadGame():
  SaveFile = open("save.txt",'r')
  lines = SaveFile.readlines()
  PlayerOneData = lines[0].replace("\n","").split("|")
  PlayerTwoData = lines[1].replace("\n","").split("|")
  TileQueueRaw = list(lines[2].replace("\n",""))
  TileQueue = QueueOfTiles(len(TileQueueRaw))
  TileQueue._Contents = TileQueueRaw
  SaveFile.close()
  return PlayerOneData,PlayerTwoData,TileQueue
  
def SaveGame(PlayerData,TileQueue):
  if os.path.exists("save.txt"):
    os.remove("save.txt")
  SaveFile = open("save.txt",'a')
  for data in PlayerData:
    for item in data:
      SaveFile.write(str(item))
      if not item == data[-1]: SaveFile.write("|")
    SaveFile.write("\n")
  TileQueueLetters = "".join(TileQueue._Contents) 
  SaveFile.write(TileQueueLetters+"\n")
  SaveFile.close()
  
def PlayGame(AllowedWords, TileDictionary, RandomStart, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, Load=False):
  if not Load:
    PlayerOneScore = 50
    PlayerTwoScore = 50
    PlayerOneTilesPlayed = 0
    PlayerTwoTilesPlayed = 0
    TileQueue = QueueOfTiles(20)
    if RandomStart:
      PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize)
      PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize)
    else:
      PlayerOneTiles = "BTAHANDENONSARJ"
      PlayerTwoTiles = "CELZXIOTNESMUAA"
  else:
    try:
      (PlayerOneScore,PlayerOneTilesPlayed,PlayerOneTiles),(PlayerTwoScore,PlayerTwoTilesPlayed,PlayerTwoTiles),TileQueue = LoadGame()
      PlayerOneScore = int(PlayerOneScore)
      PlayerTwoScore = int(PlayerTwoScore)
      PlayerOneTilesPlayed = int(PlayerOneTilesPlayed)
      PlayerTwoTilesPlayed = int(PlayerTwoTilesPlayed)
    except:
      print()
      print("Save file not found")
      print()
      return False

  while PlayerOneTilesPlayed <= MaxTilesPlayed and PlayerTwoTilesPlayed <= MaxTilesPlayed and len(PlayerOneTiles) < MaxHandSize and len(PlayerTwoTiles) < MaxHandSize:
    PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileQueue = HaveTurn("Player One", PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
    print()
    input("Press Enter to continue")
    print()
    PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileQueue = HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
    print()
    if input("Would you like to save the game? (Y/N)").lower() == "y":
      PlayerOneData = (PlayerOneScore,PlayerOneTilesPlayed,PlayerOneTiles)
      PlayerTwoData = (PlayerTwoScore,PlayerTwoTilesPlayed,PlayerTwoTiles)
      SaveGame((PlayerOneData,PlayerTwoData),TileQueue)

  PlayerOneScore = UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
  PlayerTwoScore = UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
  DisplayWinner(PlayerOneScore, PlayerTwoScore)

def DisplayMenu():
  print()
  print("=========")
  print("MAIN MENU")
  print("=========")
  print()
  print("1. Play game with random start hand")
  print("2. Play game with training start hand")
  print("3. Load saved game")
  print("9. Quit")
  print()
  
def Main():
  print("++++++++++++++++++++++++++++++++++++++")
  print("+ Welcome to the WORDS WITH AQA game +")
  print("++++++++++++++++++++++++++++++++++++++")
  print()
  print()
  AllowedWords = LoadAllowedWords()
  TileDictionary = CreateTileDictionary()
  MaxHandSize = 20
  MaxTilesPlayed = 50
  NoOfEndOfTurnTiles = 3
  StartHandSize = 15
  Choice = ""
  while Choice != "9":
    DisplayMenu()
    Choice = input("Enter your choice: ")
    if Choice == "1":
      PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
    elif Choice == "2":
      PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
    elif Choice == "3":
      if PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles,Load=True) is False: Main()


Python (Json):

#Python 3.6+
#The original solution used Pickle to serialise the TileQueue object however pickle is not on the spec and is Python specific
#Json is on the spec therefore it would be more relevant if the solution was implemented solely in Json, plus its 'cleaner' 

import json, sys
  
def saveGame(flag, PlayerOneScore, PlayerOneTilesPlayed,  PlayerOneTiles, PlayerTwoScore, PlayerTwoTilesPlayed, PlayerTwoTiles, TileQueue):
  game_data = {'flag':flag, 'player1data': [PlayerOneScore, PlayerOneTilesPlayed,  PlayerOneTiles], 'player2data':[PlayerTwoScore, PlayerTwoTilesPlayed, PlayerTwoTiles], 'queue_obj': TileQueue.__dict__ }
  json.dump(game_data, open('save.txt','w'), separators=(',', ': '))
  print('saved')

def loadGame():
  game_data = json.loads(open('save.txt','r').read().strip())
  TileQueue = QueueOfTiles(20).__dict__ = game_data['queue_obj']
  return game_data, TileQueue
  
def PlayGame(AllowedWords, TileDictionary, RandomStart, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, load=0):
  if not load:
    flag = 1
    PlayerOneScore = 50
    PlayerTwoScore = 50
    PlayerOneTilesPlayed = 0
    PlayerTwoTilesPlayed = 0
    TileQueue = QueueOfTiles(20)

    if RandomStart:
      PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize)
      PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize)
    else:
      PlayerOneTiles = "BTAHANDENONSARJ"
      PlayerTwoTiles = "CELZXIOTNESMUAA"

  else:
    try:
        game_data, TileQueue = loadGame()
    except BaseException as e:
      print(f'Error: Unable to load game -> {e}', file=sys.stderr)
      return

    flag = game_data['flag']
    PlayerOneScore = game_data['player1data'][0]
    PlayerTwoScore = game_data['player2data'][0]
    PlayerOneTilesPlayed = game_data['player1data'][1]
    PlayerTwoTilesPlayed = game_data['player2data'][1]
    PlayerOneTiles = game_data['player1data'][-1] 
    PlayerTwoTiles = game_data['player2data'][-1]    

  while PlayerOneTilesPlayed <= MaxTilesPlayed and PlayerTwoTilesPlayed <= MaxTilesPlayed and len(PlayerOneTiles) < MaxHandSize and len(PlayerTwoTiles) < MaxHandSize:
    if flag:
      flag = 0
      PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileQueue = HaveTurn("Player One", PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
    else:
      flag = 1
      PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileQueue = HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
  
    print()
    choice = input("Press Enter to continue or type save to save game: ")
    if choice == "save":
      try:
        saveGame(flag, PlayerOneScore, PlayerOneTilesPlayed,  PlayerOneTiles, PlayerTwoScore, PlayerTwoTilesPlayed, PlayerTwoTiles, TileQueue)
      except BaseException as e:
        print(f'Error: Unable to save game -> {e}', file=sys.stderr)
    print()
    
  PlayerOneScore = UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
  PlayerTwoScore = UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)

def DisplayMenu():
  print()
  print("=========")
  print("MAIN MENU")
  print("=========")
  print()
  print("1. Play game with random start hand")
  print("2. Play game with training start hand")
  print('3. Load Game')
  print("9. Quit")
  print()
  
def Main():
  print("++++++++++++++++++++++++++++++++++++++")
  print("+ Welcome to the WORDS WITH AQA game +")
  print("++++++++++++++++++++++++++++++++++++++")
  print()
  print()
  AllowedWords = LoadAllowedWords()
  TileDictionary = CreateTileDictionary()
  MaxHandSize = 20
  MaxTilesPlayed = 50
  NoOfEndOfTurnTiles = 3
  StartHandSize = 15
  Choice = ""
  while Choice != "9":
    DisplayMenu()
    Choice = input("Enter your choice: ")
    if Choice == "1":
      PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
    elif Choice == "2":
      PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
    elif Choice == '3':
      PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, load=1)


VB.NET:

Sub SaveGame(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer)
        Dim SaveFileReader As New IO.StreamReader("savegame.txt")
        Dim SaveFileText As New List(Of String)
        While SaveFileReader.EndOfStream <> True
            SaveFileText.Add(SaveFileReader.ReadLine())
        End While
        SaveFileReader.Close()
        Dim SaveFileWriter As New IO.StreamWriter("savegame.txt")
        If PlayerName = "Player One" Then
            SaveFileWriter.WriteLine(PlayerName & ControlChars.Tab & PlayerTiles & ControlChars.Tab & PlayerTilesPlayed & ControlChars.Tab & PlayerScore)
            If SaveFileText.Count <> 0 Then
                SaveFileWriter.WriteLine(SaveFileText(1))
            Else
                SaveFileWriter.WriteLine("Player Two")
            End If
        Else
            If SaveFileText.Count <> 0 Then
                SaveFileWriter.WriteLine(SaveFileText(0))
            Else
                SaveFileWriter.WriteLine("Player One")
            End If
            SaveFileWriter.WriteLine(PlayerName & ControlChars.Tab & PlayerTiles & ControlChars.Tab & PlayerTilesPlayed & ControlChars.Tab & PlayerScore)
        End If
        SaveFileWriter.Close()
    End Sub

Sub LoadGame(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer)
        Dim SaveFileReader As New IO.StreamReader("savegame.txt")
        Dim SaveFileText As New List(Of String)
        Dim PlayerData As String() = New String(3) {}
        While SaveFileReader.EndOfStream <> True
            SaveFileText.Add(SaveFileReader.ReadLine())
        End While
        SaveFileReader.Close()
        If PlayerName = "Player One" Then
            PlayerData = SaveFileText(0).Split(ControlChars.Tab)
        Else
            PlayerData = SaveFileText(1).Split(ControlChars.Tab)
        End If
        PlayerTiles = PlayerData(1)
        PlayerTilesPlayed = PlayerData(2)
        PlayerScore = PlayerData(3)
    End Sub

Sub HaveTurn(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer, ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef TileQueue As QueueOfTiles, ByRef AllowedWords As List(Of String), ByVal MaxHandSize As Integer, ByVal NoOfEndOfTurnTiles As Integer)
        SaveGame(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore) 'saves game at the start of each turn (for the player whose turn it is currently)
        Dim NewTileChoice As String
        Dim Choice As String
        Dim ValidChoice As Boolean
        Dim ValidWord As Boolean
        Console.WriteLine()
        Console.WriteLine(PlayerName & " it is your turn.")
        DisplayTilesInHand(PlayerTiles)
        NewTileChoice = "2"
        ValidChoice = False
        While Not ValidChoice
            Choice = GetChoice()
            If Choice = "1" Then
                DisplayTileValues(TileDictionary, AllowedWords)
            ElseIf Choice = "4" Then
                TileQueue.Show()
            ElseIf Choice = "5" Then
                ReshuffleTilesInHand(PlayerTiles)
            ElseIf Choice = "6" Then
                ValidChoice = True
                AddPenaltyTiles(TileQueue, PlayerTiles)
            ElseIf Choice = "7" Then
                DisplayTilesInHand(PlayerTiles)
            ElseIf Choice = "0" Then
                ValidChoice = True
                FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
            Else
                ValidChoice = True
                If Len(Choice) = 0 Then
                    ValidWord = False
                Else
                    ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
                End If
                If ValidWord Then
                    ValidWord = CheckWordIsValid(Choice, AllowedWords)
                    If ValidWord Then
                        Console.WriteLine()
                        Console.WriteLine("Valid word")
                        Console.WriteLine()
                        UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
                        NewTileChoice = GetNewTileChoice()
                    End If
                End If
                If Not ValidWord Then
                    Console.WriteLine()
                    Console.WriteLine("Not a valid attempt, you lose your turn.")
                    Console.WriteLine()
                End If
                If NewTileChoice <> "4" Then
                    AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
                End If
                Console.WriteLine()
                Console.WriteLine("Your word was: " & Choice)
                Console.WriteLine("Your new score is: " & PlayerScore)
                Console.WriteLine("You have played " & PlayerTilesPlayed & " tiles so far in this game.")
            End If
        End While
    End Sub

Sub PlayGame(ByRef AllowedWords As List(Of String), ByVal TileDictionary As Dictionary(Of Char, Integer), ByVal RandomStart As Boolean, ByVal StartHandSize As Integer, ByVal MaxHandSize As Integer, ByVal MaxTilesPlayed As Integer, ByVal NoOfEndOfTurnTiles As Integer, Optional ByVal LoadGameFromFile As Boolean = False)
        Dim PlayerOneScore As Integer
        Dim PlayerTwoScore As Integer
        Dim PlayerOneTilesPlayed As Integer
        Dim PlayerTwoTilesPlayed As Integer
        Dim PlayerOneTiles As String
        Dim PlayerTwoTiles As String
        Dim TileQueue As New QueueOfTiles(20)
        PlayerOneScore = 50
        PlayerTwoScore = 50
        PlayerOneTilesPlayed = 0
        PlayerTwoTilesPlayed = 0
        If RandomStart Then
            PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize)
            PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize)
        Else
            PlayerOneTiles = "BTAHANDENONSARJ"
            PlayerTwoTiles = "CELZXIOTNESMUAA"
        End If
        If LoadGameFromFile Then 'loads variables for both players if load game is chosen
            LoadGame("Player One", PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore)
            LoadGame("Player Two", PlayerTwoTiles, PlayerOneTilesPlayed, PlayerOneScore)
        End If
        While PlayerOneTilesPlayed <= MaxTilesPlayed And PlayerTwoTilesPlayed <= MaxTilesPlayed And Len(PlayerOneTiles) < MaxHandSize And Len(PlayerTwoTiles) < MaxHandSize
            HaveTurn("Player One", PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
            Console.WriteLine()
            Console.Write("Press Enter to continue")
            Console.ReadLine()
            Console.WriteLine()
            HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
        End While
        UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
        UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
        DisplayWinner(PlayerOneScore, PlayerTwoScore)
    End Sub

    Sub DisplayMenu()
        Console.WriteLine()
        Console.WriteLine("=========")
        Console.WriteLine("MAIN MENU")
        Console.WriteLine("=========")
        Console.WriteLine()
        Console.WriteLine("1. Play game with random start hand")
        Console.WriteLine("2. Play game with training start hand")
        Console.WriteLine("3. Load last game")
        Console.WriteLine("9. Quit")
        Console.WriteLine()
    End Sub

Sub Main()
        Dim MaxHandSize As Integer
        Dim MaxTilesPlayed As Integer
        Dim NoOfEndOfTurnTiles As Integer
        Dim StartHandSize As Integer
        Dim Choice As String
        Dim AllowedWords As New List(Of String)
        Dim TileDictionary As New Dictionary(Of Char, Integer)()
        Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
        Console.WriteLine("+ Welcome to the WORDS WITH AQA game +")
        Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
        Console.WriteLine()
        Console.WriteLine()
        LoadAllowedWords(AllowedWords)
        TileDictionary = CreateTileDictionary()
        MaxHandSize = 20
        MaxTilesPlayed = 50
        NoOfEndOfTurnTiles = 3
        StartHandSize = 15
        Choice = ""
        While Choice <> "9"
            DisplayMenu()
            Console.Write("Enter your choice: ")
            Choice = Console.ReadLine()
            If Choice = "1" Then
                PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
            ElseIf Choice = "2" Then
                PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
            ElseIf Choice = "3" Then 'if load game option chosen, parameter LoadGameFromFile set to true
                PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, True)
            End If
        End While
    End Sub


Improve the searching algorithm

edit

To check if the word is valid the program uses linear search O(n), improve it using a more efficient algorithm, e.g. binary search O(logn)

C#:

private static bool CheckWordIsValid(string Word, List<string> AllowedWords)
        {
            /* 
             * Performs a Binary Search for Word in AllowedWords.
             */
            int mid;
            int low = 0;
            int high = AllowedWords.Count - 1;
            while (low <= high)
            {
                mid = (low + high) / 2;
                if (String.Compare(Word, AllowedWords[mid]) < 0)
                {
                    /* Word < AllowedWords[mid] so only consider lower half of list*/
                    high = mid - 1;
                }
                else if (String.Compare(Word, AllowedWords[mid]) > 0)
                {
                    /* Word > AllowedWords[mid] so only consider upper half of list*/
                    low = mid + 1;
                }
                else
                {
                    /* The word was found */
                    return true;
                }
            }
            return false;
        }


Delphi/Pascal:

function CheckWordIsValid(Word:string;AllowedWords:TStringArray):boolean;
var
  ValidWord:boolean;
  middle,low,high:integer;
begin
  ValidWord:=false;
  low:=0;
  high:=length(AllowedWords)-1;
  while(low<=high) AND (NOT ValidWord) do
    begin
     middle:=(low+high) div 2;
     if(AllowedWords[middle]=Word)then ValidWord:=true
     else if(AllowedWords[middle]<Word)then low:=middle+1
     else high:=middle-1;
    end;
  CheckWordIsValid:=ValidWord;
end;


Java:

//Sheamol

boolean checkWordIsValid(String word, String[] allowedWords)
    {
        boolean validWord = false;
        while (start<=end){
            mid=(start+end)/2;
            if (allowedWords[mid].equals(word)){
                validWord=true;
                return validWord;
            }
            else if(allowedWords[mid].compareTo(word)>0){
                end=mid-1;
            }
            else if(allowedWords[mid].compareTo(word)<0){
                start=mid+1;
            } 
        }
        return validWord;
    }


Python:

#By HAMMAD MEHMOOD, HERSCHEL GRAMMAR SCHOOL
def CheckWordIsValid(Word, AllowedWords):
  """
  It uses an iterative binary search to find the word in the list instead of
  linear search, Complexity of Linear Search is O(n) and Binary Search is O(log n)
  """
  ValidWord = False
  First = 0
  Last = len(AllowedWords)- 1
  while First <= Last and not ValidWord:
    Midpoint = (First + Last)//2
    if AllowedWords[Midpoint] == Word:
      ValidWord = True
      Index = Midpoint
    else:
      if AllowedWords[Midpoint] < Word:
        First = Midpoint + 1
      else:
        Last = Midpoint - 1
  return ValidWord
#By HAMMAD MEHMOOD, HERSCHEL GRAMMAR SCHOOL

#Will this only work when the words are in alphabetical order? (If you add words to the document will this not work?) rb
#Yes, only works on alphabetically / sorted lists. pl

#By Anas Baig reppin Brentside high WEST LONDON
def CheckWordIsValid(Word, AllowedWords, first, last, ValidWord):
  if last < first:
    ValidWord = False
    return ValidWord
  mid = (first + last)//2
  if Word == AllowedWords[mid]:
    ValidWord = True
    return ValidWord
  else:
    if Word < AllowedWords[mid]:
      return CheckWordIsValid(Word, AllowedWords, first, mid-1, ValidWord)
    else:
      return CheckWordIsValid(Word, AllowedWords, mid+1, last, ValidWord)
#binary search implemented using recursion. 
#change the call to ValidWord = CheckWordIsValid(Choice, AllowedWords, 0, len(AllowedWords)-1, ValidWord) in HaveTurn function

#By Jozef Tanzer, Truro School
def CheckWordIsValid(Word, AllowedWords):
  if AllowedWords[int(len(AllowedWords)/2)] == Word:
  	return True
  elif len(AllowedWords) == 1:
#the fact that it got here means that the current word is not valid
  	return False
  elif AllowedWords[int(len(AllowedWords)/2)] < Word:
  	return CheckWordIsValid(Word, AllowedWords[int(len(AllowedWords)/2):])
  return CheckWordIsValid(Word, AllowedWords[:int(len(AllowedWords)/2)])
#without changing parameters
#the min value of int(len(AllowedWords)/2) is 1 so Allowed words will always be at least one long.

#By Foz - Binary search algorithm
def CheckWordIsValid(Word, AllowedWords):
    ValidWord = False
    lower_bound = 0
    upper_bound = len(AllowedWords)
    while not ValidWord and lower_bound != upper_bound:
        middle = (upper_bound + lower_bound) // 2
        if Word == AllowedWords[middle]:
            ValidWord = True
        elif Word > AllowedWords[middle]:
            lower_bound = middle
        elif Word < AllowedWords[middle]:
            upper_bound = middle
    return ValidWord
#To the person who edited this, the whole list is checked without the -1
#I have written some code to test if a given version of this function can successfully find every word in the list:
if __name__ == "__main__":
    allowed_words = LoadAllowedWords()
    file_valid = True
    for word in allowed_words:
        if not CheckWordIsValid(word, allowed_words):
            file_valid = False
    print(file_valid)
    #Main()
#Your solution did not pass the test, as it is unable to find the first word in a list


VB.NET:


Binary Search by Joe - KES Bath

'Worst case ~25 searches on the large text file, compared to ~400,000 searches with linear search.
'Worst case ~14 searches on the small text file, compared to ~10,000 searches with linear search. 
'worst case ~12 searches on the large text file

Function CheckWordIsValid(ByVal Word As String, ByRef AllowedWords As List(Of String)) As Boolean

        Dim ValidWord, Found As Boolean
        Dim First, Last, Midpoint, Loopcount As Integer

        ValidWord = False
        Found = False
        LoopCount = 0
        First = 0
        Last = AllowedWords.Count - 1

        While First <= Last And Found = False

            LoopCount += 1
            Midpoint = Math.Round((First + Last) / 2)
            If AllowedWords(Midpoint) = Word Then
                Found = True
            Else
                If AllowedWords(Midpoint) <> Word Then

                    If Word > AllowedWords(Midpoint) Then
                        First = Midpoint + 1
                    Else
                        Last = Midpoint - 1
                    End If

                End If
            End If
        End While

        If Found = True Then
            ValidWord = True
            Console.WriteLine("Word found in " & LoopCount & " searches")
        Else
            Console.WriteLine("Word not found in " & LoopCount & " searches")
        End If
        Return ValidWord
    End Function

Alternative Solution by Anon tiger:

 Function CheckWordIsValid(ByVal Word As String, ByRef AllowedWords As List(Of String)) As Boolean
        Dim Pointer As Integer = (AllowedWords.Count - 1) / 2
        Dim UpperLimit As Integer = AllowedWords.Count - 1
        Dim LowerLimit As Integer = 0
        Dim ValidWord As Boolean
        Dim Count As Integer
        Count = 0
        ValidWord = False
        While Not ValidWord
            If AllowedWords(Pointer) = Word Then
                ValidWord = True
            ElseIf Word < AllowedWords(Pointer) Then
                UpperLimit = Pointer
                Pointer = LowerLimit + ((Pointer - LowerLimit) / 2)
            Else
                LowerLimit = Pointer
                Pointer = Pointer + ((UpperLimit - Pointer) / 2)
            End If
        End While
        Return ValidWord
    End Function

alternative fast solution

    Function CheckWordIsValid(ByVal Word As String, ByRef AllowedWords As List(Of String)) As Boolean
        Dim ValidWord As Boolean
        Dim index As Integer
        index = AllowedWords.BinarySearch(Word)
        If index >= 0 Then
            ValidWord = True
        Else validword = False
        End If
        Return ValidWord
    End Function


Alert the User if aqawords.txt File is Missing

edit

Tell the player(s) if the required text file is missing

C#:

private static void LoadAllowedWords(ref List<string> AllowedWords)
{
    try
    {
        StreamReader FileReader = new StreamReader("aqawords.txt");
        while (!FileReader.EndOfStream)
        {
            AllowedWords.Add(FileReader.ReadLine().Trim().ToUpper());
        }
        FileReader.Close();
    }
    catch (FileNotFoundException)
    {
        Console.WriteLine("**** FILE NOT FOUND : aqawords.txt");
        Console.WriteLine("**** aqawords.txt does not exist. Please download.");
        Console.ReadKey();
        Environment.Exit(0);
        // AllowedWords.Clear();
    }
}


Delphi/Pascal:

The below unit can be applied to any file name and therefore is very versatile. For example, if the inappropriate words .txt file is needed then this can also be searched for.

unit checkForFile;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

function checkForFileFunc (filename : string) : boolean;
procedure checkForFileProc(filename : string);

implementation

  function checkForFileFunc (filename : string) : boolean;
    begin
      checkForFileFunc := True;
      if FileExists(filename) = False then
        checkForFileFunc := False
    end;

  procedure checkForFileProc(filename : string);
    begin
      if checkForFileFunc(filename) = False then
        begin
          writeln(filename,' cannot be successfully located... closing program');
          sleep(3000);
          halt;
        end
      else
        begin
          writeln(filename,' aqawords.txt successfully located');
          sleep(3000)
        end;
    end;
end.

The above unit's main procedure 'checkForFileStartup()', which runs the subroutines within the unit, can be referenced in the LoadAllowedWords() function so it now looks like this:

function LoadAllowedWords() : TStringArray;
  var
    AllowedWords : TStringArray;
    ListSize : integer;
    TFIn : textfile;
    Word : string;
  begin
    ListSize := 0;
    checkForFileStartup('aqawords.txt');
    AssignFile(TFIn, 'aqawords.txt');
    try
      reset(TFIn);
      while not eof(TFIn) do
        begin
          inc(ListSize);
          SetLength(AllowedWords, ListSize);
          readln(TFIn, Word);
          Word := UpperCase(Word);
          AllowedWords[ListSize - 1] := Word;
        end;
      CloseFile(TFIn);
    finally
    end;
    LoadAllowedWords := AllowedWords;
  end;


Java:

String[] loadAllowedWords()
    {
        String[] allowedWords = {};
        try {
            Path filePath = new File("aqawords.txt").toPath();
            Charset charset = Charset.defaultCharset();        
            List<String> stringList = Files.readAllLines(filePath, charset);
            allowedWords = new String[stringList.size()];
            int count = 0;
            for(String word: stringList)
            {
                allowedWords[count] = word.trim().toUpperCase();
                count++;
            }
        } catch (IOException e) {
        	System.out.println("The aqawords Text File is missing, please download it.");
        }
        return allowedWords;            
    }


Python:

import sys

def LoadAllowedWords():
  AllowedWords = []
  try:
    WordsFile = open("aqawords.txt", "r")
    for Word in WordsFile:
      AllowedWords.append(Word.strip().upper())
    WordsFile.close()
  except FileNotFoundError:
    print("The aqawords.txt file is missing, please download it", file=sys.stderr)
    exit()


VB.NET:

    Sub LoadAllowedWords(ByRef AllowedWords As List(Of String))
        Try
            Dim FileReader As New System.IO.StreamReader("aqawords.txt")
            While FileReader.EndOfStream <> True
                AllowedWords.Add(FileReader.ReadLine().Trim().ToUpper())
            End While
            FileReader.Close()
        Catch
            ' AllowedWords.Clear()
            MsgBox("WARNING: aqawords.txt is missing. Please download it!", MsgBoxStyle.Exclamation)
            Environment.Exit(0)
        End Try
    End Sub


Allow players to add new words to the game

edit

Ask the players to add new word(s) to be added to the valid word list.

C#:

private static void DisplayMenu()
{
    Console.WriteLine();
    Console.WriteLine("=========");
    Console.WriteLine("MAIN MENU");
    Console.WriteLine("=========");
    Console.WriteLine();
    Console.WriteLine("1. Play game with random start hand");
    Console.WriteLine("2. Play game with training start hand");
    Console.WriteLine("3. Add Words to Dictionary");
    Console.WriteLine("9. Quit");
    Console.WriteLine();
}

static void Main(string[] args)
{
    List<String> AllowedWords = new List<string>();
    Dictionary<Char, int> TileDictionary = new Dictionary<char, int>();
    int MaxHandSize = 20;
    int MaxTilesPlayed = 50;
    int NoOfEndOfTurnTiles = 3;
    int StartHandSize = 15;
    string Choice = "";
    Console.WriteLine("++++++++++++++++++++++++++++++++++++++");
    Console.WriteLine("+ Welcome to the WORDS WITH AQA game +");
    Console.WriteLine("++++++++++++++++++++++++++++++++++++++");
    Console.WriteLine("");
    Console.WriteLine("");
    LoadAllowedWords(ref AllowedWords);
    CreateTileDictionary(ref TileDictionary);
    while (Choice != "9")
    {
        DisplayMenu();
        Console.Write("Enter your choice: ");
        Choice = Console.ReadLine();
        if (Choice == "1")
        {
            PlayGame(AllowedWords, TileDictionary, true, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles);
        }
        else if (Choice == "2")
        {
            PlayGame(AllowedWords, TileDictionary, false, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles);
        }
        else
        {
            AddWords(AllowedWords)
        }
        else
            Console.WriteLine("\n \n invalid Choice \n");
    }
}

private static void AddWords(ref List<string> AllowedWords)
{
     Dim word As String
        Dim FileWriter As New System.IO.StreamWriter("\\coc-shares-1\studentdocuments$\Intake 2011\11johnsons\Documents\Visual Studio 2008\Projects\Altererd prelim\aqawords.txt")
        Console.WriteLine("Enter the word you would like to add to the text file.")
        word = Console.ReadLine().ToUpper
        AllowedWords.Add(word)

        For Each i In AllowedWords             'This is the sub which will add a word to the fil ADDEDDDD CODEEEEEQ!?!?!?!?
            FileWriter.WriteLine(i)            'WILL HAVE TO MAKE SURE THAT WORD IS ORGANISED IF SEARCH TO BE DONE

        Next

        FileWriter.Close()

    End Sub
}


Delphi/Pascal:

  procedure AddWord(var AllowedWords:TStringArray);
var
  newWord:String;
  TFIn : textfile;
begin
    AssignFile(TFIn, 'aqawords.txt');
    Append(TFIn);
    writeln('Please enter word to add:');
    readln(newWord);
    SetLength(AllowedWords,Length(AllowedWords)+1);
    AllowedWords[length(AllowedWords)-1]:=newWord;
    writeln(TFIn,UpperCase(newWord));
    CloseFile(TFIn);

end;


Java:

//by margaret thatcher of the conservative and unionist party
    void displayMenu() {
        Console.println();
        Console.println("=========");
        Console.println("MAIN MENU");
        Console.println("=========");
        Console.println();
        Console.println("1. Play game with random start hand");
        Console.println("2. Play game with training start hand");
        Console.println("3. Add a word"); //option added
        Console.println("9. Quit");
        Console.println();
    }
    
    //important
    void addWord(String[] allowedWords) {
        try  (BufferedWriter bw = new BufferedWriter(new FileWriter("aqawords.txt", true)))  { //true is added to modify the file
            Console.writeLine("Enter the word you would like to add");
            String word = Console.readLine(); //gets word
            bw.newLine(); //new line
            bw.write(word.toUpperCase()); //writes
            bw.close(); //closes stream
            Console.writeLine("Added word!");
        } catch (Exception e) {
            System.out.println("Couldn't add word!"); //exception
        }
    }

    Main() {
...
        while (!choice.equals("9")) {
            displayMenu();
            Console.println("Enter your choice: ");
            choice = Console.readLine();
            if (choice.equals("1")) {
                playGame(allowedWords, tileDictionary, true, startHandSize,
                        maxHandSize, maxTilesPlayed, noOfEndOfTurnTiles);
            } else if (choice.equals("2")) {
                playGame(allowedWords, tileDictionary, false, 15, maxHandSize,
                        maxTilesPlayed, noOfEndOfTurnTiles);
            }
            else if (choice.equals("3")) { //added here
                addWord(allowedWords);
            }
        }
    }


Python:

def LoadAllowedWords(): #Function to load the allowed words into the program from the AQA textfile
  AllowedWords = []
  NewWord = input("Would you like to add a new word: Y/N ")
  if NewWord == "Y" or NewWord == "y":
    UsersWord = input("Please Enter Your Word: ")
    try:
      AddWordToFile = open("aqawords.txt", 'a')
      AddWordToFile.write('\n')
      AddWordToFile.write(str(UsersWord).upper())
      AddWordToFile.close()
    except FileNotFoundError:
      print("Could not find aqawords.txt file")
      exit()
  try:
    WordsFile = open("aqawords.txt", 'r')
    for Word in WordsFile:
      AllowedWords.append(Word.strip().upper())
    WordsFile.close()
  except FileNotFoundError:
    print("Could not find aqawords.txt file")
    exit()
  for i in range(0, len(AllowedWords)-1):
    for j in range(0, len(AllowedWords)-1 - i):
      if AllowedWords[j] > AllowedWords[j+1]:
        AllowedWords[j], AllowedWords[j+1] = AllowedWords[j+1], AllowedWords[j]
  return AllowedWords #Return the list of Allowed Words.

def CheckWordIsValid(Word, AllowedWords): #Function to check if the player's word is valid
    ValidWord = False #Initially set to False
    Rear = len(AllowedWords) - 1
    Front = 0
    while Front <= Rear and not ValidWord:
      mid = (Front + Rear) // 2
      if Word == AllowedWords[mid]:
        ValidWord = True
      else:
        if Word < AllowedWords[mid]:
          Rear = mid - 1
        else:
          Front = mid + 1
    print(ValidWord)
    return ValidWord

#First the users is prompted if they wish to add a new word, if they do then the word they type in is appended to the aqawords.txt file. The contents of the file are then loaded in the AllowedWords list. However, since I have altered the sorting algorithm in the CheckWord is Valid Function to a binary search, the words in the AllowedWords list must be in alphabetical order. Therefore a sorting algorithm is necessary, I have used a bubble sort which is quite slow with a worst case time complexity of О(n^2).


VB.NET:

Sub Main()
        Dim MaxHandSize As Integer
        Dim MaxTilesPlayed As Integer
        Dim NoOfEndOfTurnTiles As Integer
        Dim StartHandSize As Integer
        Dim Choice As String
        Dim AllowedWords As New List(Of String)
        Dim TileDictionary As New Dictionary(Of Char, Integer)()
        Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
        Console.WriteLine("+ Welcome to the WORDS WITH AQA game +")
        Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
        Console.WriteLine()
        Console.WriteLine()
        LoadAllowedWords(AllowedWords) 'Calls LoadAllowedWords function passing AllowedWords variable(List).
        TileDictionary = CreateTileDictionary() 'Assigns variable TitleDictionary with value returned from CreateTileDictionary function.
        MaxHandSize = 20
        MaxTilesPlayed = 50
        NoOfEndOfTurnTiles = 3
        StartHandSize = 15
        Choice = ""
        While Choice <> "9" 'Loops until choice is 9.
            DisplayMenu() 'Calls DisplayMenu function.
            Console.Write("Enter your choice: ")
            Choice = Console.ReadLine() 'Assigns Choice the users input.
            If Choice = "1" Then
                PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles) 'Calls PlayGame function with random hand parameters.
            ElseIf Choice = "2" Then
                PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles) 'Calls PlayGame with Practice hand parameters.
            ElseIf Choice = "3" Then 'New option for adding word.
                AddWord(AllowedWords)
                AllowedWords.Clear() 'Clears current list of words. Means when the new word is loaded there is no repetition of words.
                LoadAllowedWords(AllowedWords) 'Calls LoadAllowedWords function to create list of words (including new words).
            End If
        End While
    End Sub
--------------------------------------------------------------------------------------------------------------------------------------
      Sub AddWord(ByVal AllowedWords As List(Of String))
        Dim FileWriter As New System.IO.StreamWriter("aqawords.txt")
        Dim NewWord As String
        Dim ValidWord As Boolean = True

        Console.WriteLine("Enter the new word")
        NewWord = Console.ReadLine
        Dim CharArray() As Char = NewWord.ToCharArray

        For looper = 0 To Len(NewWord) - 1
            If Char.IsNumber(CharArray(looper)) Then
                If ValidWord = True Then
                    ValidWord = False
                    Console.WriteLine("Word is invalid as it has a number")
                End If
            End If
        Next
        If ValidWord = True Then
            For looper = 0 To AllowedWords.Count - 1
                FileWriter.WriteLine(AllowedWords(looper))
            Next
            FileWriter.WriteLine("")
            FileWriter.WriteLine(NewWord.ToUpper)
            FileWriter.Close()
            Console.WriteLine("Word added to the list")

        End If
    End Sub
--------------------------------------------------------------------------------------------------------------------------------------
    Sub DisplayMenu() 'Draws menu.
        Console.WriteLine()
        Console.WriteLine("=========")
        Console.WriteLine("MAIN MENU")
        Console.WriteLine("=========")
        Console.WriteLine()
        Console.WriteLine("1. Play game with random start hand")
        Console.WriteLine("2. Play game with training start hand")
        Console.WriteLine("3. Add word to game.") 'New option in menu for adding word.
        Console.WriteLine("9. Quit")
        Console.WriteLine()
    End Sub

Jake Campbell


High-score table feature

edit

Add a high score table

C#:

Create a function to read from a high score CSV file, containing the players score and then the players name. The file is read and the sorted using a bubble sort, the top 5 scores and names are then output.

        private static void DisplayHighscore()
        {
            StreamReader FileReader = new StreamReader("HighScore.csv"); //Reads a high score csv file    
            List<string[]> Values = new List<string[]>();	//Records the data inside the file

            while (!FileReader.EndOfStream) 
            {
                Values.Add(FileReader.ReadLine().Trim().ToUpper().Split(','));  //Data is saved as score then name in a csv.
            }
            FileReader.Close();
            if (Values.Count == 0)	//If there was no data in the file
            {
                Console.WriteLine("There are currently no high scores.");
            }
            else
            {
				//Use a bubble sort to find the five highest scores.
                string[] temp;
                for (int write = 0; write < Values.Count(); write++)
                {
                    for (int sort = 0; sort < Values.Count() - 1; sort++)
                    {
                        if (Convert.ToInt32(Values[sort][0]) > Convert.ToInt32(Values[sort + 1][0]))
                        {
                            temp = Values[sort + 1];
                            Values[sort + 1] = Values[sort];
                            Values[sort] = temp;
                        }
                    }
                }
				//Print out the top 5 in the list (original list was order smallest to highest - so last 5)
                for (int i = Values.Count - 1; i >= Values.Count - 5; i--)
                {
                    Console.WriteLine(Values[i][1] + " Scored: " + Values[i][0]);
                }
            }
        }

Saves the high score into this new file.

        private static void SaveHighScore(int Score, string name)	//The winners details are sent across
        {
            Console.WriteLine("Dose " + name + " want to save their high score? (Y/N)");	//Asked if they wish to save their results
            string input = Console.ReadLine();
            if (input == "Y" || input == "y" || input == "yes")
            {
                Console.WriteLine("Please enter in your name:");
                string Name = Console.ReadLine();
                File.AppendAllText("HighScore.csv", Environment.NewLine + Score + "," + Name );	//Save their result into the file.	
                Console.WriteLine("New High Score Saved");
                Console.WriteLine("");
            }
        }

The winner of the game is asked if they would like to save their high score.

 private static void DisplayWinner(int PlayerOneScore, int PlayerTwoScore)	//Prints out the end screen winner
        {
            Console.WriteLine();
            Console.WriteLine("**** GAME OVER! ****");
            Console.WriteLine();
            Console.WriteLine("Player One your score is " + PlayerOneScore);	//Prints both players scores.
            Console.WriteLine("Player Two your score is " + PlayerTwoScore);
            if (PlayerOneScore > PlayerTwoScore)		//If Player one has more points than two
            {
                Console.WriteLine("Player One wins!");
                SaveHighScore(PlayerOneScore, "PlayerOne");
            }
            else if (PlayerTwoScore > PlayerOneScore)   //If Player two has more points than one
            {
                Console.WriteLine("Player Two wins!");
                SaveHighScore(PlayerTwoScore, "PlayerTwo"); //Trigger the save high score function
            }
            else	//Else they both have the exact same score.
            {
                Console.WriteLine("It is a draw!"); //No clear player has won so now high score is entered.
            }
            Console.WriteLine();
        }

The "Main" function is updated to allow a choice 3 to display the current high scores.

...
                DisplayMenu();		//Call function to print menu
                Console.Write("Enter your choice: ");	//Ask the user for an input
                Choice = Console.ReadLine();			//User input equals menu choice
                if (Choice == "1")	//If choice is one play game with random start hand (each players starting tiles are randomly generated)
                {
                    PlayGame(AllowedWords, TileDictionary, true, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles);
                }
                else if (Choice == "2")	//If 2, start game with the players start hand being predefined/ same each time- so no randomisation. 
                {
                    PlayGame(AllowedWords, TileDictionary, false, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles);
                }
                else if (Choice == "3") //If 3 (the new choice is entered)
                {
                    DisplayHighscore(); //Display the high scores
                }
...

The "DisplayMenu" method is also updated to show the user can press 3 to display the high score.

         private static void DisplayMenu()	//Used to print out the starting main menu.
        {
            Console.WriteLine();
            Console.WriteLine("=========");
            Console.WriteLine("MAIN MENU");
            Console.WriteLine("=========");
            Console.WriteLine();
            Console.WriteLine("1. Play game with random start hand");
            Console.WriteLine("2. Play game with training start hand");
            Console.WriteLine("3. Display high scores.");
            Console.WriteLine("9. Quit");
            Console.WriteLine();
        }


Delphi/Pascal:


Java:


Python2:

The original solution stored the player score as a string type, it then went on to use a lambda function to compare these string values, which ultimately did not work (for me it did not), apologies for the crude code, but it worked for me anyway. Please do edit this if you have a better solution than to this


This is solution elaborates over from the first solution-----
#Idris Khan (edit)
def save_scores(player_name, score): # when called, pass the player name (if they had customised names) and player score
	file_name = 'highscore.csv' # call the filename whatever you want
	with open(file_name, 'a+') as save_obj: # use the with statement, so python will automatically close the file when done, and reference the filename as whatever variable you want
		save_obj.write(player_name + ',' + str(score) + '\n') # write these to the file: player name, followed by comma, their score as a string and end it off with a newline


def load_scores():
	file_name = 'highscore.csv'
	try:
		with open(file_name, 'r') as save_obj: # open the file in read-mode
				temp_list = [] # this is a temporary list to store the player name and their score
				player_name_list = [] # another temporary list to store the player name in the order of what they were saved in the file
				player_score_list = [] # lastly the third temporary list to store the player score in the order of what they saved in the file
				for line in save_obj.read().split('\n'): # read each line in the file and split it at each new line (basically take the first line of each playername and playerscore)
					if ',' in line:
						temp_list.append(line.split(',')) # append the player name and player score (this will be in their own nested list)
				for item in temp_list: # iterate through each item in the temp list and store the player name and score in the order they were saved in the file
					player_name_list.append(item[0]) 
					player_score_list.append(item[1])
				player_score_list = list(map(int, player_score_list)) # convert all the values in player score list to an int value
				highscores = [] # this is the list where all the real scores will be saved
				for i in range(len(player_name_list)):
					entry = (player_name_list[i], player_score_list[i]) # store the player name and score as a tuple
					highscores.append(entry)
		return highscores
	except FileNotFoundError:
		print(file_name + 'cannot be found') 
		return []


def display_high_scores():
	highscores = load_scores()
	best_scores = sorted(highscores, reverse=True, key=lambda x: x[1]) # lambda function to sort the list in desc order
	if len(best_scores) > 5: 
		best_scores = best_scores[:5] # take the first five top results
	print('\n==========')
	print('HighScores')
	print('==========\n')
	for i in range(len(best_scores)):
		print(
			str(i + 1) + '. ' + best_scores[i][0] + " " + str(best_scores[i][1]) # and print
		)
	if len(best_scores) == 0:
		print('There are currently no highscores\n')
	while input('To return to main menu? [y/n]') == 'y'.lower():
		break
	else:
		display_high_scores()

def display_winner(player_one_score, player_two_score, player_one_name,
                   player_two_name):
    ---snippet---

    if input('{} would you like to save your score? [Y/N] '.format( 
            player_one_name)).upper() == 'Y':
        save_scores(player_one_name, player_one_score)
    if input('{} would like to save your score? [Y/N] '.format(
            player_two_name)).upper() == 'Y':
        save_scores(player_two_name, player_two_score)
# at the end of the game, ask both players if they want to save their scores

def display_menu():
    print('\n===========')
    print(' MAIN MENU')
    print('===========\n')
    print('1. Play game with random start hand')
    print('2. Play game with training hand')
    print('3. Load Game')
    print('4. Display highscores') # add a new option to display menu()
    print('9. Quit\n')

def main():
    print('++++++++++++++++++++++++++++++++++++++++')
    print('+\tWelcome to the WORDS WITH AQA game\t+')
    print('++++++++++++++++++++++++++++++++++++++++\n\n')
    allowed_words = load_allowed_words()
    tile_dictionary = create_tile_dictionary()
    max_hand_size = 20
    max_tiles_played = 50
    no_of_end_of_turn_tiles = 3
    start_hand_size = 15
    choice = ''
    while choice != '9':
        display_menu()
        choice = input('Enter your choice: ')
        if choice == '1':
            # 1 is pressed start each player with a start hand from the tile
            #  queue
            play_game(
                allowed_words, tile_dictionary, True, start_hand_size,
                max_hand_size, max_tiles_played, no_of_end_of_turn_tiles
            )
        elif choice == '2':
            # 2 is pressed start each player with a set string
            play_game(
                allowed_words, tile_dictionary, False, 15,
                max_hand_size, max_tiles_played, no_of_end_of_turn_tiles
            )
        elif choice == '4':
            display_high_scores() # and call the function from main menu

Python:

def SaveHighscore(Name,Score):
  ScoreFile = open("highscores.csv","a")
  ScoreFile.write(Name+","+str(Score)+"\n")
  ScoreFile.close()
  
def LoadHighscores():
  try:
    ScoreFile = open("highscores.csv","r")
  except: return []
  Highscores = []
  for Line in ScoreFile.read().split("\n"):
    if ',' in Line:
      Highscores.append(Line.split(","))
  return Highscores
  
def DisplayHighscores():
  Highscores = LoadHighscores()
  BestHighscores = sorted(Highscores,reverse=True,key=lambda x: x[1])
  if len(BestHighscores) > 5:
    BestHighscores = BestHighscores[:5]
  print()
  print("==========")
  print("Highscores")
  print("==========")
  print()
  for i in range(len(BestHighscores)):
    print(str(i+1)+". "+BestHighscores[i][0]+" - "+BestHighscores[i][1])
  if len(BestHighscores) == 0:
    print("There are currently no highscores")
  print()
  input("Press any key to continue")

def DisplayWinner(PlayerOneScore, PlayerTwoScore):
  print()
  print("**** GAME OVER! ****")
  print()
  print("Player One your score is", PlayerOneScore)
  print("Player Two your score is", PlayerTwoScore)
  if PlayerOneScore > PlayerTwoScore:
    print("Player One wins!")
  elif PlayerTwoScore > PlayerOneScore:
    print("Player Two wins!")
  else:
    print("It is a draw!")
  if input("Player One would you like to save your score? (Y/N)").upper() == "Y":
    SaveHighscore(input("Please enter your name: "),PlayerOneScore)
  if input("Player Two would you like to save your score? (Y/N)").upper() == "Y":
    SaveHighscore(input("Please enter your name: "),PlayerTwoScore)
  print()

def DisplayMenu():
  print()
  print("=========")
  print("MAIN MENU")
  print("=========")
  print()
  print("1. Play game with random start hand")
  print("2. Play game with training start hand")
  print("3. Display highscores")
  print("9. Quit")
  print()
  
def Main():
  print("++++++++++++++++++++++++++++++++++++++")
  print("+ Welcome to the WORDS WITH AQA game +")
  print("++++++++++++++++++++++++++++++++++++++")
  print()
  print()
  AllowedWords = LoadAllowedWords()
  TileDictionary = CreateTileDictionary()
  MaxHandSize = 20
  MaxTilesPlayed = 50
  NoOfEndOfTurnTiles = 3
  StartHandSize = 15
  Choice = ""
  while Choice != "9":
    DisplayMenu()
    Choice = input("Enter your choice: ")
    if Choice == "1":
      PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
    elif Choice == "2":
      PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
    elif Choice == "3":
      DisplayHighscores()


VB.NET:

' the below code works however it will only allow 3 things adding both players scores, showing the high score table or doing both.
'there is also no cap on how many high scores can be in the high score table. There are many extensions possible from this,such as 'highlighting scores for the game just played or limiting the high score table to 15> items or only letting the highest scoring player enter 'their high score, if you want to edit it and re-upload np I just wasn't too bothered.


 Sub DisplayWinner(ByVal PlayerOneScore As Integer, ByVal PlayerTwoScore As Integer)
        Dim HighScoreSelect As String
        Console.WriteLine()
        Console.WriteLine("**** GAME OVER! ****")
        Console.WriteLine()
        Console.WriteLine("Player One your score is " & PlayerOneScore) 'displays players scores
        Console.WriteLine("Player Two your score is " & PlayerTwoScore)
        If PlayerOneScore > PlayerTwoScore Then
            Console.WriteLine("Player One wins!") 'if player one has more points they win
        ElseIf PlayerTwoScore > PlayerOneScore Then
            Console.WriteLine("Player Two wins!") 'if player two has more they win
        Else
            Console.WriteLine("It is a draw!") 'if they both have the same it's a draw
        End If
        Console.WriteLine("1.Would you like to add these scores to the highscore table ")
        Console.WriteLine("2.Display the highscore table ")
        Console.WriteLine("3.Display the highscore table and add these scores ")
        Console.WriteLine("4.Or exit to main menu")
        Console.WriteLine()
        HighScoreSelect = Console.ReadLine

        If HighScoreSelect = 1 Then
            UpdateHighScoreTable(PlayerOneScore, PlayerTwoScore)
        ElseIf HighScoreSelect = 2 Then
            DisplayAndOrderHighScoreTable()
        ElseIf HighScoreSelect = 3 Then
            UpdateHighScoreTable(PlayerOneScore, PlayerTwoScore)
            DisplayAndOrderHighScoreTable()
        Else
        End If

    End Sub '


    Sub UpdateHighScoreTable(ByVal PlayerOneScore As Integer, ByVal PlayerTwoScore As Integer)
        Dim HighScorefilepath As String = "highscore.text" 'set file path
        Dim HighScorewriter As New System.IO.StreamWriter(HighScorefilepath, True) 'create a reader

        HighScorewriter.WriteLine(PlayerOneScore) 'write p1 score to the end of the file
        HighScorewriter.WriteLine(PlayerTwoScore) ' write p2 score to the end of the file
        HighScorewriter.Close()

    End Sub


    Sub DisplayAndOrderHighScoreTable()
        Dim HighScorefilepath As String = "highscore.text" 'set file path
        Dim HighScoreReader As New System.IO.StreamReader(HighScorefilepath) 'define a reader
        Dim Line As String = ""
        Dim count As Integer = 1
        Dim ScoreArray = New List(Of Integer)() ' define a list 

        Console.WriteLine()
        Do
            Line = HighScoreReader.ReadLine() 
            If Not Line Is Nothing And Line <> "" Then 'checks if there are more records if there are enters the if 
                ScoreArray.Add(Line) 'adds the current high score being read to the list 
            End If
        Loop Until Line Is Nothing 

        HighScoreReader.Close()
        ScoreArray.Sort() 'sort the list

        For Each Line In ScoreArray 'go through the whole list
            Console.WriteLine(count & "." & Line) 'write the position and the score
            count += 1 'increment the posistion
        Next
        Console.ReadLine()

    End Sub

Jason Beard Havant collage


Time limit when entering choice

edit

Add a time limit for turns

C#:

private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles)
        {
            Console.WriteLine();
            Console.WriteLine(PlayerName + " it is your turn.");
            DisplayTilesInHand(PlayerTiles);
            string NewTileChoice = "2";
            bool ValidChoice = false;
            bool ValidWord = false;
            string Choice = "";
            int timeLimit = 1000 * 10; /* Ten seconds */
            Stopwatch timer = new Stopwatch(); /* Stopwatch is a useful class for measuring elapsed time */
            timer.Start(); /* Begin timing */
            Console.WriteLine("You have {0} seconds!", timeLimit / 1000);
            while (!ValidChoice)
            {
                if (timer.ElapsedMilliseconds > timeLimit) /* The time limit has elapsed */
                {
                    Console.WriteLine("{0} has run out of time!",PlayerName); /* Notify the user that their turn is over */
                    break; /* Exit the turn loop */
                }
                Choice = GetChoice();
                if (Choice == "1")
                {
                    DisplayTileValues(TileDictionary, AllowedWords);
                }
                else if (Choice == "4")
                {
                    TileQueue.Show();
                }
                else if (Choice == "7")
                {
                    DisplayTilesInHand(PlayerTiles);
                }
                else if (Choice == "0")
                {
                    ValidChoice = true;
                    FillHandWithTiles(ref TileQueue, ref PlayerTiles, MaxHandSize);
                }
                else
                {
                    ValidChoice = true;
                    if (Choice.Length == 0)
                    {
                        ValidWord = false;
                    }
                    else
                    {
                        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles);
                    }
                    if (ValidWord)
                    {
                        ValidWord = CheckWordIsValid(Choice, AllowedWords);
                        if (ValidWord)
                        {
                            Console.WriteLine();
                            Console.WriteLine("Valid word");
                            Console.WriteLine();
                            UpdateAfterAllowedWord(Choice, ref PlayerTiles, ref PlayerScore, ref PlayerTilesPlayed, TileDictionary, AllowedWords);
                            NewTileChoice = GetNewTileChoice();
                        }
                    }
                    if (!ValidWord)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Not a valid attempt, you lose your turn.");
                        Console.WriteLine();
                    }
                    if (NewTileChoice != "4")
                    {
                        AddEndOfTurnTiles(ref TileQueue, ref PlayerTiles, NewTileChoice, Choice);
                    }
                    Console.WriteLine();
                    Console.WriteLine("Your word was:" + Choice);
                    Console.WriteLine("Your new score is:" + PlayerScore);
                    Console.WriteLine("You have played " + PlayerTilesPlayed + " tiles so far in this game.");
                }
            }
        }


Delphi/Pascal:

unit timeLimit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DateUtils;

procedure timeLimitProc (out Choice : string);

implementation
  procedure timeLimitProc (out Choice : string);
  var
    T1, T2 : TDateTime;
  const
    timeSeconds = 30;
  begin
    T1 := Now;
    write('> ');
    readln(Choice);
    Choice := UpperCase(Choice);
    T2 := Now;
    if (SecondsBetween(T1, T2)) > timeSeconds then
    begin
      write('You exceeded your time limit of ',timeSeconds,' seconds.');
      Choice := '';
    end;
  end;
end.

This unit can be referenced in GetChoice() so it now looks like this:

function GetChoice() : string;
  var
    Choice : string;
  begin
    writeln;
    writeln('Either:');
    writeln('     enter the word you would like to play OR');
    writeln('     press 1 to display the letter values OR');
    writeln('     press 4 to view the tile queue OR');
    writeln('     press 7 to view your tiles again OR');
    writeln('     press 0 to fill hand and stop the game.');
    timeLimitProc(Choice);
    writeln;
    GetChoice := Choice;
  end;


Java:

String getChoice()
    {
    	long time_beginning =  System.currentTimeMillis();	
        Console.println();
        Console.println("Either:");
        Console.println("     enter the word you would like to play OR");
        Console.println("     press 1 to display the letter values OR");
        Console.println("     press 4 to view the tile queue OR");
        Console.println("     press 7 to view your tiles again OR");
        Console.println("     press 0 to fill hand and stop the game.");
        Console.print("> ");
        Console.println();
        boolean time_left = true;
        String choice = "";
        choice = Console.readLine();
        choice = choice.toUpperCase(); 
            if (System.currentTimeMillis() - time_beginning > 10000) {
            	choice = "";
            	Console.print("you ran out of time");
            	time_left = false;
            }
       return choice;
    }
///done by Nima


Python:

import time

def GetChoice():
  MaxChoiceTime = 20
  StartChoiceTime = time.time()
  print()
  print("Either:")
  print("     enter the word you would like to play OR")
  print("     press 1 to display the letter values OR")
  print("     press 4 to view the tile queue OR")
  print("     press 7 to view your tiles again OR")
  print("     press 0 to fill hand and stop the game.")
  print()
  print("You have {} seconds to make your choice or your turn will be invalidated".format(MaxChoiceTime))
  print()
  Choice = input(">")
  EndChoiceTime = time.time()
  
  if EndChoiceTime-StartChoiceTime > MaxChoiceTime:
    print()
    print("You took too long entering your choice and your turn has been invalidated!")
    return "" #EMPTY CHOICE WILL RESULT IN AN INVALID TURN - NO NEED TO MODIFY OTHER AQA CODE
  #Has no real purpose as time limit as it does not stop the turn once the time is up it allows the user to take as long as they want
#to make their choice and then stops the move once it has been entered
  print()
  Choice = Choice.upper()
  return Choice


VB.NET:

Sub HaveTurn(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer, ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef TileQueue As QueueOfTiles, ByRef AllowedWords As List(Of String), ByVal MaxHandSize As Integer, ByVal NoOfEndOfTurnTiles As Integer)
        SaveGame(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore) 'this is from a previous task [saving the game]
        Dim NewTileChoice As String
        Dim Choice As String
        Dim ValidChoice As Boolean
        Dim ValidWord As Boolean
        Dim Timer As DateTime
        Dim TimeTaken As Integer
        Console.WriteLine()
        Console.WriteLine(PlayerName & " it is your turn.")
        DisplayTilesInHand(PlayerTiles)
        NewTileChoice = "2"
        ValidChoice = False
        While Not ValidChoice
            Console.WriteLine("You have 20 seconds to choose a word.")
            Timer = DateTime.Now.AddSeconds(20) 'sets the maximum time in which a player should be allowed to submit word
            Choice = GetChoice()
            If Choice = "1" Then
                DisplayTileValues(TileDictionary, AllowedWords)
            ElseIf Choice = "4" Then
                TileQueue.Show()
            ElseIf Choice = "5" Then
                ReshuffleTilesInHand(PlayerTiles)
            ElseIf Choice = "6" Then
                ValidChoice = True
                AddPenaltyTiles(TileQueue, PlayerTiles)
            ElseIf Choice = "7" Then
                DisplayTilesInHand(PlayerTiles)
            ElseIf Choice = "0" Then
                ValidChoice = True
                FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
            Else
                ValidChoice = True
                If Len(Choice) = 0 Then
                    ValidWord = False
                Else
                    ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
                    TimeTaken = DateTime.Compare(Timer, DateTime.Now) 'compares maximum time player allowed to take
                    'with the time they took --> returns the integer value '-1' if the player took longer than the maximum time.
                End If
                If ValidWord And (TimeTaken <> -1) Then
                    ValidWord = CheckWordIsValid(Choice, AllowedWords)
                    If ValidWord Then
                        Console.WriteLine()
                        Console.WriteLine("Valid word")
                        Console.WriteLine()
                        UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
                        NewTileChoice = GetNewTileChoice()
                    End If
                ElseIf TimeTaken = -1 Then 'if user took too long they draw 3 penalty tiles
                    Console.WriteLine()
                    Console.WriteLine("You took too long to answer.")
                    Console.WriteLine("Adding 3 penalty cards to your hand...")
                    Console.WriteLine()
                    AddPenaltyTiles(TileQueue, PlayerTiles) 'see solution for task requiring the adding of penalty tiles
                    'for an equivalent to this subroutine
                End If
                If Not ValidWord Then
                    Console.WriteLine()
                    Console.WriteLine("Not a valid attempt, you lose your turn.")
                    Console.WriteLine()
                End If
                If NewTileChoice <> "4" Then
                    AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
                End If
                Console.WriteLine()
                Console.WriteLine("Your word was: " & Choice)
                Console.WriteLine("Your new score is: " & PlayerScore)
                Console.WriteLine("You have played " & PlayerTilesPlayed & " tiles so far in this game.")
            End If
        End While
    End Sub


Inappropriate-word filter

edit

Filter out inappropriate words

C#:

        private static string GetChoice()
        {
            string Choice;
            string[] filteredWords = { "WORD1", "WORD2", "WORD3" };
            Console.WriteLine();
            Console.WriteLine("Either:");
            Console.WriteLine("     enter the word you would like to play OR");
            Console.WriteLine("     press 1 to display the letter values OR");
            Console.WriteLine("     press 4 to view the tile queue OR");
            Console.WriteLine("     press 7 to view your tiles again OR");
            Console.WriteLine("     press 0 to fill hand and stop the game.");
            Console.Write("> ");
            Choice = Console.ReadLine();
            Console.WriteLine();
            Choice = Choice.ToUpper();

            if(filteredWords.Contains(Choice))
            {
                Console.WriteLine();
                Console.WriteLine("Your word has been filtered, please try again");
                return GetChoice();
            }

            return Choice;
        }


Delphi/Pascal:

//Step 1) Implement a constant to store the words that you want filtered out
const
  inappropriateWords : array[0..1] of string = ('poo', 'toilet');


//Step 2) Implement a function that returns true when the word is appropriate, or false when it should be removed
function CheckIfWordIsAppropriate(word : string) : boolean;
  var
    i : integer;
    wordIsAppropriate : boolean;
  begin
    wordIsAppropriate:=true;

    for i:=0 to Length(inappropriateWords) do
      begin
        if(lowercase(inappropriateWords[i]) = lowercase(word)) then
          begin
            wordIsAppropriate:=false;
          end;
      end;
    CheckIfWordIsAppropriate:=wordIsAppropriate;
  end;

//Step 3) Modify the GetChoice() function to include your tasteful censorship
function GetChoice() : string;
  var
    Choice : string;
  begin
    writeln;
    writeln('Either:');
    writeln('     enter the word you would like to play OR');
    writeln('     press 1 to display the letter values OR');
    writeln('     press 4 to view the tile queue OR');
    writeln('     press 7 to view your tiles again OR');
    writeln('     press 0 to fill hand and stop the game.');
    write('> ');
    readln(Choice);
    writeln;
    Choice := UpperCase(Choice);

    if(CheckIfWordIsAppropriate(Choice)) then
      begin
        //Word is fine. Return choice as normal
        GetChoice := Choice;
      end
    else
      begin
        //Word is inappropriate, inform user, request new input (recursively)
        writeLn('The word you entered is inappropriate, please try again');
        GetChoice := GetChoice();
        //Note: Persistently inappropriate users could probably create a stack overflow, but AQA probably won't notice
      end;

  end;


Java:

//M Clarke - Thomas Adams College
//This requires a pre-existing banned.txt file with a list of inappropriate words
//please generate this before attempting to run the program
//all methods already exist except this one below. Adjust pre-existing methods as needed
//you will need to import a specific library for this. the aqaconsole scanner takes all inputted values and penalises you for an invalid input even if you delete the input of the text handle screen prior to entering

import java.util.Scanner;
//put this with the import methods at the start of the program prior to any methods
    String[] loadDisAllowedWords()
    {
        //similar set up/replicated the aqaWordsMethod but altered as needed
        String[] disallowedWords = {};
        try {
            Path filePath1 = new File("banned.txt").toPath();
            Charset charset1 = Charset.defaultCharset();        
            List<String> stringList1 = Files.readAllLines(filePath1, charset1);
            disallowedWords = new String[stringList1.size()];
            int count = 0;
            for(String word: stringList1)
            {
                disallowedWords[count] = word.trim().toUpperCase();
                count++;
            }
        } catch (IOException e) {
            //notifies user if the file is missing
            System.out.println("The aqabannedwords Text File is missing, please download it before continuing.");
            System.exit(0);
        }
        return disallowedWords;            
    }

void haveTurn(String playerName, Tiles playerTiles, 
    TileCount playerTilesPlayed, Score playerScore, Map tileDictionary, 
    QueueOfTiles tileQueue, String[] allowedWords, int maxHandSize, 
    int noOfEndOfTurnTiles, String[] disallowedWords, String[] maxWordScore, String autoWin)
    {
        Scanner scan=new Scanner(System.in);
        Console.println();
        Console.println(playerName + ", it is your turn.");
        displayTilesInHand(playerTiles.playerTiles);
        String newTileChoice = "2";
        boolean validChoice = false;
        boolean validWord;
        boolean validInput=false;
        boolean validCheck=false;
        String choice=null;
        while (!validChoice)
        {
            while(validInput==false){
                choice = getChoice();
                while(validCheck==false){
                    System.out.println("");
                    System.out.println("Do you want "+choice+" to be your input? Y/N?");
                    //extended beyond where it initially is. validation on the user input.
                    try{
                        String res=scan.nextLine();
                        res=res.toUpperCase();
                        if(String.valueOf(res.charAt(0)).equals("Y")){
                            System.out.println("");
                            validInput=true;
                            validCheck=true;
                        }
                        else if(String.valueOf(res.charAt(0)).equals("N")){
                            validCheck=true;
                        }
                        else{
                            System.out.println("INVALID INPUT!");
                        }
                    }
                    catch(Exception e){
                        System.out.println("INVALID INPUT!");
                    }
                }
            }
            boolean inapp=false;
            for(int x=0;x<40;x++){
                if((choice.toUpperCase()).equals(disallowedWords[x].toUpperCase())){
                    inapp=true;
                    Console.println("**INAPPROPRIATE WORD INPUTTED**");
                }
            }
            if(inapp==false){
            //this goes around the choice input block. If the word isn't an invalid choice, then the system checks against the typical responses

            }
            else{ // response if the input is a inappropriate word
                Console.println();
                playerScore.score -= 5;
                //users penalised for attempting to put in a rude word
                Console.println("Not a valid attempt, you lose your turn.");
                Console.println();
                Console.println();
                Console.println("Your word is invalid");
                Console.println("Your new score is:" + playerScore.score);
                Console.println("You have played " + playerTilesPlayed.numberOfTiles + " tiles so far in this game.");
                validChoice = true;
            }
        }
    }

 Main()
    {
        Console.println("++++++++++++++++++++++++++++++++++++++");
        Console.println("+ Welcome to the WORDS WITH AQA game +");
        Console.println("++++++++++++++++++++++++++++++++++++++");
        Console.println();
        Console.println();
        String[] allowedWords = loadAllowedWords();
        String[] disallowedWords = loadDisAllowedWords(); 
        //essential call for the system to recognise that new secondary dictionary is in place
        Map tileDictionary = createTileDictionary();
        int maxHandSize = 20;
        int maxTilesPlayed = 50;
        int noOfEndOfTurnTiles = 3;
        int startHandSize = 15;
        String choice = "";
        while(!choice.equals("9"))
        {
            displayMenu();
            Console.println("Enter your choice: ");
            choice = Console.readLine();
            if (choice.equals("1"))
            {
                autoWin=allowedWords[rng.nextInt(allowedWords.length)];
                playGame(allowedWords, tileDictionary, true, startHandSize, 
                    maxHandSize, maxTilesPlayed, noOfEndOfTurnTiles, disallowedWords);
                //passes secondary dictionary to the appropriate method so system can verify word input
            }
            else if (choice.equals("2"))
            {
                autoWin=allowedWords[rng.nextInt(allowedWords.length)];
                playGame(allowedWords, tileDictionary, false, 15, maxHandSize, 
                    maxTilesPlayed, noOfEndOfTurnTiles, disallowedWords);
            }
        }
    }


Python:

def GetChoice():
  FilteredWords = ["WORD1","WORD2","WORD3","WORD4"]
  print()
  print("Either:")
  print("     enter the word you would like to play OR")
  print("     press 1 to display the letter values OR")
  print("     press 4 to view the tile queue OR")
  print("     press 7 to view your tiles again OR")
  print("     press 0 to fill hand and stop the game.")
  Choice = input(">")
  print()
  Choice = Choice.upper()
  
  if Choice in FilteredWords:
    print()
    print("Your word has been filtered, please try again")
    return GetChoice()
  
  return Choice


VB.NET:

'The inappropriate words are stored in a text file and loaded into list
'Works the same way as the allowed words list, is created in main and passed through PlayGame and then into HaveTurn
'Functions are called in HaveTurn that determine whether the players' word is inappropriate and therefore invalid

Module WordsWithAQA
    Sub Main()
        Dim MaxHandSize As Integer
        Dim MaxTilesPlayed As Integer
        Dim NoOfEndOfTurnTiles As Integer
        Dim StartHandSize As Integer
        Dim Choice As String
        Dim AllowedWords As New List(Of String)
        Dim InappropriateWords As New List(Of String)
        Dim TileDictionary As New Dictionary(Of Char, Integer)()
        Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
        Console.WriteLine("+ Welcome to the WORDS WITH AQA game +")
        Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
        Console.WriteLine()
        Console.WriteLine()
        LoadAllowedWords(AllowedWords)
        LoadInappropriateWords(InappropriateWords) 'call a subroutine that will load inappropriate words from a file
        TileDictionary = CreateTileDictionary()
        MaxHandSize = 20
        MaxTilesPlayed = 50
        NoOfEndOfTurnTiles = 3
        StartHandSize = 15
        Choice = ""
        While Choice <> "9"
            DisplayMenu()
            Console.Write("Enter your choice: ")
            Choice = Console.ReadLine()
            If Choice = "1" Then
                PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, InappropriateWords)
            ElseIf Choice = "2" Then
                PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, InappropriateWords)
            End If
        End While
    End Sub

    Sub LoadInappropriateWords(ByRef InappropriateWords As List(Of String))
        Try
            Dim FileReader As New System.IO.StreamReader("inappropriatewords.txt")
            While FileReader.EndOfStream <> True
                InappropriateWords.Add(FileReader.ReadLine().Trim().ToUpper())
            End While
            FileReader.Close()
        Catch
            InappropriateWords.Clear()
        End Try
    End Sub

    Sub HaveTurn(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer, ByVal TileDictionary As 
    Dictionary(Of Char, Integer), ByRef TileQueue As QueueOfTiles, ByRef AllowedWords As List(Of String), ByVal MaxHandSize As Integer, ByVal NoOfEndOfTurnTiles As 
    Integer, ByRef InappropriateWords As List(Of String))
        Dim NewTileChoice As String
        Dim Choice As String
        Dim ValidChoice As Boolean
        Dim ValidWord As Boolean
        Dim Inappropriate As Boolean = False
        Console.WriteLine()
        Console.WriteLine(PlayerName & " it is your turn.")
        DisplayTilesInHand(PlayerTiles)
        NewTileChoice = "2"
        ValidChoice = False
        While Not ValidChoice
            Choice = GetChoice()
            If Choice = "1" Then
                DisplayTileValues(TileDictionary, AllowedWords)
            ElseIf Choice = "4" Then
                TileQueue.Show()
            ElseIf Choice = "7" Then
                DisplayTilesInHand(PlayerTiles)
            ElseIf Choice = "0" Then
                ValidChoice = True
                FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
            Else
                ValidChoice = True
                If Len(Choice) = 0 Then
                    ValidWord = False
                Else
                    ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
                End If
                If ValidWord Then
                    Inappropriate = CheckIfWordIsInappropriate(Choice, InappropriateWords) 'the function call to check the word
                    If Inappropriate = False Then
                        ValidWord = CheckWordIsValid(Choice, AllowedWords) 'the CheckWordIsValid call is only made if the word is appropriate
                    Else
                        Console.WriteLine("That is an inappropriate word and will be classed as invalid.")
                        ValidWord = False 'if the word is inappropriate, it is classed as an invalid word
                    End If
                    If ValidWord Then
                        Console.WriteLine()
                        Console.WriteLine("Valid word")
                        Console.WriteLine()
                        UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
                        NewTileChoice = GetNewTileChoice()
                    End If
                End If
                If Not ValidWord Then
                    Console.WriteLine()
                    Console.WriteLine("Not a valid attempt, you lose your turn.")
                    Console.WriteLine()
                End If
                If NewTileChoice <> "4" Then
                    AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
                End If
                Console.WriteLine()
                Console.WriteLine("Your word was: " & Choice)
                Console.WriteLine("Your new score is: " & PlayerScore)
                Console.WriteLine("You have played " & PlayerTilesPlayed & " tiles so far in this game.")
            End If
        End While
    End Sub

    'below is the function that checks the word
    Function CheckIfWordIsInappropriate(ByVal Word As String, ByRef InappropriateWords As List(Of String)) As Boolean
        Dim Inappropriate As Boolean = False
        Dim Count As Integer
        Count = 0

        While Count < InappropriateWords.Count And Not Inappropriate
            If InappropriateWords(Count) = Word Then 'looping through the list of inappropriate words the user input is checked against them
                Inappropriate = True
            End If
            Count += 1
        End While

        Return Inappropriate
    End Function


Ask user to confirm their word choice

edit

Ask the user to confirm their word choice or go back and change it if they made a typo etc.

C#:

        private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles)
        {
            Console.WriteLine();
            Console.WriteLine(PlayerName + " it is your turn.");
            DisplayTilesInHand(PlayerTiles);
            string NewTileChoice = "2";
            bool ValidChoice = false;
            bool ValidWord = false;
--------------------------------------------------------------------------------------
            string Choice = "";
--------------------------------------------------------------------------------------
            string strConfirm = "";
            while (!ValidChoice)
            {
                Choice = GetChoice();
                if (Choice == "1")
                {
                    DisplayTileValues(TileDictionary, AllowedWords);
                }
                else if (Choice == "4")
                {
                    TileQueue.Show();
                }
                else if (Choice == "7")
                {
                    DisplayTilesInHand(PlayerTiles);
                }
                else if (Choice == "0")
                {
                    ValidChoice = true;
                    FillHandWithTiles(ref TileQueue, ref PlayerTiles, MaxHandSize);
                }
                else
                {
                    ValidChoice = true;
                    if (Choice.Length == 0)
                    {
                        ValidWord = false;
                    }
                    else
                    {
--------------------------------------------------------------------------------------
                        while(strConfirm != "YES")
                        {
                            Console.WriteLine("Are you sure you want to use " + Choice + "? (YES or NO)");
                            strConfirm = Console.ReadLine().ToUpper();
                            if(strConfirm == "NO")
                            {
                                Console.WriteLine("Re-Input Word");
                                Choice = Console.ReadLine().ToUpper();
                            }
                        }
--------------------------------------------------------------------------------------
                        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles);
                    }
                    if (ValidWord)
                    {
                        ValidWord = CheckWordIsValid(Choice, AllowedWords);
                        if (ValidWord)
                        {
                            Console.WriteLine();
                            Console.WriteLine("Valid word");
                            Console.WriteLine();
                            UpdateAfterAllowedWord(Choice, ref PlayerTiles, ref PlayerScore, ref PlayerTilesPlayed, TileDictionary, AllowedWords);
                            NewTileChoice = GetNewTileChoice();
                        }
                    }
                    if (!ValidWord)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Not a valid attempt, you lose your turn.");
                        Console.WriteLine();
                    }
                    if (NewTileChoice != "4")
                    {
                        AddEndOfTurnTiles(ref TileQueue, ref PlayerTiles, NewTileChoice, Choice);
                    }
                    Console.WriteLine();
                    Console.WriteLine("Your word was:" + Choice);
                    Console.WriteLine("Your new score is:" + PlayerScore);
                    Console.WriteLine("You have played " + PlayerTilesPlayed + " tiles so far in this game.");
                }
            }
        }


Delphi/Pascal:

procedure HaveTurn(PlayerName : string; var PlayerTiles : string; var PlayerTilesPlayed : integer;
var PlayerScore : integer; TileDictionary : TTileDictionary; var TileQueue : QueueOfTiles;
var AllowedWords : TStringArray; MaxHandSize : integer; NoOfEndOfTurnTiles : integer);
  var
    NewTileChoice : string;
    ValidChoice : boolean;
    ValidWord : boolean;
    Choice : string;
    ConfirmationInput : string; //Add a variable to store the user's reply to whether or not they confirm their choice
  begin
    writeln;
    writeln(PlayerName, ' it is your turn.');
    DisplayTilesInHand(PlayerTiles);
    NewTileChoice := '2';
    ValidChoice := False;
    ConfirmationInput:=''; //Assign the variable to an empty string by default (they haven't confirmed yet)
    while not ValidChoice do
      begin
        Choice := GetChoice();
        if Choice = '1' then
          DisplayTileValues(TileDictionary, AllowedWords)
        else if Choice = '4' then
          TileQueue.Show()
        else if Choice = '7' then
          DisplayTilesInHand(PlayerTiles)
        else if Choice = '0' then
          begin
            ValidChoice := True;
            FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize);
          end
        else
          begin
            ValidChoice := True;
            if length(Choice) = 0 then
              ValidWord := False
            else
              //Add the check to this if/else statement so that it runs whenever they had previously input a valid word
              begin
                repeat
                  writeLn('Are you sure you would like to use ''' + Choice + ''' as your choice? Enter ''y'' to confirm.');
                  readLn(ConfirmationInput);
                  if(ConfirmationInput <> 'y') then
                    begin
                      writeLn('Please input your new choice of word: ');
                      readLn(Choice);
                      Choice := UpperCase(Choice);
                    end;
                until ConfirmationInput = 'y';

                ValidWord := CheckWordIsInTiles(Choice, PlayerTiles);
              end;

            if ValidWord then
              begin
                ValidWord := CheckWordIsValid(Choice, AllowedWords);
                if ValidWord then
                  begin
                    writeln;
                    writeln('Valid word');
                    writeln;
                    UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords);
                    NewTileChoice := GetNewTileChoice();
                  end;
              end;
            if not ValidWord then
              begin
                writeln;
                writeln('Not a valid attempt, you lose your turn.');
                writeln;
              end;
            if not(NewTileChoice = '4') then
              AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice);
            writeln;
            writeln('Your word was: ', Choice);
            writeln('Your new score is: ', PlayerScore);
            writeln('You have played ', PlayerTilesPlayed, ' tiles so far in this game.');
          end;
      end;
  end;


Java:

void haveTurn(String playerName, Tiles playerTiles, 
            TileCount playerTilesPlayed, Score playerScore, Map tileDictionary, 
            QueueOfTiles tileQueue, String[] allowedWords, int maxHandSize, 
            int noOfEndOfTurnTiles)
    {
      Console.println();
      Console.println(playerName + " it is your turn.");
      displayTilesInHand(playerTiles.playerTiles);
      String newTileChoice = "2";
      boolean validChoice = false;
      boolean validWord;
      while (!validChoice)
      {
        String choice = getChoice();
        if (choice.equals("1"))
        {
          displayTileValues(tileDictionary, allowedWords);
        }
        else if (choice.equals("4"))
        {
          tileQueue.show();
        }
        else if (choice.equals("7"))
        {
          displayTilesInHand(playerTiles.playerTiles);
        }
        else if (choice.equals("0"))
        {
          validChoice = true;
          fillHandWithTiles(tileQueue, playerTiles, maxHandSize);
        }
        else
        {                
                Console.println();
                Console.println("Are you sure? Y or N");
                String ans = Console.readLine().toUpperCase();
                
                if (ans.equals("Y")) {
                
          validChoice = true;
          if (choice.length() == 0)
          {
            validWord = false;
          }
          else
          {
            validWord = checkWordIsInTiles(choice, playerTiles.playerTiles);
          }
          if (validWord)
          {
            validWord = checkWordIsValid(choice, allowedWords);
            if (validWord)
            {
              Console.println();
              Console.println("Valid word");
              Console.println();
              updateAfterAllowedWord(choice, playerTiles, playerScore, 
                      playerTilesPlayed, tileDictionary, allowedWords);
              newTileChoice = getNewTileChoice();
            }
          }
          if (!validWord)
          {
            Console.println();
            Console.println("Not a valid attempt, you lose your turn.");
            Console.println();
          }
          if (!newTileChoice.equals("4"))
          {
            addEndOfTurnTiles(tileQueue, playerTiles, newTileChoice, choice);
          }
          Console.println();
          Console.println("Your word was:" + choice);
          Console.println("Your new score is:" + playerScore.score);
          Console.println("You have played " + playerTilesPlayed.numberOfTiles + " tiles so far in this game.");
        }
                else if (ans.equals("N"))
                {
                displayTilesInHand(playerTiles.playerTiles);
                getChoice();
                }
                else
                {
                    Console.println();
                    Console.println("Invalid Choice!");
                    displayTilesInHand(playerTiles.playerTiles);
                    getChoice();
                }
        }
        }
      }


Python:

def HaveTurn(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles):
	print()
	print(PlayerName, "it is your turn.")
	DisplayTilesInHand(PlayerTiles)
	NewTileChoice = "2"
	ValidChoice = False
	while not ValidChoice:
		Choice = GetChoice()
		if Choice == "1":
			DisplayTileValues(TileDictionary, AllowedWords)
		elif Choice == "4":
			TileQueue.Show()
		elif Choice == "7":
			DisplayTilesInHand(PlayerTiles)      
		elif Choice == "0":
			ValidChoice = True
			TileQueue, PlayerTiles = FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
###
		elif Choice != input("Confirm the word entered:\n>").upper():
			print("Chosen words do not match, please try again")
			print()
###
		else:
			ValidChoice = True
			if len(Choice) == 0:
				ValidWord = False
			else:
				ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
			if ValidWord:
				ValidWord = CheckWordIsValid(Choice, AllowedWords)
				if ValidWord:
					print()
					print("Valid word")
					print()
					PlayerTiles, PlayerScore, PlayerTilesPlayed = UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
					NewTileChoice = GetNewTileChoice()
			if not ValidWord:
				print()
				print("Not a valid attempt, you lose your turn.")
				print()
			if NewTileChoice != "4":
				TileQueue, PlayerTiles = AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
			print()
			print("Your word was:", Choice)
			print("Your new score is:", PlayerScore)
			print("You have played", PlayerTilesPlayed, "tiles so far in this game.")
	return PlayerTiles, PlayerTilesPlayed, PlayerScore, TileQueue


Python 2:

# Idris Khan Oak Acadmey
# put this in getChoice() just before choice.upper assignement    
    confirmed = False  # flag that controls the loop
    while not confirmed:
        confirmation = input('press enter to confirm your choice, '
                             'press (x) to enter again or type your new word\n>')
        if confirmation.lower() == 'x':
            confirmed = False  
        elif confirmation.lower() == '':
            confirmed = True  # if user presses enter, take this as confirmed choice
        elif confirmation.lower().isalpha():  # checks to see if input contains alphabetical characters
            choice = confirmation  # take the user's new word as choice
            confirmed = True 

def HaveTurn(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles): 
  print()
  print(PlayerName, "it is your turn.") 
  DisplayTilesInHand(PlayerTiles)
  NewTileChoice = "2"
  ValidChoice = False
  while not ValidChoice:
    confirmed = False
    while not confirmed:
      Choice = GetChoice()
      if Choice == "1":
        DisplayTileValues(TileDictionary, AllowedWords)
      elif Choice == "4":
        TileQueue.Show()
      elif Choice == "7":
        DisplayTilesInHand(PlayerTiles)      
      elif Choice == "0":
        ValidChoice = True
        TileQueue, PlayerTiles = FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
      else:
        confirmation = input("Press enter to confirm your choice, press x to enter again\n>") # unfortunately this will take the users first input, (e.g misspelt "hand" as "hnad"), and not the new confirmed one
        if confirmation.lower() == "x":
          confirmed = False
        else: 
          confirmed = True
    ValidChoice = True
    if len(Choice) == 0:
      ValidWord = False 
    else:
      ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
    if ValidWord:
      ValidWord = CheckWordIsValid(Choice, AllowedWords)
      if ValidWord:
        print()
        print("Valid word")
        print()
        PlayerTiles, PlayerScore, PlayerTilesPlayed = UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
        NewTileChoice = GetNewTileChoice()
    if not ValidWord:
      print()
      print("Not a valid attempt, you lose your turn.")
      print()
    if NewTileChoice != "4":
      TileQueue, PlayerTiles = AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
    print()
    print("Your word was:", Choice)
    print("Your new score is:", PlayerScore)
    print("You have played", PlayerTilesPlayed, "tiles so far in this game.")
  return PlayerTiles, PlayerTilesPlayed, PlayerScore, TileQueue


VB.NET:

Sub HaveTurn(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer, ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef TileQueue As QueueOfTiles, ByRef AllowedWords As List(Of String), ByVal MaxHandSize As Integer, ByVal NoOfEndOfTurnTiles As Integer)
        Dim NewTileChoice As String
        Dim Choice As String
        Dim ValidChoice As Boolean
        Dim ValidWord As Boolean
----------------------------------------------------------------------------------------------------------------------------
        Dim Confirm As String
----------------------------------------------------------------------------------------------------------------------------
        Console.WriteLine()
        Console.WriteLine(PlayerName & " it is your turn.")
        DisplayTilesInHand(PlayerTiles)
        NewTileChoice = "2"
        ValidChoice = False
        While Not ValidChoice
            Choice = GetChoice()
            If Choice = "1" Then
                DisplayTileValues(TileDictionary, AllowedWords)
            ElseIf Choice = "4" Then
                TileQueue.Show()
            ElseIf Choice = "7" Then
                DisplayTilesInHand(PlayerTiles)
            ElseIf Choice = "0" Then
                ValidChoice = True
                FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
            ElseIf Choice = "9" Then
                ValidChoice = True
                SaveGame(PlayerTiles, PlayerScore, PlayerTiles, PlayerScore, TileQueue)
            Else
                ValidChoice = True
                If Len(Choice) = 0 Then
                    ValidWord = False
                Else
---------------------------------------------------------------------------------------------------------------------------------------
                    While Confirm <> "YES"
                        Console.WriteLine("Are you sure you want to use " & Choice & "? (Yes or No)")
                        Confirm = Console.ReadLine().ToUpper
                        If Confirm = "NO" Then
                            Console.WriteLine("Re-Input Word")
                            Choice = Console.ReadLine().ToUpper
                        End If
                    End While
---------------------------------------------------------------------------------------------------------------------------------------
                    ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
                End If
                If ValidWord Then
                    ValidWord = CheckWordIsValid(Choice, AllowedWords)
                    If ValidWord Then
                        Console.WriteLine()
                        Console.WriteLine("Valid word")
                        Console.WriteLine()
                        UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
                        NewTileChoice = GetNewTileChoice()
                    End If
                End If
                If Not ValidWord Then
                    Console.WriteLine()
                    Console.WriteLine("Not a valid attempt, you lose your turn.")
                    Console.WriteLine()
                End If
                If NewTileChoice <> "4" Then
                    AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
                End If
                Console.WriteLine()
                Console.WriteLine("Your word was: " & Choice)
                Console.WriteLine("Your new score is: " & PlayerScore)
                Console.WriteLine("You have played " & PlayerTilesPlayed & " tiles so far in this game.")
            End If
        End While
    End Sub
'There's probably a better way to do this, but this is one solution.
Michael Slater - Jumeirah College


Prevent duplication of words being entered

edit

Description of question

C#:

The have turn method was edited, such that it adds a valid word to a used word list and then checks each time the user inputs a word if it is inside of this list, if not it has not been used before and is a valid word.

 private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles, ref List<string> UsedWords)
{

...
                else
                {
                    ValidChoice = true;
                    if (Choice.Length == 0)	
                    {
                        ValidWord = false;
                    }
                    else 
                    {
                        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles);	
                    }
                    if (ValidWord)	
                    {
                        ValidWord = CheckWordIsValid(Choice, AllowedWords); 
                        if (ValidWord)
                        {
                            ValidWord = !UsedWords.Contains(Choice); //If the word is not in the used words file, make valid choice true.
                            if (ValidWord) 
                            {
                                UsedWords.Add(Choice); //If the word was valid, add it to the used word list
                                Console.WriteLine();
                                Console.WriteLine("Valid word");
                                Console.WriteLine();
                                UpdateAfterAllowedWord(Choice, ref PlayerTiles, ref PlayerScore, ref PlayerTilesPlayed, TileDictionary, AllowedWords);
                                NewTileChoice = GetNewTileChoice(); 
                            }
                            else
                            {
                                Console.WriteLine("This word has already been played by another player.");
                            }
                        }
                    }
...

The PlayGame method is also amended to contain the UsedWords string list, this stores all of the valid words which have been played and is passed to the HaveTurn function by ref.

 private static void PlayGame(List<string> AllowedWords, Dictionary<char, int> TileDictionary, bool RandomStart, int StartHandSize, int MaxHandSize, int MaxTilesPlayed, int NoOfEndOfTurnTiles)
        {
            List<string> UsedWords = new List<string>(); //Used to store the used valid words
...
while (PlayerOneTilesPlayed <= MaxTilesPlayed && PlayerTwoTilesPlayed <= MaxTilesPlayed && PlayerOneTiles.Length < MaxHandSize && PlayerTwoTiles.Length < MaxHandSize)
            {
                HaveTurn("Player One", ref PlayerOneTiles, ref PlayerOneTilesPlayed, ref PlayerOneScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, ref UsedWords);
                Console.WriteLine();
                Console.WriteLine("Press Enter to continue");
                Console.ReadLine();
                Console.WriteLine();
                HaveTurn("Player Two", ref PlayerTwoTiles, ref PlayerTwoTilesPlayed, ref PlayerTwoScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, ref UsedWords);
            }


Delphi/Pascal:


Java:

//By Tekashi 6ix9ine of Scum Gang Sixth Form
//this is literally like the c# example but the List is an ArrayList and modified to suit the java syntax rules
 void haveTurn(String playerName, Tiles playerTiles, 
            TileCount playerTilesPlayed, Score playerScore, Map tileDictionary, 
            QueueOfTiles tileQueue, String[] allowedWords, int maxHandSize, 
            int noOfEndOfTurnTiles, ArrayList usedWords)
    {
...
          if (validWord)
          {
            validWord = checkWordIsValid(choice, allowedWords);
            if (validWord) { 
                validWord = !usedWords.contains(choice); //this just makes the valid choice true
                if (validWord)
            {
              usedWords.add(choice); //adds it to the arraylist
              Console.println();
              Console.println("Valid word");
              Console.println();
              updateAfterAllowedWord(choice, playerTiles, playerScore, 
                      playerTilesPlayed, tileDictionary, allowedWords);
              newTileChoice = getNewTileChoice();
            }
            else {
              Console.writeLine("This word has already been played by another player."); //hey presto!!!
            }
            }
          }
...

//later on in the program

    void playGame(String[] allowedWords, Map tileDictionary, boolean randomStart, 
            int startHandSize, int maxHandSize, int maxTilesPlayed, 
            int noOfEndOfTurnTiles)
    {
      ArrayList<String> usedWords = new ArrayList<>(); //declare it here
      ...
while (playerOneTilesPlayed.numberOfTiles <= maxTilesPlayed && 
              playerTwoTilesPlayed.numberOfTiles <= maxTilesPlayed && 
              playerOneTiles.playerTiles.length() < maxHandSize && 
              playerTwoTiles.playerTiles.length() < maxHandSize)
      {
        haveTurn("Player One", playerOneTiles, playerOneTilesPlayed, playerOneScore, 
                tileDictionary, tileQueue, allowedWords, maxHandSize, 
                noOfEndOfTurnTiles, usedWords); //add here
        Console.println();
        Console.println("Press Enter to continue");
        Console.readLine();
        Console.println();
        haveTurn("Player Two", playerTwoTiles, playerTwoTilesPlayed, playerTwoScore, 
                tileDictionary, tileQueue, allowedWords, maxHandSize, 
                noOfEndOfTurnTiles, usedWords); //ditto
      }
...


Python:

#BY HAMMAD MEHMOOD, HERSCHEL GRAMMAR SCHOOL
#All the changes have comments
def PlayGame(AllowedWords, TileDictionary, RandomStart, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles):
  PlayerOneScore = 50
  PlayerTwoScore = 50
  PlayerOneTilesPlayed = 0
  PlayerTwoTilesPlayed = 0
  TileQueue = QueueOfTiles(20)
  WordsUsed = [] #New list to store the valid words used
  #Pass the list in HaveTurn as a parameter
  if RandomStart:
    PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize)
    PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize)
  else:
    PlayerOneTiles = "BTAHANDENONSARJ"
    PlayerTwoTiles = "CELZXIOTNESMUAA"   
  while PlayerOneTilesPlayed <= MaxTilesPlayed and PlayerTwoTilesPlayed <= MaxTilesPlayed and len(PlayerOneTiles) < MaxHandSize and len(PlayerTwoTiles) < MaxHandSize:
    PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileQueue = HaveTurn("Player One", PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, WordsUsed)
    print()
    input("Press Enter to continue")
    print()
    PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileQueue = HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, WordsUsed)
  PlayerOneScore = UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
  PlayerTwoScore = UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
  DisplayWinner(PlayerOneScore, PlayerTwoScore)
def HaveTurn(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, WordsUsed):
  # The list WordsUsed is an argument in this function
  print()
  print(PlayerName, "it is your turn.")
  DisplayTilesInHand(PlayerTiles)
  NewTileChoice = "2"
  ValidChoice = False
  while not ValidChoice:
    Choice = GetChoice()
    if Choice == "1":
      DisplayTileValues(TileDictionary, AllowedWords)
    elif Choice == "4":
      TileQueue.Show()
    elif Choice == "7":
      DisplayTilesInHand(PlayerTiles)      
    elif Choice == "0":
      ValidChoice = True
      TileQueue, PlayerTiles = FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
    else:
      ValidChoice = True
      if len(Choice) == 0:
        ValidWord = False
      else:
        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
      if ValidWord:
        ValidWord = CheckWordIsValid(Choice, AllowedWords, WordsUsed)#Pass the list a parameter
        if ValidWord:
          WordsUsed.append(Choice) #Add the word used in the list
          print()
          print("Valid word")
          print()
          PlayerTiles, PlayerScore, PlayerTilesPlayed = UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
          NewTileChoice = GetNewTileChoice()
      if not ValidWord:
        print()
        print("Not a valid attempt, you lose your turn.")
        print()
      if NewTileChoice != "4":
        TileQueue, PlayerTiles = AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
      print()
      print("Your word was:", Choice)
      print("Your new score is:", PlayerScore)
      print("You have played", PlayerTilesPlayed, "tiles so far in this game.")
  return PlayerTiles, PlayerTilesPlayed, PlayerScore, TileQueue  
def CheckWordIsValid(Word, AllowedWords, WordsUsed):
  ValidWord = False
  Count = 0
  while Count < len(AllowedWords) and not ValidWord:
    if AllowedWords[Count] == Word:
      ValidWord = True
    Count += 1
  if Word in WordsUsed: #If the word is in the list (it has already been used)
    print("Sorry the word",Word,"has already been used once") #Tell the user
    ValidWord = False #Word is not valid
  return ValidWord
#BY HAMMAD MEHMOOD, HERSCHEL GRAMMAR SCHOOL


VB.NET:

'This involves one new function being created and called just after the CheckWordIsValid call. pretty gnar
'SLAAAAAAAAAAAYEEEEEEER !!!

    Public WordCount As Integer 'this count, starting from 0, counts how many words have been played that are unique
    Public UsedWordsArray(WordCount) As String 'this array holds the words that have already been entered
    'I declared these as public variables as the values they hold need to be kept for both players turns
    'and declaring them in the functions/subroutines means the values they hold get cleared when that instruction set is called
    Sub Main()
        Dim MaxHandSize As Integer
        Dim MaxTilesPlayed As Integer
        Dim NoOfEndOfTurnTiles As Integer
        Dim StartHandSize As Integer
        Dim Choice As String
        Dim AllowedWords As New List(Of String)
        Dim TileDictionary As New Dictionary(Of Char, Integer)()
        Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
        Console.WriteLine("+ Welcome to the WORDS WITH AQA game +")
        Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
        Console.WriteLine()
        Console.WriteLine()
        LoadAllowedWords(AllowedWords)
        TileDictionary = CreateTileDictionary()
        MaxHandSize = 20
        MaxTilesPlayed = 50
        NoOfEndOfTurnTiles = 3
        StartHandSize = 15
        Choice = ""
        While Choice <> "9"
            DisplayMenu()
            Console.Write("Enter your choice: ")
            Choice = Console.ReadLine()
            If Choice = "1" Then
                PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
            ElseIf Choice = "2" Then
                PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
            End If
        End While
    End Sub

    Function CheckWordIsUnused(ByVal Word As String)
        Dim ValidWord As Boolean
        Dim UsedWord As Boolean = False
        For i = 0 To WordCount 'the array containing used words is looped through and the player's input word is compared to each
            If Word = UsedWordsArray(i) Then
                UsedWord = True
                ValidWord = False 'if the word has already been used, the input is an invalid word
                Console.WriteLine("That word has already been used.") 'the player is informed that they are bad at a word game
                Exit For
            End If
        Next

        If UsedWord = False Then 'if the word has not been used yet, it is added to the array
            WordCount = WordCount + 1 'the size of the array is incremented by one to make space for the next possible word
            ReDim Preserve UsedWordsArray(WordCount) 'the array is redefined with this new length and preserved to keep the used words in it
            UsedWordsArray(WordCount - 1) = Word 'the player's word is put into the next available space
            ValidWord = True
        End If

        Return ValidWord
    End Function

    Sub HaveTurn(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer, ByVal TileDictionary 
        As Dictionary(Of Char, Integer), ByRef TileQueue As QueueOfTiles, ByRef AllowedWords As List(Of String), ByVal MaxHandSize As Integer, ByVal 
        NoOfEndOfTurnTiles As Integer)
        Dim NewTileChoice As String
        Dim Choice As String
        Dim ValidChoice As Boolean
        Dim ValidWord As Boolean
        Console.WriteLine()
        Console.WriteLine(PlayerName & " it is your turn.")
        DisplayTilesInHand(PlayerTiles)
        NewTileChoice = "2"
        ValidChoice = False
        While Not ValidChoice
            Choice = GetChoice()
            If Choice = "1" Then
                DisplayTileValues(TileDictionary, AllowedWords)
            ElseIf Choice = "4" Then
                TileQueue.Show()
            ElseIf Choice = "7" Then
                DisplayTilesInHand(PlayerTiles)
            ElseIf Choice = "0" Then
                ValidChoice = True
                FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
            Else
                ValidChoice = True
                If Len(Choice) = 0 Then
                    ValidWord = False
                Else
                    ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
                End If
                If ValidWord Then
                    ValidWord = CheckWordIsValid(Choice, AllowedWords)
                    If ValidWord Then
                        ValidWord = CheckWordIsUnused(Choice) 'the function is called that checks if the input word has already been used
                        If ValidWord Then
                            Console.WriteLine()
                            Console.WriteLine("Valid word")
                            Console.WriteLine()
                            UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
                            NewTileChoice = GetNewTileChoice()
                        End If
                    End If
                End If
                If Not ValidWord Then
                    Console.WriteLine()
                    Console.WriteLine("Not a valid attempt, you lose your turn.")
                    Console.WriteLine()
                End If
                If NewTileChoice <> "4" Then
                    AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
                End If
                Console.WriteLine()
                Console.WriteLine("Your word was: " & Choice)
                Console.WriteLine("Your new score is: " & PlayerScore)
                Console.WriteLine("You have played " & PlayerTilesPlayed & " tiles so far in this game.")
            End If
        End While
    End Sub


Bonus points if two or more high-scoring tiles are played ie JAZZ (J=5, A=1 ,Z=5, Z=5)

edit

Description of question...

C#:

private static int GetScoreForWord(string Word, Dictionary<char, int> TileDictionary)	
{
    int Score = 0;  
	int HighScoring = 0;	//Used to record the number of high-scoring points
    for (int Count = 0; Count < Word.Length; Count++)	
    {
        Score = Score + TileDictionary[Word[Count]];    
		if(TileDictionary[Word[Count]] >= 5)	//If 5 points or more
		{
			HighScoring++;	//Add one to HighScoring
		}
	}
    if (Word.Length > 7)	
    {
        Score = Score + 20;	
    }
    else if (Word.Length > 5)	
    {
        Score = Score + 5;	
    }

	if (HighScoring >= 2)	//If more than two letters were high-scoring
	{
		Score += HighScoring * 2; //Score = Score + High-score * 2
	}
   
	return Score;	
}


Delphi/Pascal:

function GetScoreForWord(Word : string; TileDictionary : TTileDictionary) : integer;
  var
    Score : integer;
    Count,value,count1: integer;
    hs:string;
  begin
    Score := 0;
    value:= 0 ;
    for Count := 1 to length(Word) do
      Score := Score + TileDictionary.FindTileScore(Word[Count]);
    if length(Word) > 7 then
      Score := Score + 20
    else if length(Word) > 5 then
      Score := Score + 5;
  begin
    hs:= 'JZ';
    for count := 1 to length(hs) do
      begin
        for count1:= 1 to length(word) do
          if word[count1] = hs[count] then
              value := value + 1;
      end;
  end;
    if value >= 2 then
      begin
        score := score + 1;
        writeln('additional score addd');
      end;

    GetScoreForWord := Score;
  end;
// developed by Samuel Ajayi Graveney school.


Java:

//c#
    int getScoreForWord(String word, Map tileDictionary) {
        int score = 0;
        int highScoring = 0; //for counting
        for (int count = 0; count < word.length(); count++) {
            score += (int) tileDictionary.get(word.charAt(count));
            if ((int) tileDictionary.get(word.charAt(count)) >= 5) { //over a score of 5
                highScoring++; //increment
            }
        }
        if (word.length() > 7) {
            score += 20;
        } else if (word.length() > 5) {
            score += 5;
        }
        
        if (highScoring >= 2) { //more than two
            score += highScoring * 2;
            Console.println("High Score Bonus!");
        }
        return score;
    }


Python:

def GetScoreForWord(Word, TileDictionary):
  Score = 0
  HighScoring = 0
  for Count in range (len(Word)):
    Score += TileDictionary[Word[Count]]
    if TileDictionary[Word[Count]] >= 5:
      HighScoring += 1
  if len(Word) > 7:
    Score += 20
  elif len(Word) > 5:
    Score += 5
  if HighScoring >= 2:
    Score += HighScoring * 2 # 2 extra points per high scoring letter if two or more high-scoring tiles played
  return Score


VB.NET:

'Matthew Woods, Havant College
    Function GetScoreForWord(ByVal Word As String, ByVal TileDictionary As Dictionary(Of Char, Integer)) As Integer
        Dim Score, HighScoreingLetter As Integer 'The new variable for storing HighScore has been added.
        Score = 0
        HighScoreingLetter = 0 'The HighScore variable is initialised as 0
        For Count = 0 To Len(Word) - 1
            Score += TileDictionary(Word(Count))
            If TileDictionary(Word(Count)) >= 5 Then 'A high score is a letter with more than 5 points, the HighScore variable is incremented when this is played.
                HighScoreingLetter += 1
            End If
        Next
        If Len(Word) > 7 Then
            Score += 20
        ElseIf Len(Word) > 5 Then
            Score += 5
        End If
        If HighScoringLetter >= 2 Then 'Finally, if there is more than one high-scoring letter played, the number of high-scoring letters is doubled and added to the score.
            Score += HighScoringLetter * 2
        End If
        Return Score
    End Function
'Matthew Woods, Havant College


Bonus points at end of game for longest and highest-scored words

edit

Description of question

C#:


Delphi/Pascal:


Java:

//M Clarke - Thomas Adams College
//this has the integrations for the inappropriate word filter, deducting points for invalid words, name entering and secret word (This is not fully shown but sections can be assumed to be ignored)


int getScoreForWord(String word, Map tileDictionary, String[][] maxWordScore, String playerName)
    {
        int score = 0;
        for (int count = 0; count < word.length(); count++) 
        {
            score += (int)tileDictionary.get(word.charAt(count));
        }
        if(word.length() > 7)
        {
            score += 20;
        }
        else if(word.length() == 7){
            score += 15;
        }
        else if(word.length() == 6){
            score += 10;
        }
        else if(word.length() == 5)
        {
            score += 5;
        }
        if(score>(Integer.parseInt(maxWordScore[1][0]))){
            maxWordScore[0][0]=word;
            maxWordScore[1][0]=String.valueOf(score);
            maxWordScore[2][0]=playerName;
            Console.println("New Highest Scoring Word Played!");
            Console.println("");
        }
        if(word.length()>Integer.parseInt(maxWordScore[1][1])){
            maxWordScore[0][1]=word;
            maxWordScore[1][1]=String.valueOf(word.length());
            maxWordScore[2][1]=playerName;
        }
        return score;       
    }


void updateAfterAllowedWord(String word, Tiles playerTiles, 
    Score playerScore, TileCount playerTilesPlayed, Map tileDictionary, 
    String[] allowedWords, String[][] maxWordScore, String playerName)
    {
        playerTilesPlayed.numberOfTiles += word.length();
        for(char letter : word.toCharArray())
        {
            playerTiles.playerTiles = playerTiles.playerTiles.replaceFirst(letter + "", "");
        }
        playerScore.score += getScoreForWord(word, tileDictionary, maxWordScore, playerName);      
    }


void haveTurn(String playerName, Tiles playerTiles, 
    TileCount playerTilesPlayed, Score playerScore, Map tileDictionary, 
    QueueOfTiles tileQueue, String[] allowedWords, int maxHandSize, 
    int noOfEndOfTurnTiles, String[] disallowedWords, String[][] maxWordScore, String autoWin, String wordLog[], int wordCount)
    {
        Scanner scan=new Scanner(System.in);
        Console.println();
        Console.println(playerName + ", it is your turn.");
        displayTilesInHand(playerTiles.playerTiles);
        String newTileChoice = "2";
        boolean validChoice = false;
        boolean validWord;
        boolean validInput=false;
        boolean validCheck=false;
        String choice=null;
        while (!validChoice){
            while(validInput==false){
                choice = getChoice();
                validCheck=false;
                while(validCheck==false){
                    System.out.println("");
                    System.out.println("Do you want "+choice+" to be your input? Y/N?");
                    try{
                        String res=scan.nextLine();
                        res=res.toUpperCase();
                        if(String.valueOf(res.charAt(0)).equals("Y")){
                            System.out.println("");
                            validInput=true;
                            validCheck=true;
                        }
                        else if(String.valueOf(res.charAt(0)).equals("N")){
                            validCheck=true;
                        }
                        else{
                            System.out.println("INVALID INPUT!");
                        }
                    }
                    catch(Exception e){
                        System.out.println("INVALID INPUT!");
                    }
                }
            }
            boolean inapp=false;
            validCheck=false;
            validInput=false;
            for(int x=0;x<40;x++){
                if((choice.toUpperCase()).equals(disallowedWords[x].toUpperCase())){
                    inapp=true;
                    Console.println("**INAPPROPRIATE WORD INPUTTED**");
                }
            }
            if(inapp==false){
                if (choice.equals("1"))
                {
                    displayTileValues(tileDictionary, allowedWords);
                }
                else if (choice.equals("4"))
                {
                    tileQueue.show();
                }
                else if(choice.equals("3")){
                    playerTiles.playerTiles=shuffleHand(playerTiles.playerTiles);
                    displayTilesInHand(playerTiles.playerTiles);
                }               
                else if (choice.equals("7"))
                {
                    displayTilesInHand(playerTiles.playerTiles);
                }
                else if (choice.equals("0"))
                {
                    validChoice = true;
                    fillHandWithTiles(tileQueue, playerTiles, maxHandSize);
                }
                else if(choice.equals("2") || choice.equals("3") || choice.equals("5") || choice.equals("6") || choice.equals("8") || choice.equals("9") || choice.equals("1")) {
                    validChoice = false;                    
                }
                else
                {
                    validChoice = true;
                    if (choice.length() == 0)
                    {
                        validWord = false;
                    }
                    else
                    {
                        validWord = checkWordIsInTiles(choice, playerTiles.playerTiles);
                    }
                    if (validWord)
                    {
                        validWord = checkWordIsValid(choice, allowedWords);
                        if (validWord)
                        {
                            Console.println();
                            Console.println("Valid word");
                            if(choice.equals(autoWin)){
                                autoWinExe(playerName, autoWin);
                            }
                            else{
                                Console.println();
                                wordLog[wordCount]=String.valueOf(choice);
                                updateAfterAllowedWord(choice, playerTiles, playerScore, 
                                    playerTilesPlayed, tileDictionary, allowedWords, maxWordScore, playerName);
                                newTileChoice = getNewTileChoice();
                            }
                        }
                    }
                    if (!validWord)
                    {
                        Console.println();
                        playerScore.score -= 1;
                        wordLog[wordCount]=String.valueOf(choice);
                        Console.println("Not a valid attempt, you lose your turn.");
                        Console.println();
                    }
                    if (!newTileChoice.equals("4"))
                    {
                        addEndOfTurnTiles(tileQueue, playerTiles, newTileChoice, choice);
                    }
                    Console.println();
                    Console.println("Your word was:" + choice);
                    Console.println("Your new score is:" + playerScore.score);
                    Console.println("You have played " + playerTilesPlayed.numberOfTiles + " tiles so far in this game.");
                }
            }
            else{
                Console.println();
                playerScore.score -= 5;
                Console.println("Not a valid attempt, you lose your turn.");
                Console.println();
                Console.println();
                Console.println("Your word is invalid");
                Console.println("Your new score is:" + playerScore.score);
                Console.println("You have played " + playerTilesPlayed.numberOfTiles + " tiles so far in this game.");
                validChoice = true;
                wordLog[wordCount]="Invalid Response";
            }
        }
    }


void displayWinner(String player1Name, String player2Name, int playerOneScore, int playerTwoScore, String[][] maxWordScore, String wordLog[], String wordLog1[], int wordCount, int wordCount1)
    {
        Scanner scan=new Scanner(System.in);
        Console.println();
        Console.println("**** GAME OVER! ****");
        Console.println();
        if(player1Name.equals(maxWordScore[2][0])){
            playerOneScore += 10;
        }
        else{
            playerTwoScore += 10;
        }
        if(player1Name.equals(maxWordScore[2][1])){
            playerOneScore += 10;
        }
        else{
            playerTwoScore += 10;
        }
        Console.println(player1Name + " your score is " + playerOneScore);
        Console.println(player2Name + " your score is " + playerTwoScore);
        if (playerOneScore > playerTwoScore)
        {
            Console.println(player1Name + " wins!");
        }
        else if (playerTwoScore > playerOneScore)
        {
            Console.println(player2Name + " wins!");
        }
        else
        {
            Console.println("It is a draw!");
        }
        Console.println("The highest scoring word of the game was "+maxWordScore[0][0]+", scored "+maxWordScore[1][0]+" points and was played by "+maxWordScore[2][0]);
        Console.println();
        Console.println("The longest word of the game was "+maxWordScore[0][1]+", consisted of "+maxWordScore[1][1]+" characters and was played by "+maxWordScore[2][1]);
        boolean validRes=false;
        String res="";
        while(validRes==false){
            try{
                Console.println("Do you wish to see "+player1Name+"'s log of played words? Y/N?");
                res=scan.nextLine();
                res=res.toUpperCase();
                if(String.valueOf(res.charAt(0)).equals("Y")){
                    Console.println("");
                    for(int x=0;x<wordCount;x++){
                        try{
                            if(wordLog[x].equals(null)){
                            }
                            else{
                                System.out.println(String.valueOf(wordLog[x]));
                            }
                        }
                        catch(Exception e){
                            Console.println("");
                            x=999;
                        }
                    }
                    validRes=true;
                }
                else if(String.valueOf(res.charAt(0)).equals("N")){
                    validRes=true;
                }
                else{
                    System.out.println("INVALID INPUT!");
                }
            }
            catch(Exception e){
                Console.println("Invalid Response!");
            }
        }
        validRes=false;
        while(validRes==false){
            try{
                Console.println("Do you wish to see "+player2Name+"'s log of played words? Y/N?");
                res=scan.nextLine();
                res=res.toUpperCase();
                if(String.valueOf(res.charAt(0)).equals("Y")){
                    Console.println("");
                    for(int x=0;x<wordCount1;x++){
                        try{
                            if(wordLog1[x].equals(null)){
                            }
                            else{
                                System.out.println(String.valueOf(wordLog1[x]));
                            }
                        }
                        catch(Exception e){
                            Console.println("");
                            x=999;
                        }
                    }
                    validRes=true;
                }
                else if(String.valueOf(res.charAt(0)).equals("N")){
                    validRes=true;
                }
                else{
                    System.out.println("INVALID INPUT!");
                }
            }
            catch(Exception e){
                Console.println("Invalid Response!");
            }
        }
    }


void playGame(String[] allowedWords, Map tileDictionary, boolean randomStart, 
    int startHandSize, int maxHandSize, int maxTilesPlayed, 
    int noOfEndOfTurnTiles, String[] disallowedWords, String[][] maxWordScore, String autoWin, String wordLog[], int wordCount, String wordLog1[], int wordCount1)
    {
        boolean nameVerif=false;
        Scanner scan=new Scanner(System.in);
        String player1Name=null;
        String player2Name=null;
        while(nameVerif==false){
            try{
                Console.println("Enter Name for Player 1: ");
                player1Name=scan.nextLine();
                nameVerif=true;
            }
            catch(Exception e){
                System.out.println("Invalid Input Try Again!");
            }
        }
        nameVerif=false;
        while(nameVerif==false){
            try{
                Console.println("Enter Name for Player 2: ");
                player2Name=scan.nextLine();
                nameVerif=true;
            }
            catch(Exception e){
                System.out.println("Invalid Input Try Again!");
            }
        }
        Score playerOneScore = new Score();
        playerOneScore.score = 50;
        Score playerTwoScore = new Score();
        playerTwoScore.score = 50;
        TileCount playerOneTilesPlayed = new TileCount();
        playerOneTilesPlayed.numberOfTiles = 0;
        TileCount playerTwoTilesPlayed = new TileCount();
        playerTwoTilesPlayed.numberOfTiles = 0;
        Tiles playerOneTiles = new Tiles();
        Tiles playerTwoTiles = new Tiles();
        QueueOfTiles tileQueue  = new QueueOfTiles(20); 
        if(randomStart)
        {
            playerOneTiles.playerTiles = getStartingHand(tileQueue, startHandSize);
            playerTwoTiles.playerTiles = getStartingHand(tileQueue, startHandSize);
        }
        else
        {
            playerOneTiles.playerTiles = "BTAHANDENONSARJ";
            playerTwoTiles.playerTiles = "CELZXIOTNESMUAA";
        }
        while (playerOneTilesPlayed.numberOfTiles <= maxTilesPlayed && 
        playerTwoTilesPlayed.numberOfTiles <= maxTilesPlayed && 
        playerOneTiles.playerTiles.length() < maxHandSize && 
        playerTwoTiles.playerTiles.length() < maxHandSize)
        {
            haveTurn(player1Name, playerOneTiles, playerOneTilesPlayed, playerOneScore, 
                tileDictionary, tileQueue, allowedWords, maxHandSize, 
                noOfEndOfTurnTiles, disallowedWords, maxWordScore, autoWin, wordLog, wordCount);
            wordCount++;
            Console.println();
            Console.println("Press Enter to continue");
            Console.readLine();
            Console.println();
            haveTurn(player2Name, playerTwoTiles, playerTwoTilesPlayed, playerTwoScore, 
                tileDictionary, tileQueue, allowedWords, maxHandSize, 
                noOfEndOfTurnTiles, disallowedWords, maxWordScore, autoWin, wordLog1, wordCount1);
            wordCount1++;
        }
        playerOneScore.score = updateScoreWithPenalty(playerOneScore.score, 
            playerOneTiles.playerTiles, tileDictionary);
        playerTwoScore.score = updateScoreWithPenalty(playerTwoScore.score, 
            playerTwoTiles.playerTiles, tileDictionary);
        displayWinner(player1Name, player2Name, playerOneScore.score, playerTwoScore.score, maxWordScore, wordLog, wordLog1, wordCount, wordCount1);   
    }


Main()
    {
        Console.println("++++++++++++++++++++++++++++++++++++++");
        Console.println("+ Welcome to the WORDS WITH AQA game +");
        Console.println("++++++++++++++++++++++++++++++++++++++");
        Random rng=new Random();
        Console.println();
        Console.println();
        String[] allowedWords = loadAllowedWords();
        String[] disallowedWords = loadDisAllowedWords();
        Map tileDictionary = createTileDictionary();
        int maxHandSize = 20;
        int maxTilesPlayed = 50;
        int noOfEndOfTurnTiles = 3;
        int startHandSize = 15;
        String choice = "";
        String maxWordScore[][]=new String[3][2];
        String wordLog[]=new String[200];
        int wordCount=0;
        String wordLog1[]=new String [200];
        int wordCount1=0;
        maxWordScore[0][0]="";
        maxWordScore[1][0]="0";
        maxWordScore[2][0]="";
        maxWordScore[0][1]="";
        maxWordScore[1][1]="0";
        maxWordScore[2][1]="";
        String autoWin="";
        while(!choice.equals("9"))
        {
            displayMenu();
            Console.println("Enter your choice: ");
            choice = Console.readLine();
            if (choice.equals("1"))
            {
                autoWin=allowedWords[rng.nextInt(allowedWords.length)];
                playGame(allowedWords, tileDictionary, true, startHandSize, 
                    maxHandSize, maxTilesPlayed, noOfEndOfTurnTiles, disallowedWords, maxWordScore, autoWin, wordLog, wordCount, wordLog1, wordCount1);
            }
            else if (choice.equals("2"))
            {
                autoWin=allowedWords[rng.nextInt(allowedWords.length)];
                playGame(allowedWords, tileDictionary, false, 15, maxHandSize, 
                    maxTilesPlayed, noOfEndOfTurnTiles, disallowedWords, maxWordScore, autoWin, wordLog, wordCount, wordLog1, wordCount1);
            }
        }
    }


Python:

#By Hammad Mehmood Herschel Grammar School, All the changes have comments on them

def PlayGame(AllowedWords, TileDictionary, RandomStart, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles):
  PlayerOneScore = 50
  LongestWords = ["",""] #List that will store the longest word as well as the player who played it
  HighestScoreWord = ["0","",""] #List that will store the highest score, the word and the player who played it
  PlayerTwoScore = 50
  PlayerOneTilesPlayed = 0
  PlayerTwoTilesPlayed = 0
  TileQueue = QueueOfTiles(20)
  if RandomStart:
    PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize)
    PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize)
  else:
    PlayerOneTiles = "BTAHANDENONSARJ"
    PlayerTwoTiles = "CELZXIOTNESMUAA"
  while PlayerOneTilesPlayed <= MaxTilesPlayed and PlayerTwoTilesPlayed <= MaxTilesPlayed and len(PlayerOneTiles) < MaxHandSize and len(PlayerTwoTiles) < MaxHandSize:
    PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileQueue = HaveTurn("Player One", PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, LongestWords, HighestScoreWord)
    #The two lists and the player name are passed as parameters in HaveTurn
    print()
    input("Press Enter to continue")
    print()
    PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileQueue = HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, LongestWords, HighestScoreWord)
    #The two lists and the player name are passed as parameters in HaveTurn
  PlayerOneScore = UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
  PlayerTwoScore = UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
  print("Longest Word:", LongestWords[0])#print longest word
  print("Played by:", LongestWords[1])#print player of the longest word
  print("Highest score word:", HighestScoreWord[1], "with a score of", HighestScoreWord[0]) #print highest score word and the score
  print("Played by:", HighestScoreWord[2])#print the player of the highest score word
  BonusScore = 2 #the extra bonus points
  if  LongestWords[1] == "Player One": #If the longest word was playes by Player 1
    PlayerOneScore += BonusScore #Add to player one score
  else: #If it was player 2
    PlayerTwoScore += BonusScore #Add to player two score
  if HighestScoreWord[2] == "Player One":#If the highest score word was playes by Player 1
    PlayerOneScore += BonusScore#Add to player one score
  else:#If it was player 2
    PlayerTwoScore += BonusScore#Add to player two score
  DisplayWinner(PlayerOneScore, PlayerTwoScore)

#This is in HaveTurn
#Pass the lists to UpdateAfterAllowedWord
PlayerTiles, PlayerScore, PlayerTilesPlayed = UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords, LongestWords, HighestScoreWord, PlayerName) 


def UpdateAfterAllowedWord(Word, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords, LongestWords, HighestScoreWord, PlayerName):
  PlayerTilesPlayed += len(Word)
  for Letter in Word:
    PlayerTiles = PlayerTiles.replace(Letter, "", 1)
  Score = GetScoreForWord(Word, TileDictionary)
  PlayerScore += Score
  if len(Word) > len(LongestWords[0]): #Check whether the length of the word is greater than the length of previously longest word
    LongestWords[0] = Word #If so then change the current longest score
    LongestWords[1] = PlayerName #Add which player playes it
  if Score > int(HighestScoreWord[0]): #Check is the score of the previous highest score word is greater than the score of the word played
    HighestScoreWord[0] = Score #If so, then change the highest score
    HighestScoreWord[1] = Word #Add the word
    HighestScoreWord[2] = PlayerName #Add the player
  #print statements to check
  print("LongestWords", LongestWords)
  print("HighestScoreWord", HighestScoreWord)
  #We are not appending to the list, we just change the value of a specific position 
  return PlayerTiles, PlayerScore, PlayerTilesPlayed

#By Hammad Mehmood, Herschel Grammar School


VB.NET:

Sub PlayGame(ByRef AllowedWords As List(Of String), ByVal TileDictionary As Dictionary(Of Char, Integer), ByVal RandomStart As Boolean, ByVal StartHandSize As Integer, ByVal MaxHandSize As Integer, ByVal MaxTilesPlayed As Integer, ByVal NoOfEndOfTurnTiles As Integer)
        Dim PlayerOneScore As Integer
        Dim PlayerTwoScore As Integer
        Dim PlayerOneTilesPlayed As Integer
        Dim PlayerTwoTilesPlayed As Integer
        Dim PlayerOneTiles As String
        Dim PlayerTwoTiles As String
        Dim TileQueue As New QueueOfTiles(20)
        Dim LongestWord As String() = New String(1) {} 'string array used to keep track of longest word + player who entered it
        PlayerOneScore = 50
        PlayerTwoScore = 50
        PlayerOneTilesPlayed = 0
        PlayerTwoTilesPlayed = 0
        If RandomStart Then
            PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize)
            PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize)
        Else
            PlayerOneTiles = "BTAHANDENONSARJ"
            PlayerTwoTiles = "CELZXIOTNESMUAA"
        End If
        While PlayerOneTilesPlayed <= MaxTilesPlayed And PlayerTwoTilesPlayed <= MaxTilesPlayed And Len(PlayerOneTiles) < MaxHandSize And Len(PlayerTwoTiles) < MaxHandSize
           'HaveTurn is now called with LongestWord as an extra parameter
            HaveTurn("Player One", PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, LongestWord)
            Console.WriteLine()
            Console.Write("Press Enter to continue")
            Console.ReadLine()
            Console.WriteLine()
            HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, LongestWord)
        End While
        If LongestWord(1) = "Player One" Then 'player who entered the longest word in the game gets an extra 10 points
            Console.WriteLine("Player One: + 10 points for longest word")
            PlayerOneScore += 10
        Else
            Console.WriteLine("Player Two: + 10 points for longest word")
            PlayerTwoScore += 10
        End If
        UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
        UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
        DisplayWinner(PlayerOneScore, PlayerTwoScore)
    End Sub

 Sub HaveTurn(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer, ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef TileQueue As QueueOfTiles, ByRef AllowedWords As List(Of String), ByVal MaxHandSize As Integer, ByVal NoOfEndOfTurnTiles As Integer, ByRef LongestWord As String())
        Dim NewTileChoice As String
        Dim Choice As String
        Dim ValidChoice As Boolean
        Dim ValidWord As Boolean
        Console.WriteLine()
        Console.WriteLine(PlayerName & " it is your turn.")
        DisplayTilesInHand(PlayerTiles)
        NewTileChoice = "2"
        ValidChoice = False
        While Not ValidChoice
            Choice = GetChoice()
            If Choice = "1" Then
                DisplayTileValues(TileDictionary, AllowedWords)
            ElseIf Choice = "4" Then
                TileQueue.Show()
            ElseIf Choice = "7" Then
                DisplayTilesInHand(PlayerTiles)
            ElseIf Choice = "0" Then
                ValidChoice = True
                FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
            Else
                ValidChoice = True
                If Len(Choice) = 0 Then
                    ValidWord = False
                Else
                    ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
                End If
                If ValidWord Then
                    ValidWord = CheckWordIsValid(Choice, AllowedWords)
                    If ValidWord Then
                        Console.WriteLine()
                        Console.WriteLine("Valid word")
                        Console.WriteLine()
                        UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
                        NewTileChoice = GetNewTileChoice()
                        If Len(Choice) > Len(LongestWord(0)) Then 'if submitted word is longer than the current longest word then 
                            'the longest word + player who entered it is changed in the LongestWord array (as LongestWord is taken in HaveTurn subroutine
                            'as ByRef, this changes the values in the array in the location in which it was initialised - in this case, PlayGame())
                            Console.WriteLine()
                            Console.WriteLine("New longest word: " & Choice)
                            Console.WriteLine()
                            LongestWord(0) = Choice
                            LongestWord(1) = PlayerName
                        End If
                    End If
                End If
                If Not ValidWord Then
                    Console.WriteLine()
                    Console.WriteLine("Not a valid attempt, you lose your turn.")
                    Console.WriteLine()
                End If
                If NewTileChoice <> "4" Then
                    AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
                End If
                Console.WriteLine()
                Console.WriteLine("Your word was: " & Choice)
                Console.WriteLine("Your new score is: " & PlayerScore)
                Console.WriteLine("You have played " & PlayerTilesPlayed & " tiles so far in this game.")
            End If
        End While
    End Sub


Deduct points from score if an invalid word is entered

edit

Remove points from the players score if they do not enter a valid word

C#:

private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles)
{
	...
	//All the above code did not changed, so not published; only the edited code is shown below. 
	if (!ValidWord)	
    {
        Console.WriteLine();
        Console.WriteLine("Not a valid attempt, you lose your turn.");
		Console.WriteLine("You also lose 5 points for entering an invalid word");
		Console.WriteLine();
		PlayerScore -= 5;
	}


Delphi/Pascal:

procedure HaveTurn(PlayerName : string; var PlayerTiles : string; var PlayerTilesPlayed : integer;
var PlayerScore : integer; TileDictionary : TTileDictionary; var TileQueue : QueueOfTiles;
var AllowedWords : TStringArray; MaxHandSize : integer; NoOfEndOfTurnTiles : integer);
	...
	//All the above code did not changed, so not published; only the edited code is shown below. 
	  if not ValidWord then
             begin
                WriteLn('Not a valid attempt, you lose your turn.');
		    WriteLn('You also lose 5 points for entering an invalid word');
		    PlayerScore := PlayerScore - 5;
             end;


Java:

    void haveTurn(String playerName, Tiles playerTiles, 
            TileCount playerTilesPlayed, Score playerScore, Map tileDictionary, 
            QueueOfTiles tileQueue, String[] allowedWords, int maxHandSize, 
            int noOfEndOfTurnTiles)
    {
      Console.println();
      Console.println(playerName + " it is your turn.");
      displayTilesInHand(playerTiles.playerTiles);
      String newTileChoice = "2";
      boolean validChoice = false;
      boolean validWord;
      while (!validChoice)
      {
        String choice = getChoice();
        if (choice.equals("1"))
        {
          displayTileValues(tileDictionary, allowedWords);
        }
        else if (choice.equals("4"))
        {
          tileQueue.show();
        }
        else if (choice.equals("7"))
        {
          displayTilesInHand(playerTiles.playerTiles);
        }
        else if (choice.equals("0"))
        {
          validChoice = true;
          fillHandWithTiles(tileQueue, playerTiles, maxHandSize);
        }
        else
        {
          validChoice = true;
          if (choice.length() == 0)
          {
            validWord = false;
          }
          else
          {
            validWord = checkWordIsInTiles(choice, playerTiles.playerTiles);
          }
          if (validWord)
          {
            validWord = checkWordIsValid(choice, allowedWords);
            if (validWord)
            {
              Console.println();
              Console.println("Valid word");
              Console.println();
              updateAfterAllowedWord(choice, playerTiles, playerScore, 
                      playerTilesPlayed, tileDictionary, allowedWords);
              newTileChoice = getNewTileChoice();
            }
          }
          if (!validWord)
          {
            Console.println();
            Console.println("Not a valid attempt, you lose your turn.");
           //Added
            playerScore.score -= 1;
            Console.println();
          }
          if (!newTileChoice.equals("4"))
          {
            addEndOfTurnTiles(tileQueue, playerTiles, newTileChoice, choice);
          }
          Console.println();
          Console.println("Your word was:" + choice);
          Console.println("Your new score is:" + playerScore.score);
          Console.println("You have played " + playerTilesPlayed.numberOfTiles + " tiles so far in this game.");
        }
      }
    }
//By Arslan Badarbetes


Python:

def HaveTurn(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles):
  print()
  print(PlayerName, "it is your turn.")
  DisplayTilesInHand(PlayerTiles)
  NewTileChoice = "2"
  ValidChoice = False
  while not ValidChoice:
    Choice = GetChoice()
    if Choice == "1":
      DisplayTileValues(TileDictionary, AllowedWords)
    elif Choice == "4":
      TileQueue.Show()
    elif Choice == "7":
      DisplayTilesInHand(PlayerTiles)      
    elif Choice == "0":
      ValidChoice = True
      TileQueue, PlayerTiles = FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
    else:
      ValidChoice = True
      if len(Choice) == 0:
        ValidWord = False
      else:
        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
      if ValidWord:
        ValidWord = CheckWordIsValid(Choice, AllowedWords)
        if ValidWord:
          print()
          print("Valid word")
          print()
          PlayerTiles, PlayerScore, PlayerTilesPlayed = UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
          NewTileChoice = GetNewTileChoice()
      if not ValidWord:
        # CHANGED CODE \/\/
        InvalidPoints = 5
        print()
        print("Not a valid attempt, you lose your turn.")
        print("You also lose {} points for entering an invalid word".format(InvalidPoints))
        print()
        PlayerScore -= InvalidPoints
        # CHANGED CODE /\/\
      if NewTileChoice != "4":
        TileQueue, PlayerTiles = AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
      print()
      print("Your word was:", Choice)
      print("Your new score is:", PlayerScore)
      print("You have played", PlayerTilesPlayed, "tiles so far in this game.")
  return PlayerTiles, PlayerTilesPlayed, PlayerScore, TileQueue


VB.NET:

    Sub HaveTurn(ByVal PlayerName As String, ByRef PlayerTiles As String, ByRef PlayerTilesPlayed As String, ByRef PlayerScore As Integer, ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef TileQueue As QueueOfTiles, ByRef AllowedWords As List(Of String), ByVal MaxHandSize As Integer, ByVal NoOfEndOfTurnTiles As Integer)
        Dim NewTileChoice As String
        Dim Choice As String
        Dim ValidChoice As Boolean
        Dim ValidWord As Boolean
        Console.WriteLine()
        Console.WriteLine(PlayerName & " it is your turn.")
        DisplayTilesInHand(PlayerTiles)
        NewTileChoice = "2"
        ValidChoice = False
        While Not ValidChoice
            Choice = GetChoice()
            If Choice = "1" Then
                DisplayTileValues(TileDictionary, AllowedWords)
            ElseIf Choice = "4" Then
                TileQueue.Show()
            ElseIf Choice = "7" Then
                DisplayTilesInHand(PlayerTiles)
            ElseIf Choice =