Skip to content

Commit 16ed5b1

Browse files
committed
system design
1 parent 7ac2b69 commit 16ed5b1

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed

system_design/design_chess_game.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
@Author: Aseem Jain
3+
@profile: https://www.linkedin.com/in/premaseem/
4+
5+
Chess is a two-player strategy board game played on a chessboard, which is a checkered gameboard with 64 squares arranged in an 8×8 grid. There are a few versions of game types that people play all over the world. In this design problem, we are going to focus on designing a two-player online chess game.
6+
7+
System Requirements
8+
===================
9+
10+
The system should support two online players to play a game of chess.
11+
All rules of international chess will be followed.
12+
Each player will be randomly assigned a side, black or white.
13+
Both players will play their moves one after the other. The white side plays the first move.
14+
Players can’t cancel or roll back their moves.
15+
The system should maintain a log of all moves by both players.
16+
Each side will start with 8 pawns, 2 rooks, 2 bishops, 2 knights, 1 queen, and 1 king.
17+
The game can finish either in a checkmate from one side, forfeit or stalemate (a draw), or resignation.
18+
19+
Actors:
20+
=======
21+
Player: A registered account in the system, who will play the game. The player will play chess moves.
22+
Admin: To ban/modify players.
23+
24+
Here are the top use cases for chess:
25+
======================================
26+
Player moves a piece: To make a valid move of any chess piece.
27+
Resign or forfeit a game: A player resigns from/forfeits the game.
28+
Register new account/Cancel membership: To add a new member or cancel an existing member.
29+
Update game log: To add a move to the game log.
30+
31+
"""
32+
33+

system_design/stack_overflow.py

+180
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,185 @@
3636
Moderators can close, delete, and un-delete any question.
3737
3838
"""
39+
from datetime import datetime
40+
from enum import Enum
3941

4042

43+
class QuestionStatus(Enum):
44+
OPEN, CLOSED, ON_HOLD, DELETED = 1, 2, 3, 4
45+
46+
47+
class QuestionClosingRemark(Enum):
48+
DUPLICATE, OFF_TOPIC, TOO_BROAD, NOT_CONSTRUCTIVE, NOT_A_REAL_QUESTION, PRIMARILY_OPINION_BASED = 1, 2, 3, 4, 5, 6
49+
50+
51+
class AccountStatus(Enum):
52+
ACTIVE, CLOSED, CANCELED, BLACKLISTED, BLOCKED = 1, 2, 3, 4, 5
53+
54+
# For simplicity, we are not defining getter and setter functions. The reader can
55+
# assume that all class attributes are private and accessed through their respective
56+
# public getter methods and modified only through their public methods function.
57+
58+
59+
class Account:
60+
def __init__(self, id, password, name, address, email, phone, status=AccountStatus.Active):
61+
self.__id = id
62+
self.__password = password
63+
self.__name = name
64+
self.__address = address
65+
self.__email = email
66+
self.__phone = phone
67+
self.__status = status
68+
self.__reputation = 0
69+
70+
def reset_password(self):
71+
None
72+
73+
74+
class Member:
75+
def __init__(self, account):
76+
self.__account = account
77+
self.__badges = []
78+
79+
def get_reputation(self):
80+
return self.__account.get_reputation()
81+
82+
def get_email(self):
83+
return self.__account.get_email()
84+
85+
def create_question(self, question):
86+
None
87+
88+
def create_tag(self, tag):
89+
None
90+
91+
92+
class Admin(Member):
93+
def block_member(self, member):
94+
None
95+
96+
def unblock_member(self, member):
97+
None
98+
99+
100+
class Moderator(Member):
101+
def close_question(self, question):
102+
None
103+
104+
def undelete_question(self, question):
105+
None
106+
107+
class Badge:
108+
def __init__(self, name, description):
109+
self.__name = name
110+
self.__description = description
111+
112+
113+
class Tag:
114+
def __init__(self, name, description):
115+
self.__name = name
116+
self.__description = description
117+
self.__daily_asked_frequency = 0
118+
self.__weekly_asked_frequency = 0
119+
120+
# import datetime
121+
122+
123+
class Notification:
124+
def __init__(self, id, content):
125+
self.__notification_id = id
126+
self.__created_on = datetime.datetime.now()
127+
self.__content = content
128+
129+
def send_notification(self):
130+
None
131+
132+
import datetime
133+
134+
class Photo:
135+
def __init__(self, id, path, member):
136+
self.__photo_id = id
137+
self.__photo_path = path
138+
self.__creation_date = datetime.datetime.now()
139+
self.__creating_member = member
140+
141+
def delete(self):
142+
None
143+
144+
# import datetime
145+
146+
147+
class Bounty:
148+
def __init__(self, reputation, expiry):
149+
self.__reputation = reputation
150+
self.__expiry = expiry
151+
152+
def modify_reputation(self, reputation):
153+
None
154+
155+
from abc import ABC, abstractmethod
156+
157+
class Search(ABC):
158+
def search(self, query):
159+
None
160+
161+
import datetime
162+
163+
class Question(Search):
164+
def __init__(self, title, description, bounty, asking_member):
165+
self.__title = title
166+
self.__description = description
167+
self.__view_count = 0
168+
self.__vote_count = 0
169+
self.__creation_time = datetime.datetime.now()
170+
self.__update_time = datetime.datetime.now()
171+
self.__status = QuestionStatus.OPEN
172+
self.__closing_remark = QuestionClosingRemark.DUPLICATE
173+
174+
self.__bounty = bounty
175+
self.__asking_member = asking_member
176+
self.__photos = []
177+
self.__comments = []
178+
self.__answers = []
179+
180+
def close(self):
181+
None
182+
183+
def undelete(self):
184+
None
185+
186+
def add_comment(self, comment):
187+
None
188+
189+
def add_bounty(self, bounty):
190+
None
191+
192+
def search(self, query):
193+
# return all questions containing the string query in their title or description.
194+
None
195+
196+
197+
class Comment:
198+
def __init__(self, text, member):
199+
self.__text = text
200+
self.__creation_time = datetime.datetime.now()
201+
self.__flag_count = 0
202+
self.__vote_count = 0
203+
self.__asking_member = member
204+
205+
def increment_vote_count(self):
206+
None
207+
208+
209+
class Answer:
210+
def __init__(self, text, member):
211+
self.__answer_text = text
212+
self.__accepted = False
213+
self.__vote_count = 0
214+
self.__flag_count = 0
215+
self.__creation_time = datetime.datetime.now()
216+
self.__creating_member = member
217+
self.__photos = []
218+
219+
def increment_vote_count(self):
220+
None

0 commit comments

Comments
 (0)