Reputation: 3495
I'm just doing this to try improve my skills a little, I'm trying to recreate a game I thought of years ago when I was bored in school.
Basically it's kinda like a 3d connect 4, also supposed to flip over so it'll screw up your ideas if your spacial awareness isn't great. I've tried making a very basic display in python (I know how to build things in maya, not in python, so for now I'll stick with a rubbish looking design until I get it working), and it can also lookup the points and place them in.
However, I'm stuck on actually reading these points to find any rows of 4. I know I could physically type it all out, but I'm sure there would be a much better way.For example, each corner point could go either x, y, z, xy, xz, yz, or xyz, I don't expect anyone to do the code for me, but it'd be appreciated if you could tell me a way of calculating this without typing out each possible combination (I think there's 76 in total unless I missed any)
I would have done a loop if not for the diagonals, but I have no idea how I'd go about checking these
Cheers
The code so far is here -
#x(width),z(depth),y(height),playernum
points=[1,4,1,1],[2,1,3,1],[4,4,1,2],[3,4,1,2],[2,4,1,2],[1,1,2,1]
for j in range(1,5):
#set indent
spaces=" "
#draw top
print spaces + "________________"
#search points on certain level
validPointsY=[]
for point in points:
if point[2]==5-j:
validPointsY.append( point )
#draw middle
for i in range(1,5):
#search points on certain row
validPointsZ=[]
for point in validPointsY:
if point[1]==5-i:
validPointsZ.append( point )
#place points in correct box
point1=" "
point2=" "
point3=" "
point4=" "
for point in validPointsZ:
if point[0]==1:
point1=str(point[3])
if point[0]==2:
point2=str(point[3])
if point[0]==3:
point3=str(point[3])
if point[0]==4:
point4=str(point[3])
#remove one from space
spaces = spaces[:-1]
print spaces + "/ "+point1+" / "+point2+" / "+point3+" / "+point4+" /"
spaces = spaces[:-1]
print spaces + "/___/___/___/___/"
Just for the record, the game is quite fun if you have nothing to do, obviously it's not gonna look great at all from this though haha
Upvotes: 1
Views: 108
Reputation: 12900
The problem doesn't need optimizations (it is small enough), so a "naive" solution works well enough. You can enumerate all possible alignments, starting at each possible points and going in any possible direction:
for x in [1, 2, 3, 4]:
for y in [1, 2, 3, 4]:
for z in [1, 2, 3, 4]:
# start of the alignment is given by (x, y, z).
for dx in [-1, 0, +1]:
for dy in [-1, 0, +1]:
for dz in [-1, 0, +1]:
# add (dx, dy, dz) for each successive point.
# check that it makes a valid alignment first.
if ((dx, dy, dz) != (0, 0, 0) and
1 <= x + 3*dx <= 4 and
1 <= y + 3*dy <= 4 and
1 <= z + 3*dz <= 4):
# that's a valid alignment.
print (x, y, z), (x + 3*dx, y + 3*dy, z + 3*dz)
This prints 152 lines, i.e. 2 times 76: each alignment is found from both ends. It should not matter. You can replace the "print" line with a check that all 4 points have a stone from the same player.
Upvotes: 1