Kodewerx

Our culture has advanced beyond all that you could possibly comprehend with one hundred percent of your brain.
It is currently Thu Sep 04, 2025 4:28 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Jan 12, 2007 6:43 am 
Offline
Kommunist
Kommunist

Joined: Mon Oct 02, 2006 9:01 am
Posts: 106
I'm working on a hangman game and have it fully working 100% with a working 2 Player Mode, I am now trying to add a hint button that will hint towards to the word OR give the player a free letter. Oh and I'm using Visual Basic 6.

Now the first hint method, hinting towards the word, I'm thinking of using a dual-array. I've never used one before, but I think I know how they work.

First off, do I dim the array as a string or double? Because I'm not sure but I think you dim dual-arrays as double.

Dim whateverarray as double
Dim whateverarray as string

Now how do I actually put data into the second column of the array?

I can put data into the first, and maybe I'm missing something really simple, but how do I insert it into the second, I'm trying to pair the word and the hint in the array.

Second method is giving the player a letter.

Now I am having a hard time figuring out how I can get the program to randomly choose a letter that is correct and hasn't been chosen yet. Some hints, clues, pseudocode or code would be appreciated. If you guys want to see the code here it is:

Quote:

REM Edgar Baez, Hangman, 1/12/07

Option Explicit
Dim stralpha As String
Const conLeftC As Integer = 270
Const conNumw As Integer = 50
Const conHung As Integer = 5
Dim strwordbank() As String
Dim intchoice As Integer
Dim intlength As Integer
Dim intnumpicked As Integer
Dim blnGameOver As Boolean
Dim intNextStep As Integer
Dim blnNomatch As Boolean
Dim strletter As String
Dim multi As Boolean
Dim playerword As String



Private Sub cmd2P_Click()
MsgBox "Two player mode is now on, select New Word to Start.", vbOKOnly, "Multiplayer!"
multi = True
End Sub

Private Sub PlayerInput()
playerword = InputBox("Player One Will Enter Word ... in all lowercase letters, Player Two Will Guess, No Peeking!", "2 Player Mode!")
multi = True
End Sub

Private Sub cmdExit_Click()
End
End Sub

Private Sub cmdNewWord_Click()
Dim i As Integer
If multi = True Then
Call PlayerInput
End If
Call NewGame
Call setupwordbank
Call ChooseWord
For i = 0 To 25
lblAlphabet(i).Visible = True
Next i
End Sub


Private Sub NewGame()
Dim i As Integer
For i = 0 To 25
lblAlphabet(i).Caption = Mid(stralpha, i + 1, 1)
lblAlphabet(i).Visible = True
lblAlphabet(i).Enabled = True
Next i
blnGameOver = False
intNextStep = 0
intnumpicked = 0
lblmessage.Caption = "click a letter to select"
imgscene.Picture = imgstorage.Picture
End Sub

Private Sub ChooseWord()
Dim strCap As String
Dim i As Integer
Call setupwordbank
Randomize
intchoice = Int(conNumw * Rnd)
If multi = False Then
intlength = Len(strwordbank(intchoice))
Else
intlength = Len(playerword)
End If
strCap = ""
For i = 1 To intlength
strCap = strCap & "-"
Next i
lblHiddenPlace.Caption = strCap
End Sub


Private Sub Form_Load()
Dim i As Integer
stralpha = "abcdefghijklmnopqrstuvwxyz"
For i = 1 To 25
Load lblAlphabet(i)
lblAlphabet(i).Left = lblAlphabet(i).Left + i * conLeftC
lblAlphabet(i).Caption = Mid(stralpha, i + 1, 1)
lblAlphabet(i).Visible = True
Next i

End Sub

Private Sub setupwordbank()
ReDim strwordbank(conNumw)
strwordbank(0) = "movie"
strwordbank(1) = "quixotic"
strwordbank(2) = "muon"
strwordbank(3) = "sillygoose"
strwordbank(4) = "lamprey"
strwordbank(5) = "popcorn"
strwordbank(6) = "computer"
strwordbank(7) = "thunder"
strwordbank(8) = "corporation"
strwordbank(9) = "combustable"
strwordbank(10) = "medication"
strwordbank(11) = "soothing"
strwordbank(12) = "vocalist"
strwordbank(13) = "negative"
strwordbank(14) = "gesticulate"
strwordbank(15) = "neutrality"
strwordbank(16) = "resistance"
strwordbank(17) = "sensitive"
strwordbank(18) = "discourse"
strwordbank(19) = "reprehensible"
strwordbank(20) = "potent"
strwordbank(21) = "astute"
strwordbank(22) = "ideal"
strwordbank(23) = "aesthetic"
strwordbank(24) = "scrub"
strwordbank(25) = "impeach"
End Sub


Private Sub Form_Click()
If blnGameOver Then
MsgBox "Game Over"
Else
MsgBox "You have clicked a blank. Pick a new letter."
End If
End Sub



Private Sub lblAlphabet_Click(Index As Integer)
Dim i As Integer
If blnGameOver = True Then
MsgBox "The game is already over, please select new game to select a new word."
Else
blnNomatch = True
lblAlphabet(Index).Visible = False
If multi = False Then
For i = 1 To intlength
strletter = Mid(strwordbank(intchoice), i, 1)
If strletter = lblAlphabet(Index).Caption Then
intnumpicked = intnumpicked + 1
blnNomatch = False
lblHiddenPlace.Caption = Left(lblHiddenPlace.Caption, i - 1) & strletter & Right(lblHiddenPlace.Caption, intlength - i)
End If
Next i
Else
For i = 1 To intlength
strletter = Mid(playerword, i, 1)
If strletter = lblAlphabet(Index).Caption Then
intnumpicked = intnumpicked + 1
blnNomatch = False
lblHiddenPlace.Caption = Left(lblHiddenPlace.Caption, i - 1) & strletter & Right(lblHiddenPlace.Caption, intlength - i)
End If
Next i


If intnumpicked = intlength Then
lblmessage.Caption = "YOU WIN!"
blnGameOver = True
Else
If blnNomatch = True Then
imgscene.Picture = imghangman(intNextStep).Picture
intNextStep = intNextStep + 1
If intNextStep = conHung Then
lblmessage.Caption = "YOU LOSE!"
blnGameOver = True
lblHiddenPlace.Caption = playerword
End If
End If
End If
End If
End If
End Sub

The only change is that a cut down the wordbank for the size of saving space and no real need for the other words.

Again, any help is appreciated.


I know the code seems hard to follow, because the single player and multiplayer was implemented a bit sloppily, sorry. Also the little strcap section is just to put dashes in the label for each word.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jan 12, 2007 4:07 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
I believe you can do 2D arrays in VB like this:
Code:
Dim strwordbank()() As String


Then in your setup subroutine, you would add the word, followed by the hint:
Code:
Private Sub setupwordbank()
ReDim strwordbank(conNumw)(2)
strwordbank(0)(0) = "movie"
strwordbank(0)(1) = "This is a hint for movie..."
strwordbank(1)(0) = "quixotic"
strwordbank(1)(1) = "This is a hint for quixotic..."
strwordbank(2)(0) = "muon"
strwordbank(2)(1) = "..."
strwordbank(3)(0) = "sillygoose"
strwordbank(4)(0) = "lamprey"
strwordbank(5)(0) = "popcorn"
strwordbank(6)(0) = "computer"
strwordbank(7)(0) = "thunder"
strwordbank(8)(0) = "corporation"
strwordbank(9)(0) = "combustible"
strwordbank(10)(0) = "medication"
strwordbank(11)(0) = "soothing"
strwordbank(12)(0) = "vocalist"
strwordbank(13)(0) = "negative"
strwordbank(14)(0) = "gesticulate"
strwordbank(15)(0) = "neutrality"
strwordbank(16)(0) = "resistance"
strwordbank(17)(0) = "sensitive"
strwordbank(18)(0) = "discourse"
strwordbank(19)(0) = "reprehensible"
strwordbank(20)(0) = "potent"
strwordbank(21)(0) = "astute"
strwordbank(22)(0) = "ideal"
strwordbank(23)(0) = "aesthetic"
strwordbank(24)(0) = "scrub"
strwordbank(25)(0) = "impeach"
End Sub

Note that I also fixed a misspelling: combustible. ;)


For the second hint method, it seems you will have to change how you calculate/apply the chosen letters. Rather than solely keeping the 'hidden label' as the only reference to which letters have already been chosen, you should create an array of booleans with a minimum length of the word. Let each array index 0 to (wordlen - 1) indicate a chosen letter. Fill the array entirely with FALSE when starting a new game. Place TRUE into the index when a letter is chosen. When you wish to randomly choose a letter for a hint, you can do an endless loop until index (Rand() % wordlength) returns false. You then have a random index of an unchosen letter that you can give to the player.

_________________
I have to return some video tapes.

Feed me a stray cat.


Last edited by Parasyte on Fri Jan 12, 2007 4:26 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 12, 2007 4:23 pm 
Offline
Kommunist
Kommunist

Joined: Tue Oct 10, 2006 9:32 am
Posts: 445
Blazingwolf wrote:
Now I am having a hard time figuring out how I can get the program to randomly choose a letter that is correct and hasn't been chosen yet. Some hints, clues, pseudocode or code would be appreciated.


Not sure what you really expect, but here is how I would do it (and sorry, but I didn't read your code - just throwing out an idea here ;) ).

First you must store the hidden/uncovered letter status of the word somewhere (I'd choose a 32 bits (or 64) value for exemple, each bit marking a letter status : 0 = hidden, 1 = uncovered).

Now let's call N the number of hidden letters of the word.

Of course, each time the player 'uncovers' a new letter, lessen N by 1 (or 2, or 3..., depending on how much time this letter appears in the word), and 'mark' the bits of the hidden/uncovered letters status variable.

Now, when you need to 'randomly choose a letter that is correct and hasn't been chosen yet', pickup a random number between 1 and N.
Then use this number to search for the nth hidden letter in the word (by searching for the nth 0 in the hidden/uncovered letter status variable, then 'selecting ' this letter (for exemple you picked up an 'e') and uncovering all of its apparance in the word, and finally lessen N by the number of letters you uncovered.


Edit : my method is 'bad', because if you have for exemple 3 'e' and one 'r' still uncovered, the 'e' will get higher changes to get uncovered, and I guess both letter should have the same chances. I'll add another way to do it.


Ok here is it :
Use a 32bits value which bits will store all the letters of the alphabet.
Then flag the bits of the 'not in the word' letters to 1, and calculate how much different letters the word have (= N).
When a letter is uncovered, set its bit to 1 and decrement N.
Now, when you need to pickup a random letter to uncover, get a random number R from 1 to N, search for the Rth 0 you find in the 32bits value, set it to 1, uncover the letter you choosed, and decrement N.
That way each letter will have the same chances of appearing, making the game 'harder' (because with the other method, making the more appearing letters have more chance to be uncovered might just make things too easy).


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jan 12, 2007 4:37 pm 
I didn't read past "I'm using Visual Basic 6"


Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 65 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group