Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 16.34 KB | None | 0 0
  1. Module Module1
  2.  
  3. Class QueueOfTiles
  4.         Protected Contents() As Char
  5.         Protected Rear As Integer
  6.         Protected MaxSize As Integer
  7.  
  8.         Public Sub New(ByVal MaxSize As Integer)
  9.             Randomize()
  10.             Rear = -1
  11.             Me.MaxSize = MaxSize
  12.             ReDim Contents(Me.MaxSize - 1)
  13.             For Count = 0 To Me.MaxSize - 1
  14.                 Contents(Count) = ""
  15.                 Add()
  16.             Next
  17.         End Sub
  18.  
  19.         Public Function IsEmpty() As Boolean
  20.             If Rear = -1 Then
  21.                 Return True
  22.             Else
  23.                 Return False
  24.             End If
  25.         End Function
  26.  
  27.         Public Function Remove() As Char
  28.             Dim Item As Char
  29.             If IsEmpty() Then
  30.                 Return Nothing
  31.             Else
  32.                 Item = Contents(0)
  33.                 For Count = 1 To Rear
  34.                     Contents(Count - 1) = Contents(Count)
  35.                 Next
  36.                 Contents(Rear) = ""
  37.                 Rear -= 1
  38.                 Return Item
  39.             End If
  40.         End Function
  41.  
  42.         Public Sub Add()
  43.             Dim RandNo As Integer
  44.             If Rear < MaxSize - 1 Then
  45.                 RandNo = Int(Rnd() * 26)
  46.                 Rear += 1
  47.                 Contents(Rear) = Chr(65 + RandNo)
  48.             End If
  49.         End Sub
  50.  
  51.         Public Sub Show()
  52.             If Rear <> -1 Then
  53.                 Console.WriteLine()
  54.                 Console.Write("The contents of the queue are: ")
  55.                 For Each Item In Contents
  56.                     Console.Write(Item)
  57.                 Next
  58.                 Console.WriteLine()
  59.             End If
  60.         End Sub
  61.     End Class
  62.     Sub Main()
  63.         ' Skeleton Program code for the AQA A Level Paper 1 2018 examination
  64.         ' this code should be used in conjunction with the Preliminary Material
  65.         ' written by the AQA Programmer Team
  66.         ' developed using Visual Studio Community Edition
  67.  
  68.  
  69.         Dim MaxHandSize As Integer
  70.         Dim MaxTilesPlayed As Integer
  71.         Dim NoOfEndOfTurnTiles As Integer
  72.         Dim StartHandSize As Integer
  73.         Dim Choice As String
  74.         Dim AllowedWords As New List(Of String)
  75.         Dim TileDictionary As New Dictionary(Of Char, Integer)() 'Global Variables
  76.         Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
  77.         Console.WriteLine("+ Welcome to the WORDS WITH AQA game +")
  78.         Console.WriteLine("++++++++++++++++++++++++++++++++++++++")
  79.         Console.WriteLine()
  80.         Console.WriteLine()
  81.         LoadAllowedWords(AllowedWords)
  82.         TileDictionary = CreateTileDictionary()
  83.         MaxHandSize = 20
  84.         MaxTilesPlayed = 50
  85.         NoOfEndOfTurnTiles = 3
  86.         StartHandSize = 15
  87.         Choice = ""
  88.         While Choice <> "9"
  89.             DisplayMenu()
  90.             Console.Write("Enter your choice: ")
  91.             Choice = Console.ReadLine()
  92.             If Choice = "1" Then
  93.                 PlayGame(AllowedWords, TileDictionary, True, StartHandSize, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
  94.             ElseIf Choice = "2" Then
  95.                 PlayGame(AllowedWords, TileDictionary, False, 15, MaxHandSize, MaxTilesPlayed, NoOfEndOfTurnTiles)
  96.             ElseIf Choice = "3" Then
  97.                 AddToDictionary(AllowedWords)
  98.             End If
  99.         End While
  100.     End Sub
  101.  
  102.     Function CreateTileDictionary() As Dictionary(Of Char, Integer)
  103.         Dim TileDictionary As New Dictionary(Of Char, Integer)()
  104.         For Count = 0 To 25 'Loop through letters (A = 0)
  105.             If Array.IndexOf({0, 4, 8, 13, 14, 17, 18, 19}, Count) > -1 Then
  106.                 TileDictionary.Add(Chr(65 + Count), 1) 'Assign score of 1 for letters above (A, E, I, N, O, R, S, T)
  107.             ElseIf Array.IndexOf({1, 2, 3, 6, 11, 12, 15, 20}, Count) > -1 Then
  108.                 TileDictionary.Add(Chr(65 + Count), 2) 'Assign score of 2 for letters above (B, C, D, G, L, M, P, U)
  109.             ElseIf Array.IndexOf({5, 7, 10, 21, 22, 24}, Count) > -1 Then
  110.                 TileDictionary.Add(Chr(65 + Count), 3) 'Assign score of 3 for letters above (F, H, K, V, W, Y)
  111.             Else
  112.                 TileDictionary.Add(Chr(65 + Count), 5) 'Assign score of 5 for remaining letters (J, Q, X, Z)
  113.             End If
  114.         Next 'Uses ASCII for each letter and scores 1, 2, 3, 5
  115.         Return TileDictionary
  116.     End Function 'Maybe add blank tile / wildcard here (* = 42, ? = 63 in ASCII)
  117.  
  118.     Sub DisplayTileValues(ByVal TileDictionary As Dictionary(Of Char, Integer), ByRef AllowedWords As List(Of String))
  119.         Console.WriteLine()
  120.         Console.WriteLine("TILE VALUES")
  121.         Console.WriteLine()
  122.         For Each Tile As KeyValuePair(Of Char, Integer) In TileDictionary
  123.             Console.WriteLine("Points for " & Tile.Key & ": " & Tile.Value)
  124.         Next
  125.         Console.WriteLine()
  126.     End Sub
  127.  
  128.     Function GetStartingHand(ByRef TileQueue As QueueOfTiles, ByVal StartHandSize As Integer) As String
  129.         Dim Hand As String
  130.         Hand = ""
  131.         For Count = 0 To StartHandSize - 1
  132.             Hand += TileQueue.Remove() 'Removes first tile from queue, and adds to hand
  133.             TileQueue.Add() 'Adds tile to rear of queue
  134.         Next
  135.         Return Hand
  136.     End Function
  137.  
  138.     Sub LoadAllowedWords(ByRef AllowedWords As List(Of String))
  139.         Try
  140.             Dim FileReader As New System.IO.StreamReader("aqawords.txt")
  141.             While FileReader.EndOfStream <> True 'Read every line from file
  142.                 AllowedWords.Add(FileReader.ReadLine().Trim().ToUpper()) 'Ensure everything is the same case
  143.             End While
  144.             FileReader.Close()
  145.         Catch
  146.             AllowedWords.Clear()
  147.         End Try
  148.     End Sub
  149.  
  150.     Function CheckWordIsInTiles(ByVal Word As String, ByVal PlayerTiles As String) As Boolean
  151.         Dim CopyOfTiles As String
  152.         Dim InTiles As Boolean
  153.         InTiles = True
  154.         CopyOfTiles = PlayerTiles
  155.         For Count = 0 To Len(Word) - 1
  156.             If CopyOfTiles.Contains(Word(Count)) Then
  157.                 CopyOfTiles = Replace(CopyOfTiles, Word(Count), "", , 1)
  158.             Else
  159.                 InTiles = False
  160.             End If
  161.         Next
  162.         Return InTiles
  163.     End Function
  164.  
  165.     Function CheckWordIsValid(ByVal Word As String, ByRef AllowedWords As List(Of String)) As Boolean
  166.         Dim ValidWord As Boolean
  167.         Dim Count As Integer
  168.         Count = 0
  169.         ValidWord = False
  170.         While Count < AllowedWords.Count And Not ValidWord
  171.             If AllowedWords(Count) = Word Then
  172.                 ValidWord = True
  173.             End If
  174.             Count += 1
  175.         End While
  176.         Return ValidWord
  177.     End Function
  178.  
  179.     Sub AddEndOfTurnTiles(ByRef TileQueue As QueueOfTiles, ByRef PlayerTiles As String, ByVal NewTileChoice As String, ByVal Choice As String)
  180.         Dim NoOfEndOfTurnTiles As Integer
  181.         If NewTileChoice = "1" Then
  182.             NoOfEndOfTurnTiles = Len(Choice)
  183.         ElseIf NewTileChoice = "2" Then
  184.             NoOfEndOfTurnTiles = 3
  185.         Else
  186.             NoOfEndOfTurnTiles = Len(Choice) + 3
  187.         End If
  188.         For Count = 0 To NoOfEndOfTurnTiles - 1
  189.             PlayerTiles += TileQueue.Remove()
  190.             TileQueue.Add()
  191.         Next
  192.     End Sub
  193.  
  194.     Sub FillHandWithTiles(ByRef TileQueue As QueueOfTiles, ByRef PlayerTiles As String, ByVal MaxHandSize As Integer)
  195.         While Len(PlayerTiles) <= MaxHandSize
  196.             PlayerTiles += TileQueue.Remove()
  197.             TileQueue.Add()
  198.         End While
  199.     End Sub
  200.  
  201.     Function GetScoreForWord(ByVal Word As String, ByVal TileDictionary As Dictionary(Of Char, Integer)) As Integer
  202.         Dim Score As Integer
  203.         Score = 0
  204.         For Count = 0 To Len(Word) - 1
  205.             Score += TileDictionary(Word(Count))
  206.         Next
  207.         If Len(Word) > 7 Then
  208.             Score += 20
  209.         ElseIf Len(Word) > 5 Then
  210.             Score += 5
  211.         End If
  212.         Return Score
  213.     End Function
  214.  
  215.     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))
  216.         PlayerTilesPlayed += Len(Word)
  217.         For Each Letter In Word
  218.             PlayerTiles = Replace(PlayerTiles, Letter, "", , 1)
  219.         Next
  220.         PlayerScore += GetScoreForWord(Word, TileDictionary)
  221.     End Sub
  222.  
  223.     Sub UpdateScoreWithPenalty(ByRef PlayerScore As Integer, ByVal PlayerTiles As String, ByVal TileDictionary As Dictionary(Of Char, Integer))
  224.         For Count = 0 To Len(PlayerTiles) - 1
  225.             PlayerScore -= TileDictionary(PlayerTiles(Count))
  226.         Next
  227.     End Sub
  228.  
  229.     Function GetChoice()
  230.         Dim Choice As String
  231.         Console.WriteLine()
  232.         Console.WriteLine("Either:")
  233.         Console.WriteLine("     enter the word you would like to play OR")
  234.         Console.WriteLine("     press 1 to display the letter values OR")
  235.         Console.WriteLine("     press 4 to view the tile queue OR")
  236.         Console.WriteLine("     press 7 to view your tiles again OR")
  237.         Console.WriteLine("     press 0 to fill hand and stop the game.")
  238.         Console.Write("> ")
  239.         Choice = Console.ReadLine()
  240.         Console.WriteLine()
  241.         Choice = Choice.ToUpper()
  242.         Return Choice
  243.     End Function
  244.  
  245.     Function GetNewTileChoice()
  246.         Dim NewTileChoice As String
  247.         NewTileChoice = ""
  248.         While Array.IndexOf({"1", "2", "3", "4"}, NewTileChoice) = -1
  249.             Console.WriteLine("Do you want to:")
  250.             Console.WriteLine("     replace the tiles you used (1) OR")
  251.             Console.WriteLine("     get three extra tiles (2) OR")
  252.             Console.WriteLine("     replace the tiles you used and get three extra tiles (3) OR")
  253.             Console.WriteLine("     get no new tiles (4)?")
  254.             Console.Write("> ")
  255.             NewTileChoice = Console.ReadLine()
  256.         End While
  257.         Return NewTileChoice
  258.     End Function
  259.  
  260.     Sub DisplayTilesInHand(ByVal PlayerTiles As String)
  261.         Console.WriteLine()
  262.         Console.WriteLine("Your current hand: " & PlayerTiles)
  263.     End Sub
  264.  
  265.     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)
  266.         Dim NewTileChoice As String
  267.         Dim Choice As String
  268.         Dim ValidChoice As Boolean
  269.         Dim ValidWord As Boolean
  270.         Console.WriteLine()
  271.         Console.WriteLine(PlayerName & " it is your turn.")
  272.         DisplayTilesInHand(PlayerTiles)
  273.         NewTileChoice = "2"
  274.         ValidChoice = False
  275.         While Not ValidChoice
  276.             Choice = GetChoice()
  277.             If Choice = "1" Then
  278.                 DisplayTileValues(TileDictionary, AllowedWords)
  279.             ElseIf Choice = "4" Then
  280.                 TileQueue.Show()
  281.             ElseIf Choice = "7" Then
  282.                 DisplayTilesInHand(PlayerTiles)
  283.             ElseIf Choice = "0" Then
  284.                 ValidChoice = True
  285.                 FillHandWithTiles(TileQueue, PlayerTiles, MaxHandSize)
  286.             Else
  287.                 ValidChoice = True
  288.                 If Len(Choice) = 0 Then
  289.                     ValidWord = False
  290.                 Else
  291.                     ValidWord = CheckWordIsInTiles(Choice, PlayerTiles)
  292.                 End If
  293.                 If ValidWord Then
  294.                     ValidWord = CheckWordIsValid(Choice, AllowedWords)
  295.                     If ValidWord Then
  296.                         Console.WriteLine()
  297.                         Console.WriteLine("Valid word")
  298.                         Console.WriteLine()
  299.                         UpdateAfterAllowedWord(Choice, PlayerTiles, PlayerScore, PlayerTilesPlayed, TileDictionary, AllowedWords)
  300.                         NewTileChoice = GetNewTileChoice()
  301.                     End If
  302.                 End If
  303.                 If Not ValidWord Then
  304.                     Console.WriteLine()
  305.                     Console.WriteLine("Not a valid attempt, you lose your turn.")
  306.                     Console.WriteLine()
  307.                 End If
  308.                 If NewTileChoice <> "4" Then
  309.                     AddEndOfTurnTiles(TileQueue, PlayerTiles, NewTileChoice, Choice)
  310.                 End If
  311.                 Console.WriteLine()
  312.                 Console.WriteLine("Your word was: " & Choice)
  313.                 Console.WriteLine("Your new score is: " & PlayerScore)
  314.                 Console.WriteLine("You have played " & PlayerTilesPlayed & " tiles so far in this game.")
  315.             End If
  316.         End While
  317.     End Sub
  318.  
  319.     Sub DisplayWinner(ByVal PlayerOneScore As Integer, ByVal PlayerTwoScore As Integer)
  320.         Console.WriteLine()
  321.         Console.WriteLine("**** GAME OVER! ****")
  322.         Console.WriteLine()
  323.         Console.WriteLine("Player One your score is " & PlayerOneScore)
  324.         Console.WriteLine("Player Two your score is " & PlayerTwoScore)
  325.         If PlayerOneScore > PlayerTwoScore Then
  326.             Console.WriteLine("Player One wins!")
  327.         ElseIf PlayerTwoScore > PlayerOneScore Then
  328.             Console.WriteLine("Player Two wins!")
  329.         Else
  330.             Console.WriteLine("It is a draw!")
  331.         End If
  332.         Console.WriteLine()
  333.     End Sub
  334.  
  335.     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)
  336.         Dim PlayerOneScore As Integer
  337.         Dim PlayerTwoScore As Integer
  338.         Dim PlayerOneTilesPlayed As Integer
  339.         Dim PlayerTwoTilesPlayed As Integer
  340.         Dim PlayerOneTiles As String
  341.         Dim PlayerTwoTiles As String
  342.         Dim TileQueue As New QueueOfTiles(20)
  343.         PlayerOneScore = 50
  344.         PlayerTwoScore = 50
  345.         PlayerOneTilesPlayed = 0
  346.         PlayerTwoTilesPlayed = 0
  347.         If RandomStart Then
  348.             PlayerOneTiles = GetStartingHand(TileQueue, StartHandSize)
  349.             PlayerTwoTiles = GetStartingHand(TileQueue, StartHandSize)
  350.         Else
  351.             PlayerOneTiles = "BTAHANDENONSARJ"
  352.             PlayerTwoTiles = "CELZXIOTNESMUAA"
  353.         End If
  354.         While PlayerOneTilesPlayed <= MaxTilesPlayed And PlayerTwoTilesPlayed <= MaxTilesPlayed And Len(PlayerOneTiles) < MaxHandSize And Len(PlayerTwoTiles) < MaxHandSize
  355.             HaveTurn("Player One", PlayerOneTiles, PlayerOneTilesPlayed, PlayerOneScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
  356.             Console.WriteLine()
  357.             Console.Write("Press Enter to continue")
  358.             Console.ReadLine()
  359.             Console.WriteLine()
  360.             HaveTurn("Player Two", PlayerTwoTiles, PlayerTwoTilesPlayed, PlayerTwoScore, TileDictionary, TileQueue, AllowedWords, MaxHandSize, NoOfEndOfTurnTiles)
  361.         End While
  362.         UpdateScoreWithPenalty(PlayerOneScore, PlayerOneTiles, TileDictionary)
  363.         UpdateScoreWithPenalty(PlayerTwoScore, PlayerTwoTiles, TileDictionary)
  364.         DisplayWinner(PlayerOneScore, PlayerTwoScore)
  365.     End Sub
  366.  
  367.     Sub DisplayMenu()
  368.         Console.WriteLine()
  369.         Console.WriteLine("=========")
  370.         Console.WriteLine("MAIN MENU")
  371.         Console.WriteLine("=========")
  372.         Console.WriteLine()
  373.         Console.WriteLine("1. Play game with random start hand")
  374.         Console.WriteLine("2. Play game with training start hand")
  375.         Console.WriteLine("3. Add a word to the dictionary")
  376.         Console.WriteLine("9. Quit")
  377.         Console.WriteLine()
  378.     End Sub
  379.  
  380.     Sub AddToDictionary(ByRef AllowedWords As List(Of String))
  381.         Dim WordToAdd As String
  382.         Console.WriteLine()
  383.         Console.WriteLine("Enter word to add to dictionary:")
  384.         Console.Write("> ")
  385.         WordToAdd = Console.ReadLine
  386.         Console.WriteLine()
  387.         AllowedWords.Add(WordToAdd.Trim.ToUpper)
  388.         AllowedWords.Sort()
  389.         Dim FileWriter As New System.IO.StreamWriter("aqawords.txt")
  390.         For i = 0 To AllowedWords.Count - 1
  391.             FileWriter.WriteLine(AllowedWords(i))
  392.         Next
  393.         FileWriter.Close()
  394.         Console.WriteLine("Word successfully added to dictionary")
  395.         Console.WriteLine()
  396.     End Sub
  397. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement