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)
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