How do I move the position of the player based on user input. (Represented by O). Code I have written So far. Pls answer in vb - vb.net

Module Program
Dim startx As Integer
Dim starty As Integer
Dim endx As Integer
Dim endy As Integer
Dim maze As String(,) = {
{"x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x"},
{"O ", " ", " ", " ", " ", " ", " ", " ", " ", "x"},
{"x ", "x ", "x ", "x ", "x ", " ", "x ", "x ", " ", "x"},
{"x ", "x ", " ", "x ", "x ", " ", "x ", "x ", " ", "x"},
{"x ", "x ", " ", " ", " ", " ", "x ", "x ", " ", "x"},
{"x ", "x ", "x ", "x ", "x ", " ", "x ", "x ", "x ", "x"},
{"x ", " ", " ", " ", " ", " ", " ", " ", " ", "x"},
{"x ", " ", "x ", "x ", "x ", "x ", "x ", "x ", " ", "x"},
{"x ", " ", " ", " ", " ", "x ", "x ", "x ", " ", " "},
{"x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x ", "x"}}
Sub Main(args As String())
Console.WriteLine("This is a maze game")
Console.WriteLine("Please enter where you want to go (x and y cordinate)")
start
displaychessboard()
End Sub
Sub displaychessboard()
Console.WriteLine(" 0 1 2 3 4 5 6 7 8 9")
For x As Integer = 0 To 9
Console.Write(x & " ")
For y As Integer = 0 To 9
Console.Write(maze(x, y))
Next
Console.WriteLine()
Next
End Sub
Sub makemove()
End Sub
End Module

How do I move the position of the player based on user input.
You store the position of a player in one or more variables. Since you have a two-dimensional maze, you can use a two-element tuple or, for simplicity, a player_x and a player_y variable.
When the user provides input, you modify the variables accordingly. For example, "moving left" would decrease player_x by one.
When drawing your maze, you look at the current values of player_x and player_y. If they match the current position in your loop, you output "O" instead of a space.
Pls answer in vb
The Stack Overflow community will gladly help you understand concepts and teach you how to improve your Visual Basic skills, but we are not a "here is my task, you write code" service.

Related

Reading Code From a Text File Efficiently And How to Distinguish Blocks of Code In The Text File

I'm creating a utility for a game that creates spreadsheets of data by reading the game's code.
One of the most basic aspects of this is being able to read the game's code efficiently. The game is written in C#. I'm using VB.Net for my program because my mind can't grasp the abstraction of more complex languages. Historically the way I had my program recognise code blocks in the code text file is by counting the brackets that encapsulated them and working off of that. This seems a bit clunky, though. Is there any simpler way to help the program figure out code blocks?
For Example, code in the text file might look like this:
6673={
key="d_example_duchy_1"
de_facto_liege=12
variables={
data={ {
flag="traditional_de_jure_kingdom"
tick=0
data={
type=lt
identity=6632
}
}
}
}
de_jure_liege=6632
de_jure_vassals={ 6674 6678 6682 6686 }
holder=558
name="Example Duchy 1"
date=1253.1.1
heir={ 880 }
history={ 1253.1.1=558 }
capital=6674
history_government="tribal_government"
coat_of_arms_id=2320
}
6674={
key="c_example_county_1"
variables={
data={ {
flag="influx_culture"
tick=0
data={
type=culture
identity=11
}
}
{
flag="influx_faith"
tick=0
data={
type=faith
identity=57
}
}
{
flag="fog_resistance"
tick=0
data={
type=value
}
}
}
}
de_jure_liege=6673
de_jure_vassals={ 6675 6676 6677 }
holder=558
name="Example County 1"
date=1243.6.26
heir={ 880 }
capital=6674
coat_of_arms_id=2323
}
6675={
key="b_example_barony_1"
de_facto_liege=6674
de_jure_liege=6674
holder=558
name="Example Barony 1"
date=1254.1.1
capital_barony=yes
duchy_capital_barony=yes
capital=6674
coat_of_arms_id=2328
}
6676={
key="b_example_barony_2"
de_facto_liege=6674
de_jure_liege=6674
holder=11609
name="Example Barony 2"
date=1253.7.28
capital=6674
coat_of_arms_id=2332
}
6677={
key="b_example_barony_3"
de_facto_liege=6674
de_jure_liege=6674
holder=558
name="Example Barony 3"
date=1232.2.8
heir={ 880 }
capital=6674
theocratic_lease=yes
coat_of_arms_id=2334
}
Thanks in advance. :)
The solution I eventually settled on, for discerning the overall code-blocks (e.g. all the data nested under 6674={ ... }) was basically:
Dim Code As String = File.ReadAllText("PATH/TO/CODE/FILE.txt")
Dim Output As List(Of String) = Code.Split(vbCrLf & "}" & vbCrLf).ToList
So in plain English, reading all the code into a single string, then splitting the string according to how the original code's language distinguished the initial nest of code (i.e. a carriage return followed by a curly bracket followed by another carriage return).
The list at the end would look something like this:
Output(0)
6673={" & vbCrLf & " key=""d_example_duchy_1""" & vbCrLf & " de_facto_liege=12" & vbCrLf & " variables={" & vbCrLf & " data={ {" & vbCrLf & " flag=""traditional_de_jure_kingdom""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=lt" & vbCrLf & " identity=6632" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " de_jure_liege=6632" & vbCrLf & " de_jure_vassals={ 6674 6678 6682 6686 }" & vbCrLf & " holder=558" & vbCrLf & " name=""Example Duchy 1""" & vbCrLf & " date=1253.1.1" & vbCrLf & " heir={ 880 }" & vbCrLf & " history={ 1253.1.1=558 }" & vbCrLf & " capital=6674" & vbCrLf & " history_government=""tribal_government""" & vbCrLf & " coat_of_arms_id=2320
Output(1)
6674={" & vbCrLf & " key=""c_example_county_1""" & vbCrLf & " variables={" & vbCrLf & " data={ {" & vbCrLf & " flag=""influx_culture""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=culture" & vbCrLf & " identity=11" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " {" & vbCrLf & " flag=""influx_faith""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=faith" & vbCrLf & " identity=57" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " {" & vbCrLf & " flag=""fog_resistance""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=value" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " de_jure_liege=6673" & vbCrLf & " de_jure_vassals={ 6675 6676 6677 }" & vbCrLf & " holder=558" & vbCrLf & " name=""Example County 1""" & vbCrLf & " date=1243.6.26" & vbCrLf & " heir={ 880 }" & vbCrLf & " capital=6674" & vbCrLf & " coat_of_arms_id=2323
And so on with the remaining three.

2d array of generic length in a class

I have a class that currently works like this.
Class Maze
Public board(10, 10) As Object
Dim maze1 As New Maze With {.board = {{" ", " ", "X", "X", " ", "X", "X", "X", " ", "X"},
{"X", " ", " ", " ", " ", "X", " ", " ", " ", "X"},
{" ", " ", "X", " ", "X", " ", " ", " ", " ", "X"},
{"X", " ", "X", " ", " ", "X", "X", "X", " ", " "},
{"X", " ", "X", "X", " ", " ", " ", " ", "X", " "},
{" ", "X", " ", " ", "X", " ", " ", "X", " ", " "},
{" ", "X", "X", " ", "X", " ", " ", " ", " ", "X"},
{" ", " ", " ", " ", " ", "X", "X", " ", " ", "X"},
{"X", "X", " ", "X", " ", " ", " ", "X", " ", " "},
{" ", " ", " ", "X", "X", " ", " ", " ", "X", " "}}}
However I cant find away to remove the prespecified length without methods such as GetLength now not working.
In the .Net world, if you define a type as Object you're almost always doing something very wrong you will soon come to regret. In this case, it looks like String may be more appropriate. However, perhaps based on how this is used later it might turn out that Integer or a specific class type could be better, alongside a special method to translate this into text or graphics for output.
Moving on to the specific issue of size... consider a List:
Class Maze
Dim board As List(Of List(Of String))
board = New List(Of List(Of string)) From {
New List(Of String) From {" ", " ", "X", "X", " ", "X", "X", "X", " ", "X"},
New List(Of String) From {"X", " ", " ", " ", " ", "X", " ", " ", " ", "X"},
New List(Of String) From {" ", " ", "X", " ", "X", " ", " ", " ", " ", "X"},
New List(Of String) From {"X", " ", "X", " ", " ", "X", "X", "X", " ", " "},
New List(Of String) From {"X", " ", "X", "X", " ", " ", " ", " ", "X", " "},
New List(Of String) From {" ", "X", " ", " ", "X", " ", " ", "X", " ", " "},
New List(Of String) From {" ", "X", "X", " ", "X", " ", " ", " ", " ", "X"},
New List(Of String) From {" ", " ", " ", " ", " ", "X", "X", " ", " ", "X"},
New List(Of String) From {"X", "X", " ", "X", " ", " ", " ", "X", " ", " "},
New List(Of String) From {" ", " ", " ", "X", "X", " ", " ", " ", "X", " "}
}
Now you can add a line to end like so:
board.Add(New List(Of String) From {" ", "X", " ", " ", " " ... })
Or add a column like this:
For Each c As List(Of String) In board
c.Add(" ")
Next
This allows you to have 2d array without specifying length explicitly
Public board(,) As Object = {}

VB net. Morse code translator: cut correct signs using Mid() and match them with the letter

I am trying to translate Morse code into text in VB.net.
Using below code, some of the letters in the text stays untranslated. I believe the problem is with "oddel" which should indicated how many letters should be cut in Mid(morsC, i, oddel), while selecting the case.
Code should be able to translate this >…./ ./ .-../ .-../ ---/< into Hello.
What's weird is that while translating "Hello", first letter "H" is correctly cut as " ..../" but it's not matched with the Case "H"
'Hello = …./ ./ .-../ .-../ ---/
Sub EE15a()
Dim veta As String, morsCod As String, st As String
morsCod = InputBox("Enter the morse code.")
veta = morsCodF(morsCod)
st = "Preklad dle funkce " + Chr(10) + veta + Chr(10)
MsgBox(st)
End Sub
Function morsCodF(morsC As String) As String
Dim pismeno As String, znak As String
Dim i As Integer, j As Integer, oddel As Integer
Dim nasel As Boolean
znak = "/"
morsC = Trim(morsC)
j = 1
For i = 1 To (morsC.Length)
i = j
Do
If Mid(morsC, j, 1) = znak Then
oddel = j - oddel
nasel = True
Exit Do
End If
j = j + 1
Loop Until nasel = True Or j > (morsC.Length)
If i = 1 Then
pismeno = (" " + Mid(morsC, i, oddel))
Else
pismeno = Mid(morsC, i, oddel)
End If
Select Case pismeno '" ...-/"
Case " .-/" : morsCodF = morsCodF + " a"
Case " -.../" : morsCodF = morsCodF + " b"
Case " -.-./" : morsCodF = morsCodF + " c"
Case " -../" : morsCodF = morsCodF + " d"
Case " ./" : morsCodF = morsCodF + " e"
Case " ..-./" : morsCodF = morsCodF + " f"
Case " --./" : morsCodF = morsCodF + " g"
Case " ..../" : morsCodF = morsCodF + " h"
Case " ../" : morsCodF = morsCodF + " i"
Case " .---/" : morsCodF = morsCodF + " j"
Case " -.-/" : morsCodF = morsCodF + " k"
Case " .-../" : morsCodF = morsCodF + " l"
Case " --/" : morsCodF = morsCodF + " m"
Case " -./" : morsCodF = morsCodF + " n"
Case " ---/" : morsCodF = morsCodF + " o"
Case " .--./" : morsCodF = morsCodF + " p"
Case " --.-/" : morsCodF = morsCodF + " q"
Case " .-./" : morsCodF = morsCodF + " r"
Case " .../" : morsCodF = morsCodF + " s"
Case " -/" : morsCodF = morsCodF + " t"
Case " ..-/" : morsCodF = morsCodF + " u"
Case " ...-/" : morsCodF = morsCodF + " v"
Case " .--/" : morsCodF = morsCodF + " w"
Case " -..-/" : morsCodF = morsCodF + " x"
Case " -.--/" : morsCodF = morsCodF + " y"
Case " --../" : morsCodF = morsCodF + " z"
Case "#" : morsCodF = morsCodF + " "
End Select
nasel = False
pismeno = ""
j = j + 1
Next
End Function
Code should translate whole word or sentence, but instead is translating only part of the text. E.g. > …./ ./ .-../ .-../ ---/
expected: "Hello", actual: "e o"
split the string morsCod with "/" as separator
Public Class Form1
Sub EE15a()
Dim veta As String, morsCod As String, st As String
'morsCod = InputBox("Enter the morse code.")
morsCod = "..../ ./ .-../ .-../ ---/ --"
morsCod = " .-/ -.../ -.-./ -../ ./ ..-./ --./ ..../ ../ .---/ -.-/ .-../ --/ -./ ---/ .--./ --.-/ .-./ .../ -/ ..-/ ...-/ .--/ -..-/ -.--/ --.."
veta = morsCodF(morsCod)
st = "Preklad dle funkce " + Chr(10) + veta + Chr(10)
MsgBox(st)
End Sub
Function morsCodF(morsC As String) As String
Dim arr As String() = morsC.Split("/"c)
For Each s As String In arr
Select Case s
Case " .-" : morsCodF = morsCodF + " a"
Case " -..." : morsCodF = morsCodF + " b"
Case " -.-." : morsCodF = morsCodF + " c"
Case " -.." : morsCodF = morsCodF + " d"
Case " ." : morsCodF = morsCodF + " e"
Case " ..-." : morsCodF = morsCodF + " f"
Case " --." : morsCodF = morsCodF + " g"
Case " ...." : morsCodF = morsCodF + " h"
Case " .." : morsCodF = morsCodF + " i"
Case " .---" : morsCodF = morsCodF + " j"
Case " -.-" : morsCodF = morsCodF + " k"
Case " .-.." : morsCodF = morsCodF + " l"
Case " --" : morsCodF = morsCodF + " m"
Case " -." : morsCodF = morsCodF + " n"
Case " ---" : morsCodF = morsCodF + " o"
Case " .--." : morsCodF = morsCodF + " p"
Case " --.-" : morsCodF = morsCodF + " q"
Case " .-." : morsCodF = morsCodF + " r"
Case " ..." : morsCodF = morsCodF + " s"
Case " -" : morsCodF = morsCodF + " t"
Case " ..-" : morsCodF = morsCodF + " u"
Case " ...-" : morsCodF = morsCodF + " v"
Case " .--" : morsCodF = morsCodF + " w"
Case " -..-" : morsCodF = morsCodF + " x"
Case " -.--" : morsCodF = morsCodF + " y"
Case " --.." : morsCodF = morsCodF + " z"
Case "#" : morsCodF = morsCodF + " "
End Select
Next
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
EE15a()
End Sub
End Class
If you need some letters uppercase, you need provide additional logic (for instance, first letter is always uppercase). The following code creates dictionary with codes and their letters, parses Morse code string with Regex, extracting codes one-by-one and fetching letters based on these codes. I have created two versions - with Regex and Split.
Dim morse = New Dictionary(Of String, String) From
{
{".-", "a"},
{"-...", "b"},
{"-.-.", "c"},
{".", "e"},
{"....", "h"},
{".-..", "l"},
{"---", "o"},
{"#", " "}
}
Dim s = "..../ ./ .-../ .-../ ---/"
'// 1. REGEX
'// Search for '.', '-' and '#' in any combinations followed by '/'
Dim mc = Regex.Matches(s, "[\.\-#]+(?=/)")
'// Fetch each code from dictionary and concatenate them
Dim x = String.Join("", mc.Cast(Of Match).Select(Function(m) morse(m.Value)))
'// 2. SPLIT
'// Dim x = String.Join("", s.Split({"/"}, StringSplitOptions.RemoveEmptyEntries).
'// Select(Function(z) morse(z.Replace("/", "").Trim())))
Console.WriteLine(x)
'// Output: hello
I think you will get more feedback of the code if you add default case for the Select Case switch.
Select Case s
Case " .-" : morsCodF = morsCodF + " a"
Case "#" : morsCodF = morsCodF + " "
Case Else : morsCodF = morsCodF + " WRONG! "
End Select
Check does this line oddel = j - oddel works as expected.
Based on the logic you have oddel should be amount of characters after previously found / character. If so, then calculation oddel = j - oddel doesn't look right.
Because you updating i with current j calculation should be oddel = j - (i -1)
Check For loop.
Because i updated every time with current value of j, code will make one extra loop which will produce "wrong" value.
Other suggestions.
Decoding morse code to the text is simple mapping from one string to another.
In vb.net Dictionary(Of String, String) could be good tool for this.
Do you really need / character? Instead you can use simple space as letter delimeter.
Function FromMorse(code As String) As String
Const DELIMETER As Char = " "c
Static Secret As New Dictionary(Of String, String) From
{
{".-", "a"},
{"-...", "b"},
{"-.-.", "c"},
{"-..", "d"},
{".", "e"},
{"..-.", "f"},
{"--.", "g"},
{"....", "h"},
{"..", "i"},
{".---", "j"},
{"-.-", "k"},
{".-..", "l"},
{"--", "m"},
{"-.", "n"},
{"---", "o"},
{".--.", "p"},
{"--.-", "q"},
{".-.", "r"},
{"...", "s"},
{"-", "t"},
{"..-", "u"},
{"...-", "v"},
{".--", "w"},
{"-..-", "x"},
{"-.--", "y"},
{"--..", "z"},
{"#", " "}
}
Return code.Split(DELIMETER).
Select(Function(c) Secret(c)).
Aggregate(New StringBuilder(), Function(b, c) b.Append(c)).
ToString()
End Function
Usage
Dim code = ".... . .-.. .-.. --- # .-- --- .-. .-.. -.."
Dim decoded = FromMorse(code)
Console.WriteLine(decoded) ' => "hello world"
A Dictionary for fast look-up, String.Split for brevity and a StringBuilder to keep from creating new strings on every iteration.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = GetTextFromMorse("..../ ./ .-../ .-../ ---/")
Debug.Print(s)
End Sub
Private DictCode As New Dictionary(Of String, String) From
{
{".-", "a"},
{"-...", "b"},
{"-.-.", "c"},
{"-..", "d"},
{".", "e"},
{"..-.", "f"},
{"--.", "g"},
{"....", "h"},
{"..", "i"},
{".---", "j"},
{"-.-", "k"},
{".-..", "l"},
{"--", "m"},
{"-.", "n"},
{"---", "o"},
{".--.", "p"},
{"--.-", "q"},
{".-.", "r"},
{"...", "s"},
{"-", "t"},
{"..-", "u"},
{"...-", "v"},
{".--", "w"},
{"-..-", "x"},
{"-.--", "y"},
{"--..", "z"},
{"#", " "}
}
Private Function GetTextFromMorse(input As String) As String
Dim strArray() As String = input.Split({"/"}, StringSplitOptions.RemoveEmptyEntries)
Dim sb As New StringBuilder
For Each s In strArray
sb.Append(DictCode(s.Trim))
Next
Return sb.ToString
End Function

Getting system outofmemoryexception building string in vb

I have a dataset that is with arround 30000records, so loping inside it and building the string fileStavki trows exeption, systemoutofmemmory. How can i solve this. Is working with arrays possible solution or something else?
Dim sb As New StringBuilder
For Each inRow In ds_in.Tables(0).Rows
rbr += 1
pisi_red = "2" & Enc3.prefill(rbr, "0", 6) & Share.RightPrefill(inRow("prioritet"), "0", 5)
pisi_red &= Enc3.prefill(Enc3.IsNull(inRow("smetka_davac"), "0"), "0", 15)
pisi_red &= Share.RightPrefill(Enc3.lat2kir(Enc3.IsNull(inRow("naziv_davac"), "")), " ", 70)
pisi_red &= Share.RightPrefill(Enc3.IsNull(inRow("povik_broj_davac"), ""), " ", 24)
pisi_red &= Share.RightPrefill(Enc3.lat2kir(Enc3.IsNull(inRow("cel_na_doznaka"), "")), " ", 70)
pisi_red &= Enc3.prefill(inRow("sif_tip_plakanje"), "0", 6)
pisi_red &= "+" & Enc3.prefill(FormatNumber(inRow("iznos"), 5, TriState.False, TriState.False, TriState.False), "0", 21)
pisi_red &= inRow("smetka_primac") & Share.RightPrefill(Enc3.lat2kir(Trim(inRow("naziv_primac"))), " ", 70)
pisi_red &= Share.RightPrefill(Enc3.IsNull(inRow("povik_broj_primac"), ""), " ", 24)
pisi_red &= inRow("tip_instrument") & Share.RightPrefill(Enc3.IsNull(inRow("broj_transakcija"), ""), " ", 16)
pisi_red &= inRow("sistem_poravnuva") & Enc3.prefill(Enc3.IsNull(inRow("edb"), ""), "0", 13)
pisi_red &= vbCrLf
sb.Append(pisi_red)
'fileStavki &= pisi_red
Next
after this i have
sw.Write(fileStavki)
sw.Flush()
sw.Close()
I think even if i use stringbuilder the string fileStavki will be build like this
filestavki = sb.ToString
And here i will also get outofmemory exeption

VB.NET Lambda expression instead of iterator

I am fairly certain that this could be rewritten as a Lambda expression, but every attempt fails miserably. I know, C# Lambda reads cleaner, but I'm stuck with VB.NET. Here is the code - could someone point me in the right direction? Thanks!
For Each e As EventToMonitor In Events
If e.TypeID = 1 Then
If ("," & e.Values).Contains("," & b.ChoiceID & ",") Then
Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine &
"User: " & cacheValues.CurrentUserName & Environment.NewLine & _
"Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString)
End If
End If
Next
Something like this should work if you want to convert the whole thing to lambda:
Events.Where(Function(e) e.TypeID = 1 AndAlso ("," & e.Values).Contains("," & b.ChoiceID & ",")) _
.ToList() _
.ForEach(Sub(e) Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine & _
"User: " & cacheValues.CurrentUserName & Environment.NewLine & _
"Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString))
Honestly, though, I usually prefer to just use them to filter down results or build a collection:
Dim eventsList = Events.Where(Function(e) e.TypeID = 1 AndAlso ("," & e.Values).Contains("," & b.ChoiceID & ","))
For Each e As EventToMonitor In eventsList
Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine &
"User: " & cacheValues.CurrentUserName & Environment.NewLine & _
"Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString)
Next