-
Notifications
You must be signed in to change notification settings - Fork 0
/
q7_8.py
119 lines (86 loc) · 2.36 KB
/
q7_8.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
# Chapter 7: Object-oriented Design
# Problem 2:
Othello is played as follows: Each Othello piece is white on one side and black on the other
When a piece is surrounded by its opponents on both the left and right sides, or both the top and bottom,
it is said to be captured and its color is flipped On your turn,
you must capture at least one of your opponent’s pieces The game ends when either user has no more valid moves,
and the win is assigned to the person with the most pieces Implement the object oriented design for Othello
"""
################################################################################
################################################################################
################################################################################
# SOLUTIONS
"""
Solution 1 :
designed for 2 players
only one main class that hides a lot of inner functions
when started, runs continously until game is over
"""
class Question(object) :
white = 1
black = 2
def start(self) :
"""
Sets up board into valid starting positions
"""
def _won(self) :
"""
Retunrns winner if any, else 0
"""
def _canGo(self, color) :
"""
returns True if color has valid move on his turn, else false
"""
def _isValid(self, color, pos) :
"""
returns true if pos is local move for color
"""
def _getMove(self, color) :
"""
prompts player for move, exception is invalid input
"""
def _addMove(self, color, pos) :
"""
adds valid move to the board, and updates pieces on board
"""
def _printBoard(self) :
"""
prints current state of board
"""
def _game(self) :
"""
runs continously until a player wins
"""
self._printBoard()
while(self._won() == 0) :
valid = False
while(not valid) :
try :
self._getMove()
valid = True
except :
print "enter valid coordinate"
self._printBoard()
if self._won() != 3 :
if self._won() == 1 :
print "white won"
else :
print "black won"
else :
print "draw!"
"""
Solution 2 :
Setting up like we did for the card game in peter norvig's class
"""
class Game(object) :
def __init__(self) :
def newGame(self, numPlayers, size) :
def getTurn() :
def isFinished() :
class Board(object) :
def __init__(self) :
def getMoves(self, color) :
def setMove(self, color, loc) :
def printBoard(self) :
#player is strategy function