jhylands

Server command line

Aug 20th, 2014
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.47 KB | None | 0 0
  1. '''
  2. Created on 7 Aug 2014
  3.  
  4. @author: Lilian
  5. '''
  6. #!/usr/bin/python # This is server.py file
  7. import socket # Import socket module
  8. import time
  9.  
  10.  
  11. import const
  12. #from battleships_gui import BattleshipsGraphics
  13. from player_socket import PlayerSocket
  14.  
  15.  
  16. ##########################################
  17. ##           PARAMETERS SETTING         ##
  18. ##########################################
  19.  
  20. sizeFleet = 21 # the number of square covered by the fleet
  21.  
  22. # Check whether the fleet is sunk
  23. def checkWinner(board):
  24.     # We just need to test whether the number of hits equals the total number of squares in the fleet
  25.     hits = 0
  26.     for i in range(12):
  27.         hits += board[i].count(4)
  28.     return hits==sizeFleet
  29.  
  30. def giveOutcome(player_board, i1, i2):
  31.     """
  32.    Check the board to see if the move is a hit or a miss.
  33.    Return the value const.HIT or const.MISSED
  34.    """
  35.     if ((player_board[i1][i2]==const.OCCUPIED)
  36.         or (player_board[i1][i2]==const.HIT)):
  37.         # They may (stupidly) hit the same square twice so we check for occupied or hit
  38.         player_board[i1][i2]=const.HIT
  39.         result =const.HIT
  40.     else:
  41.         result = const.MISSED
  42.     return result
  43.  
  44.  
  45. def playMatch(firstPlayer, secondPlayer, rounds):
  46.     """
  47.    function handling a match between two players. Each match may be composed of several games/rounds.
  48.    @param <BattleshipsGraphics> gui, the graphic interface displaying the match.
  49.    @param <PlayerSocket> firstPlayer, secondPlayer: The player objects
  50.    """
  51.     firstPlayer.newPlayer(secondPlayer.getName())
  52.     secondPlayer.newPlayer(firstPlayer.getName())
  53.     scorePlayer1 = scorePlayer2 = 0
  54.     for game in range(rounds):
  55.         firstPlayer.newRound()
  56.         secondPlayer.newRound()
  57.         #gui.turtle.clear()
  58.         #gui.drawBoards()
  59.         #gui.drawPlayer(firstPlayer.getName(), firstPlayer.getDescription(), 'left')
  60.         #gui.drawPlayer(secondPlayer.getName(), secondPlayer.getDescription(), 'right')
  61.         #gui.drawScore (scorePlayer1, scorePlayer2)
  62.         turn = (-1)**game
  63.  
  64.  
  65.         p1, p2 = playGame(firstPlayer, secondPlayer, turn)
  66.          
  67.         scorePlayer1 += p1
  68.         scorePlayer2 += p2
  69.         #gui.drawScore (scorePlayer1, scorePlayer2)
  70.    
  71.     if scorePlayer2 > scorePlayer1 :
  72.     print 'right is the winner'
  73.         #gui.drawWinner('right')
  74.     elif scorePlayer2 == scorePlayer1:
  75.         pass
  76.     else:
  77.     print 'Left is the winner'
  78.         #gui.drawWinner('left')
  79.    
  80.     print "---------------- ",firstPlayer.getName(), scorePlayer1,"-",
  81.     print scorePlayer2, secondPlayer.getName(), "----------------"
  82.     return (scorePlayer1, scorePlayer2)
  83.  
  84. def playGame( firstPlayer, secondPlayer, turn):
  85.     """
  86.    function handling a game between two players (e.g. one single round).
  87.    @param <BattleshipsGraphics> gui, the graphic interface displaying a round.
  88.    @param <PlayerSocket> firstPlayer, secondPlayer: The player objects.
  89.    @param <int> turn: parameter indicating who is starting the game.
  90.    if turn == 1 then firstPlayer starts, else if turn == -1 then secondPlayer starts.
  91.    """
  92.    
  93.     # Distribute the fleet onto each player board
  94.     player1_board = firstPlayer.deployFleet()
  95.  
  96.     for row in range(len(player1_board)):
  97.         for col in range(len(player1_board[row])):
  98.             if player1_board[row][col] == const.OCCUPIED:
  99.         print 'right boats'
  100.                 #gui.drawBoat('right', row, col)
  101.            
  102.     player2_board = secondPlayer.deployFleet()
  103.  
  104.     for row in range(len(player2_board)):
  105.         for col in range(len(player2_board[row])):
  106.             if player2_board[row][col] == const.OCCUPIED:
  107.                 #gui.drawBoat('left', row, col)
  108.         print 'left boats'
  109.    
  110.    
  111.  
  112.     haveWinner = False
  113.     shots = 1
  114.     while not haveWinner:
  115.         if turn > 0:
  116.             i1,i2 = firstPlayer.chooseMove()
  117.             outcome = giveOutcome(player2_board, i1, i2)
  118.             if outcome == const.HIT:
  119.         print 'left hit at' + str(i1) + ':' + str(i2)
  120.                 #gui.drawHit('left', i1, i2)
  121.             else:
  122.         print 'left miss at' + str(i1) + ':' + str(i2)
  123.                 #gui.drawMiss('left', i1, i2)
  124.                  
  125.             firstPlayer.setOutcome(outcome, i1, i2)
  126.             secondPlayer.getOpponentMove(i1, i2)
  127.  
  128.             turn *= -1
  129.             shots += 1
  130.             haveWinner = checkWinner(player2_board)
  131.  
  132.         else:
  133.             i1,i2 = secondPlayer.chooseMove()
  134.             outcome = giveOutcome(player1_board, i1, i2)
  135.             if outcome == const.HIT:
  136.         print 'right hit at' + str(i1) + ':' + str(i2)
  137.                 #gui.drawHit('right', i1, i2)
  138.             else:
  139.         print 'right miss at' + str(i1) + ':' + str(i2)
  140.                 #gui.drawMiss('right', i1, i2)
  141.                
  142.             secondPlayer.setOutcome(outcome, i1, i2)
  143.             firstPlayer.getOpponentMove(i1, i2)
  144.        
  145.             turn *= -1
  146.             haveWinner = checkWinner(player1_board)
  147.              
  148.          
  149.     winner = "Player 1"
  150.     if turn > 0 :
  151.         winner = "Player 2"
  152.         result = (0,1)
  153.     else:
  154.         result = (1,0)
  155.        
  156.     time.sleep(2)
  157.  
  158.     print "---------------- " + winner + "is the winner in " + str(shots) + "shots ----------------"
  159.    
  160.     return result
  161.  
  162.  
  163.  
  164. # Main
  165.  
  166. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    # Create a socket object
  167. sock.bind((const.GAME_SERVER_ADDR, const.GAME_SERVER_PORT)) # The same port as used by the server & clients
  168. sock.listen(2)                                              # Now wait for client connection (max of 2 client at a time).
  169.  
  170. while True:
  171.     client1, addr1 = sock.accept()                          # Establish connection with first client.
  172.     print 'Got connection from', addr1
  173.     player1 = PlayerSocket(client1, addr1)                  # Create first player with that connection
  174.     player1.acknowledgeConnection()
  175.     print "player",player1.getName(),"is connected..."    
  176.  
  177.     client2, addr2 = sock.accept()                          # Establish connection with second client.
  178.     print 'Got connection from', addr2
  179.     player2 = PlayerSocket(client2, addr2)                  # Create second player with second connection
  180.     player2.acknowledgeConnection()
  181.     print "player",player2.getName(),"is connected..."
  182.  
  183.     #gui = BattleshipsGraphics(const.GRID_SIZE)
  184.     playMatch(player1, player2, const.ROUNDS)
  185.  
  186.     player1.close()                                         # Close the connection
  187.     player2.close()                                         # Close the connection
  188.    
  189.     break                                                   # End of game, exit game loop
  190.  
  191.  
  192. ## Must be the last line of code
  193. #gui.screen.exitonclick()
Advertisement
Add Comment
Please, Sign In to add comment