NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

' Skeleton Program for the AQA A1 Summer 2017 examination
' this code should be used in conjunction with the Preliminary Material
' written by the AQA AS1 Programmer Team
' developed in the VB.Net 2008 environment


Imports System.IO

Module Module1
Const SOIL As Char = "."
Const SEED As Char = "S"
Const PLANT As Char = "P"
Const ROCKS As Char = "X"
Const FIELDLENGTH As Integer = 20
Const FIELDWIDTH As Integer = 35

Function GetHowLongToRun() As Integer
Dim Years As Integer
Console.WriteLine("Welcome to the Plant Growing Simulation")
Console.WriteLine()
Console.WriteLine("You can step through the simulation a year at a time")
Console.WriteLine("or run the simulation for 0 to 5 years")
Console.WriteLine("How many years do you want the simulation to run?")
Console.WriteLine()
Do
Console.Write("Enter a number between 0 and 5, or -1 for stepping mode: ")
Years = Console.ReadLine()
Loop Until Years = 1 Or Years = 2 Or Years = 3 Or Years = 4 Or Years = 5 Or Years = -1
Return Years
End Function

Function CreateNewField() As Char(,)
Dim Row As Integer
Dim Column As Integer
Dim Field(FIELDLENGTH, FIELDWIDTH) As Char
For Row = 0 To FIELDLENGTH - 1
For Column = 0 To FIELDWIDTH - 1
Field(Row, Column) = SOIL
Next
Next
For Row = 0 To 9
Column = Int(FIELDWIDTH * Rnd() + 1)
Field(Row, Column) = ROCKS
Next
Row = FIELDLENGTH 2
Column = FIELDWIDTH 2
Field(Row, Column) = SEED
Return Field
End Function

Function ReadFile() As Char(,)
Dim Row As Integer
Dim Column As Integer
Dim Field(FIELDLENGTH, FIELDWIDTH) As Char
Dim FileName As String
Dim FieldRow As String
Dim FileHandle As IO.StreamReader
Console.Write("Enter file name: ")
FileName = Console.ReadLine()
Try
FileHandle = New IO.StreamReader(FileName)
For Row = 0 To FIELDLENGTH - 1
FieldRow = FileHandle.ReadLine
For Column = 0 To FIELDWIDTH - 1
Field(Row, Column) = FieldRow(Column)
Next
Next
FileHandle.Close()
Catch
Field = CreateNewField()
End Try
Return Field
End Function

Function InitialiseField() As Char(,)
Dim Field(FIELDLENGTH, FIELDWIDTH) As Char
Dim Response As String
Console.Write("Do you want to load a file with seed positions? (Y/N): ")
Response = Console.ReadLine()
If Response = "Y" Then
Field = ReadFile()
Else
Field = CreateNewField()
End If
Return Field
End Function

Sub Display(ByVal Field(,) As Char, ByVal Season As String, ByVal Year As Integer)
Dim Row As Integer
Dim Column As Integer
Console.WriteLine("Season: " & Season & " Year number: " & Year)
For Row = 0 To FIELDLENGTH - 1
For Column = 0 To FIELDWIDTH - 1
Console.Write(Field(Row, Column))
Next
Console.WriteLine("|" & Str(Row).PadLeft(3))
Next
Console.WriteLine()
End Sub

Sub CountPlants(ByVal Field(,) As Char)
Dim Row As Integer
Dim Column As Integer
Dim NumberOfPlants As Integer
NumberOfPlants = 0
For Row = 0 To FIELDLENGTH - 1
For Column = 0 To FIELDWIDTH - 1
If Field(Row, Column) = PLANT Then
NumberOfPlants += 1
End If
Next
Next
If NumberOfPlants = 1 Then
Console.WriteLine("There is 1 plant growing")
Else
Console.WriteLine("There are " & NumberOfPlants & " plants growing")
End If
End Sub

Function SimulateSpring(ByVal Field As Char(,)) As Char(,)
Dim Frost As Boolean
Dim PlantCount As Integer
For Row = 0 To FIELDLENGTH - 1
For Column = 0 To FIELDWIDTH - 1
If Field(Row, Column) = SEED Then
Field(Row, Column) = PLANT
End If
Next
Next
CountPlants(Field)
If Int(Rnd() * 2) = 1 Then
Frost = True
Else
Frost = False
End If
If Frost Then
PlantCount = 0
For Row = 0 To FIELDLENGTH - 1
For Column = 0 To FIELDWIDTH - 1
If Field(Row, Column) = PLANT Then
PlantCount += 1
If PlantCount Mod 3 = 0 Then
Field(Row, Column) = SOIL
End If
End If
Next
Next
Console.WriteLine("There has been a frost")
CountPlants(Field)
End If
Return Field
End Function

Function SimulateSummer(ByVal Field(,) As Char) As Char(,)
Dim RainFall As Integer
Dim PlantCount As Integer
RainFall = Int(Rnd() * 3)
If RainFall = 0 Then
PlantCount = 0
For Row = 0 To FIELDLENGTH - 1
For Column = 0 To FIELDWIDTH - 1
If Field(Row, Column) = PLANT Then
PlantCount += 1
If PlantCount Mod 2 = 0 Then
Field(Row, Column) = SOIL
End If
End If
Next
Next
Console.WriteLine("There has been a severe drought")
CountPlants(Field)
End If
Return Field
End Function

Function SeedLands(ByVal Field(,) As Char, ByVal Row As Integer, ByVal Column As Integer) As Char(,)
If Row >= 0 And Row < FIELDLENGTH And Column >= 0 And Column < FIELDWIDTH Then
If Field(Row, Column) = SOIL Then
Field(Row, Column) = SEED
End If
End If
Return Field
End Function

Function SimulateAutumn(ByVal Field(,) As Char) As Char(,)
For Row = 0 To FIELDLENGTH - 1
For Column = 0 To FIELDWIDTH - 1
If Field(Row, Column) = PLANT Then
Field = SeedLands(Field, Row - 1, Column - 1)
Field = SeedLands(Field, Row - 1, Column)
Field = SeedLands(Field, Row - 1, Column + 1)
Field = SeedLands(Field, Row, Column - 1)
Field = SeedLands(Field, Row, Column + 1)
Field = SeedLands(Field, Row + 1, Column - 1)
Field = SeedLands(Field, Row + 1, Column)
Field = SeedLands(Field, Row + 1, Column + 1)
End If
Next
Next
Return Field
End Function

Function SimulateWinter(ByVal Field As Char(,)) As Char(,)
For Row = 0 To FIELDLENGTH - 1
For Column = 0 To FIELDWIDTH - 1
If Field(Row, Column) = PLANT Then
Field(Row, Column) = SOIL
End If
Next
Next
Return Field
End Function

Sub SimulateOneYear(ByVal Field(,) As Char, ByVal Year As Integer)
Field = SimulateSpring(Field)
Display(Field, "spring", Year)
Field = SimulateSummer(Field)
Display(Field, "summer", Year)
Field = SimulateAutumn(Field)
Display(Field, "autumn", Year)
Field = SimulateWinter(Field)
Display(Field, "winter", Year)
End Sub

Sub Simulation()
Dim YearsToRun As Integer
Dim Continuing As Boolean
Dim Response As String
Dim Year As Integer
Dim Field(FIELDWIDTH, FIELDLENGTH) As Char
Dim currentDisplay As String
YearsToRun = GetHowLongToRun()
If YearsToRun <> 0 Then
Field = InitialiseField()
If YearsToRun >= 1 Then
For Year = 1 To YearsToRun
SimulateOneYear(Field, Year)
Next
Else
Continuing = True
Year = 0
While Continuing
Year += 1
SimulateOneYear(Field, Year)
Console.Write("Press Enter to run simulation for another Year, Input X to stop, S to save: ")
Response = Console.ReadLine()
If Response = "x" Or Response = "X" Then
Continuing = False
End If
If Response = "s" Or Response = "S" Then
currentDisplay = ""
SaveMyField(currentDisplay)
End If
End While
End If
Console.WriteLine("End of Simulation")
End If
Console.ReadLine()
End Sub

Sub SaveMyField(ByRef currentField)
Dim fieldfile, filename As String
Dim currentfilewriter As IO.StreamWriter

filename = "MyField.txt"
fieldfile = currentField

currentfilewriter.WriteLine(fieldfile)
currentfilewriter.Close()

Console.WriteLine("Field has been saved!")
End
End Sub
Sub Main()
Randomize()
Simulation()
End Sub
End Module





     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.