Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added the "board" command to the game.

  • Loading branch information...
commit d6ac1582f47fd2bb91c5a5eefcc4faef9c967666 1 parent 8e824f4
@rodolfo3 rodolfo3 authored
Showing with 36 additions and 6 deletions.
  1. +3 −2 baseServer.py
  2. +33 −4 game.py
View
5 baseServer.py
@@ -169,10 +169,11 @@ def play(self, cid, command):
game = self.all_games[player.gid]
try:
- result = game.execute(cid, command);
+ result, send_to_all = game.execute(cid, command)
except game.INVALID_EXCEPTION, exception:
raise ProtocolError('Invalid game action!')
- self._notify_players(player.gid, result, exclude_cid=cid)
+ if send_to_all:
+ self._notify_players(player.gid, result, exclude_cid=cid)
return result
View
37 game.py
@@ -1,6 +1,8 @@
+
class Piece:
def __init__(self, player):
self._player = player
+ self._id = 'B'
def can_catch(self, piece):
return True
@@ -30,6 +32,8 @@ class BasicGame:
INVALID_EXCEPTION = InvalidAction
+ BOARD_DIMENTIONS = (3, 3)
+
def __init__(self, owner_cid):
self._pieces = {}
self._players = [owner_cid]
@@ -43,6 +47,7 @@ def join(self, player_cid):
# TODO check max game players
# TODO check same player twice
self._players.append(player_cid)
+ print len(self._players), self.TOTAL_PLAYERS, len(self._players) == self.TOTAL_PLAYERS
if len(self._players) == self.TOTAL_PLAYERS:
self.initPlayerPieces()
@@ -93,14 +98,38 @@ def _move(self, cid, *args):
return self.move(initial, final)
-
- COMMANDS = {
- 'move': _move, #is a classmethod, need self as parameter
+ def _board(self, cid):
+ out = ['%s %s' % self.BOARD_DIMENTIONS]
+ for x in xrange(self.BOARD_DIMENTIONS[0]):
+ a_row = []
+ for y in xrange(self.BOARD_DIMENTIONS[1]):
+ pos = x * self.BOARD_DIMENTIONS[1] + y
+ row = 6 * '_'
+ print self._pieces, (x,y), (x,y) in self._pieces
+ if (x,y) in self._pieces:
+ piece = self._pieces[(x,y)]
+ player_id = '%0.3i' % piece._player
+ piece_id = '%3s' % piece._id
+ row = player_id + piece_id
+ a_row.append(row)
+ out.append('|'.join(a_row))
+ return unicode('\n'.join(out))
+
+
+ # COMMANDS and STAUS are classmethods, need self as parameter
+ COMMANDS = { # do actions, send confirmation to all players
+ 'move': _move,
+ }
+ STATUS = { # get information/status about game, sent to requested player
+ 'board': _board,
}
def execute(self, cid, command):
print '[execute]', repr(command)
if command[0] in self.COMMANDS:
- return self.COMMANDS[command[0]](self, cid, *command[1:])
+ return self.COMMANDS[command[0]](self, cid, *command[1:]), True
+ if command[0] in self.STATUS:
+ return self.STATUS[command[0]](self, cid, *command[1:]), False
+
raise InvalidAction()
Please sign in to comment.
Something went wrong with that request. Please try again.