A-level Computing/AQA/Paper 1/Skeleton program/2018
This is for the new Computer Science specification (2018)
Possible Section C Questions
[edit | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 | edit source]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 = "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.")
' CHANGED CODE BELOW
PlayerScore -= 5
' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
Display list of words (valid and invalid) used by each player at the end of the game
[edit | edit source]Description of question
C#:
Firstly a list of strings variable called "PlayedWords" is created inside of the PlayGame Subroutine, this is used to store each of the words that our played by the player. This variable is then passed to the HaveTurn method and too the Display Winners Method.
private static void PlayGame(List<string> AllowedWords, Dictionary<char, int> TileDictionary, bool RandomStart, int StartHandSize, int MaxHandSize, int MaxTilesPlayed, int NoOfEndOfTurnTiles)
{
List<string> PlayedWords = new List<string>();
...
...
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 PlayedWords);
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 PlayedWords);
}
UpdateScoreWithPenalty(ref PlayerOneScore, PlayerOneTiles, TileDictionary);
UpdateScoreWithPenalty(ref PlayerTwoScore, PlayerTwoTiles, TileDictionary);
DisplayWinner(PlayerOneScore, PlayerTwoScore, PlayedWords);
...
The HaveTurn method is then edited such it accepts the PlayedWords parameter as ref, if the user plays a word and the word is not 0 in length (blank), then it will be added to the list.
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> PlayedWords)
{
...
else
{
ValidChoice = true;
if (Choice.Length == 0)
{
ValidWord = false;
}
else
{
ValidWord = CheckWordIsInTiles(Choice, PlayerTiles);
PlayedWords.Add(Choice);
}
The DisplayWinner method is also eddied such that it cycles through all the word plays and prints them to the console, using a foreach loop.
private static void DisplayWinner(int PlayerOneScore, int PlayerTwoScore, List<string> PlayedWords)
{
...
Console.WriteLine("In this game the words played were:");
foreach (var Word in PlayedWords)
{
Console.WriteLine(Word);
}
Console.WriteLine();
...
Delphi/Pascal:
Java:
//M Clarke - Thomas Adams College
void haveTurn(String playerName, Tiles playerTiles,
TileCount playerTilesPlayed, Score playerScore, Map tileDictionary,
QueueOfTiles tileQueue, String[] allowedWords, int maxHandSize,
int noOfEndOfTurnTiles, String[] disallowedWords, 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 INPUT**");
//from submethod above
}
}
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");
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 wordLog[], String wordLog1[], int wordCount, int wordCount1)
{
Scanner scan=new Scanner(System.in);
Console.println();
Console.println("**** GAME OVER! ****");
Console.println();
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!");
}
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{
//trycatch required for null values if user inputs 0 for last input value
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{
//trycatch required for null values if user inputs 0 for last input value
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 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){
//validation loop and trycatch for name input
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, 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, 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, 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();
//please view section above for inappropriate words
Map tileDictionary = createTileDictionary();
int maxHandSize = 20;
int maxTilesPlayed = 50;
int noOfEndOfTurnTiles = 3;
int startHandSize = 15;
String choice = "";
String wordLog[]=new String[200];
int wordCount=0;
String wordLog1[]=new String [200];
int wordCount1=0;
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, 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, 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
PlayerTwoScore = 50
PlayerOneTilesPlayed = 0
PlayerTwoTilesPlayed = 0
PlayerOneValidWords = [] #List for the valid words of player 1
PlayerTwoValidWords = [] #List for the valid words of player 2
PlayerOneInvalidWords = [] #List for the invalid words of player 1
PlayerTwoInvalidWords = [] #List for the invalid words of player 2
#Pass these lists to have turn
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, PlayerOneValidWords, PlayerTwoValidWords, PlayerOneInvalidWords, PlayerTwoInvalidWords)
print()
input("Press Enter to continue")
print()
PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileQueue = HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, PlayerOneValidWords, PlayerTwoValidWords, PlayerOneInvalidWords, PlayerTwoInvalidWords)
PlayerOneScore = UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
PlayerTwoScore = UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
DisplayWinner(PlayerOneScore, PlayerTwoScore)
#Ask the players if they want to see the words
SeeWordsPlayed = input("Do you want to see all the words played (y/n)?").upper()
if SeeWordsPlayed == "Y": #If yes, then print all the lists
print("PlayerOneValidWords:", PlayerOneValidWords)
print("PlayerTwoValidWords:", PlayerTwoValidWords)
print("PlayerOneInvalidWords:", PlayerOneInvalidWords)
print("PlayerTwoInvalidWords:", PlayerTwoInvalidWords)
def HaveTurn(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, PlayerOneValidWords, PlayerTwoValidWords, PlayerOneInvalidWords, PlayerTwoInvalidWords):
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:
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.")
#This is inside this else statement, because that is when the player enters a word
if PlayerName == "Player One": #If it is the player one
if ValidWord: #And the word is valid
PlayerOneValidWords.append(Choice) #Add the word to the corresponding list
else: #If the word in invalid
PlayerOneInvalidWords.append(Choice) #Add the word to the corresponding list
else: #If it is the player two
if ValidWord: #And the word is valid
PlayerTwoValidWords.append(Choice) #Add the word to the corresponding list
else: #If the word in invalid
PlayerTwoInvalidWords.append(Choice) #Add the word to the corresponding list
return PlayerTiles, PlayerTilesPlayed, PlayerScore, TileQueue
#By Hammad Mehmood, Herschel Grammar School
VB.NET:
---------------------------------
'use 2d array in this format:
'[Valid/NotValid] | ["Word"]
---------------------------------
Module WordsWithAQA
Dim WordsP1(1, 0) As String 'reDim allows for the right most dimension to be increased only so this is why the orientation. (Increases y coordinate, not x)
Dim WordsP2(1, 0) As String
Sub Main()
---------------------------------
'This function increases the size of the 2D array after adding the word chosen
---------------------------------
Sub wordHistory(ByVal word As String, ByVal valid As String, ByVal playername As String)
If playername = "Player One" Then
WordsP1(0, (WordsP1.Length / 2) - 1) = word
WordsP1(1, (WordsP1.Length / 2) - 1) = valid
ReDim Preserve WordsP1(1, (WordsP1.Length / 2))
Else
WordsP2(0, (WordsP2.Length / 2) - 1) = word
WordsP2(1, (WordsP2.Length / 2) - 1) = valid
ReDim Preserve WordsP2(1, (WordsP2.Length / 2))
End If
Console.WriteLine()
End Sub
---------------------------------
'Added a wordHistory Call
---------------------------------
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
Console.WriteLine()
Console.WriteLine("Valid word")
wordHistory(Choice, "Valid", PlayerName) 'Calls Function Above
Console.WriteLine()
UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords, PlayerName)
NewTileChoice = GetNewTileChoice()
End If
End If
If Not ValidWord Then
Console.WriteLine()
Console.WriteLine("Not a valid attempt, you lose your turn.")
wordHistory(Choice, "Not Valid", PlayerName) 'Calls Function Above
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
---------------------------------
'Cycle through for loop, stopping at "(WordsP1.Length / 2) - 2"
'This is probably bad practice but allows me to keep the reDim statement at the end of every wordHistory Addition
'without worrying about having a blank last record that is filled with each iteration
---------------------------------
Sub DisplayWinner(ByVal PlayerOneScore As Integer, ByVal PlayerTwoScore As Integer)
Console.WriteLine()
Console.WriteLine("**** GAME OVER! ****")
Console.WriteLine()
Console.WriteLine("**** Words Used ****")
Console.WriteLine()
Console.WriteLine("Player One's Words Used:")
If WordsP1(0, 0) <> "" Then
For WordP1 = 0 To (WordsP1.Length / 2) - 2
Console.WriteLine(" " & WordsP1(0, WordP1) & " : " & WordsP1(1, WordP1))
Next
Else
Console.WriteLine(" NO VALID WORDS")
End If
Console.WriteLine("Player Two's Words Used:")
If WordsP2(0, 0) <> "" Then
For WordP2 = 0 To (WordsP2.Length / 2) - 2 '-2 to cut off blank record that is filled with each wordHistory addition
Console.WriteLine(" " & WordsP2(0, WordP2) & " : " & WordsP2(1, WordP2))
Next
Else
Console.WriteLine(" NO VALID WORDS")
End If
Luke Price, Havant College
Display the highest-scored word by each player at the end of the game
[edit | edit source]Description of question
C#:
private static void PlayGame(List<string> AllowedWords, Dictionary<char, int> TileDictionary, bool RandomStart,
int StartHandSize, int MaxHandSize, int MaxTilesPlayed, int NoOfEndOfTurnTiles) {
int PlayerOneScore = 50;
int PlayerTwoScore = 50;
int PlayerThreeScore = 50;
int PlayerOneTilesPlayed = 0;
int PlayerTwoTilesPlayed = 0;
int PlayerThreeTilesPlayed = 0;
string PlayerOneTiles = "";
string PlayerTwoTiles = "";
string PlayerThreeTiles = "";
string PlayerOneHighestScoringWord = ""; //
string PlayerTwoHighestScoringWord = ""; // 3 Basic variables to store Words
string PlayerThreeHighestScoringWord = ""; //
QueueOfTiles TileQueue = new QueueOfTiles(20);
if (RandomStart) {
PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize);
PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize);
PlayerThreeTiles = GetStartingHand(TileQueue, StartHandSize);
}
else {
PlayerOneTiles = "--AHANDENONSARJ";
PlayerTwoTiles = "CELZXIOTNESMUAA";
PlayerThreeTiles = "ABCDEFGHIJKLMNO";
}
while (PlayerOneTilesPlayed <= MaxTilesPlayed && PlayerTwoTilesPlayed <= MaxTilesPlayed && PlayerThreeTilesPlayed <= MaxTilesPlayed
&& PlayerOneTiles.Length < MaxHandSize && PlayerTwoTiles.Length < MaxHandSize && PlayerThreeTiles.Length < MaxHandSize) {
HaveTurn("Player One", ref PlayerOneTiles, ref PlayerOneTilesPlayed, ref PlayerOneScore, TileDictionary,
ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, ref PlayerOneHighestScoringWord); //
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 PlayerTwoHighestScoringWord); //
Console.WriteLine();
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
Console.WriteLine();
HaveTurn("Player Three", ref PlayerThreeTiles, ref PlayerThreeTilesPlayed, ref PlayerThreeScore, TileDictionary,
ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, ref PlayerThreeHighestScoringWord); //
}
UpdateScoreWithPenalty(ref PlayerOneScore, PlayerOneTiles, TileDictionary);
UpdateScoreWithPenalty(ref PlayerTwoScore, PlayerTwoTiles, TileDictionary);
UpdateScoreWithPenalty(ref PlayerThreeScore, PlayerThreeTiles, TileDictionary);
DisplayWinner(PlayerOneScore, PlayerOneHighestScoringWord, PlayerTwoScore, PlayerTwoHighestScoringWord,
PlayerThreeScore, PlayerThreeHighestScoringWord); //
}
//Edited Portion of HaveTurn
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 string HighestScoringWord) {
.
.
if (ValidWord) {
string AmendedChoice = ResolveBlanks(Choice);
ValidWord = CheckWordIsValid(AmendedChoice, AllowedWords);
if (ValidWord) {
int WordScore = GetScoreForWord(AmendedChoice, TileDictionary);
if (WordScore > GetScoreForWord(HighestScoringWord, TileDictionary))
HighestScoringWord = AmendedChoice; //Updates variable if new word is more points than current highest scoring word
Console.WriteLine();
Console.WriteLine($"Valid word. {AmendedChoice} scored {WordScore} points.");
Console.WriteLine();
UpdateAfterAllowedWord(Choice, AmendedChoice, ref PlayerTiles, ref PlayerScore, ref PlayerTilesPlayed, TileDictionary,
AllowedWords);
NewTileChoice = GetNewTileChoice();
}
}
.
.
}
private static void DisplayWinner(int PlayerOneScore, string PlayerOneHighestScoringWord, int PlayerTwoScore, //
string PlayerTwoHighestScoringWord, int PlayerThreeScore, string PlayerThreeHighestScoringWord) { //
Console.WriteLine();
Console.WriteLine("**** GAME OVER! ****");
Console.WriteLine();
Console.WriteLine($"Player One your score is {PlayerOneScore} and you best word was {PlayerOneHighestScoringWord}"); //
Console.WriteLine($"Player Two your score is {PlayerTwoScore} and you best word was {PlayerTwoHighestScoringWord}"); //Edited Code
Console.WriteLine($"Player Three your score is {PlayerThreeScore} and you best word was {PlayerThreeHighestScoringWord}");//
if (PlayerOneScore > PlayerTwoScore && PlayerOneScore > PlayerThreeScore) {
Console.WriteLine("Player One wins!");
}
else if (PlayerTwoScore > PlayerOneScore && PlayerTwoScore > PlayerThreeScore) {
Console.WriteLine("Player Two wins!");
}
else if (PlayerThreeScore > PlayerOneScore && PlayerThreeScore > PlayerTwoScore) {
Console.WriteLine("Player Three wins!");
}
else {
Console.WriteLine("No clear winner");
}
Console.WriteLine();
}
Delphi/Pascal:
Java:
Python:
#This code also works perfectly with the player names code lower down
# code compares player name to an empty dictionary, I used a list instead
# Idris Khan Oak Academy (edit)
def HaveTurn(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, BestWords):
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:
WordScore = GetScoreForWord(Choice,TileDictionary)
# edit
word_score = get_score_for_word(choice, tile_dictionary) # get the score for the word
if player_name in BestWords[0][0]: # access the first item in the list, and access the first item in the second list
if word_score > BestWords[0][2]: # compare the int score values, if word score is greater then
BestWords[0][1] = choice # set player one's best word as the word they just played
BestWords[0][2] = word_score # set the new highest score for player one
else:
pass
if player_name in BestWords[1][0]:
if word_score > BestWords[1][2]:
BestWords[1][1] = choice
BestWords[1][2] = word_score
else:
pass
# edit
# original below
if PlayerName in BestWords.keys():
if WordScore > BestWords[PlayerName][0]:
BestWords[PlayerName] = [WordScore,Choice]
else:
BestWords[PlayerName] = [WordScore,Choice]
if PlayerName in BestWords.keys():
if WordScore > BestWords[PlayerName][0]:
BestWords[PlayerName] = [WordScore,Choice]
else:
BestWords[PlayerName] = [WordScore,Choice]
# orginal above
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, BestWords
def DisplayWinner(PlayerOneScore, PlayerTwoScore, BestWords):
print()
print("**** GAME OVER! ****")
print()
print("Player One your score is", PlayerOneScore)
print("Player Two your score is", PlayerTwoScore)
print()
# edit
# print each item in the nested list for each player
print('player one your best word was ' + best_words[0][1] +
' scoring ' + str(best_words[0][2])
)
print('player two your best word was ' + BestWords[1][1] +
' scoring ' + str(BestWords[1][2])
)
# edit
# original
for Name in BestWords.keys():
print("{} your best word was {}, scoring {} points".format(Name,BestWords[Name][1],BestWords[Name][0]))
# original
if PlayerOneScore > PlayerTwoScore:
print("Player One wins!")
elif PlayerTwoScore > PlayerOneScore:
print("Player Two wins!")
else:
print("It is a draw!")
print()
def PlayGame(AllowedWords, TileDictionary, RandomStart, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles):
#edit
# create a nested list, one being for player one and the other for being player two, each storing their best word and score with it
BestWords = [
['player one', '', 0]
['player two', '', 0]
]
# edit
# original code
BestWords = {}
# original code
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"
while PlayerOneTilesPlayed <= MaxTilesPlayed and PlayerTwoTilesPlayed <= MaxTilesPlayed and len(PlayerOneTiles) < MaxHandSize and len(PlayerTwoTiles) < MaxHandSize:
PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileQueue, BestWords = HaveTurn("Player One", PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, BestWords)
print()
input("Press Enter to continue")
print()
PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileQueue, BestWords = HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, BestWords)
PlayerOneScore = UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
PlayerTwoScore = UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
DisplayWinner(PlayerOneScore, PlayerTwoScore, BestWords)
VB.NET:
--------------------------------------
'Define simple array structure to store HS, looks nice and does the job.
'# should be removed before compiling else it will be displayed in the highscore display at the end of a game if no valid words are given all game; leave it blank...
--------------------------------------
Module WordsWithAQA
Sub Main()
Dim TopScoreP1() As String = {"#", 0}
Dim TopScoreP2() As String = {"#", 0}
--------------------------------------
'Check the scores as they are updated. This is done in this module to keep all update procedures together
--------------------------------------
Sub UpdateAfterAllowedWord(ByVal Word As String, ByRef PlayerTiles As String, ByRef PlayerScore As Integer, ByRef PlayerTilesPlayed As Integer, ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef AllowedWords As List(Of String), ByVal CurrentPlayer As String)
'note the currentplayer is also passed
Dim TurnScore As New Integer 'as new initialises the variable with the default value, 0, rather than this having to be done on a separate line
PlayerTilesPlayed += Len(Word)
For Each Letter In Word
PlayerTiles = Replace(PlayerTiles, Letter, "", , 1)
Next
TurnScore = GetScoreForWord(Word, TileDictionary) 'used another variable to improve efficiency as it only requires the GetScoreForWord function to be run once
PlayerScore += TurnScore
If CurrentPlayer = "Player One" Then 'check to ensure the program updates the correct person's highscore
If TopScoreP1(1) < TurnScore Then
TopScoreP1(1) = TurnScore
TopScoreP1(0) = Word
Console.WriteLine("New Highscore! Player 1 increased their highscore with:") 'new highscore info is displayed
Console.WriteLine(Word & " : " & TurnScore)
End If
Else
If TopScoreP2(1) < TurnScore Then
TopScoreP2(1) = TurnScore
TopScoreP2(0) = Word
Console.WriteLine("New Highscore! Player 2 increased their highscore with:")
Console.WriteLine(Word & " : " & TurnScore)
End If
End If
End Sub
--------------------------------------
'As the program ends, it displays the high scores
--------------------------------------
Sub DisplayWinner(ByVal PlayerOneScore As Integer, ByVal PlayerTwoScore As Integer)
Console.WriteLine()
Console.WriteLine("**** GAME OVER! ****")
Console.WriteLine()
Console.WriteLine("**** Highscore ****")
If TopScoreP1(1) > TopScoreP2(1) Then
Console.WriteLine("Player One had the highest score of " & TopScoreP1(1) & " with " & TopScoreP1(0))
ElseIf TopScoreP1(1) < TopScoreP2(1) Then
Console.WriteLine("Player Two had the highest score of " & TopScoreP2(1) & " with " & TopScoreP2(0))
Else 'the scores match
Console.WriteLine("Both players scored the joint highest score of " & TopScoreP1(1))
Console.WriteLine("Player 1" & TopScoreP1(0))
Console.WriteLine("Player 2" & TopScoreP2(0))
End If
Console.WriteLine()
--------------------------------------
Luke Price, Havant College
Wild card / blank tile added
[edit | edit source]Description of question
C#:
public void Add()
{
int RandNo = 0;
if (Rear < MaxSize - 1)
{
/* The exclusive upper bound of the random int has been increased by 1 so there is a chance of rolling
* the new Wildcard character */
RandNo = Rnd.Next(0, 27);
Rear++;
/* We don't actually want to add the character after 'Z' ('[')
* so RandNo is updated so 65 + Rando == '_' */
if (RandNo == 26) { RandNo = 30; }
Contents[Rear] = Convert.ToChar(65 + RandNo).ToString(); /* Now we can add the character as normal */
}
}
private static void CreateTileDictionary(ref Dictionary<char, int> TileDictionary)
{
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 };
/* Add the wildcard character to the dictionary separately.
* I have chosen the underscore character '_' but this is an arbitrary
* decision */
TileDictionary.Add((char)(65 + 30), 0); /* I chose a point value of 0 for the wildcard character*/
for (int Count = 0; Count < 26; Count++)
{
if (Array.IndexOf(Value1, Count) > -1)
{
TileDictionary.Add((char)(65 + Count), 1);
}
else if (Array.IndexOf(Value2, Count) > -1)
{
TileDictionary.Add((char)(65 + Count), 2);
}
else if (Array.IndexOf(Value3, Count) > -1)
{
TileDictionary.Add((char)(65 + Count), 3);
}
else
{
TileDictionary.Add((char)(65 + Count), 5);
}
}
}
private static bool CheckWordIsValid(string Word, List<string> AllowedWords)
{
bool ValidWord = false;
int Count = 0;
while (Count < AllowedWords.Count && !ValidWord)
{
/* The
* If the two words don't have any letters difference, they must be the same */
if (LettersDiff(AllowedWords[Count], Word) == 0)
{
/* This line isn't necessary, but I thought it would be cool to see what word the wildcards matched to*/
Console.WriteLine("Matching word {0}", AllowedWords[Count]);
ValidWord = true;
}
Count++;
}
return ValidWord;
}
/* LettersDiff() is a method that returns the number of letters between two strings that do not match */
private static int LettersDiff(string s1, string s2)
{
/* If s1 is longer than s2, the maximum difference in letters between them is the length of s1.
* Likewise, if s2 is longer than s1, the maximum difference is the length of s2.
*
* diff = (condition)?(value1):(value2) is another way of writing:
* if((condition))
* {
* diff = (value1)
* }
* else
* {
* diff = (value2)
* }
*/
int diff = s1.Length > s2.Length ? s1.Length : s2.Length;
for(int i = 0; i < s1.Length && i < s2.Length; i++) /* Loop until one of the words has ended */
{
/* This condition checks to see if the letters in both strings at index i are the same.
* If either word has a wildcard at index i, it will be considered equal to any other
* character */
if(s1[i] == s2[i] || s2[i]=='_'|| s2[i]=='_')
{
diff--; /* If the letters are the same, that is one less letter that is different */
}
}
return diff;
}
C#: Another Idea:
This method is also a 'blank tile' however this works as in Scrabble, where it allows user to choose which letter the blank may represent.
//This works like the previous idea, but directly adds an underscore, instead of using the ASCII code
public void Add()
{
int RandNo = 0;
if (Rear < MaxSize - 1)
{
RandNo = Rnd.Next(0, 27);
Rear++;
if (RandNo == 26)
{
Contents[Rear] = "_";
}
else
{
Contents[Rear] = Convert.ToChar(65 + RandNo).ToString();
}
}
}
// This goes within HaveTurn in the while loop.
else if (Choice.Contains("_")) //if the word contains a blank tile
{
ValidChoice = true;
if (Choice.Count(c => c == '_') != 1) //only allows for 1 blank to be used at a time
{
ValidWord = false;
}
else
{
char userInput = ' ';
while (!validBlank)
{
bool validChar = false;
while (!validChar)
{
Console.Write($"Enter blank letter: "); //takes in the users choice of letter for the blank
try
{
userInput = Convert.ToChar(Console.ReadLine().ToUpper()); //validates it is a singular character
validChar = true;
}
catch
{
Console.WriteLine("Invalid blank, try again.");
}
}
if (char.IsLetter(userInput)) //makes sure its a letter
{
Choice = Choice.Replace('_', userInput); //replaces the blank with the letter
validBlank = true;
ValidWord = true;
}
else
{
Console.WriteLine("That was not a letter, please try again.");
}
}
}
if (ValidWord) //just a repeat of the Else statement from this, couldn't find a way of running it afterwards without copy/paste
{
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:
Java:
String resolveBlanks(String word)
{
String newLetter;
for(int x = 0; x < word.length(); x++)
{
if(word.charAt(x) == '-')
{
Console.print("Enter value of blank tile: ");
newLetter = Console.readLine().toUpperCase();
word = word.substring(0, x) + newLetter + word.substring(1, x);
}
}
return word;
}
Python:
import re
class QueueOfTiles():
def __init__(self, MaxSize):
self._Contents = []
self._Rear = -1
self._MaxSize = MaxSize
for Count in range(self._MaxSize):
self._Contents.append("")
self.Add()
def IsEmpty(self):
if self._Rear == -1:
return True
else:
return False
def Remove(self):
if self.IsEmpty():
return None
else:
Item = self._Contents[0]
for Count in range (1, self._Rear + 1):
self._Contents[Count - 1] = self._Contents[Count]
self._Contents[self._Rear] = ""
self._Rear -= 1
return Item
def Add(self):
if self._Rear < self._MaxSize - 1:
RandNo = random.randint(0, 26)
self._Rear += 1
if RandNo == 26:
self._Contents[self._Rear] = '#'
else:
self._Contents[self._Rear] = chr(65 + RandNo)
def Show(self):
if self._Rear != -1:
print()
print("The contents of the queue are: ", end="")
for Item in self._Contents:
print(Item, end="")
print()
def CreateTileDictionary():
TileDictionary = dict()
for Count in range(26):
if Count in [0, 4, 8, 13, 14, 17, 18, 19]:
TileDictionary[chr(65 + Count)] = 1
elif Count in [1, 2, 3, 6, 11, 12, 15, 20]:
TileDictionary[chr(65 + Count)] = 2
elif Count in [5, 7, 10, 21, 22, 24]:
TileDictionary[chr(65 + Count)] = 3
else:
TileDictionary[chr(65 + Count)] = 5
TileDictionary['#'] = 1
return TileDictionary
def DisplayTileValues(TileDictionary, AllowedWords):
print()
print("TILE VALUES")
print()
for Letter, Points in TileDictionary.items():
print("Points for " + Letter + ": " + str(Points))
print("Points for #: 1")
print()
def CheckWordIsInTiles(Word, PlayerTiles):
InTiles = True
CopyOfTiles = PlayerTiles
for Count in range(len(Word)):
if Word[Count] in CopyOfTiles:
CopyOfTiles = CopyOfTiles.replace(Word[Count], "", 1)
elif '#' in CopyOfTiles:
CopyOfTiles = CopyOfTiles.replace('#', "", 1)
else:
InTiles = False
return InTiles
def CheckWordIsValid(Word, AllowedWords):
if '#' in Word: return CheckWordWildcard(Word,AllowedWords)
ValidWord = False
Count = 0
while Count < len(AllowedWords) and not ValidWord:
if AllowedWords[Count] == Word:
ValidWord = True
Count += 1
return ValidWord
def CheckWordWildcard(Word, AllowedWords):
ValidWord = False
reWord = "^"+Word.replace("#",".")+"$"
for AllowedWord in AllowedWords:
if len(Word) != len(AllowedWord):
continue
if re.match(reWord,AllowedWord):
return True
return False
VB.NET:
'HASNAIN
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 CopyOfChoice 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)
Else
ValidChoice = True
If Len(Choice) = 0 Then
ValidWord = False
Else
ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
If ValidWord Then
CopyOfChoice = Choice
Choice = ResolveBlanks(Choice)
End If
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, CopyOfChoice)
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
Function ResolveBlanks(word As String) As String
Dim NewLetter As Char
Dim NewWord As String = ""
For count = 0 To Len(word) - 1
If word(count) = "-" Then
Console.WriteLine("Enter value of blank tile")
NewLetter = UCase(Console.ReadLine())
NewWord += NewLetter
Else
NewWord += word(count)
End If
Next
Return NewWord
End Function
Sub UpdateAfterAllowedWord(ByVal Word As String, ByRef PlayerTiles As String, ByRef PlayerScore As Integer, ByRef PlayerTilesPlayed As Integer, ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef AllowedWords As List(Of String), CopyOfChoice As String)
PlayerTilesPlayed += Len(Word)
For Each Letter In CopyOfChoice
PlayerTiles = Replace(PlayerTiles, Letter, "", , 1)
Next
PlayerScore += GetScoreForWord(Word, TileDictionary)
End Sub
'HASNAIN
The player that has the letter that is closest to “A” will begin the game. A blank tile will win the start of the game
[edit | edit source]Description of question
C#:
Delphi/Pascal:
Java:
Python:
# In comments, * denotes a wildcard in the variable name, eg PlayerFirst*
# could be PlayerFirstScore, PlayerFirstName etc.
#
# PlayerOne* and PlayerTwo* variables get renamed into PlayerFirst*
# and PlayerSecond* variables depending on order of play.
#
# PlayerOne DOES NOT ALWAYS BECOME PlayerFirst
# and likewise,
# PlayerTwo DOES NOT ALWAYS BECOME PlayerSecond
#
# Four ~new~ variables, PlayerOneName, PlayerTwoName, PlayerFirstName &
# PlayerSecondName, have been introduced to keep the naming constant
# at the end of the game regardless of who went first.
#
# This works with blank cards as long as the blank card ASCII value is < A.
# This also works with the player name code.
def DisplayWinner(PlayerFirstScore, PlayerSecondScore,PlayerFirstName,PlayerSecondName):
print()
print("**** GAME OVER! ****")
print()
print(PlayerFirstName+" your score is", PlayerFirstScore)
print(PlayerSecondName+" your score is", PlayerSecondScore)
if PlayerFirstScore > PlayerSecondScore:
print(PlayerFirstName+" wins!")
elif PlayerSecondScore > PlayerFirstScore:
print(PlayerSecondName+" wins!")
else:
print("It is a draw!")
print()
def PlayGame(AllowedWords, TileDictionary, RandomStart, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles):
PlayerFirstScore = 50
PlayerSecondScore = 50
PlayerFirstTilesPlayed = 0
PlayerSecondTilesPlayed = 0
TileQueue = QueueOfTiles(20)
PlayerOneName = "Player One"
PlayerTwoName = "Player Two"
if RandomStart:
PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize)
PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize)
else:
PlayerOneTiles = "BTAHANDENONSARJ"
PlayerTwoTiles = "CELZXIOTNESMUAA"
# Obtain lowest tiles
PlayerOneLowestTile = min([ord(c) for c in PlayerOneTiles])
PlayerOneLowestAmount = PlayerOneTiles.count(chr(PlayerOneLowestTile))
PlayerTwoLowestTile = min([ord(c) for c in PlayerTwoTiles])
PlayerTwoLowestAmount = PlayerTwoTiles.count(chr(PlayerTwoLowestTile))
if PlayerOneLowestTile <= PlayerTwoLowestTile and PlayerOneLowestAmount >= PlayerTwoLowestAmount:
# If player one has the lowest tile, make PlayerOne into PlayerFirst and PlayerTwo into PlayerSecond
PlayerFirstName = PlayerOneName
PlayerFirstTiles = PlayerOneTiles
PlayerSecondName = PlayerTwoName
PlayerSecondTiles = PlayerTwoTiles
print("\n{} gets to go first as they had the lowest tile with '{}'".format(PlayerFirstName,chr(PlayerOneLowestTile)))
else:
# If player two has the lowest tile, make PlayerTwo into PlayerFirst and PlayerOne into PlayerSecond
PlayerFirstName = PlayerTwoName
PlayerFirstTiles = PlayerTwoTiles
PlayerSecondName = PlayerOneName
PlayerSecondTiles = PlayerOneTiles
print("\n{} gets to go first as they had the lowest tile with '{}'".format(PlayerFirstName,chr(PlayerTwoLowestTile)))
# All Player* variables past this point are now PlayerFirst* and PlayerSecond*
while PlayerFirstTilesPlayed <= MaxTilesPlayed and PlayerSecondTilesPlayed <= MaxTilesPlayed and len(PlayerFirstTiles) < MaxHandSize and len(PlayerSecondTiles) < MaxHandSize:
PlayerFirstTiles, PlayerFirstTilesPlayed, PlayerFirstScore, TileQueue = HaveTurn(PlayerFirstName, PlayerFirstTiles, PlayerFirstTilesPlayed, PlayerFirstScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
print()
input("Press Enter to continue")
print()
PlayerSecondTiles, PlayerSecondTilesPlayed, PlayerSecondScore, TileQueue = HaveTurn(PlayerSecondName, PlayerSecondTiles, PlayerSecondTilesPlayed, PlayerSecondScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
PlayerFirstScore = UpdateScoreWithPenalty(PlayerFirstScore, PlayerFirstTiles, TileDictionary)
PlayerSecondScore = UpdateScoreWithPenalty(PlayerSecondScore, PlayerSecondTiles, TileDictionary)
DisplayWinner(PlayerFirstScore, PlayerSecondScore,PlayerFirstName,PlayerSecondName)
VB.NET:
Public Sub Add()
Dim RandNo As Integer = 0
If Rear < MaxSize - 1 Then
RandNo = Rnd.[Next](0, 27)
Rear += 1
If RandNo = 26 Then
RandNo = 30
End If
Contents(Rear) = Convert.ToChar(65 + RandNo).ToString()
End If
End Sub
Private Shared Sub CreateTileDictionary(ByRef TileDictionary As Dictionary(Of Char, Integer))
Dim Value1 As Integer() = {0, 4, 8, 13, 14, 17, 18, 19}
Dim Value2 As Integer() = {1, 2, 3, 6, 11, 12, 15, 20}
Dim Value3 As Integer() = {5, 7, 10, 21, 22, 24}
TileDictionary.Add(CChar((65 + 30)), 0)
For Count As Integer = 0 To 26 - 1
If Array.IndexOf(Value1, Count) > -1 Then
TileDictionary.Add(CChar((65 + Count)), 1)
ElseIf Array.IndexOf(Value2, Count) > -1 Then
TileDictionary.Add(CChar((65 + Count)), 2)
ElseIf Array.IndexOf(Value3, Count) > -1 Then
TileDictionary.Add(CChar((65 + Count)), 3)
Else
TileDictionary.Add(CChar((65 + Count)), 5)
End If
Next
End Sub
Private Shared Function CheckWordIsValid(ByVal Word As String, ByVal AllowedWords As List(Of String)) As Boolean
Dim ValidWord As Boolean = False
Dim Count As Integer = 0
While Count < AllowedWords.Count AndAlso Not ValidWord
If LettersDiff(AllowedWords(Count), Word) = 0 Then
Console.WriteLine("Matching word {0}", AllowedWords(Count))
ValidWord = True
End If
Count += 1
End While
Return ValidWord
End Function
Private Shared Function LettersDiff(ByVal s1 As String, ByVal s2 As String) As Integer
Dim diff As Integer = If(s1.Length > s2.Length, s1.Length, s2.Length)
While i < s1.Length AndAlso i < s2.Length
If s1(i) = s2(i) OrElse s2(i) = "_"c OrElse s2(i) = "_"c Then
diff -= 1
End If
i += 1
End While
Return diff
End Function
Vowel dump scores, words with multiple vowels ie AEON, AERO gain bonus points
[edit | edit source]Description of question
C#:
private static int GetScoreForWord(string Word, Dictionary<char, int> TileDictionary)
{
int Score = 0;
char[] Vowels = { 'A', 'E', 'I', 'O', 'U' }; //Declares all the vowels
int VowelCount = 0; //Used to record the number of vowels used
for (int Count = 0; Count < Word.Length; Count++)
{
Score = Score + TileDictionary[Word[Count]];
if (Vowels.Contains(Word[Count])) //If the letter is a vowel
{
VowelCount++; //Increment vowel count
}
}
if (Word.Length > 7)
{
Score = Score + 20;
}
else if (Word.Length > 5)
{
Score = Score + 5;
}
if(VowelCount >=3) //If more then three vowels were used
{
Score += VowelCount * 2; //Score = score + number of vowels * 2
}
return Score;
}
Delphi/Pascal:
procedure UpdateAfterAllowedWord(Word : string; var PlayerTiles : string; var PlayerScore : integer; var PlayerTilesPlayed : integer; TileDictionary : TTileDictionary; var AllowedWords : TStringArray);
var
Letter : char;
Index,i,vowelcount : integer;
begin
vowelcount:=0;
PlayerTilesPlayed := PlayerTilesPlayed + length(Word);
for Index := 1 to length(Word) do
begin
Letter := Word[Index];
Delete(PlayerTiles, pos(Letter, PlayerTiles), 1);
end;
PlayerScore := PlayerScore + GetScoreForWord(Word, TileDictionary);
For i:= 1 to length(word)
do
begin
if (word[i]='a')OR(word[i]='e')OR(word[i]='i')OR(word[i]='o')OR(word[i]='u') then vowelcount:=vowelcount+1;
end;
if vowelcount=2 then PlayerScore:=PlayerScore + 5
else if vowelcount=3 then PlayerScore:=PlayerScore+10
else if vowelcount>3 then playerscore:=playerscore+15;
end;
Java:
//by vriska serket of alternia sixth form college
//directish translation of the c#
int getScoreForWord(String word, Map tileDictionary) {
int score = 0;
List vowels = Arrays.asList('A', 'E', 'I', 'O', 'U'); //make a list which contains vowels
int vowelCount = 0; //also count the vowels
for (int count = 0; count < word.length(); count++) {
score += (int) tileDictionary.get(word.charAt(count));
if (vowels.contains(word.charAt(count))) {
vowelCount++; //increment vowel count if a vowel is found
}
}
if (word.length() > 7) {
score += 20;
} else if (word.length() > 5) {
score += 5;
}
if (vowelCount >= 3) //if 3 or more vowels are found
{
score += vowelCount * 2; //score + number of vowels * 2;
}
return score;
}
Python:
def GetScoreForWord(Word, TileDictionary):
Vowels = "AEIOU"
Score = 0
VowelCount = 0
for Count in range (len(Word)):
Score += TileDictionary[Word[Count]]
if Word[Count] in Vowels:
VowelCount += 1
if len(Word) > 7:
Score += 20
elif len(Word) > 5:
Score += 5
if VowelCount >= 3:
Score += VowelCount * 2 # 2 extra points per vowel if three or more vowels played
return Score
def GetScoreForWord(Word, TileDictionary):
Score = 0
for Count in range (len(Word)):
Score += TileDictionary[Word[Count]]
if len(Word) > 7:
Score += 20
elif len(Word) > 5:
Score += 5
Vowels = re.findall('[AEIOU]',Word)
if len(Vowels) > 1:
for i in len(Vowels):
Score+=1
return Score
Regex method
VB.NET:
Function GetScoreForWord(ByVal Word As String, ByVal TileDictionary As Dictionary(Of Char, Integer)) As Integer
Dim Score As Integer
Dim Vowels As String = "AEIOU" 'declares all the vowels
Dim VowelChar() As Char = Vowels.ToCharArray 'puts each letter of the vowel string into an array
Dim VowelCount As Integer = 0 ' records the number of vowels
Score = 0
For x = 0 To Len(Word) - 1
Score += TileDictionary(Word(x))
For y = 0 To Len(Vowels) - 1
If Word(x) = VowelChar(y) Then 'if the letter is a vowel
VowelCount += 1 'add one to VowelCount
End If
Next
Next
If Len(Word) > 7 Then
Score += 20
ElseIf Len(Word) > 5 Then
Score += 5
End If
If VowelCount >= 3 Then
Score += VowelCount * 2 'increase by vowel count * 2
End If
Return Score
End Function
In queue-class definition, make use of the IsEmpty method in Show method
[edit | edit source]In queue-class definition, make use of the IsEmpty method in Show method.
C#:
public void Show()
{
if (!IsEmpty())
{
Console.WriteLine();
Console.Write("The contents of the queue are: ");
foreach (var item in Contents)
{
Console.Write(item);
}
Console.WriteLine();
}
}
Delphi/Pascal:
Java:
Python:
class QueueOfTiles():
def __init__(self, MaxSize):
self._Contents = []
self._Rear = -1
self._MaxSize = MaxSize
for Count in range(self._MaxSize):
self._Contents.append("")
self.Add()
def IsEmpty(self):
if self._Rear == -1:
return True
else:
return False
def Remove(self):
if self.IsEmpty():
return None
else:
Item = self._Contents[0]
for Count in range (1, self._Rear + 1):
self._Contents[Count - 1] = self._Contents[Count]
self._Contents[self._Rear] = ""
self._Rear -= 1
return Item
def Add(self):
if self._Rear < self._MaxSize - 1:
RandNo = random.randint(0, 25)
self._Rear += 1
self._Contents[self._Rear] = chr(65 + RandNo)
def Show(self):
if self.IsEmpty():
print()
print("The queue is empty!")
print()
else:
print()
print("The contents of the queue are: ", end="")
for Item in self._Contents:
print(Item, end="")
print()
VB.NET:
'another absolute work of genius by kam pokesie on twitter dot com forward slash actuallypokesie, your fav egirl
'give me a follow i need 1k
Public Sub Show()
If Not IsEmpty() Then
Console.WriteLine()
Console.Write("The contents of the queue are: ")
For Each Item In Contents
Console.Write(Item)
Next
Console.WriteLine()
Else
Console.WriteLine("The queue is empty!")
End If
End Sub
Add the ability to set the player names
[edit | edit source]Add the ability to set the player names
C#:
private static void PlayGame(List<string> AllowedWords, Dictionary<char, int> TileDictionary, bool RandomStart, int StartHandSize, int MaxHandSize, int MaxTilesPlayed, int NoOfEndOfTurnTiles)
{
int PlayerOneScore = 50;
int PlayerTwoScore = 50;
int PlayerOneTilesPlayed = 0;
int PlayerTwoTilesPlayed = 0;
string PlayerOneTiles = "";
string PlayerTwoTiles = "";
QueueOfTiles TileQueue = new QueueOfTiles(20);
Console.WriteLine("Player one, enter your name:");
string PlayerOneName = Console.ReadLine();
Console.WriteLine("Player Two, enter your name:");
string PlayerTwoName = Console.ReadLine();
if (RandomStart)
{
PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize);
PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize);
}
else
{
PlayerOneTiles = "BTAHANDENONSARJ";
PlayerTwoTiles = "CELZXIOTNESMUAA";
}
while (PlayerOneTilesPlayed <= MaxTilesPlayed && PlayerTwoTilesPlayed <= MaxTilesPlayed && PlayerOneTiles.Length < MaxHandSize && PlayerTwoTiles.Length < MaxHandSize)
{
HaveTurn(PlayerOneName, 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(PlayerTwoName, ref PlayerTwoTiles, ref PlayerTwoTilesPlayed, ref PlayerTwoScore, TileDictionary, ref TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles);
}
UpdateScoreWithPenalty(ref PlayerOneScore, PlayerOneTiles, TileDictionary);
UpdateScoreWithPenalty(ref PlayerTwoScore, PlayerTwoTiles, TileDictionary);
DisplayWinner(PlayerOneScore, PlayerTwoScore, PlayerOneName, PlayerTwoName);
}
private static void DisplayWinner(int PlayerOneScore, int PlayerTwoScore, string PlayerOneName, string PlayerTwoName)
{
Console.WriteLine();
Console.WriteLine("**** GAME OVER! ****");
Console.WriteLine();
Console.WriteLine(PlayerOneName + " your score is " + PlayerOneScore);
Console.WriteLine(PlayerTwoName + " your score is " + PlayerTwoScore);
if (PlayerOneScore > PlayerTwoScore)
{
Console.WriteLine(PlayerOneName + " wins!");
}
else if (PlayerTwoScore > PlayerOneScore)
{
Console.WriteLine(PlayerTwoName + " wins!");
}
else
{
Console.WriteLine("It is a draw!");
}
Console.WriteLine();
}
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;
begin
writeln;
//Display input player names instead of Player One or Player Two
if PlayerName = 'Player One' then
writeln(Player1Name, ' it is your turn.')
Else
writeln(Player2Name, ' it is your turn.');
DisplayTilesInHand(PlayerTiles);
NewTileChoice := '2';
ValidChoice := False;
while not ValidChoice do
procedure DisplayWinner(PlayerOneScore : integer; PlayerTwoScore : integer);
begin
writeln;
writeln('**** GAME OVER! ****');
writeln;
writeln(Player1Name + ' your score is ', PlayerOneScore);
//Replaced Player One with Player1Name
writeln(Player2Name + ' your score is ', PlayerTwoScore);
//Replaced Player Two With Player2Name
if PlayerOneScore > PlayerTwoScore then
writeln(Player1Name + ' wins!')
//Replaced Player One with Player1Name
else if PlayerTwoScore > PlayerOneScore then
writeln(Player2Name + ' wins!')
//Replaced Player Two With Player2Name
else
writeln('It is a draw!');
writeln;
end;
//Procedure to input players names called from choice 1 in Main()
Procedure InputPlayerNames;
Begin
Write('Enter First Player Name: ');
Readln(Player1Name);
Write('Enter Second Player Name: ');
Readln(Player2Name);
End;
Java:
void displayWinner(int playerOneScore, int playerTwoScore, String PlayerOneName, String PlayerTwoName) // Added playerone and playertwo to the function
{
Console.println();
Console.println("**** GAME OVER! ****");
Console.println();
Console.println(PlayerOneName +" your score is " + playerOneScore); // Change from string to variable.
Console.println(PlayerTwoName +"Player Two your score is " + playerTwoScore);
if (playerOneScore > playerTwoScore)
{
Console.println(PlayerOneName+ "wins!");
}
else if (playerTwoScore > playerOneScore)
{
Console.println( PlayerTwoName +"wins!");
}
else
{
Console.println("It is a draw!");
}
Console.println() ;
}
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);
String PlayerOneName = "";
String PlayerTwoName = "";
Console.write("Player one, please enter your name");
PlayerOneName = Console.readLine(); // Gets the names of the players
Console.write("Player Two, please enter your name");
PlayerTwoName = Console.readLine();
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(PlayerOneName, playerOneTiles, playerOneTilesPlayed, playerOneScore,
tileDictionary, tileQueue, allowedWords, maxHandSize,
noOfEndOfTurnTiles);
Console.println();
Console.println("Press Enter to continue");
Console.readLine();
Console.println();
haveTurn(PlayerTwoName, playerTwoTiles, playerTwoTilesPlayed, playerTwoScore,
tileDictionary, tileQueue, allowedWords, maxHandSize,
noOfEndOfTurnTiles);
}
playerOneScore.score = updateScoreWithPenalty(playerOneScore.score,
playerOneTiles.playerTiles, tileDictionary);
playerTwoScore.score = updateScoreWithPenalty(playerTwoScore.score,
playerTwoTiles.playerTiles, tileDictionary);
displayWinner(playerOneScore.score, playerTwoScore.score, PlayerOneName, PlayerTwoName);
}
Python:
def DisplayWinner(PlayerOneScore, PlayerTwoScore, PlayerOneName, PlayerTwoName):
print()
print("**** GAME OVER! ****")
print()
print("{} your score is {}".format(PlayerOneName, PlayerOneScore))
print("{} your score is {}".format(PlayerTwoName, PlayerTwoScore))
if PlayerOneScore > PlayerTwoScore:
print("{} wins!".format(PlayerOneName))
elif PlayerTwoScore > PlayerOneScore:
print("{} wins!".format(PlayerTwoName))
else:
print("It is a draw!")
print()
def PlayGame(AllowedWords, TileDictionary, RandomStart, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles):
PlayerOneName = input("Player One, enter your name: ")
PlayerTwoName = input("Player Two, enter your name: ")
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"
while PlayerOneTilesPlayed <= MaxTilesPlayed and PlayerTwoTilesPlayed <= MaxTilesPlayed and len(PlayerOneTiles) < MaxHandSize and len(PlayerTwoTiles) < MaxHandSize:
PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileQueue = HaveTurn(PlayerOneName, PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
print()
input("Press Enter to continue")
print()
PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileQueue = HaveTurn(PlayerTwoName, PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
PlayerOneScore = UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
PlayerTwoScore = UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
DisplayWinner(PlayerOneScore, PlayerTwoScore, PlayerOneName, PlayerTwoName)
VB.NET:
Sub DisplayWinner(ByVal PlayerOneScore As Integer, ByVal PlayerTwoScore As Integer, ByVal PlayerOneName As String, ByVal PlayerTwoName As String)
Console.WriteLine()
Console.WriteLine("**** GAME OVER! ****")
Console.WriteLine()
Console.WriteLine(PlayerOneName & " your score is " & PlayerOneScore)
Console.WriteLine(PlayerTwoName & " your score is " & PlayerTwoScore)
If PlayerOneScore > PlayerTwoScore Then
Console.WriteLine(PlayerOneName & " wins!")
ElseIf PlayerTwoScore > PlayerOneScore Then
Console.WriteLine(PlayerTwoName & " wins!")
Else
Console.WriteLine("It is a draw!")
End If
Console.WriteLine()
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)
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)
' CHANGED \/
Dim PlayerOneName As String
Dim PlayerTwoName As String
Console.Write("Player one, enter your name: ")
PlayerOneName = Console.ReadLine()
Console.Write("Player two, enter your name: ")
PlayerTwoName = Console.ReadLine()
' CHANGED /\
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
' CHANGED \/
HaveTurn(PlayerOneName, PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
Console.WriteLine()
Console.Write("Press Enter to continue")
Console.ReadLine()
Console.WriteLine()
HaveTurn(PlayerTwoName, PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
' CHANGED /\
End While
UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
' CHANGED \/
DisplayWinner(PlayerOneScore, PlayerTwoScore, PlayerOneName, PlayerTwoName)
End Sub
Identification of Acronyms and abbreviations, user given option to enter word/acronym/abbreviation - different points awarded for each
[edit | edit source]Description of question
C#:
Delphi/Pascal:
Java:
Python:
VB.NET:
Create a circular queue to improve efficiency.
[edit | edit source]Create a circular tile queue to improve efficiency of program.
C#:
class QueueOfTiles {
private List<string> Contents = new List<string>(); // List to contain strings
private int Rear; // Identifier for end of list
private int MaxSize;
Random Rnd = new Random();
private int cur;
private int front;
public QueueOfTiles(int MaxSize)
{
// List initialised with empty strings for max size.
this.MaxSize = MaxSize;
this.Rear = -1;
this.front = 0;
for (int Count = 0; Count < this.MaxSize; Count++)
{
Contents.Add("");
this.Add();
}
}
public bool IsEmpty()
{
// Checks if rear pointer is not a valid number
if (Contents.Count == 0) // changed method to calculate empty
{
return true;
}
else
{
return false;
}
}
public string getFront()
{
return Contents[front % MaxSize];
}
public string Remove()
{
string toRemove = getFront();
Contents[front % MaxSize] = null;
front++;
cur--;
return toRemove;
}
public void Add()
{
if (!isFull())
{
int RandNo = Rnd.Next(0, 26);
Rear++;
Contents[Rear % MaxSize] = Convert.ToChar(65 + RandNo).ToString();
cur++;
}
}
public void Show()
{
if (Rear != -1)
{
Console.WriteLine();
Console.Write("The contents of the queue are: ");
foreach (var item in Contents)
{
Console.Write(item); // Lists each item using a foreach loop
}
Console.WriteLine();
}
}
private bool isFull()
{
if (cur == MaxSize)
{
return true;
}
else
{
return false;
}
}
}
Delphi/Pascal:
Java:
package com.company;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class QueueOfTiles {
private int front; //front pointer
protected char[] contents; //array
protected int rear; //rear pointer
protected int maxSize; //maxsize
private int curr = 0; //count of elements in array
QueueOfTiles(int maxSize) {
contents = new char[maxSize];
rear = -1; //-1 As they cannot start as identical positions otherwise require redundant array position
front = 0;
this.maxSize = maxSize;
for (int count = 0; count < maxSize; count++) {
add();
}
}
boolean isEmpty() {
if (curr==0) { //is empty if no elements in array counted
return true;
} else {
return false;
}
}
char remove() {
if(isEmpty()){
return '\n';
} else {
curr-=1; //removes 1 from count
char item = contents[front]; //gets contents of data in position of front pointer
front = (front+1) % maxSize; //adds one to front pointer but will wrap if it exceeds max size
return item;
}
}
void add() {
rear = (rear+1) % contents.length; //adds one to rear pointer but will wrap if it exceeds max size
if (curr<19) {
curr += 1;
Random rnd = new Random();
int randNo = rnd.nextInt(25);
contents[rear] = (char) (65 + randNo);
}
}
void show() {
int current = front;
if (current != rear) {
Console.println();
Console.print("The contents of the queue are: ");
while (current != rear){
current = (current+1) % contents.length;
Console.print(contents[current]);
}
Console.println();
}
}
}
Python:
#By Hammad Mehmood, Herschel Grammar School. All changes have comments on them
#Make sure you look at how a circular queue works or you won't understand!
class QueueOfTiles():
def __init__(self, MaxSize):
self._Contents = []
self.head = 0 #The head
self.tail = 0 #The Tail
self._MaxSize = MaxSize
for Count in range(self._MaxSize):
self.Add()
def IsEmpty(self):
if self.Size() == 0: #If the size is 0
return True
else:
return False
def Remove(self):
if self.IsEmpty():
return None
Item = self._Contents[self.head]
self.head = (self.head + 1) % self._MaxSize
#this helps us to avoid reinitializing tail and head to 0 when the queue becomes full.
return Item
def Add(self):
if self.Size() != self._MaxSize-1: #If it is not full
RandNo = random.randint(0, 25)
self._Contents[self.tail] = chr(65 + RandNo)
self.tail = (self.tail + 1) % self._MaxSize
#this helps us to avoid reinitializing tail and head to 0 when the queue becomes full.
def Size(self): #Calculating the size of the queue
if self.tail >= self.head:
return (self.tail - self.head)
#This return below is when the the head >= tail
return (self._MaxSize - (self.head - self.tail))
def Show(self):
if self.Size() != 0: #If it is not empty
print()
print("The contents of the queue are: ", end="")
if self.tail >= self.head:
for Item in range(self.head, self.tail): #All the items
print(self._Contents[Item], end="")
else: #When the head > tail
Letters = ""
for Item in range(self.head, self._MaxSize):
Letters += self._Contents[Item]
for Item in range(0, self.tail):
Letters += self._Contents[Item]
print(Letters, end="")
print()
#By Hammad Mehmood, Herschel Grammar School
VB.NET:
Class QueueOfTiles
Protected Contents() As Char
Protected Rear As Integer
Protected Front As Integer
Protected MaxSize As Integer
Protected Size As Integer
Public Sub New(ByVal MaxSize As Integer)
Randomize()
Rear = -1
Front = 0
Size = 0
Me.MaxSize = MaxSize
ReDim Contents(Me.MaxSize - 1)
For Count = 0 To Me.MaxSize - 1
Contents(Count) = ""
Add()
Next
End Sub
Public Function IsEmpty() As Boolean
If Rear = -1 Then
Return True
Else
Return False
End If
End Function
Public Function Remove() As Char
Dim Item As Char
If IsEmpty() Then
Return Nothing
Else
Item = Contents(Front)
Contents(Front) = ""
Front = (Front + 1) Mod MaxSize
Size -= 1
Return Item
End If
End Function
Public Function isFull() As Boolean
If Size = MaxSize Then
Return True
Else
Return False
End If
End Function
Public Sub Add()
Dim RandNo As Integer
Dim letters As String
Dim NewLet As String
If Me.isFull() Then
Console.WriteLine("Tile Queue is full")
Else
Rear = (Rear + 1) Mod MaxSize
RandNo = Int(Rnd() * 26)
Contents(Rear) = Chr(65 + RandNo)
Size += 1
End If
End Sub
Public Sub Show()
If IsEmpty() Then
Console.WriteLine("Queue is empty.")
Else
Console.WriteLine()
Console.Write("The contents of the queue are: ")
For Each Item In Contents
Console.Write(Item)
Next
Console.WriteLine()
End If
End Sub
End Class
Jake Campbell
Implement a single-player game against the computer
[edit | edit source]C#:
// By David-senpai, leading classmate of SPS Leaver's Computing Department
// Add switch for when AI is in play
static bool isAI = false;
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 if (Choice == "3") {
isAI = true;
PlayGame(AllowedWords, TileDictionary, true, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles);
}
}
}
...
// Add options to display all words you can make and the best word you can make
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 2 to display all words that you can make OR");
Console.WriteLine(" press 3 to display the best word you can make 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();
return Choice;
}
// A quick function to make it easier to play
private static void DisplayAllValidWordsInHand(ref string PlayerTiles, List<string> AllowedWords) {
foreach (var v in AllowedWords) {
bool good = true;
Dictionary<char, int> counted = v.GroupBy(c => c).Select(g => new { g.Key, Count = g.Count() }).ToDictionary(x => x.Key, x => x.Count);
foreach (char toBestTested in PlayerTiles)
if (counted.ContainsKey(toBestTested))
counted[toBestTested]--;
foreach (KeyValuePair<char, int> j in counted) {
if (j.Value > 0) {
good = false; break;
}
}
if (good)
Console.WriteLine(v);
}
}
// Simple AI algorithm that just plays the highest scoring word available
private static string PickHighestScoringWordInHand(ref string PlayerTiles, List<string> AllowedWords, Dictionary<char, int> TileDictionary) {
string bestWord = "";
int highestScore = 0;
foreach (var v in AllowedWords) {
int score = GetScoreForWord(v, TileDictionary);
if (highestScore > score)
continue;
bool good = true;
Dictionary<char, int> counted = v.GroupBy(c => c).Select(g => new { g.Key, Count = g.Count() }).ToDictionary(x => x.Key, x => x.Count);
foreach (char toBestTested in PlayerTiles)
if (counted.ContainsKey(toBestTested))
counted[toBestTested]--;
foreach (KeyValuePair<char, int> j in counted) {
if (j.Value > 0) {
good = false; break;
}
}
if (good) {
highestScore = score;
bestWord = v;
}
}
return bestWord;
}
// Add AI player
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 = "";
// Main difference below here
if (PlayerName == "Player Two" && isAI) {
ValidChoice = true; ValidWord = true;
Choice = PickHighestScoringWordInHand(ref PlayerTiles, AllowedWords, TileDictionary);
if (Choice == "")
ValidWord = false;
if (ValidWord) {
Console.WriteLine();
Console.WriteLine("Valid word");
Console.WriteLine();
UpdateAfterAllowedWord(Choice, ref PlayerTiles, ref PlayerScore, ref PlayerTilesPlayed, TileDictionary, AllowedWords);
NewTileChoice = "1"; //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.");
return;
}
while (!ValidChoice) {
Choice = GetChoice();
if (Choice == "1") {
DisplayTileValues(TileDictionary, AllowedWords);
} else if (Choice == "2") {
DisplayAllValidWordsInHand(ref PlayerTiles, AllowedWords);
} else if (Choice == "3") {
string word = PickHighestScoringWordInHand(ref PlayerTiles, AllowedWords, TileDictionary);
Console.WriteLine(word + " " + GetScoreForWord(word, TileDictionary));
} 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.");
}
}
}
// Modify menu to include computer opponent option
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. Play game with random start hand against a computer");
Console.WriteLine("9. Quit");
Console.WriteLine();
}
}
}
This involves using an anagram-finding algorithm to make the game unnecessarily difficult to win. I can't imagine this will come up in the exam but I want to avoid doing my actual homework and you're in for the ride.
Python:
Okay so the first thing we're going to do is the function for finding anagrams. The function will be called when the computer needs to enter a word and it will return a valid word.
#By LINGUINE PIZZAPASTA, TORTELLINI HIGH
def ComputerChoice(PlayerTiles, AllowedWords, MaxHandSize):
#start with the maximum possible anagram length and iterate backwards to 1
#this is done so that the computer chooses the longest anagram possible
for Count in range(MaxHandSize, 0, -1):
for Word in AllowedWords: #access each word in the list
if len(Word) == Count: #if the word is too short, skip it and come back later if necessary
RemainingLetters = PlayerTiles #reset RemainingLetters
#now iterate through the letters of the current word to see if it's an anagram of the tiles
for Index, Letter in enumerate(Word): #index is the index of the letter and letter is the character
if Letter in RemainingLetters: #it might be an anagram so continue
if Index == len(Word) - 1: #if the letter is the last letter of the word
#an anagram has been found!
return Word
#remove the letter so it can't be used twice
RemainingLetters = RemainingLetters.replace(Letter, "", 1)
else:
#access the next word if one of the word's letters is not in the tiles
break
return "" #return nothing when it is impossible to make a word
#By LINGUINE PIZZAPASTA, TORTELLINI HIGH
-------------------------------------------------------------------------------
# BY SPURS, Farlingaye
# Based off the minimax algorithm, this is my approach to an algorithm which will find the best word possible with the current hand in the fewest possible lines
def AI(PlayerTiles, TileDictionary,Words = LoadAllowedWords(),BestWord = "None",val = -1):
for y in Words: # iterates through all possible words
if CheckWordIsInTiles(y, PlayerTiles,"Player Two"): # Checks word is valid before evaluating it
val2 = GetScoreForWord(y,TileDictionary) # val 2 holds a temporary value to challenge val, the max value so far
if val2 > val: # if val2 is greater than val then the word corresponding to val2 is better than the previously held word
val = val2 # the new highest value is the temporary value
BestWord = y # the new best word is assigned
return BestWord # return the best word at the end of the evaluation
# BY SPURS, Farlingaye
# The algorithm requires some updates to the main algorithm, most changes have already been outlined by LINGUINE PIZZAPASTA
-----------------------------------------------------------------------------
Next, we're going to modify PlayGame to take an additional boolean parameter. This parameter will be called Computer. When Computer is True, a single-player game will be played against the computer, which is a very useful feature if like me, you don't have any real friends to play against.
def PlayGame(AllowedWords, TileDictionary, RandomStart, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, Computer):
There is another step to modifying PlayGame. The way the game will work is that Player One will always be human and Player Two will either be human or the computer according to the boolean value of Computer. So when PlayGame calls the HaveTurn function in its while loop, the value of computer can always be False for Player One. For Player Two, we can simply pass the value of Computer.
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, False)
print()
input("Press Enter to continue")
print()
PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileQueue = HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, Computer)
Now this won't work unless we update HaveTurn to accept Computer and behave differently when it's the computer's turn. Make sure you change NewTileChoice to NewTileChoice = GetNewTileChoice(Computer).
def HaveTurn(PlayerName, PlayerTiles, PlayerTilesPlayed, PlayerScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles, Computer):
print()
print(PlayerName, "it is your turn.")
DisplayTilesInHand(PlayerTiles)
NewTileChoice = "2"
ValidChoice = False
while not ValidChoice:
if Computer:
Choice = ComputerChoice(PlayerTiles, AllowedWords, MaxHandSize)
if len(Choice) >= 10: print("Wow, that's a good one!")
print()
print(Choice)
else: Choice = GetChoice() #single-line conditional statements are crescent-fresh my dude
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(Computer)
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
Okay now we are almost done. We will modify GetNewTileChoice to take Computer as a parameter. It is necessary to use GetNewTileChoice because when the computer chooses either "1", "2", "3", "4", the player needs to be reminded of the options so that they can see which one the computer chose. The number will simply be chosen randomly. No anagrams to find here :(
def GetNewTileChoice(Computer):
NewTileChoice = ""
while NewTileChoice not in ["1", "2", "3", "4"]:
print("Do you want to:")
print(" replace the tiles you used (1) OR")
print(" get three extra tiles (2) OR")
print(" replace the tiles you used and get three extra tiles (3) OR")
print(" get no new tiles (4)?")
if Computer:
NewTileChoice = random.choice(["1", "2", "3", "4"])
#so apparently random.choice is inefficient compared to random.getrandbits but whatever it enhances readability
#don't you just live for considering time complexity
print()
print("The computer chose", NewTileChoice)
else:
NewTileChoice = input(">")
return NewTileChoice
The last thing to do is change DisplayMenu and Main to allow players to choose single-player. Here is DisplayMenu:
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. Play a single-player game against the computer")
print("9. Quit")
print()
And here is Main:
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 = ""
Computer = False
while Choice != "9":
DisplayMenu()
Choice = input("Enter your choice: ")
if Choice == "1":
PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, Computer)
elif Choice == "2":
PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, Computer)
elif Choice == "3":
Computer = True
PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, Computer)
Yay! It works! Or maybe it doesn't for you because my instructions and code are hot garbage. Anyway, I hope this momentarily distracted you from the pain of A-Levels.
VB.NET:
kinda like the one above but in an inferior language
bang this function in the haveTurn sub as another choice and win ez
Function GiveMeTheBestWordPossiblePlzThx(ByVal PlayerTiles As String, ByVal AllowedWords As List(Of String), ByVal MaxHandSize As Integer)
Dim SortedWords As New List(Of String) 'SORT THE LIST OF ALLOWED WORDS SO YOU GET THAT CRISP HIGHSCORE
For Each USW In AllowedWords
If SortedWords.Count = 0 Then
SortedWords.Add(USW) 'TO ENSURE SW HAS SOMETHING TO CHECK AGAINST
Else
For Each SW In SortedWords
If USW.Length > SW.Length Then
SortedWords.Insert(SortedWords.IndexOf(SW), USW) 'TO INSERT AFTER TO KEEP THAT CRISP CLEAN ORDER
Exit For
End If
Next
End If
Next
Dim remainingletters As String 'REMAINING LETTERS TO KEEP TRACK OF REUSED TILES
For Each word In SortedWords 'START WITH FATTY WORDS TO GET CRIP HIGHSCORE
remainingletters = PlayerTiles 'RESET REMAINING LETTERS EACH WORD
For x = 0 To Len(word) - 1
If remainingletters.Contains(word(x)) Then 'IF LETTER IS IN TARGET WORD THEN
If Len(word) - 1 = x Then 'IF IT IS THE LAST LETTER IN THE WORD
Return word
End If
remainingletters = remainingletters.Replace(word(x), "") 'REMOVE LETTER SO IT ISNT REPEATED
Else
Exit For 'GIVE UP WITH THIS WORD
End If
Next
Next
Return "No Words Found"
End Function
lots of love, Luke Price. Havant College
Here is a different version to the one above, and seems to find better words than it. Eg... for the first training go, this code finds "Jonathan" (34) but the above finds "Another" (14).
Function FindBestWord(ByVal PlayerTiles As String, ByRef TileDictionary As Dictionary(Of Char, Integer), ByRef AllowedWords As List(Of String)) 'finds the best-scoring word
Dim bestWord As String = "No word found" 'variables to store the current best word
Dim bestWordScore As Integer = 0
For Each word In AllowedWords 'loops through each allowed word
Dim found = True
Dim tiles As String = PlayerTiles 'gets a list of all player tiles
For i = 0 To word.Count - 1 'loops through each character in the current word
Dim location As Integer = tiles.IndexOf(word(i)) 'finds if the current character is located in the tile list
If (location = -1) Then 'if location = -1, then not found
found = False
Exit For
Else
tiles = tiles.Remove(location, 1) 'remove this character from the tile to prevent using the same tile twice
End If
Next
If found Then
Dim score = GetScoreForWord(word, TileDictionary) 'gets the score for the current word
If (score > bestWordScore) Then 'checks if this word is better than the currently best one
bestWord = word
bestWordScore = score
End If
End If
Next
Return bestWord 'returns the best word
End Function
By Ryan
An alternate solution that uses the CheckWordIsInTile sub routine.
Function FindBestWord(ByVal PlayerTiles As String, ByRef TileDictionary As Dictionary(Of Char, Integer), ByRef AllowedWords As List(Of String)) 'finds the best-scoring word
Dim bestWord As String = "no words" Dim TempBestWord As String = "" Dim bestWordScore As Integer = 0 For Each word In AllowedWords Dim tiles As String = PlayerTiles If CheckWordIsInTiles(word, tiles) Then TempBestWord = word End If If GetScoreForWord(TempBestWord, TileDictionary) > bestWordScore Then bestWord = TempBestWord bestWordScore = GetScoreForWord(bestWord, TileDictionary) End If Next Return bestWord End FunctionFrom MGS' DenDen
Or, instead of using an anagram finder, do it the easy way: go through every single word in the text file. (A LOT easier to code)
JAVA:
String longestWord(String playerTiles, String[] allowedWords){
String output="";
for (int x=0;x<allowedWords.length;x++){
String word=allowedWords[x];
if (checkWordIsInTiles(word, playerTiles)&&word.length()>output.length()){
output=word;
}
}
return output;
}
String highScoreWord(String playerTiles, String[] allowedWords, Map tileDictionary){
String output="";
int points=0;
for (int x=0;x<allowedWords.length;x++){
String word=allowedWords[x];
if (checkWordIsInTiles(word, playerTiles)&& getScoreForWord(word, tileDictionary)>points){
output=word;
points=getScoreForWord(word, tileDictionary);
}
}
return output;
}
Make a secret word that allows the user to instantly win the game if they type it (as long as they have the letters to make it).
[edit | edit source]Python:
You can make the word anything. If the user inputs this word and has the required letters they win instantly :D
#By Sensei Sulaiman Maqsud of the hidden HGS village
import sys #This should be right at the top of the code under import random
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 Choice == ("CAT"): #The word must be in all capitals as the getChoice function makes the user input all capitals.
print("You win!") #This lets the user know they won
print()
sys.exit() #This ends the code early
elif 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
#By Sensei Sulaiman Maqsud of the hidden HGS village
==Remove new line characters from AllowedWords (Parse file more carefully)==
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 = "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 Choice = "CAT" Then 'If their word is CAT Console.WriteLine("You win!") 'says they win Console.ReadLine() 'Requires them to hit enter before it ends the program End 'ends the whole program End If If ValidWord Then ValidWord = CheckWordIsValid(Choice, AllowedWords) If ValidWord Then Console.WriteLine() Console.WriteLine("Valid word. " & Choice & " scores: " & GetScoreForWord(Choice, TileDictionary) & " points.") 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
Add more players
[edit | edit source]Adds more than 2 players to the game.
Java:
/*
* Skeleton Program code for the AQA A Level Paper 1 2018 examination
* this code should be used in conjunction with the Preliminary Material
* written by the AQA Programmer Team
* developed using Netbeans IDE 8.1
*
*/
//package words.with.aqa;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.*;
public class Main {
class QueueOfTiles {
protected char[] contents;
protected int rear;
protected int maxSize;
QueueOfTiles(int maxSize) {
contents = new char[maxSize];
rear = -1;
this.maxSize = maxSize;
for (int count = 0; count < maxSize; count++) {
add();
}
}
boolean isEmpty() {
if (rear == -1) {
return true;
} else {
return false;
}
}
char remove() {
if (isEmpty()) {
return '\n';
} else {
char item = contents[0];
for (int count = 1; count < rear + 1; count++) {
contents[count - 1] = contents[count];
}
contents[rear] = '\n';
rear -= 1;
return item;
}
}
void add() {
Random rnd = new Random();
if (rear < maxSize - 1) {
int randNo = rnd.nextInt(25);
rear += 1;
contents[rear] = (char) (65 + randNo);
}
}
void show() {
if (rear != -1) {
Console.println();
Console.print("The contents of the queue are: ");
for (char item : contents) {
Console.print(item);
}
Console.println();
}
}
}
Map createTileDictionary() {
Map<Character, Integer> tileDictionary = new HashMap<Character, Integer>();
for (int count = 0; count < 26; count++) {
switch (count) {
case 0:
case 4:
case 8:
case 13:
case 14:
case 17:
case 18:
case 19:
tileDictionary.put((char) (65 + count), 1);
break;
case 1:
case 2:
case 3:
case 6:
case 11:
case 12:
case 15:
case 20:
tileDictionary.put((char) (65 + count), 2);
break;
case 5:
case 7:
case 10:
case 21:
case 22:
case 24:
tileDictionary.put((char) (65 + count), 3);
break;
default:
tileDictionary.put((char) (65 + count), 5);
break;
}
}
return tileDictionary;
}
void displayTileValues(Map tileDictionary, String[] allowedWords) {
Console.println();
Console.println("TILE VALUES");
Console.println();
for (Object letter : tileDictionary.keySet()) {
int points = (int) tileDictionary.get(letter);
Console.println("Points for " + letter + ": " + points);
}
Console.println();
}
String getStartingHand(QueueOfTiles tileQueue, int startHandSize) {
String hand = "";
for (int count = 0; count < startHandSize; count++) {
hand += tileQueue.remove();
tileQueue.add();
}
return hand;
}
String[] loadAllowedWords() {
String[] allowedWords = {};
try {
Path filePath = new File("aqawords.txt").toPath();
Console.println("I tried to fetch the words"); //I added this
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) {
Console.println("Something has gone wrong fetching the words"); //I added this
}
return allowedWords;
}
boolean checkWordIsInTiles(String word, String playerTiles) {
boolean inTiles = true;
String copyOfTiles = playerTiles;
for (int count = 0; count < word.length(); count++) {
if (copyOfTiles.contains(word.charAt(count) + "")) {
copyOfTiles = copyOfTiles.replaceFirst(word.charAt(count) + "", "");
} else {
inTiles = false;
}
}
return inTiles;
}
boolean checkWordIsValid(String word, String[] allowedWords) {
boolean validWord = false;
int count = 0;
while (count < allowedWords.length && !validWord) {
if (allowedWords[count].equals(word)) {
validWord = true;
}
count += 1;
}
return validWord;
}
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 {
noOfEndOfTurnTiles = choice.length() + 3;
}
for (int count = 0; count < noOfEndOfTurnTiles; count++) {
playerTiles.playerTiles += tileQueue.remove();
tileQueue.add();
}
}
void fillHandWithTiles(QueueOfTiles tileQueue, Tiles playerTiles,
int maxHandSize) {
while (playerTiles.playerTiles.length() <= maxHandSize) {
playerTiles.playerTiles += tileQueue.remove();
tileQueue.add();
}
}
int getScoreForWord(String word, Map tileDictionary) {
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() > 5) {
score += 5;
}
return score;
}
void updateAfterAllowedWord(String word, Tiles playerTiles,
Score playerScore, TileCount playerTilesPlayed, Map tileDictionary,
String[] allowedWords) {
playerTilesPlayed.numberOfTiles += word.length();
for (char letter : word.toCharArray()) {
playerTiles.playerTiles = playerTiles.playerTiles.replaceFirst(letter + "", "");
}
playerScore.score += getScoreForWord(word, tileDictionary);
}
int updateScoreWithPenalty(int playerScore, String playerTiles,
Map tileDictionary) {
for (int count = 0; count < playerTiles.length(); count++) {
playerScore -= (int) tileDictionary.get(playerTiles.charAt(count));
}
return playerScore;
}
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 0 to fill hand and stop the game.");
Console.print("> ");
String choice = Console.readLine();
Console.println();
choice = choice.toUpperCase();
return choice;
}
String getNewTileChoice() {
String newTileChoice = "";
do {
Console.println("Do you want to:");
Console.println(" replace the tiles you used (1) OR");
Console.println(" get three extra tiles (2) OR");
Console.println(" replace the tiles you used and get three extra tiles (3) OR");
Console.println(" get no new tiles (4)?");
Console.print("> ");
newTileChoice = Console.readLine();
} while (!"1".equals(newTileChoice) && !"2".equals(newTileChoice) &&
!"3".equals(newTileChoice) && !"4".equals(newTileChoice));
return newTileChoice;
}
void displayTilesInHand(String PlayerTiles) {
Console.println();
Console.println("Your current hand: " + PlayerTiles);
}
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.");
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.");
}
}
}
void displayWinner(Score[] playerScore, int playerCountInt) {
Console.println();
Console.println("**** GAME OVER! ****");
Console.println();
for (int i = 0; i < playerCountInt; i++) {
Console.println("Player " + i + " your score is " + playerScore[i]);
}
int max = playerScore[0].score;
int winner = 0;
for (int i = 0; i < playerCountInt; i++) {
if (max < playerScore[i].score) {
max = playerScore[i].score;
winner = playerScore[i].score;
}
}
Console.println("Player " + winner + " wins!");
Console.println();
}
void playGame(String[] allowedWords, Map tileDictionary, boolean randomStart,
int startHandSize, int maxHandSize, int maxTilesPlayed,
int noOfEndOfTurnTiles) {
String playerCount;
int playerCountInt;
System.out.println();
System.out.println("How many players do you want?");
playerCount = Console.readLine();
playerCountInt = Integer.parseInt(playerCount);
Score playerScore[] = new Score[playerCountInt];
for (int i = 0; i < playerCountInt; i++) {
playerScore[i] = new Score();
playerScore[i].score = 50;
}
TileCount playerTilesPlayed[] = new TileCount[playerCountInt];
Tiles playerTiles[] = new Tiles[playerCountInt];
for (int i = 0; i < playerCountInt; i++) {
playerTilesPlayed[i] = new TileCount();
playerTilesPlayed[i].numberOfTiles = 0;
playerTiles[i] = new Tiles();
}
QueueOfTiles tileQueue = new QueueOfTiles(20);
boolean contine = true;
for (int i = 0; i < playerCountInt; i++) {
if (randomStart) {
playerTiles[i].playerTiles = getStartingHand(tileQueue, startHandSize);
} else {
playerTiles[1].playerTiles = "BTAHANDENONSARJ";
playerTiles[2].playerTiles = "CELZXIOTNESMUAA";
}
while (contine) {
haveTurn("Player " + i, playerTiles[i], playerTilesPlayed[i], playerScore[i],
tileDictionary, tileQueue, allowedWords, maxHandSize,
noOfEndOfTurnTiles);
Console.println();
Console.println("Press Enter to continue");
Console.readLine();
Console.println();
if (playerTilesPlayed[i].numberOfTiles > maxTilesPlayed || playerTiles[i].playerTiles.length() > maxHandSize) {
contine = false;
}
}
for (i = 0; i < playerCountInt; i++) {
playerScore[i].score = updateScoreWithPenalty(playerScore[i].score,
playerTiles[i].playerTiles, tileDictionary);
}
}
displayWinner(playerScore, playerCountInt);
}
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("9. Quit");
Console.println();
}
/*
* Class Tiles is used to allow the value of a String variable to be
* returned from a subroutine
*/
class Tiles {
String playerTiles;
}
/*
* Class TileCount is used to allow the value of an integer variable to be
* returned from a subroutine
*/
class TileCount {
int numberOfTiles;
}
/*
* Class Score is used to allow the value of an integer variable to be
* returned from a subroutine
*/
class Score {
int score;
}
Main() {
Console.println("++++++++++++++++++++++++++++++++++++++");
Console.println("+ Welcome to the WORDS WITH AQA game +");
Console.println("++++++++++++++++++++++++++++++++++++++");
Console.println();
Console.println();
Console.println("I am Here at start"); //I added this
String[] allowedWords = loadAllowedWords();
Map tileDictionary = createTileDictionary();
int maxHandSize = 20;
int maxTilesPlayed = 50;
int noOfEndOfTurnTiles = 3;
final int startHandSize = 15;
String choice = "";
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, startHandSize, maxHandSize,
maxTilesPlayed, noOfEndOfTurnTiles);
}
}
}
public static void main(String[] args) {
new Main();
}
}
Python:
from Constants import TileDictionary
import copy as Copy
import random as Random
class LetterTile:
def __init__(self, Letter):
self._Letter = Letter
self._Score = TileDictionary[self._Letter]
def GetLetter(self):
return self._Letter
def GetScore(self):
return self._Score
def __repr__(self):
return self._Letter
def __str__(self):
return self._Letter
def __eq__(self, Comparable):
return self._Letter == Comparable.GetLetter()
class QueueOfTiles:
def __init__(self, MaxSize):
self._Queue = []
self._Size = 0
self._MaxSize = MaxSize
for Count in range(self._MaxSize):
self.Add()
def IsEmpty(self):
return self._Size == 0
def Remove(self):
if not self.IsEmpty():
Tile = self._Queue.pop(0)
self._Size -= 1
return Tile
else:
return None
def Add(self):
if self._Size <= self._MaxSize:
self._Queue.append(LetterTile(chr(65 + Random.randint(0, 25))))
self._Size += 1
def Show(self):
if not self.IsEmpty():
print()
print("The contents of the queue are: ", end="")
for Item in self._Queue:
print(Item, end="")
print()
class Player:
def __init__(self, Name, Tiles, TilesPlayed, Score):
self._Name = Name
self._Tiles = Tiles
self._TilesPlayed = TilesPlayed
self._Score = Score
def GetScore(self):
return self._Score
def GetTilesPlayed(self):
return self._TilesPlayed
def GetTiles(self):
return self._Tiles
def _DisplayTiles(self):
print()
print("Your current hand: {0}".format("".join(map(lambda Tile: str(Tile), self._Tiles))))
def _FillTiles(self, Queue, MaxHandSize):
while len(self._Tiles) <= MaxHandSize:
PlayerTiles.append(Queue.Remove())
Queue.Add()
return Queue
def _CheckWordIsInTiles(self, Word):
InTiles = True
CopyOfTiles = Copy.deepcopy(self._Tiles)
for Letter in Word:
if Letter in CopyOfTiles:
CopyOfTiles.remove(Letter)
else:
InTiles = False
return InTiles
def _UpdateAfterAllowedWord(self, Word):
self._TilesPlayed += len(Word)
for Letter in Word:
self._Tiles.remove(Letter)
self._Score += sum(map(lambda Letter: Letter.GetScore(), Word))
self._Score += 20 if len(Word) > 7 else 5 if len(Word) > 5 else 0
def _AddEndOfTurnTiles(self, Word, NewTileChoice, Queue):
if NewTileChoice == "1":
NoOfEndOfTurnTiles = len(Word)
elif NewTileChoice == "2":
NoOfEndOfTurnTiles = 3
else:
NoOfEndOfTurnTiles = len(Word) + 3
for Count in range(NoOfEndOfTurnTiles):
self._Tiles.append(Queue.Remove())
Queue.Add()
return Queue
def UpdateScoreWithPenalty(self):
self._Score -= sum(map(lambda Letter: Letter.GetScore(), self._Tiles))
def HaveTurn(self, Game):
print("{0} it is your turn".format(self._Name))
self._DisplayTiles()
Queue = Game.GetQueue()
NewTileChoice = "2"
ValidChoice = False
while not ValidChoice:
Choice = Game.GetChoice()
if Choice == "1":
Game.DisplayTileValues()
elif Choice == "4":
Game.GetQueue().Show()
elif Choice == "7":
self._DisplayTiles()
elif Choice == "0":
ValidChoice = True
Queue = self._FillTiles(Queue, Game.GetMaxHandSize())
else:
Choice = [*map(lambda Letter: LetterTile(Letter), Choice)]
ValidChoice = True
if not len(Choice):
ValidWord = False
else:
ValidWord = self._CheckWordIsInTiles(Choice)
if ValidWord:
ValidWord = Game.CheckWordIsValid("".join(map(lambda Letter: str(Letter), Choice)))
if ValidWord:
print()
print("Valid Word")
print()
self._UpdateAfterAllowedWord(Choice)
NewTileChoice = Game.GetNewTileChoice()
if not ValidWord:
print()
print("Not a valid attempt, you lose your turn")
print()
if NewTileChoice != "4":
Queue = self._AddEndOfTurnTiles(Choice, NewTileChoice, Queue)
print()
print("Your word was: {0}".format("".join(map(lambda Letter: str(Letter), Choice))))
print("Your new score is: {0}".format(self._Score))
print("You have played {0} tiles so far in this game.".format(self._TilesPlayed))
print()
return Queue
class Game:
def __init__(self, NoOfPlayers, MaxHandSize, MaxTilesPlayed, StartHandSize, NoOfEndOfTurnTiles):
self._NoOfPlayers = NoOfPlayers
self._MaxHandSize = MaxHandSize
self._MaxTilesPlayed = MaxTilesPlayed
self._StartHandSize = StartHandSize
self._NoOfEndOfTurnTiles = NoOfEndOfTurnTiles
self._AllowedWords = self._LoadAllowedWords()
def GetChoice(self):
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
def GetNewTileChoice(self):
NewTileChoice = ""
while NewTileChoice not in ["1", "2", "3", "4"]:
print("Do you want to:")
print(" replace the tiles you used (1) OR")
print(" get three extra tiles (2) OR")
print(" replace the tiles you used and get three extra tiles (3) OR")
print(" get no new tiles (4)?")
NewTileChoice = input(">")
return NewTileChoice
def DisplayTileValues(self):
print()
print("TILE VALUES")
print()
for Letter, Points in TileDictionary.items():
print("Points for " + Letter + ": " + str(Points))
print()
def CheckWordIsValid(self, Word):
ValidWord = False
Count = 0
while Count < len(self._AllowedWords) and not ValidWord:
if self._AllowedWords[Count] == Word:
ValidWord = True
Count += 1
return ValidWord
def GetMaxHandSize(self):
return self._MaxHandSize
def GetQueue(self):
return self._Queue
def _LoadAllowedWords(self):
AllowedWords = []
try:
with open("aqawords.txt", "r") as WordsFile:
AllowedWords = [Word.strip().upper() for Word in WordsFile]
except:
pass
return AllowedWords
def _GetStartingHand(self):
Hand = []
for Count in range(self._StartHandSize):
Hand.append(self._Queue.Remove())
self._Queue.Add()
return Hand
def _IsOver(self, Players):
return False in [P.GetTilesPlayed() <= self._MaxTilesPlayed and len(P.GetTiles()) < self._MaxHandSize for P in Players]
def PlayGame(self):
self._Queue = QueueOfTiles(20)
#PlayerOne = Player("Player One", self._GetStartingHand() if RandomStart else [LetterTile(Letter) for Letter in "BTAHANDENONSARJ"], 0, 50)
#PlayerTwo = Player("Player Two", self._GetStartingHand() if RandomStart else [LetterTile(Letter) for Letter in "CELZXIOTNESMUAA"], 0, 50)
Players = [
Player("Player {0}".format(Count), self._GetStartingHand(), 0, 50) \
for Count in range(1, self._NoOfPlayers + 1)
]
while not self._IsOver(Players):
for P in Players:
Queue = P.HaveTurn(self)
print()
input("Press Enter to continue")
print()
for P in Players:
P.UpdateScoreWithPenalty()
self._DisplayWinner(Players)
def _DisplayWinner(self, Players):
print()
print("**** GAME OVER! ****")
print()
for P in Players:
print("Player {0} your score is {1}".format(P.GetName(), P.GetScore()))
Players.sort(key = lambda P: P.GetScore())
print("{0} wins!".format(Players[-1].GetName()))
def DisplayMenu():
print()
print("=========")
print("MAIN MENU")
print("=========")
print()
print("1. Play game")
print("9. Quit")
print()
def Main():
print("++++++++++++++++++++++++++++++++++++++")
print("+ Welcome to the WORDS WITH AQA game +")
print("++++++++++++++++++++++++++++++++++++++")
print()
print()
NoOfPlayers = 0
while NoOfPlayers < 2:
NoOfPlayers = int(input("Enter the number of players: "))
MaxHandSize = 20
MaxTilesPlayed = 50
NoOfEndOfTurnTiles = 3
StartHandSize = 15
Choice = ""
Session = Game(NoOfPlayers, MaxHandSize, MaxTilesPlayed, StartHandSize, NoOfEndOfTurnTiles)
while Choice != "9":
DisplayMenu()
Choice = input("Enter your choice: ")
if Choice == "1":
Session.PlayGame()
if __name__ == "__main__":
Main()
By Ellesmere College's Leading Computer Science Student
Do a frequency analysis on all the words in the dictionary, then assign scores based on those frequencies
[edit | edit source]In createTileDictionary it assigns 1 point to 8 letters, 2 points to 8 letters, 3 points to 6 letters and then 5 points to the rest. Instead of this, make createTileDictionary received allowedWords as an argument, do a frequency analysis on all the words in the dictionary and then assign 1 point to the 8 highest frequency letters, 2 points to the next 8, 3 points to the next 6 and 5 points to the rest.
Python:
def CreateTileDictionary(AllowedWords):
LetterFrequencies = {chr(65 + Count): 0 for Count in range(26)} #Dictionary for recording letter frequencies
for Word in AllowedWords:
for Letter in Word:
LetterFrequencies[Letter] += 1
LetterFrequencies = sorted(LetterFrequencies.keys(), key = lambda Letter: LetterFrequencies[Letter], reverse = True) #Returns a list with the most frequent letter at index 0 and least frequent at index 25
TileDictionary = {
LetterFrequencies[Count]: 1 if 0 <= Count <= 7 else 2 if 8 <= Count <= 15 else 3 if 16 <= Count <= 21 else 5 \ #Assign score according to description above
for Count in range(26)
}
return TileDictionary
By Ellesmere College's Leading Computer Science Student
Java:
//Made by MG
//VERSION 1: No complex data structures and language built-in sorting methods are used (except for the tile dictionary which is already in the original code). It's definitely not the most efficient algorithm.
Map createTileDictionary(String[] allowedWords)
{
Map<Character, Integer> tileDictionary = new HashMap<Character, Integer>();
int[] occurrences = new int[26];
for (int i = 0; i < 26; i++)
{
occurrences[i] = 0;
}
for (int i = 0; i < allowedWords.length; i++)
{
for (int j = 0; j < allowedWords[i].length(); j++)
{
occurrences[(int)allowedWords[i].charAt(j)-65] += 1;
}
}
int processed = 0;
while (processed < 26)
{
int maximum = -1;
int maxIndex = 0;
for (int i = 0; i < 26; i++)
{
if (occurrences[i] > maximum)
{
maximum = occurrences[i];
maxIndex = i;
}
}
occurrences[maxIndex] = -1;
processed += 1;
int points;
if (processed <= 8) points = 1;
else if (processed <= 16) points = 2;
else if (processed <= 22) points = 3;
else points = 5;
tileDictionary.put((char)(65+maxIndex), points);
}
return tileDictionary;
}
//VERSION 2: It uses a HashMap object to store the number of occurrences of each letter and utilises Java built-in sorting methods.
Map createTileDictionary(String[] allowedWords)
{
Map<Character, Integer> tileDictionary = new HashMap<Character, Integer>();
Map<Character, Integer> occurrences = new HashMap<>();
for (int i = 0; i < 26; i++)
{
occurrences.put((char)(65+i), 0);
}
for (int i = 0; i < allowedWords.length; i++)
{
for (int j = 0; j < allowedWords[i].length(); j++)
{
char letter = allowedWords[i].charAt(j);
occurrences.put(letter, occurrences.get(letter)+1);
}
}
ArrayList<Entry<Character, Integer>> occurrencesEntries = new ArrayList<>(occurrences.entrySet());
occurrencesEntries.sort(Entry.comparingByValue());
Collections.reverse(occurrencesEntries);
for (int i = 0; i < 26; i++)
{
int points;
if (i < 8) points = 1;
else if (i < 16) points = 2;
else if (i < 22) points = 3;
else points = 5;
tileDictionary.put(occurrencesEntries.get(i).getKey(), points);
}
return tileDictionary;
}
//COMMON FOR BOTH VERSIONS
Main()
{
Console.println("++++++++++++++++++++++++++++++++++++++");
Console.println("+ Welcome to the WORDS WITH AQA game +");
Console.println("++++++++++++++++++++++++++++++++++++++");
Console.println();
Console.println();
String[] allowedWords = loadAllowedWords();
Map tileDictionary = createTileDictionary(allowedWords); //the only line changed in this procedure
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"))
{
playGame(allowedWords, tileDictionary, true, startHandSize,
maxHandSize, maxTilesPlayed, noOfEndOfTurnTiles);
}
else if (choice.equals("2"))
{
playGame(allowedWords, tileDictionary, false, 15, maxHandSize,
maxTilesPlayed, noOfEndOfTurnTiles);
}
}
}
Implement Bubble Sort when displaying the player's tiles
[edit | edit source]In DisplayTilesInHand(), instead of just outputting the contents of PlayerTiles, use bubble sort to rearrange tiles in alphabetic order.
VB.NET:
Sub DisplayTilesInHand(ByVal PlayerTiles As String)
Dim tileList As List(Of Char) = PlayerTiles.ToList
Dim temp As Char
Dim sortedList As String = ""
For i = 1 To tileList.Count - 1
For k = 0 To tileList.Count - 1 - i
If tileList(k) > tileList(k + 1) Then
temp = tileList(k)
tileList(k) = tileList(k + 1)
tileList(k + 1) = temp
End If
Next
Next
For l = 0 To tileList.Count - 1
sortedList = sortedList + tileList(l).ToString
Next
Console.WriteLine()
Console.WriteLine("Your current hand: " & sortedList)
End Sub
Solution by Aubrey Monk - Alton College
Python 3:
def DisplayTilesInHand(PlayerTiles):
print()
VisitedTiles = [] #Place each tile in here when visited
PlayerTiles = list(PlayerTiles) #Make tiles into list (String is immutable)
while len(VisitedTiles) != len(PlayerTiles): #Should visit all tiles
for Tile in range(len(PlayerTiles)-1): #For each tile to the penultimate tile
if PlayerTiles[Tile] > PlayerTiles[Tile+1]: #Check if the current tile is greater than next
Temp = PlayerTiles[Tile+1] #Place tile that is about to be swapped temporarily
PlayerTiles[Tile+1] = PlayerTiles[Tile] #Replace that tile with the current tile
PlayerTiles[Tile] = Temp #Make the current tile the previously swapped out one
VisitedTiles.append(PlayerTiles[Tile]) #When for loop is complete the tile is fully visited
print("Your current hand:", "".join(PlayerTiles)) #Join the PlayerTiles to make it a string
##I know that the while loop condition could have been PlayerTiles != sorted(PlayerTiles)
##But that is cheating in my opinion.
##Made by the only A Level Computer Science student in The Sandon School.
##By Anas Baig/Brentside
##Another way of implementing the bubble sort algorithm...
def DisplayTilesInHand(PlayerTiles):
print()
complete = False
PlayerTiles = list(PlayerTiles)
SwapLetter = []
while not complete:
complete = True
for count in range(1,len(PlayerTiles)):
if PlayerTiles[count-1]> PlayerTiles[count]:
SwapLetter.append(PlayerTiles[count-1])
PlayerTiles[count-1] = PlayerTiles[count]
PlayerTiles[count]=SwapLetter.pop()
complete = False
PlayerTiles = "".join(PlayerTiles)
print("Your current hand:", PlayerTiles)
##Another method for Bubble Sort (Filip - SPS)
def OrderTiles(PlayerTiles): #8
n = len(PlayerTiles)
List = list(PlayerTiles)
PlayerTiles = ""
for x in range((n-1)):
for y in range((n-x-2)):
if List[y] > List[y+1]:
List[y], List[y+1] = List[y+1], List[y]
for count in range(n):
PlayerTiles += List[count]
return PlayerTiles
Java:
//Made by MG
void displayTilesInHand(String PlayerTiles)
{
char[] tiles = PlayerTiles.toCharArray();
boolean swapMade;
int endIndex = tiles.length-2;
do
{
swapMade = false;
for (int i = 0; i <= endIndex; i++)
{
if (tiles[i+1] < tiles[i])
{
char temporary = tiles[i+1];
tiles[i+1] = tiles[i];
tiles[i] = temporary;
swapMade = true;
}
}
endIndex -= 1;
}
while (swapMade && endIndex >= 0);
String sortedTiles = new String(tiles);
Console.println();
Console.println("Your current hand: " + sortedTiles);
}