In Python, starting with the 8x8 board solution that will be
appended here add the following functionality:
1) Add code to create a list, containing 8 lists, with each of the
8 lists containing 8 null strings as values. Call the list of lists
board.
code provided by prof:
import turtle
validMovesList=['A0','A2','A4','A6','B1','B3','B5','B7',
'C0','C2','C4','C6','D1','D3','D5','D7',
'E0','E2','E4','E6','F1','F3','F5','F7',
'G0','G2','G4','G6','H1','H3','H5','H7','quit']
def drawSquare(t,length,color):
t.fillcolor(color)
t.begin_fill()
for num in range(4):
t.forward(length)
t.left(90)
t.end_fill()
def drawRow(t,length,color1,color2):
for i in range(4):
drawSquare(t,length,color1)
t.forward(length)
drawSquare(t,length,color2)
t.forward(length)
def drawCircleFilled(t,size,color):
t.fillcolor(color)
t.begin_fill()
t.circle(size*.5)
t.end_fill()
def drawChecker(t,wn,row,col,size,currentPlayer):
wn.tracer(False)
t.up()
t.goto(0,0)
t.down()
y=(row*size)
x=(col*size)+(.5*size)
if currentPlayer=='b':
color="black"
outLine="dimgray"
else:
color="red"
outLine="lightcoral"
t.color(outLine,color)
t.fillcolor(color)
t.up()
t.goto(x,y)
t.down()
drawCircleFilled(t,size,color)
#rewrite as a loop
for i in range(1,5,1):
t.up()
t.goto(x,y+(i*.1)*size)
t.down()
t.circle((5-i)*.2*size*.5)
wn.tracer(True)
def drawLabels(t,wn):
wn.tracer(False)
t.up()
t.color("white")
for row in range (0,8,1):
if row%2==0:
start=0
else:
start=1
for col in range(start,8,2):
t.goto(col+.75,row+1.02)
t.write(chr(65+row)+str(col))
t.color("black")
wn.tracer(True)
def drawLabel(t,row,col):
t.color("white")
t.up()
t.goto(col+.75,row+1.02)
t.down()
t.write(chr(65+row)+str(col))
t.color("black")
def drawBoard(t,wn,length):
wn.tracer(False)
c1="gray"
c2="red"
for x in range(8):
drawRow(t,length,c1,c2)
t.backward(8*length)
t.left(90)
t.forward(length)
t.right(90)
#switch c1 and c2 contents
temp=c1
c1=c2
c2=temp
wn.tracer(True)
def setupGame(length):
wn=turtle.Screen()
wn.setworldcoordinates(-1,9,9,-1)
bob=turtle.Turtle()
bob.speed(0)
bob.hideturtle()
drawBoard(bob,wn,length)
drawLabels(bob,wn)
board=[]
row=['','','','','','','','']
for i in range(8):
board.append(row[:])
#fill the board with empty rows
return bob,wn,board
def removeChecker(bob,row,col,length):
bob.up()
bob.goto(col,row)
bob.down()
drawSquare(bob,length,"gray")
drawLabel(bob,row,col)
#this function will put checkers on the board for a new
game
def newGame(board):
for row in range(0,8):
for col in range(0,8,2):
drawChecker(row,col)
#square by square in first three rows add red checkers to valid
squares
#square by squre in last three rows add black checkers to valid
squares
def checkers(length):
bob,wn,board=setupGame(length)
newGame(board)
move=""
currentPlayer='b'
while move != 'quit':
while len(move)!=5 or (move[0:2] not in validMovesList) or
(move[3:] not in validMovesList):
move=input("Enter location to move a checker, quit to exit =>
")
if move!="quit":
fromRow=ord(move[0])-65
fromCol=int(move[1])
toRow=ord(move[3])-65
toCol=int(move[4])
removeChecker(bob,fromRow,fromCol,length)
drawChecker(bob,wn,toRow,toCol,length,currentPlayer)
move=input("Enter location to move a checker, quit to exit =>
")
checkers(1)
The code to create a list,containing 8 lists,with each list containing 8 null strings as values and call the list is as follows
a = []#list that contains 8 lists
p = [""] * 8 #create a list with 8 null strings as values
for i in range(1,9):
a.append(l) # append the list 'p' to list 'a' 8 times
print(a) #This is the required list which contains 8 lists, where
each list has 8 null strings
Can call the list in python directly by the variable name.In order to access an element with index i,a[i] is used.
In Python, starting with the 8x8 board solution that will be appended here add the following...
Using python
Here is the code
import turtle
def draw_triangle(vertex, length, k):
'''
Draw a triangle at depth k given the bottom vertex.
vertex: a tuple (x,y) that gives the coordinates of the bottom
vertex.
When k=0, vertex is the left bottom vertex for the outside
triangle.
length: the length of the original outside triangle (the
biggest one).
k: the depth of the input triangle.
As k increases by 1, the triangles shrink to a smaller
size.
When k=0, the...