Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 10.84 KB | None | 0 0
  1. Imports System.IO
  2. Module Module1
  3.     Dim TempList As New List(Of String)
  4.     Dim UsersInput, Filepath As String
  5.     Dim TempArray() As String
  6.     Dim index As Integer = 0
  7.     Dim TempVar As String
  8.     Sub Main()
  9.         intro(True, "To create a new account press (1) or if you want to login press (2)")
  10.         Do Until UsersInput = "1" Or UsersInput = "2"
  11.             intro(False, "Sorry, To create a new account press (1) or if you want to login press (2). These are the only 2 values that are accepted")
  12.         Loop
  13.         If UsersInput = "1" Then
  14.             CreateNewAccount()
  15.         Else
  16.             login("Username:")
  17.         End If
  18.         Console.Clear()
  19.         UsersInterface()
  20.         Console.ReadLine()
  21.     End Sub
  22.     Sub UsersInterface()
  23.         Console.WriteLine("Hello {0}", TempArray(0))
  24.         Do Until UsersInput = "1" Or UsersInput = "2"
  25.             intro(False, "If you would like to know your prevoius scores press (1). If you would like to play the game, then press (2)")
  26.         Loop
  27.         checkrange(UsersInput, 1, 2, "Sorry you can only type 1 or 2 ")
  28.         If UsersInput = "1" Then
  29.             PrintScores()
  30.             UsersInput = "0"
  31.             UsersInterface()
  32.         Else
  33.             Playgame(0)
  34.         End If
  35.     End Sub
  36.     Sub Playgame(ByVal carrriedscore As Integer)
  37.         Dim score As Integer = carrriedscore
  38.         intro(False, "Which shape would you like to practice from rectange(1) , circle(2) or triangle(3)")
  39.         Do Until UsersInput = "1" Or UsersInput = "2" Or UsersInput = "3"
  40.             intro(False, "You can only type 1, 2 or 3. So which shape would you like to practice from rectange(1) , circle(2) or triangle(3)")
  41.         Loop
  42.         Dim wrongans1, wrongans2, wrongans3 As Integer
  43.         wrongans1 = 0
  44.         wrongans2 = 0
  45.         wrongans3 = 0
  46.         Randomize()
  47.         While wrongans2 = wrongans1 Or wrongans3 = wrongans2 Or wrongans1 = wrongans3
  48.             wrongans1 = CInt(Int((100 * Rnd()) + 1))
  49.             wrongans2 = CInt(Int((100 * Rnd()) + 1))
  50.             wrongans3 = CInt(Int((100 * Rnd()) + 1))
  51.         End While
  52.         TempList.Clear()
  53.         TempList.Add(wrongans1)
  54.         TempList.Add(wrongans2)
  55.         TempList.Add(wrongans3)
  56.         If UsersInput = 1 Then
  57.             If shapes("rectangle") = True Then
  58.                 score += 2
  59.             Else
  60.                 Try2(score)
  61.             End If
  62.         ElseIf UsersInput = 2 Then
  63.             If shapes("circle") = True Then
  64.                 score += 2
  65.             Else
  66.                 Try2(score)
  67.             End If
  68.         ElseIf UsersInput = 3 Then
  69.             If shapes("triangle") = True Then
  70.                 score += 2
  71.             Else
  72.                 Try2(score)
  73.             End If
  74.         End If
  75.         Console.WriteLine("Your current score is now {0}", score)
  76.         intro(False, "If you would like to play again press (1), if you would like logout and save your score press (2) or if you would like to see all your past scores press (3) ")
  77.         checkrange(UsersInput, 1, 3, "Sorry you can only type 1, 2 or 3")
  78.         If UsersInput = "1" Then
  79.             Playgame(score)
  80.         ElseIf UsersInput = "2" Then
  81.             AddScoreToFile(score, Filepath)
  82.             Environment.Exit(0)
  83.         ElseIf usersInput = "3" Then
  84.             PrintScores()
  85.             intro(False, "If you would like to play again press (1), if you would like logout and save your score press (2)")
  86.             checkrange(UsersInput, 1, 3, "Sorry you can only type 1 or 2")
  87.             If UsersInput = "1" Then
  88.                 Playgame(score)
  89.             ElseIf UsersInput = "2" Then
  90.                 AddScoreToFile(score, Filepath)
  91.                 Environment.Exit(0)
  92.             End If
  93.         End If
  94.     End Sub
  95.     Sub AddScoreToFile(ByRef score As String, ByRef filepath As String)
  96.         Dim FileToWrite As New StreamWriter(filepath, True)
  97.         Dim nowtime As String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
  98.         Console.WriteLine()
  99.         FileToWrite.WriteLine(score & "   -Time this test was done at was " & nowtime)
  100.         FileToWrite.Close()
  101.     End Sub
  102.     Sub Try2(ByRef score As Integer)
  103.         If secondchance() = True Then
  104.             score += 1
  105.         Else
  106.             Console.WriteLine("The correct answer was {0}", TempVar)
  107.         End If
  108.     End Sub
  109.     Function secondchance() As Boolean
  110.         intro(False, "Choose another answer")
  111.         UsersInput = UsersInput.ToLower
  112.         Do Until UsersInput = "a" Or UsersInput = "b" Or UsersInput = "c" Or UsersInput = "d"
  113.             options("Sorry your input can only be a,b,c or d")
  114.         Loop
  115.         If TempList(Asc((UsersInput)) - Asc("a")) = TempVar Then
  116.             Console.WriteLine("Well Done. This is the correct answer !!!")
  117.             Return True
  118.         Else
  119.             Console.WriteLine("Sorry your choice {0} is incorrect", UsersInput)
  120.             Return False
  121.         End If
  122.     End Function
  123.     Function shapes(ByVal chosenshape As String) As Boolean
  124.         Randomize()
  125.         Dim length As Integer = CInt(Int((10 * Rnd()) + 1))
  126.         Dim width As Integer = CInt(Int((10 * Rnd()) + 1))
  127.         Dim ans As String = ""
  128.         If chosenshape = "rectangle" Then
  129.             ans = length * width
  130.             TempList.Add(ans)
  131.         ElseIf chosenshape = "circle" Then
  132.             TempList.Add(Math.Pow(length, 2))
  133.             ans = (Math.Pow(length, 2) & "π")
  134.             For i = 0 To 3
  135.                 TempList(i) = (TempList(i) & "π")
  136.             Next
  137.         ElseIf chosenshape = "triangle" Then
  138.             ans = length * width / 2
  139.             TempList.Add(ans)
  140.         End If
  141.         TempVar = ans
  142.         Console.Clear()
  143.         UsersInput = UsersInput.ToLower
  144.         If Not (chosenshape = "circle") Then
  145.             Console.WriteLine("What is the area of a {0} with length {1} and height {2}", chosenshape, length, width)
  146.         ElseIf chosenshape = "circle" Then
  147.             Console.WriteLine("What is the area of a circle with radius {0} ", length)
  148.         End If
  149.         Do Until UsersInput = "a" Or UsersInput = "b" Or UsersInput = "c" Or UsersInput = "d"
  150.             options("Sorry your input can only be a,b,c or d")
  151.         Loop
  152.         If (TempList(Asc((UsersInput)) - Asc("a")) = ans) Then
  153.             Console.WriteLine("This is the correct answer")
  154.             Return True
  155.         Else
  156.             Console.WriteLine("Sorry your choice {0} is incorrect", UsersInput)
  157.             Return False
  158.         End If
  159.     End Function
  160.     Sub options(ByVal output As String)
  161.         Console.WriteLine(output)
  162.         intro(False, "Is It   a) " & TempList(0) & "     B) " & TempList(1) & "    C) " & TempList(2) & "    D) " & TempList(3))
  163.     End Sub
  164.     Sub TransformFileToTempArray(ByVal filename As String)
  165.         Dim FileToRead As New StreamReader(filename)
  166.         ReDim TempArray(0)
  167.         index = 0
  168.         Do
  169.             AddElementsToArray(FileToRead.ReadLine, TempArray, index)
  170.             index += 1
  171.         Loop Until FileToRead.EndOfStream
  172.         FileToRead.Close()
  173.     End Sub
  174.     Sub PrintScores()
  175.         TransformFileToTempArray(Filepath)
  176.         If TempArray.Length - 1 <> 1 Then
  177.             Console.WriteLine("Your scores are :")
  178.             For i = 2 To TempArray.Length - 1
  179.                 Console.WriteLine(TempArray(i))
  180.             Next
  181.         Else
  182.             Console.WriteLine("You have not done any questions yet")
  183.         End If
  184.     End Sub
  185.     Sub CreateNewAccount()
  186.         intro(False, "What would you like your username to be :")
  187.         checkrange(UsersInput, "a", "z", "Sorry username must contain nothing other than letters")
  188.         While (FileExist("C:" & UsersInput & ".txt"))
  189.             Console.WriteLine("Sorry, this username has already been chosen")
  190.             CreateNewAccount()
  191.         End While
  192.         File.Create("C:" & UsersInput & ".txt").Dispose()
  193.         Filepath = "C:" & UsersInput & ".txt"
  194.         Dim FileToWrite As New StreamWriter(Filepath, True)
  195.         FileToWrite.WriteLine(UsersInput)
  196.         password(5)
  197.         FileToWrite.WriteLine(UsersInput)
  198.         FileToWrite.Close()
  199.         TransformFileToTempArray(Filepath)
  200.     End Sub
  201.     Sub password(ByVal LengthOfPass As Integer)
  202.         intro(False, "New Password :")
  203.         Do Until (UsersInput.Contains(0) Or UsersInput.Contains(1) Or UsersInput.Contains(2) Or UsersInput.Contains(3) Or UsersInput.Contains(4) Or UsersInput.Contains(5) Or UsersInput.Contains(6) Or UsersInput.Contains(7) Or UsersInput.Contains(8) Or UsersInput.Contains(9)) And UsersInput.Length >= LengthOfPass
  204.             intro(False, "Sorry passsword must contain at least a number And must be more than " & LengthOfPass & " characters: ")
  205.         Loop
  206.     End Sub
  207.     Sub login(ByVal output As String)
  208.         intro(False, output)
  209.         Dim username As String = UsersInput
  210.         intro(False, "Password:")
  211.         Dim password As String = UsersInput
  212.         Filepath = "C:" & username & ".txt"
  213.         If FileExist(Filepath) = True Then
  214.             TransformFileToTempArray(Filepath)
  215.             If TempArray(1) = password Then
  216.                 Console.WriteLine("Successfully logged In. Press Enter to continue")
  217.                 Console.ReadLine()
  218.             Else
  219.                 login("Username or password is incorrect. Try again. Username:")
  220.             End If
  221.         Else
  222.             login("Username or password is incorrect. Try again. Username:")
  223.         End If
  224.     End Sub
  225.     Sub AddElementsToArray(ByVal StringToAdd As String, ByRef storage() As String, ByVal i As Integer)
  226.         ReDim Preserve storage(i)
  227.         storage(i) = StringToAdd
  228.     End Sub
  229.     Function FileExist(ByRef filename As String) As Boolean
  230.         If File.Exists(filename) Then
  231.             Return True
  232.         Else Return False
  233.         End If
  234.     End Function
  235.     Sub checkrange(ByVal input As String, ByVal min As String, ByVal max As String, ByVal output As String)
  236.         For i = 0 To input.Length - 1
  237.             If Not (Asc(input(i)) >= Asc(min) And Asc(input(i)) <= Asc(max)) Then
  238.                 intro(False, output)
  239.                 checkrange(UsersInput, min, max, output)
  240.                 Exit Sub
  241.             End If
  242.         Next
  243.     End Sub
  244.     Sub intro(ByVal begin As Boolean, ByVal output As String)
  245.         If begin = True Then
  246.             Console.WriteLine("This is a trainer for people who wish to practice with how to work out the area of Shapes")
  247.             If output = "" Then
  248.                 intro(False, "")
  249.             Else
  250.                 intro(False, output)
  251.             End If
  252.         ElseIf begin = False And output = "" Then
  253.             Console.WriteLine(">>>>> Command :")
  254.             UsersInput = Console.ReadLine
  255.         ElseIf begin = False And Not (output = "") Then
  256.             Console.WriteLine(output)
  257.             UsersInput = Console.ReadLine
  258.         End If
  259.     End Sub
  260. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement