Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

class Move(object): 

def __init__(self, piece, right, up): 

""" 

Initialize data structure with required variables. Throws an error 

on incorrect value ranges and types given 

 

@type piece: character  

@param piece: The piece to be moved 

@type right: integer 

@param right: If the piece should be to the right by a space 

@type up: integer 

@param up: If the piece should be moved up by a space 

""" 

super(Move, self).__init__() 

self.piece = piece 

self.up = up 

self.right = right 

 

if not self.isValid(): 

raise AttributeError("Instantiated move is invalid.") 

 

def isValid(self): 

""" 

Ensure that the move is valid for the board and the piece 

is a character. 

""" 

# first check if the type of piece is a character and the 

# direction of the move is horizontal or vertical and not 

# diagnol 

return type(self.piece) == str and \ 

((self.up != 0 and self.right == 0) or \ 

(self.up != 0 and self.right == 0) or \ 

(self.up == 0 and self.right != 0) or \ 

(self.up == 0 and self.right != 0)) 

 

def equals(self, move): 

""" 

Test whether the two moves are equal. 

 

@type move: Move 

@param move: Move being tested for equality 

@rtype: boolean 

@return: Whether the two moves are equal 

""" 

return self.piece == move.piece and self.right == move.right and self.up == move.up 

 

def toStr(self): 

""" 

Convert self to string. 

 

@rtype: str 

@return: String representation of the move 

""" 

return str(self.piece + ", " + str(self.right) + ", " + str(self.up)) 

 

@staticmethod 

def left(piece, size=1): 

""" 

Instantiate a move that will go left 

 

@type piece: character 

@param piece: Character that represents the piece to be moved 

""" 

return Move(piece, -abs(size), 0) 

 

@staticmethod 

def right(piece, size=1): 

""" 

Instantiate a move that will go right 

 

@type piece: character 

@param piece: Character that represents the piece to be moved 

""" 

return Move(piece, abs(size), 0) 

 

@staticmethod 

def up(piece, size=1): 

""" 

Instantiate a move that will go up 

 

@type piece: character 

@param piece: Character that represents the piece to be moved 

""" 

return Move(piece, 0, -abs(size)) 

 

@staticmethod 

def down(piece, size=1): 

""" 

Instantiate a move that will go down 

 

@type piece: character 

@param piece: Character that represents the piece to be moved 

""" 

return Move(piece, 0, abs(size))