class Board2Point0(Board):
def movePiece(self, x1, y1, x2, y2):
# Move piece.
row = list(self.board[x2])
row[y2] = self.board[x1][y1]
self.board[x2] = "".join(row)
# Rewrite '.' to old square.
old = list(self.board[x1])
old[y1] = '.'
self.board[x1] = "".join(old)
def draw(self):
Board.draw(self)
print
b = Board2Point0()
b.add("x", 1, 1)
b.draw()
b.add("o", 0, 1)
b.draw()
b.movePiece(1, 1, 2, 2)
b.draw()
b.movePiece(0, 0, 0, 1)
b.draw()
(Edited: 2017-09-13) def _init_(self): Board.__init__(self) def movePiece(self, x1, y1, x2, y2) row = "" exchange = "" for i in range(3): if i == y1: row += "." exchange = self.board[x1][i] else: row += self.board[x1][i1] self.board[x1] = row Board.add(self, exchange, x2, y2)b = CoolBoard() b.add("x", 1, 1) b.draw() print b.add("o", 0, 1) b.draw() print b.movePiece(1,1,0,1) b.draw() print
class myBoard(Board):
def movePiece(self, x1, y1, x2, y2):
row1 = ""
row2 = ""
for i in range(3):
if i == y1:
row1 += self.board[x1][y1]
else:
row1 += self.board[x2][i]
self.board[x2] = row1
for i in range(3):
if i == y1:
row2 += "."
else:
row2 += self.board[x1][i]
self.board[x1] = row2
print("\nmyBoard\n")
b2 = myBoard()
b2.add("x", 1, 1)
b2.draw()
print()
b2.add("o", 0, 1)
b2.draw()
print()
b2.movePiece(0, 1, 1, 1)
b2.draw()
print()
def movePiece(self, x1, y1, x2, y2):
value = self.board[x1][y1]
r = ""
for i in range(3):
if i == y2:
r += value
else:
r += self.board[x2][i]
self.board[x2] = r
q = ""
for j in range(3):
if j == y1:
q += "."
else:
q += self.board[x1][j]
self.board[x1] = q
def movePieces(self, x1, y1, x2, y2):
piece = self.board[x1][y1]
if piece == "x" or piece == "o":
self.add(piece, x2, y2)
else:
#clear the second location (x2,y2)
self.clear(x2, y2)
#clear the initial piece in both instances
self.clear(x1, y1)
def clear(self, x, y):
#sets a point on the board to "."
row = ""
for i in range(3):
if i == y:
row += "."
else:
row += self.board[x][i]
self.board[x] = row
b = PegBoard()
b.add("x", 0, 1)
b.draw()
print
b.movePieces(0, 1, 1, 1)
b.draw()
print
b.add("o", 2, 2)
b.draw()
print
b.movePieces(1, 1, 2, 2)
b.draw()
print
b.movePieces(0, 0, 2, 2)
def movePiece(self, x1, y1, x2, y2):
firstRow = ""
for i in range(3):
if i == y1:
firstRow += self.board[x1][y1]
else:
firstRow += self.board[x2][i]
self.board[x2] = firstRow
secondRow = ""
for i in range(3):
if i == y1:
secondRow += "."
else:
secondRow += self.board[x1][i]
self.board[x1] = secondRow
def move_piece(self, x1, y1, x2, y2):
replace = self.board[x1][y1]
row = ""
for i in range(3):
if i == y2:
row += replace
else:
row += self.board[x2][i]
self.board[x2] = row
row = ""
for i in range(3):
if i == y1:
row += "."
else:
row += self.board[x1][i]
self.board[x1] = row