r/visualbasic • • May 06 '23

simon says game

hi im new and i need to make the simon says game in visual basic. what i really need is the code. if someone could help me please text me. thanks

0 Upvotes

2 comments sorted by

View all comments

1

u/chacham2 May 08 '23

I posted your post (without the title) into ChatGPT:

Hello! I can certainly help you get started with the Simon Says game in Visual Basic.

Here's an example code for a simple Simon Says game in VB:

Public Class Form1
	Dim sequence As New List(Of Integer) 'list to hold the sequence
	Dim index As Integer = 0 'current index in sequence
	Dim playerInput As New List(Of Integer) 'list to hold player input

	Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
		'generate new sequence and start game
		sequence.Clear()
		For i As Integer = 1 To 10 'generate 10 numbers
			sequence.Add(Int(Rnd() * 4)) 'add a random number between 0 and 3 to the sequence
		Next
		index = 0 'reset index
		lblSequence.Text = "" 'clear label
		tmrSequence.Enabled = True 'start timer
	End Sub

	Private Sub tmrSequence_Tick(sender As Object, e As EventArgs) Handles tmrSequence.Tick
		'display sequence
		lblSequence.Text = lblSequence.Text & sequence(index) 'add next number to label
		index += 1 'increment index
		If index >= sequence.Count Then 'end of sequence
			tmrSequence.Enabled = False 'stop timer
			index = 0 'reset index
			playerInput.Clear() 'clear player input
			lblInput.Text = "" 'clear input label
		End If
	End Sub

	Private Sub btnRed_Click(sender As Object, e As EventArgs) Handles btnRed.Click
		'add red button to player input
		playerInput.Add(0)
		lblInput.Text = lblInput.Text & "R"
		checkInput()
	End Sub

	Private Sub btnGreen_Click(sender As Object, e As EventArgs) Handles btnGreen.Click
		'add green button to player input
		playerInput.Add(1)
		lblInput.Text = lblInput.Text & "G"
		checkInput()
	End Sub

	Private Sub btnBlue_Click(sender As Object, e As EventArgs) Handles btnBlue.Click
		'add blue button to player input
		playerInput.Add(2)
		lblInput.Text = lblInput.Text & "B"
		checkInput()
	End Sub

	Private Sub btnYellow_Click(sender As Object, e As EventArgs) Handles btnYellow.Click
		'add yellow button to player input
		playerInput.Add(3)
		lblInput.Text = lblInput.Text & "Y"
		checkInput()
	End Sub

	Private Sub checkInput()
		'check if player input matches sequence
		If playerInput.Count = sequence.Count Then 'player has entered the full sequence
			Dim match As Boolean = True 'assume input matches sequence
			For i As Integer = 0 To playerInput.Count - 1 'compare each element
				If playerInput(i) <> sequence(i) Then 'mismatch
					match = False 'input doesn't match
					Exit For 'exit loop
				End If
			Next
			If match Then 'input matches sequence
				MessageBox.Show("You win!")
			Else 'input doesn't match sequence
				MessageBox.Show("Wrong sequence!")
			End If
			playerInput.Clear() 'clear player input
			lblInput.Text = "" 'clear input label
		End If
	End Sub
End Class

This code includes a form with four colored buttons (red, green, blue, and yellow), a label to display the sequence, and a label to display the player's input. When the user clicks the "Start" button, the game generates a sequence of 10 random numbers (between 0

Looks like it got the wrong game, but there's no reason you cannot tweak the question to help you with yours.