ok, so this is my semester project that i made for my advanced topics in computer science class. I’m not sure if this is the place to put it if it has one problem, i dont know how to remove pieces from the board, but all the rules of the game work. i have 3 external files…one is square.egg.pz which was used on the chess board, a skin for the board squares which is squareskin.JPG, and the third is gopiece.egg which is a sphere that i resized in blender.
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from direct.showbase.DirectObject import *
import sys
shift = 9.5
BLACK = Vec4(0,0,0,1)
WHITE = Vec4(1,1,1,1)
HIGHLIGHT = Vec4(0,1,1,1)
PIECEBLACK = Vec4(.15, .15, .15, 1)
def PointAtZ(z, point, vec):
return point + vec * ((z-point.getZ()) / vec.getZ())
def SquarePos(x, y):
return Point3(x-shift, y-shift, 0)
class World(DirectObject):
def __init__(s):
s.bK = 0
s.wK = 0
s.blackBrood = brood()
s.whiteBrood = brood()
s.turn = False
s.blackScore = OnscreenText(text="Black kills: " + str(s.bK), fg=(1,1,1,1), pos=(-1.3, .95), align=TextNode.ALeft, scale = .05, mayChange = True)
s.whiteScore = OnscreenText(text="White kills: " + str(s.wK), fg=(1,1,1,1), pos=(-1.3, .9), align=TextNode.ALeft, scale = .05, mayChange = True)
s.accept('escape', sys.exit)
base.disableMouse()
camera.setPosHpr(0, 0, 40, 0, -90, 0)
s.setupLights()
s.picker = CollisionTraverser()
s.pq = CollisionHandlerQueue()
s.pickerNode = CollisionNode('mouseRay')
s.pickerNP = camera.attachNewNode(s.pickerNode)
s.pickerNode.setFromCollideMask(BitMask32.bit(1))
s.pickerRay = CollisionRay()
s.pickerNode.addSolid(s.pickerRay)
s.picker.addCollider(s.pickerNP, s.pq)
s.squareRoot = render.attachNewNode("squareRoot")
s.piecesRoot = render.attachNewNode("piecesRoot")
s.square_tex = loader.loadTexture("models/squareskin.JPG")
s.squares = [[None for j in range(19)] for i in range(19)]
s.pieces = [[None for j in range(19)] for i in range(19)]
for x in range(19):
for y in range(19):
s.squares[x][y] = loader.loadModelCopy("models/square")
s.squares[x][y].setTexture(s.square_tex, 1)
s.squares[x][y].reparentTo(s.squareRoot)
s.squares[x][y].setPos(SquarePos(x,y))
s.squares[x][y].find("**/polygon").node().setIntoCollideMask(BitMask32.bit(1))
s.squares[x][y].find("**/polygon").node().setTag('square', str(x + y*19))
s.hiSq = False
s.blackBrood.otherBrood(s.whiteBrood)
s.whiteBrood.otherBrood(s.blackBrood)
s.mouseTask = taskMgr.add(s.mouseTask, 'mouseTask')
s.accept("mouse1", s.makePiece)
def makePiece(s):
if (s.hiSq is not False and not s.blackBrood.hasThis(s.hiSq[0],s.hiSq[1]) and not s.whiteBrood.hasThis(s.hiSq[0], s.hiSq[1])):
s.pq.sortEntries()
i = int(s.pq.getEntry(0).getIntoNode().getTag('square'))
x = i%19
y = (i-x)/19
if s.turn:
if s.blackBrood.valid(x, y):
s.blackBrood.addThis(x,y)
s.turn = not s.turn
else:
if s.whiteBrood.valid(x,y):
s.whiteBrood.addThis(x,y)
s.turn = not s.turn
s.pieces = [[None for j in range(19)] for i in range(19)]
for x in range(19):
for y in range(19):
if s.blackBrood.hasThis(x, y):
s.pieces[x][y] = GO(x,y,PIECEBLACK)
elif s.whiteBrood.hasThis(x, y):
s.pieces[x][y] = GO(x,y,WHITE)
s.wK = s.whiteBrood.getKills()
s.bK = s.blackBrood.getKills()
s.blackScore.setText("Black kills: " + str(int(s.bK)))
s.whiteScore.setText("White kills: " + str(int(s.wK)))
s.hiSq = False
def mouseTask(s, task):
if s.hiSq is not False:
s.squares[s.hiSq[0]][s.hiSq[1]].setColor(1,1,1,1)
s.hiSq = False
if base.mouseWatcherNode.hasMouse():
mpos = base.mouseWatcherNode.getMouse()
s.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())
s.picker.traverse(s.squareRoot)
if s.pq.getNumEntries() > 0:
s.pq.sortEntries()
i = int(s.pq.getEntry(0).getIntoNode().getTag('square'))
x = i%19
y = (i-x)/19
s.squares[x][y].setColor(HIGHLIGHT)
s.hiSq = [x, y]
return Task.cont
def setupLights(s):
lAttrib = LightAttrib.makeAllOff()
ambientLight = AmbientLight("ambientLight")
ambientLight.setColor(Vec4(.8, .8, .8, 1))
lAttrib = lAttrib.addLight(ambientLight)
directionalLight = DirectionalLight("directionalLight")
directionalLight.setDirection(Vec3(0, 45, -45))
directionalLight.setColor(Vec4( 0.2, 0.2, 0.2, 1))
lAttrib = lAttrib.addLight(directionalLight)
render.attachNewNode(directionalLight.upcastToPandaNode())
render.attachNewNode(ambientLight.upcastToPandaNode())
render.node().setAttrib(lAttrib)
######################################## Piece ######################################
class Piece:
def __init__(s, squarex, squarey, color):
s.obj = loader.loadModelCopy(s.model)
s.obj.reparentTo(render)
s.obj.setColor(color)
s.obj.setPos(Point3(squarex-shift, squarey-shift, .3))
s.loc = location(squarex, squarey)
######################################### GO ########################################
class GO(Piece):
model = "models/gopiece"
######################################## brood ######################################
class brood:
def __init__(s):
s.lastMove = None
s.mySchools = []
s.otherBrood
s.numKills = 0
def getKills(s):
return s.numKills
def removeSchool(s, count):
del s.mySchools[count]
def otherBrood(s, other):
s.otherBrood = other
def addThis(s, x, y):
s.myLoc = location(x, y)
if s.mySchools == []:
s.mySchools.append(school(x,y))
else:
s.stba = []
for i in range(len(s.mySchools)):
for z in range(len(s.myLoc.getNeighbors())):
if s.mySchools[i].hasThis(s.myLoc.getNeighbors()[z][0], s.myLoc.getNeighbors()[z][1]):
w = -1
if not s.stba == []:
for cnt in range(len(s.stba)):
if i == s.stba[cnt]:
w = 0
if not w==0:
s.stba.append(i)
if s.stba == []:
s.mySchools.append(school(x, y))
temp = s.mySchools[len(s.mySchools)-1]
counter = -1
for cnt in range(len(temp.getNeighbors())):
if not s.otherBrood.hasThis(temp.getNeighbors()[cnt][0], temp.getNeighbors()[cnt][1]):
counter = 1
else:
if s.otherBrood.schoolSurrounded(temp.getNeighbors()[cnt][0], temp.getNeighbors()[cnt][1]):
counter = 1
if counter == -1:
del s.mySchools[len(s.mySchools)-1]
else:
if len(s.stba) > 1:
for c in range(len(s.stba)):
s.mySchools[s.stba[0]].addSchool(s.mySchools[s.stba[c]])
s.stba.reverse()
for a in range(len(s.stba)-1):
del s.mySchools[s.stba[a]]
s.stba.reverse()
s.mySchools[s.stba[0]].addThis(x,y)
neigh = s.mySchools[s.stba[0]].getNeighbors()
temp = -1
for v in range(len(neigh)):
if not s.otherBrood.hasThis(neigh[v][0], neigh[v][1]):
temp = 0
else:
if s.otherBrood.schoolSurrounded(neigh[v][0], neigh[v][1]):
temp = 0
if temp == -1:
s.otherBrood.addKills(s.mySchools[s.stba[0]].getCount())
s.removeSchool(s.stba[0])
s.lastMove = s.myLoc
def schoolSurrounded(s, x, y):
index = -1
for i in range(len(s.mySchools)):
if s.mySchools[i].hasThis(x, y):
index = i
if index == -1:
return False
for cnt in range(len(s.mySchools[index].getNeighbors())):
temp = s.mySchools[index].getNeighbors()[cnt]
if not s.otherBrood.hasThis(temp[0], temp[1]):
return False
s.otherBrood.addKills(s.mySchools[index].getCount())
s.removeSchool(index)
return True
def hasThis(s, x, y):
if s.mySchools == []:
return False
else:
for i in range(len(s.mySchools)):
if s.mySchools[i].hasThis(x, y):
return True
return False
def getData(s):
if s.mySchools == []:
return s.mySchools
elif len(s.mySchools) >= 1:
return s.mySchools
else:
s.temp = school(0,0)
s.temp.removeThis(0,0)
for i in range(len(s.mySchools)):
s.temp = s.temp.addSchool(s.mySchools[i])
return s.temp
def valid(s, x, y):
s.myLoc = location(x, y)
if not s.lastMove == None:
if s.myLoc.getX() == s.lastMove.getX() and s.myLoc.getY() == s.lastMove.getY():
return False
return True
def getNeighbors(s, x, y):
for c in range(len(s.mySchools)):
if s.mySchools[c].hasThis(x, y):
neighbors = s.mySchools[c].getNeighbors(x, y)
return neighbors
def addKills(s, num):
s.numKills = s.numKills + num
###################################### location #####################################
class location:
def __init__(s, x, y):
s.xloc = x
s.yloc = y
s.neighbors = []
s.makeneighbors()
def makeneighbors(s):
if s.xloc == 0:
if s.yloc == 0:
s.neighbors = [[1,0], [0,1]]
elif s.yloc == 18:
s.neighbors = [[1,18], [0,17]]
else:
s.neighbors = [[0,s.yloc+1], [1,s.yloc], [0,s.yloc-1]]
elif s.xloc == 18:
if s.yloc == 0:
s.neighbors = [[18,1], [17,0]]
elif s.yloc == 18:
s.neighbors = [[18,17], [17,18]]
else:
s.neighbors = [[18,s.yloc+1], [18,s.yloc-1], [17,s.yloc]]
else:
if s.yloc == 0:
s.neighbors = [[s.xloc,1], [s.xloc+1,0], [s.xloc-1,0]]
elif s.yloc == 18:
s.neighbors = [[s.xloc+1,18], [s.xloc,17], [s.xloc-1,18]]
else:
s.neighbors = [[s.xloc,s.yloc+1], [s.xloc+1,s.yloc], [s.xloc,s.yloc-1], [s.xloc-1,s.yloc]]
def getNeighbors(s):
return s.neighbors
def getX(s):
return s.xloc
def getY(s):
return s.yloc
######################################## school ######################################
class school:
def __init__(s, x, y):
s.mP = [[False for j in range(19)] for i in range(19)]
s.mP[x][y] = True
s.count = 1
s.startLoc = [x, y]
def addThis(s, x, y):
s.mP[x][y] = True
s.count = 1 + s.count
def hasThis(s, x, y):
return s.mP[x][y]
def getData(s):
return s.mP
def addSchool(s, school):
s.count = s.count + school.getCount()
for i in range(19):
for j in range(19):
if school.getData()[i][j] == True:
s.mP[i][j] = True
def removeThis(s, x, y):
s.mP[x][y] = False
def getNeighbors(s):
s.temp = [[0 for j in range(19)] for i in range(19)]
for i in range(19):
for j in range(19):
if s.mP[i][j] == True:
s.temp[i][j] = 1
return s.recurse(s.temp, s.startLoc[0], s.startLoc[1])
def recurse(s, temp, x, y):
myLoc = location(x, y)
neigh = []
temp[x][y] = 2
myother = []
for cnt in range(len(myLoc.getNeighbors())):
if temp[myLoc.getNeighbors()[cnt][0]][myLoc.getNeighbors()[cnt][1]] == 1:
myother = s.recurse(temp, myLoc.getNeighbors()[cnt][0], myLoc.getNeighbors()[cnt][1])
for k in range(len(myother)):
neigh.append(myother[k])
elif temp[myLoc.getNeighbors()[cnt][0]][myLoc.getNeighbors()[cnt][1]] == 0:
neigh.append(myLoc.getNeighbors()[cnt])
return neigh
def getCount(s):
return s.count
w = World()
run()