Hi, i am doin some coursework and i need to know how to use arrays properly and how to insert values into it correctly. i think i need to use the for... loop.. or something like that. i have a strict deadline so plz reply asap!
i am making a snakes and ladders game and the user needs to insert the position and size of 10 snakes and 10 ladders. the board will be an array, and i have created the array with 100 spaces.
i am getting the input from the user 1 by 1, so the size of the snake is stored, and then its position, i then need to somehow put these values onto the board. the code i have so far is as following:
For d = 1 To 100 'for all values in the array
board(d) = 0 'turn them into 0
Next d 'complete initialisation of array
no_of_snakes = 10 'the number of snakes = 10
no_of_ladders = 10 'the number of ladders = 10
snakecount = 0 'set snake counter to 0
Do Until snakecount = no_of_snakes
Do
size_of_snake = InputBox("Enter size of snake between 10 and 30 for snake number " & snakecount + 1)
If size_of_snake < 10 Or size_of_snake > 30 Then
MsgBox ("The size of the snake has to be between 10 and 30, please retry")
End If
Loop Until size_of_snake > 9 And size_of_snake < 31
Do
position_of_snake = InputBox("Enter start position of snake for snake number " & snakecount + 1)
If position_of_snake < 11 Or position_of_snake > 99 Then
MsgBox ("Invalid position, try again")
End If
Loop Until position_of_snake > 10 And position_of_snake < 100
How is this board supposed to work?
I see a one-dimensional array, shouldn't that be two-dimensional?
Why didn't you declare the Board?
VB Code:
Private Board(100) As Long 'is it a long?
Private 2D_Board(100,100) As Long 'this one is twodimensional
What you are doing in your orange statement is store the size of the current snake (minus 1) in the element of Board where the index is the snake's position.
What do you expect/want to happen?
If I have a snake with a length of 10 and a position of 20, should the elements Board(10) to Board(30) be filled with the snake's number?
In that case you sould make a For Next loop.
VB Code:
For d = position_of_snake To position_of_snake + size_of_snake
If d > 100 Then Exit For
Board(d) = snakecount
Next d
But what if the next snake starts at 20 with a length of 5?
It would overwrite some of the elements filled by the previous snake and cut it in half.
what i am tryin to achieve is to get the snakes onto the board in the right places. i dont mind if they overwrite existing snakes for the time being, i should be able to hopefully adjust that later on. i have declared all my variables and done the 1 dimensional array... dim board(100) as integer... but i did not include everything in my post as fear of people stealing it because it is my coursework!
i want only the snakes head which is going to be the snakes position to make the player go down the size of the snake... if that makes sense?! in otherwords, if they choose a position of 20, and a size of 10, then array(20) should hold the value (-10). however the player is going to put in a positive integer, so this needs to be turned into a negative integer.
additionally i was wondering if u could tell me how to somehow output a file so i can check to make sure that the info is actualy being correctly written in the array?
thanx
Last edited by mrsingh; Apr 28th, 2005 at 02:51 PM.
the 1d array is because the task says use a 1d array lol... and erm im just coding. its too long to create different variables for each snake!
if ne1 can help me on the last few questions gr8! if not i ave some more questions... does ne1 know how to count the number of times i click the command button, and write it in a label.. because i need to somehow count the number of throws taken by a player. at the moment it is being designed for 1 player...
For counting clicks on a button, declare a variable (Long) and increase it by 1 when the button is clicked.
VB Code:
Dim Counter As Long
Private Sub Btn_Click()
Counter = Counter + 1
End Sub
its too long to create different variables for each snake!
What do you mean by that?
How do you intend to keep track of the snake's status later on if you don't store any of the snake's data?
How will you make the snakes do things if you don't know anything about them?
With your current code it will be near impossible to find out where each snake is.
Retrieving the data you threw away will be much longer than declaring a few variables to keep the data.
What exactly is your task?
What is the idea behind the game?
What are the snakes supposed to do?
Why does the task say you need to use a 1D array? Wil you get punished if you deviate a bit?
I would advise to plan/design things before you start writing code.
It will help you code with clear goals and structure and will save you time otherwise spent on changing code.
Last edited by jeroen79; Apr 28th, 2005 at 04:23 PM.
i am going to write the values to the array as i get them. the game is like any other normal snakes and ladders game, only thing is that the user gets to choose where to put the snakes and the ladders. if i use a 2d array i will not get ne marks.
i think thats enuff for 2day, thanx for ur help, ill back to u and ne1 else whos willing to help 2moz with some more questions!
in making the game, i have now drawn out the board using labels and done a dice counter to count the number of throws.. i now need to be able to get the values being input for the snakes sizes and positions onto the board from the array. ne ideas? i then need to be able to make some kind of counter, or perhaps change the colour of the labels to indicate where a player is, i also need to be able to move the player the amount the dice rolls. wow i have a lot of work to do! can ne1 help me?!?!