A 级别计算机/AQA/试卷 1/骨架程序/2018
这是针对新的计算机科学规范(2018 年)
C 部分
说明用户定义的子例程的名称 - CreateTileDictionary
说明数组变量的标识符名称 - Tiles、Contents、TStringArray、TIntegerArray
提供实例化的示例 - TileDictionary As New Dictionary(Of Char, Integer)
提供返回子例程的示例 - (1) - 整数 - TTileDictionary (2) - 布尔值 - QueueOfTiles.IsEmpty (3) - 字符串 - QueueOfTiles.Remove、GetStarttingHand
提供内置函数的名称 - print()、open()
提供赋值语句的示例 - MaxHandSize = 20
提供使用迭代的子类的名称并提供示例 - AddEndOfTurnTiles (For Count := 1 to NoOfEndOfTurnTiles) DO CheckWordIsValid(While...Do)
提供使用选择的子类的名称 - GetScoreForWord(If Length(word)>7 then Score := Score+20 etc..)
提供使用异常处理的子类的名称 - LoadALlowedWords (Try...Finally) 为什么使用 Try...Finally -
说明在游戏中添加 4 个玩家需要采取的步骤
说明使用线性搜索算法的子例程 - CheckWordIsValid(后续)说明线性搜索的时间复杂度 - O(n),线性搜索的缺点 - 必须逐步查看每个单词以确定单词是否有效,因此效率低下
递归 - 不适用
为 QueueOfTiles 绘制类图/为程序绘制层次结构图
提供方法的名称并描述其目的 -
提供所用抽象数据结构的示例 - 线性队列、字典
说明标识符的名称
用户定义的数据类型
用于存储整数的全局变量
返回字符串的函数。
仅具有一个参数的用户定义的子例程
解释当从瓷砖队列中删除一个项目时,其他项目会发生什么。
为什么这效率低下?
概述如何改进瓷砖队列的工作方式以避免这种情况。
CheckWordIsValid 函数使用线性搜索检查 Word 是否在 AllowedWords 中。
线性搜索的 Big(O) 是什么?
可以改为使用什么更有效的搜索?
概述这种更有效的搜索是如何工作的。
它的 Big(O) 是什么?
解释异常处理的含义以及如何在 GetChoice 子例程中使用异常处理。
解释结构化编程方法的含义。
在 QueueOfTiles.Remove 方法中有一个名为 Item 的变量。在 QueueOfTiles.Show 方法中也有一个名为 Item 的不同变量。解释为什么这两个不同的变量可以具有相同的标识符。
在游戏中,允许的单词列表是从文本文件加载的。也可以使用二进制文件。描述二进制文件中数据存储方式与文本文件中数据存储方式之间的区别。
值参数和引用参数有什么区别?使用骨架程序中的示例来解释您的答案。
过程和函数有什么区别?
骨架程序中的子例程避免使用全局变量 - 它们改用局部变量和参数传递。说明子例程理想情况下不应使用全局变量的两个原因。
描述 TileDictionary 的使用以及为什么字典是合适的
- 欢迎回答和添加问题 -
更改字母的分布,以便更常见的字母,如“E”,比“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:替代解决方案
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(备选)
'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
允许玩家跳过回合,并通过抽取三个或更多字母来结束回合
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
当输入的数字不是 1、4、7 或 0 时,程序不应将其用作单词尝试(向 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
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
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
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()
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
匿名老虎的备选解决方案(使用 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()
允许用户保存游戏的功能
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
为了检查单词是否有效,程序使用线性搜索 O(n),使用更有效的算法进行改进,例如二进制搜索 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
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
匿名老虎的备选解决方案
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
快速的备选解决方案
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
如果缺少所需的文本文件,请告知玩家
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
以下单元可应用于任何文件名,因此非常通用。例如,如果需要不合适的单词 .txt 文件,则也可以搜索它。
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.
上述单元的主过程“checkForFileStartup()”在单元中运行子例程,可以在 LoadAllowedWords() 函数中引用,因此它现在看起来像这样
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
要求玩家添加新的词语到有效的词语列表中。
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
添加一个最高分排行榜
C#
创建一个函数,从一个存储最高分的CSV文件读取数据,该文件包含玩家得分和玩家姓名。读取文件并使用冒泡排序进行排序,然后输出前 5 名的得分和姓名。
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]);
}
}
}
将最高分保存到这个新文件中。
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("");
}
}
询问游戏获胜者是否要保存他们的最高分。
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();
}
更新 "Main" 函数,以允许选择 3 来显示当前的最高分。
...
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
}
...
还更新 "DisplayMenu" 方法,以显示用户可以按 3 来显示最高分。
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
原始解决方案将玩家得分存储为字符串类型,然后使用 lambda 函数来比较这些字符串值,这最终不起作用(对我来说不起作用),对于粗糙的代码我表示歉意,但对我来说它仍然有效。如果你有更好的解决方案,请随时编辑它。
这个解决方案是对第一个解决方案的补充-----
#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
为回合添加时限
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.
这个单元可以参考 GetChoice(),所以它现在看起来像这样
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
过滤掉不合适的词语
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
要求用户确认他们的词语选择,或者如果他们打错了字等等,则返回并更改。
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
问题描述
C#
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 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.");
}
}
}
...
PlayGame 方法也进行了修改,以包含 UsedWords 字符串列表,该列表存储所有已玩过的有效词语,并通过引用传递给 HaveTurn 函数。
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
问题描述...
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
问题描述
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
如果玩家没有输入有效的词语,则从玩家得分中扣除分数
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
问题描述
C#
首先,在 PlayGame 子例程中创建了一个名为 "PlayedWords" 的字符串列表变量,用于存储玩家玩过的每个词语。然后将此变量传递给 HaveTurn 方法和 Display Winners 方法。
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);
...
然后修改 HaveTurn 方法,使其接受 PlayedWords 参数作为引用,如果用户玩了一个词语,并且该词语的长度不为 0(空白),则将其添加到列表中。
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);
}
DisplayWinner 方法也进行了编辑,以便它遍历所有已玩过的词语并将其打印到控制台,使用 foreach 循环。
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
问题描述
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
问题描述
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#:另一种想法
此方法也是一种 "空白瓷砖",但它的作用类似于 Scrabble,允许用户选择空白瓷砖代表哪个字母。
//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
问题描述
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
问题描述
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
正则表达式方法
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
在队列类定义中,在 Show 方法中使用 IsEmpty 方法
[edit | edit source]在队列类定义中,在Show方法中使用IsEmpty方法。
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
添加设置玩家姓名功能
[edit | edit source]添加设置玩家姓名功能
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
识别缩略语和缩写,用户可以选择输入单词/缩略语/缩写 - 每种情况得分不同
[edit | edit source]问题描述
C#
Delphi/Pascal
Java
Python
VB.NET
创建一个循环队列以提高效率。
[edit | edit source]创建一个循环瓷砖队列以提高程序效率。
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
实现人机单人游戏
[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();
}
}
}
这涉及使用一个字谜查找算法来使游戏变得毫无必要地难以获胜。我无法想象这会在考试中出现,但我不想做我的实际作业,而你却要参与其中。
Python
好的,我们要做的第一件事是寻找字谜的函数。该函数将在计算机需要输入一个单词时被调用,并且它将返回一个有效的单词。
#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
-----------------------------------------------------------------------------
接下来,我们将修改PlayGame以接受一个额外的布尔参数。这个参数将被称为Computer。当Computer为True时,将与计算机进行单人游戏,如果你像我一样,没有真正的朋友可以一起玩,这是一个非常有用的功能。
def PlayGame(AllowedWords, TileDictionary, RandomStart, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles, Computer):
修改PlayGame还有另一个步骤。游戏的玩法是玩家一始终是人类,而玩家二根据Computer的布尔值,要么是人类,要么是计算机。因此,当PlayGame在其while循环中调用HaveTurn函数时,Computer的值始终可以对玩家一为False。对于玩家二,我们可以简单地传递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)
现在,除非我们更新HaveTurn以接受Computer并在它是计算机回合时表现不同,否则这将无法正常工作。确保将NewTileChoice更改为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
好的,现在我们快完成了。我们将修改GetNewTileChoice以接受Computer作为参数。使用GetNewTileChoice是必要的,因为当计算机选择“1”、“2”、“3”、“4”时,需要提醒玩家这些选项,以便他们可以看到计算机选择了哪个选项。这个数字将简单地随机选择。这里没有要查找的字谜 :(
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
最后要做的就是更改DisplayMenu和Main以允许玩家选择单人游戏。以下是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()
以下是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)
耶!它奏效了!或者它可能对你不奏效,因为我的说明和代码是垃圾。无论如何,我希望这让你暂时摆脱了A-level的痛苦。
VB.NET
有点像上面的那个,但用一种更差的语言
在haveTurn子中添加这个函数作为另一个选择,然后轻松获胜
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
爱你的,Luke Price. Havant College
这里有一个与上面不同的版本,它似乎找到了比上面更好的单词。例如…对于第一次训练,这段代码找到了“Jonathan”(34),而上面的代码找到了“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
使用CheckWordIsInTile子例程的替代方案。
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
或者,不要使用字谜查找器,用简单的方法:遍历文本文件中的每个单词。(在编码方面要容易得多)
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;
}
创建一个秘密单词,如果用户输入它,则允许用户立即赢得游戏(只要他们有组成该单词的字母)。
[edit | edit source]Python
你可以把这个词改成任何东西。如果用户输入这个词并且拥有所需的字母,他们将立即获胜: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
添加更多玩家
[edit | edit source]在游戏中添加超过2个玩家。
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
对字典中的所有单词进行频率分析,然后根据这些频率分配分数
[edit | edit source]在createTileDictionary中,它将1点分配给8个字母,2点分配给8个字母,3点分配给6个字母,然后5点分配给其余字母。不要这样,让createTileDictionary接收allowedWords作为参数,对字典中的所有单词进行频率分析,然后将1点分配给8个最高频率字母,2点分配给接下来的8个,3点分配给接下来的6个,5点分配给其余字母。
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);
}
}
}
在显示玩家的瓷砖时实现冒泡排序
[edit | edit source]在DisplayTilesInHand()中,不要仅仅输出PlayerTiles的内容,使用冒泡排序将瓷砖按字母顺序重新排列。
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);
}