XertzHF

Untitled

Apr 24th, 2014
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.64 KB | None | 0 0
  1. 'Skeleton Program code for the AQA COMP1 Summer 2014 examination
  2. 'this code should be used in conjunction with the Preliminary Material
  3. 'written by the AQA COMP1 Programmer Team
  4. 'developed in the Visual Studio 2008 (Console Mode) programming environment (VB.NET)
  5.  
  6. Module CardPredict
  7.  
  8.     Const NoOfRecentScores As Integer = 3
  9.  
  10.     Structure TCard
  11.         Dim Suit As Integer
  12.         Dim Rank As Integer
  13.     End Structure
  14.  
  15.     Structure TRecentScore
  16.         Dim Name As String
  17.         Dim Score As Integer
  18.     End Structure
  19.     Sub Main()
  20.         Dim Choice As Char
  21.         Dim Deck(52) As TCard
  22.         Dim RecentScores(NoOfRecentScores) As TRecentScore
  23.         Randomize()
  24.         Do
  25.             DisplayMenu()
  26.             Choice = GetMenuChoice()
  27.             Select Case Choice
  28.                 Case "1"
  29.                     LoadDeck(Deck)
  30.                     ShuffleDeck(Deck)
  31.                     PlayGame(Deck, RecentScores)
  32.                 Case "2"
  33.                     LoadDeck(Deck)
  34.                     PlayGame(Deck, RecentScores)
  35.                 Case "3"
  36.                     DisplayRecentScores(RecentScores)
  37.                 Case "4"
  38.                     ResetRecentScores(RecentScores)
  39.             End Select
  40.         Loop Until Choice = "q"
  41.     End Sub
  42.  
  43.     Function GetRank(ByVal RankNo As Integer) As String
  44.         Dim Rank As String = ""
  45.         Select Case RankNo
  46.             Case 1 : Rank = "Ace"
  47.             Case 2 : Rank = "Two"
  48.             Case 3 : Rank = "Three"
  49.             Case 4 : Rank = "Four"
  50.             Case 5 : Rank = "Five"
  51.             Case 6 : Rank = "Six"
  52.             Case 7 : Rank = "Seven"
  53.             Case 8 : Rank = "Eight"
  54.             Case 9 : Rank = "Nine"
  55.             Case 10 : Rank = "Ten"
  56.             Case 11 : Rank = "Jack"
  57.             Case 12 : Rank = "Queen"
  58.             Case 13 : Rank = "King"
  59.         End Select
  60.         Return Rank
  61.     End Function
  62.  
  63.     Function GetSuit(ByVal SuitNo As Integer) As String
  64.         Dim Suit As String = ""
  65.         Select Case SuitNo
  66.             Case 1 : Suit = "Clubs"
  67.             Case 2 : Suit = "Diamonds"
  68.             Case 3 : Suit = "Hearts"
  69.             Case 4 : Suit = "Spades"
  70.         End Select
  71.         Return Suit
  72.     End Function
  73.  
  74.     Sub DisplayMenu()
  75.         Console.WriteLine()
  76.         Console.WriteLine("MAIN MENU")
  77.         Console.WriteLine()
  78.         Console.WriteLine("1.  Play game (with shuffle)")
  79.         Console.WriteLine("2.  Play game (without shuffle)")
  80.         Console.WriteLine("3.  Display recent scores")
  81.         Console.WriteLine("4.  Reset recent scores")
  82.         Console.WriteLine()
  83.         Console.Write("Select an option from the menu (or enter q to quit): ")
  84.     End Sub
  85.  
  86.     Function GetMenuChoice() As Char
  87.         Dim Choice As Char
  88.         Choice = Console.ReadLine
  89.         Console.WriteLine()
  90.         Return Choice
  91.     End Function
  92.  
  93.     Sub LoadDeck(ByRef Deck() As TCard)
  94.         Dim Count As Integer
  95.         FileOpen(1, "deck.txt", OpenMode.Input)
  96.         Count = 1
  97.         While Not EOF(1)
  98.             Deck(Count).Suit = CInt(LineInput(1))
  99.             Deck(Count).Rank = CInt(LineInput(1))
  100.             Count = Count + 1
  101.         End While
  102.         FileClose(1)
  103.     End Sub
  104.  
  105.     Sub ShuffleDeck(ByRef Deck() As TCard)
  106.         Dim NoOfSwaps As Integer
  107.         Dim Position1 As Integer
  108.         Dim Position2 As Integer
  109.         Dim SwapSpace As TCard
  110.         Dim NoOfSwapsMadeSoFar As Integer
  111.         NoOfSwaps = 1000
  112.         For NoOfSwapsMadeSoFar = 1 To NoOfSwaps
  113.             Position1 = Int(Rnd() * 52) + 1
  114.             Position2 = Int(Rnd() * 52) + 1
  115.             SwapSpace = Deck(Position1)
  116.             Deck(Position1) = Deck(Position2)
  117.             Deck(Position2) = SwapSpace
  118.         Next
  119.     End Sub
  120.  
  121.     Sub DisplayCard(ByVal ThisCard As TCard)
  122.         Console.WriteLine()
  123.         Console.WriteLine("Card is the " & GetRank(ThisCard.Rank) & " of " & GetSuit(ThisCard.Suit))
  124.         Console.WriteLine()
  125.     End Sub
  126.  
  127.     Sub GetCard(ByRef ThisCard As TCard, ByRef Deck() As TCard, ByVal NoOfCardsTurnedOver As Integer)
  128.         Dim Count As Integer
  129.         ThisCard = Deck(1)
  130.         For Count = 1 To (51 - NoOfCardsTurnedOver)
  131.             Deck(Count) = Deck(Count + 1)
  132.         Next
  133.         Deck(52 - NoOfCardsTurnedOver).Suit = 0
  134.         Deck(52 - NoOfCardsTurnedOver).Rank = 0
  135.     End Sub
  136.  
  137.     Function IsNextCardHigher(ByVal LastCard As TCard, ByVal NextCard As TCard) As Boolean
  138.         Dim Higher As Boolean
  139.         Higher = False
  140.         If NextCard.Rank > LastCard.Rank Then
  141.             Higher = True
  142.         End If
  143.         Return Higher
  144.     End Function
  145.  
  146.     Function GetPlayerName() As String
  147.         Dim PlayerName As String
  148.         Console.WriteLine()
  149.         Console.Write("Please enter your name: ")
  150.         PlayerName = Console.ReadLine
  151.         Console.WriteLine()
  152.         Return PlayerName
  153.     End Function
  154.  
  155.     Function GetChoiceFromUser() As Char
  156.         Dim Choice As Char
  157.         Console.Write("Do you think the next card will be higher than the last card (enter y or n)? ")
  158.         Choice = Console.ReadLine
  159.         Return Choice
  160.     End Function
  161.  
  162.     Sub DisplayEndOfGameMessage(ByVal Score As Integer)
  163.         Console.WriteLine()
  164.         Console.WriteLine("GAME OVER!")
  165.         Console.WriteLine("Your score was " & Score)
  166.         If Score = 51 Then
  167.             Console.WriteLine("WOW!  You completed a perfect game.")
  168.         End If
  169.         Console.WriteLine()
  170.     End Sub
  171.  
  172.     Sub DisplayCorrectGuessMessage(ByVal Score As Integer)
  173.         Console.WriteLine()
  174.         Console.WriteLine("Well done!  You guessed correctly.")
  175.         Console.WriteLine("Your score is now " & Score & ".")
  176.         Console.WriteLine()
  177.     End Sub
  178.  
  179.     Sub ResetRecentScores(ByRef RecentScores() As TRecentScore)
  180.         Dim Count As Integer
  181.         For Count = 1 To NoOfRecentScores
  182.             RecentScores(Count).Name = ""
  183.             RecentScores(Count).Score = 0
  184.         Next
  185.     End Sub
  186.  
  187.     Sub DisplayRecentScores(ByVal RecentScores() As TRecentScore)
  188.         Dim Count As Integer
  189.         Console.WriteLine()
  190.         Console.WriteLine("Recent scores:")
  191.         Console.WriteLine()
  192.         For Count = 1 To NoOfRecentScores
  193.             Console.WriteLine(RecentScores(Count).Name & " got a score of " & RecentScores(Count).Score)
  194.         Next
  195.         Console.WriteLine()
  196.         Console.WriteLine("Press the Enter key to return to the main menu")
  197.         Console.WriteLine()
  198.         Console.ReadLine()
  199.     End Sub
  200.  
  201.     Sub UpdateRecentScores(ByRef RecentScores() As TRecentScore, ByVal Score As Integer)
  202.         Dim PlayerName As String
  203.         Dim Count As Integer
  204.         Dim FoundSpace As Boolean
  205.         PlayerName = GetPlayerName()
  206.         FoundSpace = False
  207.         Count = 1
  208.         While Not FoundSpace And Count <= NoOfRecentScores
  209.             If RecentScores(Count).Name = "" Then
  210.                 FoundSpace = True
  211.             Else
  212.                 Count = Count + 1
  213.             End If
  214.         End While
  215.         If Not FoundSpace Then
  216.             For Count = 1 To NoOfRecentScores - 1
  217.                 RecentScores(Count) = RecentScores(Count + 1)
  218.             Next
  219.             Count = NoOfRecentScores
  220.         End If
  221.         RecentScores(Count).Name = PlayerName
  222.         RecentScores(Count).Score = Score
  223.     End Sub
  224.  
  225.     Sub PlayGame(ByVal Deck() As TCard, ByRef RecentScores() As TRecentScore)
  226.         Dim NoOfCardsTurnedOver As Integer
  227.         Dim GameOver As Boolean
  228.         Dim NextCard As TCard
  229.         Dim LastCard As TCard
  230.         Dim Higher As Boolean
  231.         Dim Choice As Char
  232.         GameOver = False
  233.         GetCard(LastCard, Deck, 0)
  234.         DisplayCard(LastCard)
  235.         NoOfCardsTurnedOver = 1
  236.         While NoOfCardsTurnedOver < 52 And Not GameOver
  237.             GetCard(NextCard, Deck, NoOfCardsTurnedOver)
  238.             Do
  239.                 Choice = GetChoiceFromUser()
  240.             Loop Until Choice = "y" Or Choice = "n"
  241.             DisplayCard(NextCard)
  242.             NoOfCardsTurnedOver = NoOfCardsTurnedOver + 1
  243.             Higher = IsNextCardHigher(LastCard, NextCard)
  244.             If Higher And Choice = "y" Or Not Higher And Choice = "n" Then
  245.                 DisplayCorrectGuessMessage(NoOfCardsTurnedOver - 1)
  246.                 LastCard = NextCard
  247.             Else
  248.                 GameOver = True
  249.             End If
  250.         End While
  251.         If GameOver Then
  252.             DisplayEndOfGameMessage(NoOfCardsTurnedOver - 2)
  253.             UpdateRecentScores(RecentScores, NoOfCardsTurnedOver - 2)
  254.         Else
  255.             DisplayEndOfGameMessage(51)
  256.             UpdateRecentScores(RecentScores, 51)
  257.         End If
  258.     End Sub
  259. End Module
Advertisement
Add Comment
Please, Sign In to add comment