Help needed to create game

gavinkelly

Board Regular
Joined
Jan 12, 2008
Messages
220
I am trying to create a game i am unsure of the name but it is played on a 10centimeter square grid and 2 opponents take it in turns to draw a single centimeter line on the grid. if an player makes a full square he writes his initial in the square takes another go.
When all the possible lines have been drawn by each player the player with the most initals on the board wins.

So far i have created it for a 2 by 2 grid but if i use this method for a larger grid (e.g.) a 10 by 10 grid it will take me a long long time to write it out and the program will loop more an more times to find an un used line on the grid towards the end of the game (This is because in a 2 by 2 grid there are 12 possible "moves" but each time the computer moves it generates a random number from 1-12 and then checks to see if that move is already taken, if it is taken it generates another random number between 1-12 until it finds a free space. This brute force method will just take far too long as the grid gets larger. I will add AI later but for now can anyone help with this first problem. The code is as follows (sorry it is pretty long but very repetitive)

Code:
Sub CompGo()
Zero:
'i draw first line then computer choses a line
n = 12
Dim MyValue

For Z = 1 To 10000

MyValue = Int((n * Rnd) + 1)
Select Case MyValue
Case 1
If Cells(2, 2).Borders(xlEdgeLeft).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(2, 2).Borders(xlEdgeLeft).LineStyle = xlContinuous
 Exit For

Case 2
If Cells(2, 2).Borders(xlEdgeTop).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(2, 2).Borders(xlEdgeTop).LineStyle = xlContinuous
 Exit For


Case 3
If Cells(2, 2).Borders(xlEdgeRight).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(2, 2).Borders(xlEdgeRight).LineStyle = xlContinuous
 Exit For

Case 4
If Cells(2, 2).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(2, 2).Borders(xlEdgeBottom).LineStyle = xlContinuous
 Exit For

Case 5
If Cells(2, 3).Borders(xlEdgeTop).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(2, 3).Borders(xlEdgeTop).LineStyle = xlContinuous
 Exit For

Case 6
If Cells(2, 3).Borders(xlEdgeRight).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(2, 3).Borders(xlEdgeRight).LineStyle = xlContinuous
 Exit For

Case 7
If Cells(2, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(2, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous
 Exit For

Case 8
If Cells(3, 2).Borders(xlEdgeLeft).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(3, 2).Borders(xlEdgeLeft).LineStyle = xlContinuous
 Exit For

Case 9
If Cells(3, 2).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(3, 2).Borders(xlEdgeBottom).LineStyle = xlContinuous
 Exit For

Case 10
If Cells(3, 2).Borders(xlEdgeRight).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(3, 2).Borders(xlEdgeRight).LineStyle = xlContinuous
 Exit For

Case 11
If Cells(3, 3).Borders(xlEdgeRight).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(3, 3).Borders(xlEdgeRight).LineStyle = xlContinuous
 Exit For

Case 12
 Cells(3, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous

If Cells(3, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
GoTo Handler
End If
 Cells(3, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous
 Exit For

End Select
Handler:

Next Z
M = 0
If IsEmpty(Cells(2, 2)) Then
    If Cells(2, 2).Borders(xlEdgeRight).LineStyle = xlContinuous And Cells(2, 2).Borders(xlEdgeLeft).LineStyle = xlContinuous And Cells(2, 2).Borders(xlEdgeTop).LineStyle = xlContinuous And Cells(2, 2).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
    Cells(2, 2) = "C"
    M = 1
    End If
End If

If IsEmpty(Cells(2, 3)) Then
    If Cells(2, 3).Borders(xlEdgeRight).LineStyle = xlContinuous And Cells(2, 3).Borders(xlEdgeLeft).LineStyle = xlContinuous And Cells(2, 3).Borders(xlEdgeTop).LineStyle = xlContinuous And Cells(2, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
    Cells(2, 3) = "C"
    M = 1
    End If
End If

If IsEmpty(Cells(3, 2)) Then
    If Cells(3, 2).Borders(xlEdgeRight).LineStyle = xlContinuous And Cells(3, 2).Borders(xlEdgeLeft).LineStyle = xlContinuous And Cells(3, 2).Borders(xlEdgeTop).LineStyle = xlContinuous And Cells(3, 2).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
    Cells(3, 2) = "C"
    M = 1
    End If
End If

If IsEmpty(Cells(3, 3)) Then
    If Cells(3, 3).Borders(xlEdgeRight).LineStyle = xlContinuous And Cells(3, 3).Borders(xlEdgeLeft).LineStyle = xlContinuous And Cells(3, 3).Borders(xlEdgeTop).LineStyle = xlContinuous And Cells(3, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
    Cells(3, 3) = "C"
    M = 1
    End If
End If
 If M = 1 Then
 GoTo Zero:
 End If




End Sub
Sub MyGo()
n = 0
If IsEmpty(Cells(2, 2)) Then
    If Cells(2, 2).Borders(xlEdgeRight).LineStyle = xlContinuous And Cells(2, 2).Borders(xlEdgeLeft).LineStyle = xlContinuous And Cells(2, 2).Borders(xlEdgeTop).LineStyle = xlContinuous And Cells(2, 2).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
    Cells(2, 2) = "G"
    n = 1
    End If
End If

If IsEmpty(Cells(2, 3)) Then
    If Cells(2, 3).Borders(xlEdgeRight).LineStyle = xlContinuous And Cells(2, 3).Borders(xlEdgeLeft).LineStyle = xlContinuous And Cells(2, 3).Borders(xlEdgeTop).LineStyle = xlContinuous And Cells(2, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
    Cells(2, 3) = "G"
    n = 1
    End If
End If

If IsEmpty(Cells(3, 2)) Then
    If Cells(3, 2).Borders(xlEdgeRight).LineStyle = xlContinuous And Cells(3, 2).Borders(xlEdgeLeft).LineStyle = xlContinuous And Cells(3, 2).Borders(xlEdgeTop).LineStyle = xlContinuous And Cells(3, 2).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
    Cells(3, 2) = "G"
    n = 1
    End If
End If

If IsEmpty(Cells(3, 3)) Then
    If Cells(3, 3).Borders(xlEdgeRight).LineStyle = xlContinuous And Cells(3, 3).Borders(xlEdgeLeft).LineStyle = xlContinuous And Cells(3, 3).Borders(xlEdgeTop).LineStyle = xlContinuous And Cells(3, 3).Borders(xlEdgeBottom).LineStyle = xlContinuous Then
    Cells(3, 3) = "G"
    n = 1
    End If
End If

If n = 1 Then
MsgBox ("Take another go")

Else
Call CompGo
End If

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Not a simple task.

I would tackle this by having a separate, hidden, "database" giving the game position.Could use a code array, but I think a hidden worksheet would be easier to use for debugging. Excel can check 100 rows or cells very quickly.

Generally, chess programs go through every possible move (the various levels set time limits to this) and assign a score to each. When the time limit is reached the move with the highest score is made. The cleverer chess programs can also check several moves in advance. A way of doing this would be to copy the existing array, make the move on that, and then run the checking code again on the copy.

A complication is that when a line is added to one cell it usually adds a line to an adjacent cell too.

As a guess to begin with, the database would consist of a row for each cell (1 to 100) and 5 columns which stand for Left, Right, Top, Bottom,Total Number of Lines. This would be empty to begin with (all cells zero). If, for example, a line was added to the top of a cell the "Top" column gets a 1 and the Total Lines column gets 1 added. Need to change the data for the cell above as well, which getts a bottom line.

It would be easy to write code to first search for cells with Total Lines =3 - and not make moves that would leave Total Lines =3 for the opponent -(so avoid adding a line to existing cells (and the ones next to them) where total lines =2).

There are all sorts of possible variations to this. eg. You may prefer to use a 10x10 array to mimic the display and use a text string like "LR002" or "11103".

Hope this helps.
 
Upvote 0
I would tackle this by having a separate, hidden, "database" giving the game position.Could use a code array, but I think a hidden worksheet would be easier to use for debugging. Excel can check 100 rows or cells very quickly.

Use an array, and output it to a range to look at as a debugging step. Then when you're ready for release, remove (or comment out) the debug code.

Also, I would use a queue of some kind to look for possible moves. The AI algorithm should (after thinking about this for a whole two minutes) keep track of all positions where an odd number of moves will generate a box. If there are any that are one line away, put it there. If there are any that are three, those are your next options. Next are of course the fours, and twos are last resort. It's something you can keep track of on the fly, and would be faster to run than the brute force approach you're suggesting.
 
Upvote 0
If I recall correctly, the first line is drawn in the center. Correct? And each player must draw a line that intersects a previous move from either player. Correct? I would use a dictionary or hashtable. Either of these would include the search logic for any given coordinate/key. If you use an array, you are going to have to provide your own logic which will likely be slower. Also, have you considered using autoshapes? Lines? If you were to draw out 100 lines for a 10x10 graph and setting the same onaction for all. Each shape could be indexed by its name according to its position. This would make updating the index easy. As for intersecting, figuring possible moves, figuring allowable moves... This should be done with bit operations. You will only need 18 for a 10 by 10 graph. It's the fastest and easiest way to go. Sounds like fun. Make it networthy and I'll take you on when your finished... :)
 
Upvote 0
Hi,

Sounds fun and challenging.
Please describe the rules or provide a link where they can be found.
It makes no sense working together, when not all details are known.

kind regards,
Erik
 
Upvote 0
is this complete?

playground: SQARE of squares (10 x 10)
draw lines within playground on edges of squares
starting point: FREE
placement of lines: FREE
when closing square: square gets marked as yours + extra turn

WINNER: most marked squares
 
Upvote 0
Yep, that is the complete rules. I may start off with a smaller grid though while i figure out how best to do it. I had the idea from the post challenge of making the most fun game or pass time on excel, i think they should have more regular challenges
 
Upvote 0
OK!!
I am quite ready with a version for 2 human players. Only need to add little things...

I read about arrays etcetera in this thread. Not sure if that is needed.
Currently using a "copy" of the playground filling with a numbersystem. When number = 15 (1 + 2 + 4 8) then the cell gets labeled.

This way you can update the score by using a simple COUNTIF...
 
Upvote 0
any updates, guys?

sharing what I am doing
the "copy" which I was talking of in previous reply is not needed: don't know why I used that idea ...

2 players
1 player doubleclicking (or even just selecting), 1 player rightclicking
popup appears
  • right
  • bottom
  • left
  • top
when there are already borders in the cell, the popup will have less items

click item
border and value are added to the cell itself and the "neighbourcell"
currently I'm adding different numbers, although you could do with just 1
right: 1
bottom: 2
left: 4
top: 8
I'm using those numbers, if it would anytime be usefull to retrieve very quickly a list of borders: it is faster to check values than formats. TO my sense it will be useful to let the computer choose what to do.
when a cell reaches the maxvalue it gets labeled (getting symbol belonging to the player who clicked)
countif retrieves the scores

as long as a cells contains a value the format is ;;;
that way the numbers do not appear
when the cell gets labeled the format is changed to "general"
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,449
Members
449,160
Latest member
nikijon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top