Skip to content

Instantly share code, notes, and snippets.

@bluefugue
Last active March 6, 2018 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluefugue/6bcb1b485f8cf05eab159531d11d902d to your computer and use it in GitHub Desktop.
Save bluefugue/6bcb1b485f8cf05eab159531d11d902d to your computer and use it in GitHub Desktop.
checkers2_objects
import random
import time
class Board():
def __init__(self, activePiece=None, grid=["." for i in range(64)]):
self.grid = grid
def clearBoard(self):
self.grid=["." for i in range(64)]
def placePieces(self, team1, team2):
for i in team1.roster:
try:
self.grid[i.position]=i
except TypeError:
pass
for i in team2.roster:
try:
self.grid[i.position]=i
except TypeError:
pass
def printBoard(self):
print("\n"*3)
print("* * * C H E C K E R S * * *")
print("")
print(" 0 1 2 3 4 5 6 7")
y=0
z=8
for i in range(0,8):
if (i*8)<16:
print('0'+str(i*8),end=' ')
else:
print(str(i*8), end=' ')
for x in range (y,z):
print(self.grid[x], end = " ")
print()
y += 8
z += 8
def canIMoveNormal(self, team, userPick):
if team.color=="White" and (str(self.grid[userPick+7]) == 'o') and (str(self.grid[userPick+9])=='o'):
return False
elif (team.color=="White") and (str(self.grid[userPick + 7])=='x') and (str(self.grid[userPick + 9])=='x') and (self.grid[userPick+14] != '.') and (self.grid[userPick+18] != '.'):
return False
elif team.color=="Black" and (str(self.grid[userPick-7]) =='x') and (str(self.grid[userPick-9])=='x'):
return False
elif (team.color=="Black") and (str(self.grid[userPick - 7])=='o') and (str(self.grid[userPick - 9])=='o') and (self.grid[userPick - 14] != '.') and (self.grid[userPick - 18] != '.'):
return False
else:
return True
def canIMoveKing(self, team, userPick):
pass
#select the piece that the player is going to move
def pickAPiece(self, team):
looper=True
print("{} moves!".format(team))
#the below section needs to loop if the colors don't match, until they do.
while looper==True:
userPick=int(input("Tell me the location of the piece you want to move: "))
if self.grid[userPick]==".":
print('There is no piece there!')
time.sleep(.8)
pass
elif (self.grid[userPick].isKing == False) and (self.canIMoveNormal(team, userPick) == False):
print('No legal moves for that piece!')
time.sleep(.8)
pass
elif (self.grid[userPick].isKing == True) and (self.canIMoveKing(team, userPick) == False):
print('No legal moves for that piece!')
time.sleep(.8)
pass
elif self.grid[userPick].color != team.color:
print('Wrong color! That is a {} piece'.format(self.grid[userPick].color))
time.sleep(.8)
elif self.grid[userPick].color == team.color:
self.activePiece=self.grid[userPick]
looper=False
else:
pass
class Piece():
def __init__(self, color=None, position=None, isKing=True):
self.color=color
self.position=position
self.isKing=isKing
#def __str__(self):
#return("Color: {} Position: {}".format(self.color, self.position))
def __str__(self):
if self.color=='White':
if self.isKing==False:
return("o")
else:
return("O")
elif self.color=='Black':
if self.isKing==False:
return("x")
else:
return("X")
else:
return("?")
def kingCheck(self):
if (self.color=='White') and (self.isKing==False) and (self.position >= 56):
self.isKing=True
print("This piece is now a King!")
time.sleep(.8)
elif (self.color=='Black') and (self.isKing==False) and (self.position <= 7):
self.isKing=True
print("This piece is now a King!")
time.sleep(.8)
else:
pass
def edgeCheck(self):
if self.position in [0,8,16,24,32,40,48,56]:
return 'leftEdge'
elif self.position in [7,15,23,31,39,47,55,63]:
return 'rightEdge'
else:
return False
def yo(self):
if str(self).lower() == 'o':
print('Yep!')
else:
print('Nope!')
return str(self).lower()
def checkFriendlyCollision(self, grid, moveInput):
if moveInput.upper() == 'UL':
if str(self).lower() == (str(grid[self.position - 9])).lower():
return True
else:
return False
if moveInput.upper() == 'DL':
if str(self).lower() == (str(grid[self.position + 7])).lower():
return True
else:
return False
if moveInput.upper() == 'UR':
if str(self).lower() == (str(grid[self.position - 7])).lower():
return True
else:
return False
if moveInput.upper() == 'DR':
if str(self).lower() == (str(grid[self.position + 9])).lower():
return True
else:
return False
def checkJumpable(self, grid, moveInput):
if moveInput.upper() == 'UL':
pass
if moveInput.upper() == 'DL':
pass
if moveInput.upper() == 'UR':
pass
if moveInput.upper() == 'DR':
pass
def collision(self, type):
if type == 'otherPiece':
print("Collision! Make another move.")
elif type == 'edge':
print("You're at the edge! Make another move.")
time.sleep(.8)
pass
def adjMove(self, moveInput):
if moveInput.upper()=="UL":
self.position -= 9
elif moveInput.upper()=="DL":
self.position += 7
elif moveInput.upper()=="UR":
self.position -= 7
elif moveInput.upper()=="DR":
self.position += 9
def pieceStatus(self):
print("Color: {} Position: {}".format(self.color, self.position))
def pieceMove(self, grid, team1, team2):
if self.isKing==False:
if self.color=='White':
self.downwardMove(grid, team1, team2)
if self.color=='Black':
self.upwardMove(grid, team1, team2)
#print(self.__repr__())
elif self.isKing==True:
self.kingMove(grid, team1, team2)
#'team1' and 'team2' logic doesn't work in cases where we want to use the same methods for both black and white. Currently causing ValueError bug: x not in list
def kingMove(self, grid, team1, team2):
looper=True
while looper==True:
moveInput = input("Up Left [UL], Up Right [UR], Down Left [DL], Down Right[DR]? ")
if 'L' in moveInput.upper() and self.edgeCheck == 'leftEdge':
self.collision(edge)
elif 'R' in moveInput.upper() and self.edgeCheck == 'rightEdge':
self.collision(edge)
elif self.checkFriendlyCollision(grid, moveInput) == True:
print('friendly collision')
self.collision('otherPiece')
elif
# 1. Is the destination beyond the edge?
# if so, run (edgecollision)
# 2. Does the destination contain a friendly piece?
# if so, run (collision)
# 3. Does the destination contain a hostile piece AND (the spot beyond it contains any piece or is beyond the edge)?
# if so, run (collision)
# 4. Does the destination contain a hostile piece AND the spot beyond it is clear?
# if so, run (capture)
# If 1-4 evaluate as false, then Move there!
if moveInput.upper() == 'DL' or moveInput.upper() == 'UL':
if self.edgeCheck() == 'leftEdge':
self.collision('edge')
else:
pass
elif moveInput.upper() == 'DR' or moveInput.upper() == 'UR':
if self.edgeCheck() == 'rightEdge':
self.collision('edge')
elif self.checkFriendlyCollision(grid, moveInput) == True:
print('friendly collision')
self.collision('otherPiece')
else:
self.adjMove(moveInput)
looper=False
'''
elif #destination contains a hostile piece AND (the spot beyond it contains any piece or is past the edge):
self.collision('otherPiece')
elif #destination contains a hostile piece AND the spot beyond it is clear
self.capture()
elif moveDir.upper() == 'UR':
if str(grid[self.position - 7]) == str(self):
self.collision()
elif self.position in (7,15,23,31,39,47,55,63):
print("Edge of board!")
time.sleep(.8)
pass
elif str(grid[self.position - 7]) != str(self) and str(grid[self.position - 7]) != '.':
if str(grid[self.position - 14]) == '.':
print('{} captures {}!'.format(self.color, grid[self.position-7].color))
team1.roster.remove(grid[self.position-7])
#print(team1.roster)
self.position -= 14
time.sleep(.8)
self.kingCheck()
looper=False
pass
else:
self.collision()
else:
self.position -= 7
self.kingCheck()
looper=False
elif moveDir.upper() == 'DL':
if str(grid[self.position + 7]) == str(self):
self.collision()
elif self.position in (0,8,16,24,32,40,48,56):
print("Edge of board!")
time.sleep(.8)
pass
elif str(grid[self.position + 7]) != str(self) and str(grid[self.position + 7]) != '.':
if str(grid[self.position + 14]) == '.':
print('{} captures {}!'.format(self.color, grid[self.position + 7].color))
team1.roster.remove(grid[self.position + 7])
#print(team1.roster)
self.position += 14
time.sleep(.8)
self.kingCheck()
looper=False
pass
else:
self.collision()
else:
self.position += 7
self.kingCheck()
looper=False
elif moveDir.upper() == 'DR':
if str(grid[self.position + 9]) == str(self):
self.collision()
elif self.position in (7,15,23,31,39,47,55,63):
print("Edge of board!")
time.sleep(.8)
pass
elif str(grid[self.position + 9]) != str(self) and str(grid[self.position + 9]) != '.':
if str(grid[self.position + 18]) == '.':
print('{} captures {}!'.format(self.color, grid[self.position-7].color))
team1.roster.remove(grid[self.position-7])
#print(team1.roster)
self.position += 18
time.sleep(.8)
self.kingCheck()
looper=False
pass
else:
self.collision()
else:
self.position += 9
self.kingCheck()
looper=False
'''
class Team():
def __init__(self, color='White', teamSize=12, roster=[]):
self.teamSize=teamSize
self.roster=roster
self.color=color
def __str__(self):
return("{} team".format(self.color))
def addPiecestoRoster(self, numOfPieces, color):
self.roster = [Piece(color) for i in range(numOfPieces)]
def setUpPieces(self, startPos):
listitem=0
for i in self.roster:
try:
i.position=startPos[listitem]
listitem+=1
except IndexError:
pass
def removePiece(self, piece):
self.roster.remove(piece)
def downwardMove(self, grid, team1, team2):
looper=True
while looper==True:
moveDir = input("Down [L]eft, Down [R]ight? ")
if moveDir.upper() == 'K':
self.isKing=True
pass
elif moveDir.upper() == 'S':
print('Skip turn!')
looper=False
pass
elif moveDir.upper() == 'L':
if str(grid[self.position + 7]) == 'o':
self.collision()
elif self.position in (0,8,16,24,32,40,48,56):
print("Edge of board!")
time.sleep(.8)
pass
elif str(grid[self.position + 7]) == 'x':
if str(grid[self.position + 14]) == '.':
#print(grid[self.position+7])
print('{} captures {}!'.format(self.color, grid[self.position+7].color))
team2.roster.remove(grid[self.position+7])
#print(team2.roster)
self.position += 14
time.sleep(.8)
self.kingCheck()
looper=False
pass
else:
self.collision()
else:
self.position += 7
self.kingCheck()
looper=False
elif moveDir.upper() == 'R':
if str(grid[self.position + 9]) == 'o':
self.collision()
elif self.position in (7,15,23,31,39,47,55,63):
print("Edge of board!")
time.sleep(.8)
pass
elif str(grid[self.position + 9]) == 'x':
if str(grid[self.position + 18]) == '.':
print('{} captures {}!'.format(self.color, grid[self.position+9].color))
team2.roster.remove(grid[self.position+9])
#print(team2.roster)
self.position += 18
time.sleep(.8)
self.kingCheck()
looper=False
pass
else:
self.collision()
else:
self.position += 9
self.kingCheck()
looper=False
def upwardMove(self, grid, team1, team2):
looper=True
while looper==True:
moveDir = input("Up [L]eft, Up [R]ight? ")
if moveDir.upper() == 'K':
self.isKing=True
pass
elif moveDir.upper() == 'S':
print('Skip turn!')
looper=False
pass
elif moveDir.upper() == 'L':
if str(grid[self.position - 9]) == 'x':
self.collision()
elif self.position in (0,8,16,24,32,40,48,56):
print("Edge of board!")
time.sleep(.8)
pass
elif str(grid[self.position - 9]) == 'o':
if str(grid[self.position - 18]) == '.':
print('{} captures {}!'.format(self.color, grid[self.position-9].color))
team1.roster.remove(grid[self.position-9])
#print(team1.roster)
self.position -= 18
time.sleep(.8)
self.kingCheck()
looper=False
pass
else:
self.collision()
else:
self.position -= 9
self.kingCheck()
looper=False
elif moveDir.upper() == 'R':
if str(grid[self.position - 7]) == 'x':
self.collision()
elif self.position in (7,15,23,31,39,47,55,63):
print("Edge of board!")
time.sleep(.8)
pass
elif str(grid[self.position - 7]) == 'o':
if str(grid[self.position - 14]) == '.':
print('{} captures {}!'.format(self.color, grid[self.position-7].color))
team1.roster.remove(grid[self.position-7])
#print(team1.roster)
self.position -= 14
time.sleep(.8)
self.kingCheck()
looper=False
pass
else:
self.collision()
else:
self.position -= 7
self.kingCheck()
looper=False
#to do next:
#1. keep working on kingMove logic. Abandon if doomed.
#bugtracker
#nonking white piece at position 28 moves down left where king black piece is located, position 35. Instead of Capture, it just lands on the same space. Map displays X but not o there.
'''
def kingMove(self, grid, team1, team2):
there are four directions for a King: UL, UR, DL, DR
For each direction:
1. Is the destination beyond the edge?
if so, run (edgecollision)
2. Does the destination contain a friendly piece?
if so, run (collision)
3. Does the destination contain a hostile piece AND (the spot beyond it contains any piece or is beyond the edge)?
if so, run (collision)
4. Does the destination contain a hostile piece AND the spot beyond it is clear?
if so, run (capture)
If 1-4 evaluate as false, then
Move there!
'''
#if ((moveInput.upper() == ('UL' or 'DL')) and (self.edgeCheck() == 'leftEdge')) or ((moveInput.upper() == ('UR' or 'DR')) and (self.edgeCheck() == 'rightEdge')):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment