Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Blah, blah, blah... (http://www.abandonia.com/vbullet/forumdisplay.php?f=14)
-   -   Damn Visual Basic (http://www.abandonia.com/vbullet/showthread.php?t=1521)

Kon-Tiki 01-12-2004 08:07 PM

Why isn't it working properly?
If you toss a disc in at the second row, it'll stay in the loop after the if-statement, after all that has to be done, is done, except when you toss the disc in the first column. Then it works fine.

I tried putting an else-statement there, to turn the loop off if the else-statement was called, and it works fine then, but the four on a row diagonally isn't detected then.

Rogue 01-12-2004 09:50 PM

Can you add your documentation.

This should be simple program, if you are not building AI.

I'll check the code after dinner.

Kon-Tiki 01-12-2004 10:01 PM

My documentation? As in what which calculation does?
Code:

Public Sub NESWDiagonaal(intLastSpot, bytChecking, intPosition)
 intLastSpot = intPosition
Dim intKolom As Integer
Dim intRij As Integer
Dim intMultiply As Integer
Dim intMultiplyFinal As Integer

' * Geen doorvallen langs beneden
intLastSpot = intLastSpot - 24
Do Until intLastSpot >= 0
 *intLastSpot = intLastSpot + 8
Loop

 Select Case intPosition
 *Case 14
 * *intLastSpot = 14
 *Case 21
 * *intLastSpot = 21
 *Case 28
 * *intLastSpot = 28
 *Case 35
 * *intLastSpot = 35
End Select

' * Aantal keren dat ge naar rechtsboven kunt

intKolom = 6 - intLastSpotKolom
MsgBox "intKolom = " & intKolom & "."

If intKolom < 3 Then
 *intMultiply = intKolom
Else
 *intMultiply = 3
End If

intMultiplyFinal = intMultiply * 8
MsgBox "Final = " & intMultiplyFinal & "."
Do
Call WinCheck(intLastSpot, bytChecking)

 If intMultiply > 0 Then
 * intLastSpot = intLastSpot + 8
 * intMultiply = intMultiply - 1
 * MsgBox "intMultiply = " & intMultiply & "."
 End If
Loop Until intLastSpot = (intPosition + intMultiplyFinal)
End Sub

This part of the code's the one where the bug's in. The problem, is that the loop gets stuck. The + 8 is to jump to the square one NE of the last one, and it starts three squares SW of the one a disc is dropped in (hence the -24). If that turns out to be under the playfield, it counts up 8 until it's above 0 again (thus being in the playfield)

The intKolom and intMultiply are there to take care of the wrapping around. We don't want people to win when they have two discs diagonal from each other on the right edge and two on the left.

Rogue 01-12-2004 11:07 PM

I was thinking about documentation for the project, as planning and pseudo code.

Reason program loops is in this loop:

Code:

Do
Call WinCheck(intLastSpot, bytChecking)

If intMultiply > 0 Then
 *intLastSpot = intLastSpot + 8
 *intMultiply = intMultiply - 1
 *MsgBox "intMultiply = " & intMultiply & "."
End If
Loop Until intLastSpot = (intPosition + intMultiplyFinal)

You end up looping, but criteria (intPosition + intMultiplyFinal) is not chenging. (value of intLastSpot is 24, bytChecking is 0, intPosition 8 and intMultyFinal is 24. You have to add the code to change one of values, so that your code can exit loop) It would help me if you have documentation in which you show what your code suposed to do. (That's the way they teach here programming)

When do you need to turn in this program?

Kon-Tiki 01-12-2004 11:57 PM

You mean the assignment doc? It's in Dutch, but I'll translate.

Quote:


<snips out the rules of the game, as I guess you already know those>

2. Assignment
The assignment exists out of implementing the Four On A Row/Connect Four game in Visual Basic. In this section, you'll find a summing of what we expect of you concretely.

First you have to design the user interface. This needs to represent the playfield. Of course, the players have to be able to change something to the playfield. For this, the seen objects (buttons, labels, etc) will be used. An example of a layout can be found in image ?? <another example's the one in the zip-file I linked to earlier> You can also be a bit more creative and think up your own layout. The objects that have to be present, are derivable from the requirements we ask of your program.

The program has to agree to at least these criteria:

1) The playfield has to exist out of 7 columns and 6 rows. Each tile can for example exist out of a circular shape object

2) The game has to be able to be played by two human players. You don't have to apply a computerplayer.

3) The name and turn of the player whose turn it is has to be shown. You'll have to make it possible to give the names of the players before the game can start.

4) You need to make a method to drop a coin in a column (iow. coloring the tile). This can be realised by, for example, clicking a button above the column. The coin has to behave like written in the rules. The color of the coin is, of course, the color of the current player.

5) If a player has played a coin, the turn has to switch to the other player automatically. You can do this by showing the corresponding name or color of this player somewhere.

6) You also have to detect if a player has won. This has to be made known (for example with the aid of MsgBox). Also, if the board is full and no winner is appointed, you have to make this known. After one of these situations, a new game has to be started (like clearing the playfield). When these players don't want to play anymore, you have to apply the possibility to stop the game and enter new names.

7) For each of the two players, a score has to be kept. This exists out of the amount of times each player has won. The score is to be kept for a series of consecutive games that the same players play against each other. If a new game is started manually (with inputting the playernames), you can forget the old scores. You don't have to store anything in a file or the likes.

3) EXTRAS
<snips this out, as it just gives examples of extra stuff you can add, but which isn't necessary, and got a list of my own to add>

4) TIPS 'N TRICKS

1) Make sure your program is easy to understand. This is done by supplying enough comments in your code, along with choosing clear and meaningful names for your code-elements.

2) To come to a simple program, it is important to make the right choice of building the board. To come to a good choice here, here is a tip: Make use of a Control Array. You can concider this as a row with alot of objects of the same type and with the same name. A specific object inside this row can be called on* by putting the its number behind the rowname (vb. Buttons(5)). You make a Control Array like this:
* <snips>

The organisation of the fields can look like this:

5|* * 35* * * 36* * * 37* * * 38* * * 39* * * 40* * * 41
4|* * 28* * * 29* * * 30* * * 31* * * 32* * * 33* * * 34
3|* * 21* * * 22* * * 23* * * 24* * * 25* * * 26* * * 27
2|* * 14* * * 15* * * 16* * * 17* * * 18* * * 19* * * 20
1|* * 7* * * * 8* * * * 9* * * 10* * * 11* * * 12* * 13
0|* * 0* * * * 1* * * * 2* * * 3* * * * 4* * * * 5* * * 6
* ------------------------------------------------------
* * * * 0* * * * 1* * * * 2* * * * 3* * * 4* * * * 5* * * 6

The reacting to events of objects in a Control Array (e.g. a mouse-click) happens <yadayadayada, explanation of how to use Control Arrays>

3) To make the code more readable (which makes it easier to correct mistakes), you best do the following. Define 2 global variables: AMOUNT_ROWS and AMOUNT_COLUMS, which respectively represent the amount of rows and the amount of colums (so 6 and 7). Beware, the rows and columns start counting at 0. So the first row (column) is row (column) 0. With the aid of these formulas you can calculate the row and column if your Index is given:

r = Int(index / AMOUNT_COLUMNS)
k = Index mod AMOUNT_COLUMS

These formulas are best to be poured into separate modules, who'll take the index as an argument and returns the column - or rownumber as result. Better apply a methode which does the opposite too, nl. which returns the index of a given column- and rownumber.

You can easily check if a field is at the border of the playfield this way. For example to check if a field is at the right edge of the board, you do the following:

if k = AMOUNT_COLUMNS-1 Then
...
Else
...
EndIf

You can define yet other methodes who'll make the working with indexes easier the same way. Think especially of the controling of the diagonals.

5.
<Snips, as it's some practical stuff, like when to hand it in (friday before noon), and how, and where>


Rogue 02-12-2004 12:05 AM

So,
how do you like to make sure that player won?

Just write pseudo code of your program.

Ioncannon 02-12-2004 12:08 AM

I will be takin a VB course next semester YAY :D. What is this program suppose to do? All I ever done was simmple tiny piece of code for my wolf3d mod.

Kon-Tiki 02-12-2004 12:16 AM

Position = LastPosition
counter = 0

<check horizontally>
Position = Position - 3 or until it hits the left border
do
if Position.value = CurrentPlayer {
counter ++
}
else {
counter = 0
}
if counter = 4 {
win!
}
Position ++
loop until Position = Position + 3 or Position = last column

<check vertically>
Position = Position - 21 or - 7 until it's at the bottom row
do
if Position.value = CurrentPlayer {
counter ++
}
else {
counter = 0
}
if counter = 4 {
win!
}
Position += 7
loop until Position = Position + 21 or Position = top

<check diagonally / >
Position = Position - 24 or - 8 until it's either at the leftmost column or the bottom row
do
if Position.value = CurrentPlayer {
counter ++
}
else {
counter = 0
}
if counter = 4 {
win!
}
position += 8
loop until Position = Position + 24 or Position = rightmost column or top row

<check diagonally \ >
Position = Position - 18 or - 6 until it's either at the rightmost column or the bottom row
do
if Position.value = CurrentPlayer {
counter++
}
else {
counter = 0
}
if counter = 4 {
win!
}
position += 6
loop until Position = Position + 18 or Position = leftmost column or top row.

Rogue 02-12-2004 01:01 AM

This code does not make much sense to me.

Here is what you have to do:
(pseudo code)

1. Make a function which will return index value in the cell for given row / column

2. Make a function which will return row / column for a given index

3. Make a function which will for column check where the top is and place a coin there

4. Check anywhere you have 4 cells horizontally that mach

5. Check anywhere you have 4 cells vertically that mach

6. Check anywhere you have 4 cells up diagonal that mach

7. Check anywhere you have 4 cells down diagonal that mach

8. Check if all fields are used

9. Make a function that checks steps 4 - 7 and then give a winner, move to next player, or if all fields are used (draw).


Details:

STEP 3
Check top value for that column ( using function in step 2, eg col 1, row 5)
-- if is filled: don’t allow to place piece there (message)
-- if is empty, check next one (-8)
Loop this way till you don’t place a coin on the bottom.

STEP 4

Create loop for Column 0 – 3
-- Create loop for Rows 0 - 5
----Check if CP (Current position), CP + 1 , CP + 2 and CP + 3 are the same
------If yes, we have a winner


STEP 5

Create loop for Columns 0 – 7
-- Create loop for Rows 0 - 2
----Check if CP (Current position), CP + 7 , CP + 14 and CP + 21 are the same
------If yes, we have a winner

STEP 6

Create loop for Column 0 – 3
-- Create loop for Rows 0 - 2
----Check if CP (Current position), CP + 8 , CP + 16 and CP + 24 are the same
------If yes, we have a winner

STEP 7

Create loop for Column 3 – 6
-- Create loop for Rows 3 - 5
----Check if CP (Current position), CP - 8 , CP - 16 and CP - 24 are the same
------If yes, we have a winner

STEP 8

Run separate functions similar to step 3 where you’ll find top for each column. If you have 7 hits and you at this step, then it’s draw

STEP 9

Just to make it easy, functions which incorporates steps 4 – 8

If you follow this pseudo code, it should be a piece of cake to make this program. ;)

Kon-Tiki 02-12-2004 01:19 AM

That's basically what I've been trying to do. Although I don't understand why you're only checking to row 2, or to column 3, and in the diagonals, how it would know cases like this?

0 0 0 0 0 0 0
0 0 0 0 1 0 0
0 0 0 2 2 0 0
0 0 1 2 2 0 0
0 1 2 1 1 0 0
1 2 2 1 1 0 0

Rogue 02-12-2004 01:47 AM

Because you can't have 4 pieces in diagonal if column (horisontal number) is greater then 3 and row (vertical position) is greater then 2.

So these are your start position:

2 X X X X
1 X X X X
0 X X X X
= 0 1 2 3

These are only valid first position (I colled them current position in example before), and you can't get 4 coins in up-diagonal if your first position is greater then this.

Note in your example above that start position is 0 for rows, 1 for column.

Try to make a diagonale that has start position greater then loop value.

Kon-Tiki 02-12-2004 01:53 AM

Ehm... 's 4am now and I'm already pissed off because a week's worth of work's got to be thrown away and redone within a day, so my understanding's probably off. Please explain slow and as if you were trying to get an infant to understand :tomato:

Rogue 02-12-2004 02:07 AM

:D Somehow I really good know that feeling LOL

When you check your win position, it's much easier to make it in 4 steps (like I said before) with checking next three coins based on first one. That way you don't have to go trough all rows and columns, as if you for horizontal check can't make 4 coins in row, if your position in columns is beyond position 3.

In other words:

3 - 4 - 5 - 6 (if they are the same) = win

but you can't make win position if you start from column 4

4 - 5- 6 and there is NO column 7 (count starts from 0) so you don't have to bother to check this position, as it can make win position.

Better?

Kon-Tiki 02-12-2004 02:13 AM

I see. So more like this: (Googled for example codes)
Code:

Dim Row As Integer, Count As Integer
Row = 0 * * * * * * 'Count Could have been Used,but Row makes it easyer to Read

For Count = 0 To 6
 * *'Check All Horizontal Lines
 * *If Grid(Row, 6) = iPlGo And Grid(Row, 5) = iPlGo And Grid(Row, 4) = iPlGo And Grid(Row, 3) = iPlGo Then iWin = 1
 * *If Grid(Row, 5) = iPlGo And Grid(Row, 4) = iPlGo And Grid(Row, 3) = iPlGo And Grid(Row, 2) = iPlGo Then iWin = 1
 * *If Grid(Row, 4) = iPlGo And Grid(Row, 3) = iPlGo And Grid(Row, 2) = iPlGo And Grid(Row, 1) = iPlGo Then iWin = 1
 * *If Grid(Row, 3) = iPlGo And Grid(Row, 2) = iPlGo And Grid(Row, 1) = iPlGo And Grid(Row, 0) = iPlGo Then iWin = 1
 * *'Check All Vertical Lines
 * *If Grid(6, Row) = iPlGo And Grid(5, Row) = iPlGo And Grid(4, Row) = iPlGo And Grid(3, Row) = iPlGo Then iWin = 1
 * *If Grid(5, Row) = iPlGo And Grid(4, Row) = iPlGo And Grid(3, Row) = iPlGo And Grid(2, Row) = iPlGo Then iWin = 1
 * *If Grid(4, Row) = iPlGo And Grid(3, Row) = iPlGo And Grid(2, Row) = iPlGo And Grid(1, Row) = iPlGo Then iWin = 1
 * *If Grid(3, Row) = iPlGo And Grid(2, Row) = iPlGo And Grid(1, Row) = iPlGo And Grid(0, Row) = iPlGo Then iWin = 1
 * *Row = Row + 1
Next * *'Count
Dim Row As Integer, Count As Integer
Row = 0 * * * * * * 'Count Could have been Used,but Row makes it easyer to Read

For Count = 0 To 6
 * *'Check All Horizontal Lines
 * *If Grid(Row, 6) = iPlGo And Grid(Row, 5) = iPlGo And Grid(Row, 4) = iPlGo And Grid(Row, 3) = iPlGo Then iWin = 1
 * *If Grid(Row, 5) = iPlGo And Grid(Row, 4) = iPlGo And Grid(Row, 3) = iPlGo And Grid(Row, 2) = iPlGo Then iWin = 1
 * *If Grid(Row, 4) = iPlGo And Grid(Row, 3) = iPlGo And Grid(Row, 2) = iPlGo And Grid(Row, 1) = iPlGo Then iWin = 1
 * *If Grid(Row, 3) = iPlGo And Grid(Row, 2) = iPlGo And Grid(Row, 1) = iPlGo And Grid(Row, 0) = iPlGo Then iWin = 1
 * *'Check All Vertical Lines
 * *If Grid(6, Row) = iPlGo And Grid(5, Row) = iPlGo And Grid(4, Row) = iPlGo And Grid(3, Row) = iPlGo Then iWin = 1
 * *If Grid(5, Row) = iPlGo And Grid(4, Row) = iPlGo And Grid(3, Row) = iPlGo And Grid(2, Row) = iPlGo Then iWin = 1
 * *If Grid(4, Row) = iPlGo And Grid(3, Row) = iPlGo And Grid(2, Row) = iPlGo And Grid(1, Row) = iPlGo Then iWin = 1
 * *If Grid(3, Row) = iPlGo And Grid(2, Row) = iPlGo And Grid(1, Row) = iPlGo And Grid(0, Row) = iPlGo Then iWin = 1
 * *Row = Row + 1
Next * *'Count


Rogue 02-12-2004 02:28 AM

Very similar, but I would suggest use of two loops. This way you can have much cleaner code.

Code:

For column 1 to 3
 *For row 0 to 2
 * *If Grid( rowe, column) = 1 and Grid( rowe + 1, column + 1) = 1 and Grid( rowe + 2, column + 2) = 1 and Grid( rowe + 3, column + 3) = 1Then
 * *'Win'
 * *End IF
 *Next row
Next column

Note: this is code for UP diagonal line ( / - this way)
Note 2: I used = 1 as sample for player 1, you can have three option in each array position, 0 - no coins, 1 - player 1 and 2 - player 2. Just change 1 with variable which represents which player's turn it is.

Kon-Tiki 02-12-2004 02:37 AM

Hmmm... the code in this other thingy I got, is
Code:

'Check Diagonal Lines [Up to Right]
If Grid(0, 3) = iPlGo And Grid(1, 4) = iPlGo And Grid(2, 5) = iPlGo And Grid(3, 6) = iPlGo Then iWin = 1
If Grid(2, 0) = iPlGo And Grid(3, 1) = iPlGo And Grid(4, 2) = iPlGo And Grid(5, 3) = iPlGo Then iWin = 1
If Grid(3, 1) = iPlGo And Grid(4, 2) = iPlGo And Grid(5, 3) = iPlGo And Grid(6, 4) = iPlGo Then iWin = 1
If Grid(1, 0) = iPlGo And Grid(2, 1) = iPlGo And Grid(3, 2) = iPlGo And Grid(4, 3) = iPlGo Then iWin = 1
If Grid(2, 1) = iPlGo And Grid(3, 2) = iPlGo And Grid(4, 3) = iPlGo And Grid(5, 4) = iPlGo Then iWin = 1
If Grid(3, 2) = iPlGo And Grid(4, 3) = iPlGo And Grid(5, 4) = iPlGo And Grid(6, 5) = iPlGo Then iWin = 1
If Grid(0, 0) = iPlGo And Grid(1, 1) = iPlGo And Grid(2, 2) = iPlGo And Grid(3, 3) = iPlGo Then iWin = 1
If Grid(1, 1) = iPlGo And Grid(2, 2) = iPlGo And Grid(3, 3) = iPlGo And Grid(4, 4) = iPlGo Then iWin = 1
If Grid(2, 2) = iPlGo And Grid(3, 3) = iPlGo And Grid(4, 4) = iPlGo And Grid(5, 5) = iPlGo Then iWin = 1
If Grid(3, 3) = iPlGo And Grid(4, 4) = iPlGo And Grid(5, 5) = iPlGo And Grid(6, 6) = iPlGo Then iWin = 1
If Grid(0, 1) = iPlGo And Grid(1, 2) = iPlGo And Grid(2, 3) = iPlGo And Grid(3, 4) = iPlGo Then iWin = 1
If Grid(1, 2) = iPlGo And Grid(2, 3) = iPlGo And Grid(3, 4) = iPlGo And Grid(4, 5) = iPlGo Then iWin = 1
If Grid(2, 3) = iPlGo And Grid(3, 4) = iPlGo And Grid(4, 5) = iPlGo And Grid(5, 6) = iPlGo Then iWin = 1
If Grid(0, 2) = iPlGo And Grid(1, 3) = iPlGo And Grid(2, 4) = iPlGo And Grid(3, 5) = iPlGo Then iWin = 1
If Grid(1, 3) = iPlGo And Grid(2, 4) = iPlGo And Grid(3, 5) = iPlGo And Grid(5, 3) = iPlGo Then iWin = 1
If Grid(3, 0) = iPlGo And Grid(4, 1) = iPlGo And Grid(5, 2) = iPlGo And Grid(6, 3) = iPlGo Then iWin = 1

'Check Diagonal Lines [Up to Left]
If Grid(0, 3) = iPlGo And Grid(1, 2) = iPlGo And Grid(2, 1) = iPlGo And Grid(3, 0) = iPlGo Then iWin = 1
If Grid(0, 4) = iPlGo And Grid(1, 3) = iPlGo And Grid(2, 2) = iPlGo And Grid(3, 1) = iPlGo Then iWin = 1
If Grid(1, 3) = iPlGo And Grid(2, 2) = iPlGo And Grid(3, 1) = iPlGo And Grid(4, 0) = iPlGo Then iWin = 1
If Grid(0, 5) = iPlGo And Grid(1, 4) = iPlGo And Grid(2, 3) = iPlGo And Grid(3, 2) = iPlGo Then iWin = 1
If Grid(1, 4) = iPlGo And Grid(2, 3) = iPlGo And Grid(3, 2) = iPlGo And Grid(2, 1) = iPlGo Then iWin = 1
If Grid(2, 3) = iPlGo And Grid(3, 2) = iPlGo And Grid(4, 1) = iPlGo And Grid(5, 0) = iPlGo Then iWin = 1
If Grid(0, 6) = iPlGo And Grid(1, 5) = iPlGo And Grid(2, 4) = iPlGo And Grid(3, 3) = iPlGo Then iWin = 1
If Grid(1, 5) = iPlGo And Grid(2, 4) = iPlGo And Grid(3, 3) = iPlGo And Grid(4, 2) = iPlGo Then iWin = 1
If Grid(2, 4) = iPlGo And Grid(3, 3) = iPlGo And Grid(4, 2) = iPlGo And Grid(5, 1) = iPlGo Then iWin = 1
If Grid(3, 3) = iPlGo And Grid(4, 2) = iPlGo And Grid(5, 1) = iPlGo And Grid(6, 0) = iPlGo Then iWin = 1
If Grid(1, 6) = iPlGo And Grid(2, 5) = iPlGo And Grid(3, 4) = iPlGo And Grid(4, 3) = iPlGo Then iWin = 1
If Grid(2, 5) = iPlGo And Grid(3, 4) = iPlGo And Grid(4, 3) = iPlGo And Grid(5, 2) = iPlGo Then iWin = 1
If Grid(3, 4) = iPlGo And Grid(4, 3) = iPlGo And Grid(5, 2) = iPlGo And Grid(6, 1) = iPlGo Then iWin = 1
If Grid(2, 6) = iPlGo And Grid(3, 5) = iPlGo And Grid(4, 4) = iPlGo And Grid(5, 3) = iPlGo Then iWin = 1
If Grid(3, 5) = iPlGo And Grid(4, 4) = iPlGo And Grid(5, 3) = iPlGo And Grid(6, 1) = iPlGo Then iWin = 1
If Grid(4, 6) = iPlGo And Grid(5, 5) = iPlGo And Grid(6, 4) = iPlGo And Grid(5, 3) = iPlGo Then iWin = 1

If that loop works... muuuuuuch better then.

Rogue 02-12-2004 02:41 AM

Loop should work much better then this code, as it's simple and it's easier to mentain.

Just write here other 3 loops (pseudo code same way I did first one) and that's most of your project.

Visual implementation is ok, then still you'll have to make sure that STEP #1 - 3 are working right, and your project is almost done.

Kon-Tiki 02-12-2004 02:45 AM

Ok, for the loops... what does the program need to know per direction? The row and the column and the index number? Or the row and the column? And which ones ought to be variables and which constants? (just making sure I won't crap things up at the input-part, then waste my time trying to fix output, as I don't have time for such nonsense)

Rogue 02-12-2004 02:55 AM

You need:
* an array of 42 elements ( 0 - 41)
* rows (0-5) variable
* columns (0-6) variable
* playerID (use this variable to change value in array if coins is dropped, and to check win situation)

Reuse your player name variable, as that's ok.


Just to explain couple of other functions again:

New button should loop trough array and change all the values to 0.

Drop of coin should check what is lowest position in that column and change value for that cell ( function that returns array value for the given row/column) to player value

Redraw function will go trough array and place player coins in the cells on the visual interface. Do this after player placed a coin!

Two variables (optional) if you like to keep a score for players, and function to null both values if players like to start from the beginning.


Just to make sure you got logic for win situation, make a code simillar to mine and post it here. It's better now to make sure there is no mistakes, then later.

Kon-Tiki 02-12-2004 03:12 AM

Trying to make such a code, and it's splitting my brain. I'm trying to understand how ...
*looks at his grid with the numbers in*
*counts*
Arglgl :wall:
...
Ok
Man, that was almost like pointers in C++ (which I understand, but don't see a use for)

Anyways, I see why only column 0-3 now. If the coin thrown in is in column 4, 5 or 6, the loop starting from column 3 will cover those too. Not so much checking around the coin that's tossed in, more as checking if there's a four on a row in the entire row.

Kon-Tiki 02-12-2004 03:22 AM

Done.

Code:

For Column = 0 To 3
 *For row = 0 To 5
 * * position = row * 7 + Column
 * * If Veld(position).Tag = player And Veld(position + 1).Tag = player And Veld(position + 2).Tag = player And Veld(position + 3).Tag = player Then
 * * * * *MsgBox "Winner!"
 * * * * *Call ClearField
 * * End If
 *Next row
Next Column


Rogue 02-12-2004 03:33 AM

Oh, I see now.

Use this line to create above function

Code:

position = row * 7 + Column
Then in the code, just call function and check if returned value is equal to playerID

Kon-Tiki 02-12-2004 03:40 AM

Hmm... might be a good idea. Never thought of making a one-line function, but it's used too many times (even in the clicking of the button on top) to keep readability. Just a slight readability improvement, but it is one.

Rogue 02-12-2004 03:46 AM

It will help you in diagonals, and it will give you back value for the box right where you need it. (It's not 1 line, as you have to return value for that position)

Kon-Tiki 02-12-2004 03:51 AM

Maybe a dumb question, but what's the syntax to return values? I've been working around it by making global vars >_>

Rogue 02-12-2004 03:58 AM

add 'as type' in declaration of function

Example:

Code:

Function GRID( row, column) as Integer
 *position = row * 7 + Column
 *GRID = array( position )
Function end

Now, when you call GRID(1,2) it will return value in that cell.

Kon-Tiki 02-12-2004 04:10 AM

Code:

Dim Row As Integer, Column As Integer, pos As Integer
Private Sub Klik_Click(Index As Integer)
Column = Klik(Index).Index
Row = 5

Do While Veld(pos).Tag = 0 Or Row > 0
 *pos = Position(Column, Row)
 *Row = Row - 1
 *MsgBox "Row = " & Row & "."
Loop
End Sub



' *****************
' ** Horizontaal **
' *****************
Sub Direction(Position, player)
Dim Column As Integer, Row As Integer
For Column = 0 To 3
 *For Row = 0 To 5
 * * Call Position(Column, Row)
 * * If Veld(Position).Tag = player And Veld(Position + 1).Tag = player And Veld(Position + 2).Tag = player And Veld(Position + 3).Tag = player Then
 * * * * *MsgBox "Winner!"
 * * * * *Call ClearField
 * * End If
 *Next Row
Next Column
End Sub

Function Position(Column, Row) As Integer
 *Position = Row * 7 + Column
 *pos = Position
End Function

This one goes to row -2, then gives an error (invalid control array index)


Edit: Nevermind. 6:30am here, so could expect such things. Changed the or to and in the loop and it works now.


Edit Edit: Seems like it always ends on row 0 >_>

Rogue 02-12-2004 04:22 AM

I see, in VB you can't use functions same as in C++

Sory about that, you can change that one line for position back to main function.

Just copy/paste this stuff, change criteria and you're done. :)

Rogue 02-12-2004 04:24 AM

Code:

Dim Row As Integer, Column As Integer, pos As Integer
Private Sub Klik_Click(Index As Integer)
Column = Klik(Index).Index
Row = 5

Do While Veld(pos).Tag = 0 Or Row > 0
 pos = Position(Column, Row)
 Row = Row - 1
 MsgBox "Row = " & Row & "."
Loop
End Sub



' *****************
' ** Horizontaal **
' *****************
Sub Direction(Position, player)
Dim Column As Integer, Row As Integer
For Column = 0 To 3
 For Row = 0 To 5
 * *Position = Row * 7 + Column
 * *If Veld(Position).Tag = player And Veld(Position + 1).Tag = player And Veld(Position + 2).Tag = player And Veld(Position + 3).Tag = player Then
 * * * * MsgBox "Winner!"
 * * * * Call ClearField
 * *End If
 Next Row
Next Column
End Sub


Rogue 02-12-2004 04:28 AM

Why you have this line criteria in the sub line?

Code:

Sub Direction(Position, player)
Also change this code to add player id to array

Code:

Private Sub Klik_Click(Index As Integer)
Column = Klik(Index).Index
Row = 5

Do While Veld(pos).Tag = 0 Or Row > 0
pos = Position(Column, Row)
Row = Row - 1
MsgBox "Row = " & Row & "."
Loop
End Sub

EDIT: Add player id to Klik sub, so that you know what player's coin you need to add to array

Kon-Tiki 02-12-2004 04:35 AM

I can take Position out, and Player is now PlayerTurn, which shouldn't be passed through, as it's a global var now too. Code now:
Code:

Dim Row As Integer, Column As Integer, position As Integer, PlayerTurn As Integer

Private Sub Form_Load()
PlayerTurn = 1
End Sub

Private Sub Klik_Click(Index As Integer)
Column = Klik(Index).Index
Row = 5

' ****************
' ** Doorvallen **
' ****************
Do While Veld(position).Tag = 0 And Row >= 0
 *position = Row * 7 + Column
 *MsgBox "Position = " & position & "."
 *Row = Row - 1
 *MsgBox "Row = " & Row & "."
Loop

' ********************
' ** Vakske kleuren **
' ********************

Veld(position).Tag = PlayerTurn
Veld(position).Picture = LoadPicture("veld1.jpg")
If PlayerTurn = 1 Then
 *PlayerTurn = 2
Else
 *PlayerTurn = 1
End If

End Sub



' *****************
' ** Horizontaal **
' *****************
Sub Direction()
Dim Column As Integer, Row As Integer
For Column = 0 To 3
 *For Row = 0 To 5
 * * position = Row * 7 + Column
 * * If Veld(position).Tag = player And Veld(position + 1).Tag = player And Veld(position + 2).Tag = player And Veld(position + 3).Tag = player Then
 * * * * *MsgBox "Winner!"
 * * * * *Call ClearField
 * * End If
 *Next Row
Next Column
End Sub

Problem with it: it drops one coin, then none... ever.

Kon-Tiki 02-12-2004 04:45 AM

Seems like I need to empty Position afterwards...

Rogue 02-12-2004 04:45 AM

Code:

If PlayerTurn = 1 Then
 PlayerTurn = 2
Else
 PlayerTurn = 1
End If

This line comes after you test all win position, then check if this is not draw position. Because these variable is now global, it will check if wrong player has won.

Kon-Tiki 02-12-2004 04:50 AM

I'm taking that out of there and put it in where it belongs once the dropping-through works fine. Right now, I can place coins, but they always drop to the bottom row. To fix the emptying of Position, I put

Position = Row * 7 + Column

above the loop too.

Rogue 02-12-2004 04:57 AM

That will fix begining of loop.

Post your code here when you're done, and I'll check it later.

I got whole 3 hours to sleep, and then I got some of my own stuff to finish. :)

Good night! :bye:

Kon-Tiki 02-12-2004 05:03 AM

I ought to go to bed too. Guess it's better to code with a clear head :whistle:
Entire code for now:
Code:

Dim Row As Integer, Column As Integer, Position As Integer, PlayerTurn As Integer

Private Sub Form_Load()
 PlayerTurn = 1
End Sub

Private Sub Klik_Click(Index As Integer)
Column = Klik(Index).Index
Row = 5

Position = Row * 7 + Column

' ****************
' ** Doorvallen **
' ****************
Do While Veld(Position).Tag = 0 And Row >= 0
 *Position = Row * 7 + Column
 *MsgBox "Position = " & Position & "."
 *Row = Row - 1
 *MsgBox "Row = " & Row & "."
Loop

' ********************
' ** Vakske kleuren **
' ********************
Veld(Position).Tag = PlayerTurn
 If PlayerTurn = 1 Then
 * Veld(Position).Picture = LoadPicture("veld1.jpg")
 * PlayerTurn = 2
 Else
 * Veld(Position).Picture = LoadPicture("veld2.jpg")
 * PlayerTurn = 1
 End If

End Sub



' *****************
' ** Horizontaal **
' *****************
Sub Direction()
Dim Column As Integer, Row As Integer
For Column = 0 To 3
 *For Row = 0 To 5
 * * Position = Row * 7 + Column
 * * If Veld(Position).Tag = player And Veld(Position + 1).Tag = player And Veld(Position + 2).Tag = player And Veld(Position + 3).Tag = player Then
 * * * * *MsgBox "Winner!"
 * * * * *Call ClearField
 * * End If
 *Next Row
Next Column
End Sub

Private Sub Exit_Click()
End
End Sub

Now off to bed :w00t:

Rogue 02-12-2004 09:28 AM

Well I am back. :)

There is one logical error in current code.

You need IF statement to make sure that first

Code:

Position = Row * 7 + Column
is not already taken, otherwise you might replace top row coin.

Kon-Tiki 02-12-2004 02:14 PM

Code:

Do Until Row < 0
 *If Veld(Position).Tag = 0 Then
 * *Position = Row * 7 + Column
 * *Row = Row - 1
 *Else
 *Row = Row + 1
 *End If
 *MsgBox "Position = " & Position & "."
 *MsgBox "Row = " & Row & "."
Loop

For some reason, this one gives the same result as the one from before I went to bed.

Edit: W00000000000000000-OOOOOOOOOOOOOOO-000000000000t :Brain: :bannana: :band: :Brain:

Code:

Do Until Row < 0
 *Position = Row * 7 + Column
 *If Veld(Position).Tag = 0 Then
 * *Row = Row - 1
 *Else
 * *Position = Position + 7
 * *Row = -1
 *End If
 *MsgBox "Position = " & Position & "."
 *MsgBox "Row = " & Row & "."
Loop

This works, but still floods over the field. Time to fix that.

Edit Edit:
Code:

Do Until Row < 0
 *Position = Row * 7 + Column
 *If Veld(Position).Tag = 0 Then
 * *Row = Row - 1
 *Else
 * *Position = Position + 7
 * * *If Position > 41 Then
 * * * *MsgBox "Uw schijf tottert zo op de tafel. Probeer'n andere kolom."
 * * * *Exit Sub
 * * *End If
 * *Row = -1
 *End If
 *MsgBox "Position = " & Position & "."
 *MsgBox "Row = " & Row & "."
Loop

Sweeeeeeeeeeeeeeeet :drool:

Rogue 02-12-2004 02:54 PM

kind of bad code.... :blink:

Try this:

Code:


Position = Row * 7 + Column
if Veld(Position).Tag = 0
 *' ****************
 *' ** Doorvallen **
 *' ****************
 *Do While Veld(Position).Tag = 0 And Row >= 0
 * Position = Row * 7 + Column
 * MsgBox "Position = " & Position & "."
 * Row = Row - 1
 * MsgBox "Row = " & Row & "."
 *Loop
Else
 * MsgBox "Column is full"
End if

This way if first cell is used, you jump over this whole code and let player choose another column.

Kon-Tiki 02-12-2004 02:59 PM

That one gives the same problem as before: the bottom line gets colored. The coins don't drop on each other, but over each other.

Rogue 02-12-2004 03:17 PM

Change only loop problem the other way you did before *I thought that code was OK*, but use that if statement to check if first top cell is colored, if yes, don't do any code that you would if player place a coin in the column (no win/draw check, and no change of players).

Kon-Tiki 02-12-2004 03:19 PM

Hence the exit sub ;) It'll stop running through the code until there's new input. Right now, I'm trying to hook the horizontal winning-checking up to the code. So far, no good. Time to put the MsgBoxes in to see what works and what doesn't.

Edit: Found the problem. Still used a hypotethical/hypothetical name for the player's turn. Changed all = player to = PlayerTurn and it works like a charm now :ok:

Edit: Woah momma, this coding goes quick now :blink: Vertical's already added too. Time for the diagonals :w00t:

Rogue 02-12-2004 03:30 PM

Use debug mode (F8 >> next step).

That's the best way to trace program and find code with the problems.

Kon-Tiki 02-12-2004 03:38 PM

All directions needed're in now :D Now to test them all...
And DAMN this code's short :blink: Especially compared to the shitload of crap I had before.

Rogue 02-12-2004 03:42 PM

Quote:

Originally posted by Kon-Tiki@Dec 2 2004, 11:38 AM
All directions needed're in now :D Now to test them all...
And DAMN this code's short :blink: Especially compared to the shitload of crap I had before.

Are you graded by the length of the code? :D

:ok:

Kon-Tiki 02-12-2004 03:50 PM

Time to finish it up, maybe add the choice of tileset in and add C64 music in (with .sid-files) if I ever knew how, then add a crapload of ascii porn in there, commented away :whistle:

Rogue 02-12-2004 03:59 PM

You should create exe and post it here (and code if you like).

So we can try and play it :)

Kon-Tiki 02-12-2004 04:28 PM

Code:

Dim Row As Integer, Column As Integer, Position As Integer, PlayerTurn As Integer, GameOn As Integer, score1 As Integer, score2 As Integer

' ***********************************************************************
' ** Zorgen dat't spel alleen kan beginnen als de namen zijn ingevoerd **
' ***********************************************************************
Private Sub Form_Load()
 GameOn = 0
End Sub

Private Sub Klik_Click(Index As Integer)
If GameOn = 1 Then
Column = Klik(Index).Index
Row = 5

Position = Row * 7 + Column

' ****************
' ** Doorvallen **
' ****************

Do Until Row < 0
 *Position = Row * 7 + Column
 *If Veld(Position).Tag = 0 Then
 * *Row = Row - 1
 *Else
 * *Position = Position + 7
 * * *If Position > 41 Then
 * * * *MsgBox "Uw schijf tottert zo op de tafel. Probeer'n andere kolom."
 * * * *Exit Sub
 * * *End If
 * *Row = -1
 *End If
Loop

' ********************
' ** Vakske kleuren **
' ********************
Veld(Position).Tag = PlayerTurn
Veld(Position).Picture = LoadPicture("tileset/default/veld" & PlayerTurn & ".jpg")
Call Direction
End If
End Sub

' *****************
' ** Horizontaal **
' *****************
Sub Direction()
For Column = 0 To 3
 *For Row = 0 To 5
 * * Position = Row * 7 + Column
 * * If Veld(Position).Tag = PlayerTurn And Veld(Position + 1).Tag = PlayerTurn And Veld(Position + 2).Tag = PlayerTurn And Veld(Position + 3).Tag = PlayerTurn Then
 * * * * *Call Winner
 * * * * *Exit Sub
 * *End If
 *Next Row
Next Column
 
' ***************
' ** Verticaal **
' ***************
For Column = 0 To 6
 *For Row = 0 To 2
 * * Position = Row * 7 + Column
 * * If Veld(Position).Tag = PlayerTurn And Veld(Position + 7).Tag = PlayerTurn And Veld(Position + 14).Tag = PlayerTurn And Veld(Position + 21).Tag = PlayerTurn Then
 * * * * *Call Winner
 * * * * *Exit Sub
 * * End If
 *Next Row
Next Column

' **************************
' ** Diagonaal naar boven **
' **************************
For Column = 0 To 3
 *For Row = 0 To 2
 * * Position = Row * 7 + Column
 * * If Veld(Position).Tag = PlayerTurn And Veld(Position + 8).Tag = PlayerTurn And Veld(Position + 16).Tag = PlayerTurn And Veld(Position + 24).Tag = PlayerTurn Then
 * * * * *Call Winner
 * * * * *Exit Sub
 * * End If
 *Next Row
Next Column
 
' ****************************
' ** Diagonaal naar beneden **
' ****************************
For Column = 3 To 6
 *For Row = 0 To 2
 * * Position = Row * 7 + Column
 * * If Veld(Position).Tag = PlayerTurn And Veld(Position + 6).Tag = PlayerTurn And Veld(Position + 12).Tag = PlayerTurn And Veld(Position + 18).Tag = PlayerTurn Then
 * * * * *Call Winner
 * * * * *Exit Sub
 * * End If
 *Next Row
Next Column

' **************************
' ** Veranderen van beurt **
' **************************
If PlayerTurn = 1 Then
 *PlayerTurn = 2
lblCurrentTurn.Caption = lblPlayer2.Caption
Else
 *PlayerTurn = 1
lblCurrentTurn.Caption = lblPlayer1.Caption
End If
End Sub

' **************************************
' ** Nomen est omen... Spel afsluiten **
' **************************************
Private Sub Exit_Click()
End
End Sub

' ***********************************************************
' ** Leegmaken van speelbord en resetten van de speelbeurt **
' ***********************************************************
Sub ClearField()
For Row = 0 To 5
 *For Column = 0 To 6
 * *Position = Row * 7 + Column
 * *Veld(Position).Tag = 0
 * *Veld(Position).Picture = LoadPicture("tileset/default/veld.jpg")
 *Next Column
Next Row
PlayerTurn = 1
lblScore1.Caption = score1
lblScore2.Caption = score2
lblCurrentTurn.Caption = lblPlayer1.Caption
End Sub

' *************************
' ** Nieuw spel beginnen **
' *************************
Private Sub MenuNew_Click()
strPlayer1 = InputBox("Geef de naam voor speler 1:", "Naam", "Player 1")
strPlayer2 = InputBox("Geef de naam voor speler 2:", "Naam", "Player 2")
lblPlayer1.Caption = strPlayer1
lblPlayer2.Caption = strPlayer2
score1 = 0
score2 = 0
Call ClearField
GameOn = 1
End Sub

Sub Winner()
If PlayerTurn = 1 Then
 *MsgBox "" & lblPlayer1 & " is a wiener!"
 *score1 = score1 + 1
Else
 *MsgBox "" & lblPlayer2 & " is a wiener!"
 *score2 = score2 + 1
End If
Call ClearField
End Sub

The texts're kinda Dutch-English... but meh. They never said which language it had to be in :whistle:
The compiled version
The compiled version with all files from the coding included

Rogue 02-12-2004 05:56 PM

Good job! :ok:

It works great!

Kon-Tiki 02-12-2004 06:03 PM

And it's so slim in size :D I'm looking into how to add music now. Preferably SIDfiles, but can't understand that fileformat documentation, so probably have to convert them to wav or midi first. Also will add the possibility of tilesets. Shouldn't be too hard to do.

Rogue 02-12-2004 06:08 PM

I would sugest to make result more clearer (it's kind of hard to read).

I did some sound work long time ago. Can't remember what format we used.

Kon-Tiki 02-12-2004 06:13 PM

Make result more clearer?

As for sound, I got a filetype documentation on .sid files, but don't see how to use that to get the songs to play in this game. Also got this as another explanation, but it feels like it's got a load of rubble in there.

Guess I'll do the tilesets first, and try to change the icon :D

Rogue 02-12-2004 06:21 PM

It's hard to read it.


Rogue 02-12-2004 06:24 PM

Changing the Windows XP theme, it changed it.

You can make background and font be OS independent.

Kon-Tiki 02-12-2004 06:52 PM

Didn't notice that :blink: Win98SE here, so no XP problems. It looked fine here, like on the lower pic, but with a grey background, not a pale yellow one.

Kon-Tiki 02-12-2004 08:20 PM

Tilesets code's done too, and works like a charm. Friend of mine's sprucing up my sheep tileset and am making another couple of sets.
Code:

' **************
' ** Tilesets **
' **************
Private Sub Default_Click()
 *TileFolder = "tileset\default"
 *Call TileChange
End Sub
Private Sub Sheep_Click()
 *TileFolder = "tileset\sheep"
 *Call TileChange
End Sub

Sub TileChange()
For Row = 0 To 5
 *For Column = 0 To 6
 * *Position = Row * 7 + Column
 * *Select Case Veld(Position).Tag
 * * *Case 0
 * * * *Veld(Position).Picture = LoadPicture("" & TileFolder & "\veld.jpg")
 * * *Case 1
 * * * *Veld(Position).Picture = LoadPicture("" & TileFolder & "\veld1.jpg")
 * * *Case 2
 * * * *Veld(Position).Picture = LoadPicture("" & TileFolder & "\veld2.jpg")
 * *End Select
 *Next Column
Next Row
Disc1.Picture = LoadPicture("" & TileFolder & "\veld1.jpg")
Disc2.Picture = LoadPicture("" & TileFolder & "\veld2.jpg")
End Sub

When the form is loading, default tileset if default.


The current time is 01:45 AM (GMT)

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.