2017-09-13

Sep 13 In-Class Exercise.

Hey Everyone,
Post your solutions to the Sep 13 In-Class Exercise to this thread.
Best, Chris
Hey Everyone, Post your solutions to the Sep 13 In-Class Exercise to this thread. Best, Chris

-- Sep 13 In-Class Exercise
by Stephen Kairos Reyes

class MoveableBoard(Board): def __init__(self): Board.__init__(self) def movePiece(self, x1, y1, x2, y2): content = self.board[x1][y1] row = "" for i in range(3): if i == y2: row += content else: row += self.board[x2][i] self.board[x2] = row row = "" for j in range(3): if j == y1: row += '.' else: row += self.board[x1][j] self.board[x1] = row b = MoveableBoard() b.add("x", 1, 1) b.draw() print b.add("o", 0, 1) b.draw() b.movePiece(0, 1, 1, 1) b.draw()
(Edited: 2017-09-13)
by Stephen Kairos Reyes ---- <nowiki> class MoveableBoard(Board): def __init__(self): Board.__init__(self) def movePiece(self, x1, y1, x2, y2): content = self.board[x1][y1] row = "" for i in range(3): if i == y2: row += content else: row += self.board[x2][i] self.board[x2] = row row = "" for j in range(3): if j == y1: row += '.' else: row += self.board[x1][j] self.board[x1] = row b = MoveableBoard() b.add("x", 1, 1) b.draw() print b.add("o", 0, 1) b.draw() b.movePiece(0, 1, 1, 1) b.draw() </nowiki>

-- Sep 13 In-Class Exercise
  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)
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()

-- Sep 13 In-Class Exercise
My real name is Alexander Duong by the way class CoolestBoard(Board): def movePiece(self, x1, y1, x2, y2): # Gets the piece to be moved piece = self.board[x1][y1] # Adds it to the new position row = "" for i in range(3): if i == y2: row += piece else: row += self.board[x2][i] self.board[x2] = row # Replaces old posiiton with "." row = "" for i in range(3): if i == y1: row += "." else: row += self.board[x1][i] self.board[x1] = row b = CoolestBoard() b.add("x", 0, 0) b.draw() print() b.movePiece(0, 0, 1, 1) b.draw()
(Edited: 2017-09-13)
My real name is Alexander Duong by the way <nowiki> class CoolestBoard(Board): def movePiece(self, x1, y1, x2, y2): # Gets the piece to be moved piece = self.board[x1][y1] # Adds it to the new position row = "" for i in range(3): if i == y2: row += piece else: row += self.board[x2][i] self.board[x2] = row # Replaces old posiiton with "." row = "" for i in range(3): if i == y1: row += "." else: row += self.board[x1][i] self.board[x1] = row b = CoolestBoard() b.add("x", 0, 0) b.draw() print() b.movePiece(0, 0, 1, 1) b.draw() </nowiki>

-- Sep 13 In-Class Exercise
Yecheng Liang
Class CoolBoard:
	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
(Edited: 2017-09-13)
Yecheng Liang Class CoolBoard: 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

-- Sep 13 In-Class Exercise
Michael Torres
    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()
(Edited: 2017-09-13)
Michael Torres ---- 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()

-- Sep 13 In-Class Exercise
Student: Phyllis Lau
class MyBoard(Board):
    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
b = MyBoard() b.add("x", 2, 2) b.draw() print b.add("o", 1, 1) b.draw() print b.movePiece(2, 2, 1, 1) b.draw()
Student: Phyllis Lau class MyBoard(Board): 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 b = MyBoard() b.add("x", 2, 2) b.draw() print b.add("o", 1, 1) b.draw() print b.movePiece(2, 2, 1, 1) b.draw()

-- Sep 13 In-Class Exercise
class PegBoard(Board):
    
    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)
        
class PegBoard(Board): 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)

-- Sep 13 In-Class Exercise
class childBoardClass(Board):
    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
test = childBoardClass() test.add("x", 0, 0) test.draw() print() test.movePiece(0, 0, 1, 1) test.draw()
class childBoardClass(Board): 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 test = childBoardClass() test.add("x", 0, 0) test.draw() print() test.movePiece(0, 0, 1, 1) test.draw()

-- Sep 13 In-Class Exercise
class CoolBoard:
    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
b = CoolBoard() b.add("x", 1, 1) b.draw() print b.add("o", 0, 1) b.draw() b.move_piece(1, 1, 2, 1) b.draw()
Student: Zahra Amin
class CoolBoard: 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 b = CoolBoard() b.add("x", 1, 1) b.draw() print b.add("o", 0, 1) b.draw() b.move_piece(1, 1, 2, 1) b.draw() Student: Zahra Amin
[ Next ]
X