diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5288987 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 flifloo + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/Morpion.py b/Morpion.py index 872d76b..585f432 100644 --- a/Morpion.py +++ b/Morpion.py @@ -1,7 +1,10 @@ +from Socket import Socket + + class Player: """self, number (int), pawn (str), name (str) A player of the party""" - def __init__(self, number: int, pawn: str, name: str): + def __init__(self, number: int, pawn: str, name: str, connexion=None): """self, number (int), pawn (str), name (str) A player of the party""" self.number = number # The number of the player @@ -9,6 +12,7 @@ class Player: self.name = name # The name of the player self.pawns = list() # All the pawns on the grid of the player self.points = 0 # The points of the player + self.connexion = connexion def add_pawn(self, enemy: list, pos: int): """self, enemy (list), pos (int) @@ -67,7 +71,7 @@ class Player: class Board: """self, p1_pawn (str), p1_name (str), p2_pawn (str), p2_name (str) A party object""" - def __init__(self, p1_pawn: str, p1_name : str, p2_pawn: str, p2_name: str): + def __init__(self, p1_pawn: str = "O", p1_name: str = "Player1", p2_pawn: str = "X", p2_name: str = "Player2"): """self, p1_pawn (str), p1_name (str), p2_pawn (str), p2_name (str) A party object""" # Security if conflict with players pawns and name @@ -76,19 +80,18 @@ class Board: elif p1_name == p2_name: raise ValueError("The name are the same !") - self.player1 = Player(1, p1_pawn, p1_name) # Set player 1 - self.player2 = Player(2, p2_pawn, p2_name) # Set player 2 - self.curr_turn = [self.player1, self.player2] # Set turn order + self.players = [Player(1, p1_pawn, p1_name), Player(2, p2_pawn, p2_name)] # Set players + self.curr_turn = self.players # Set turn order def check(self): """self Check if someone win the game""" - if (len(self.player1) + len(self.player2) == 9)\ - and not(self.player1.check() or self.player2.check()): # Check equality + if (len(self.players[0]) + len(self.players[1]) == 9)\ + and not(self.players[0].check() or self.players[1].check()): # Check equality return 3 - elif self.player1.check(): # Check player 1 + elif self.players[0].check(): # Check player 1 return 1 - elif self.player2.check(): # Check player 2 + elif self.players[1].check(): # Check player 2 return 2 else: # If nobody win return 0 @@ -114,9 +117,48 @@ class Board: for i in list(p): pawns[i] = p.pawn # Format and print the grid from the dict - board = f"""P1: {self.player1.points} | P2: {self.player2.points} + board = f"""P1: {self.players[0].points} | P2: {self.players[1].points} [{pawns[0]},{pawns[1]},{pawns[2]}] [{pawns[3]},{pawns[4]},{pawns[5]}] [{pawns[6]},{pawns[7]},{pawns[8]}] -{self.player1}: {self.player1.points}/{self.player2}: {self.player2.points}""" - print(board) \ No newline at end of file +{self.players[0]}: {self.players[0].points}/{self.players[1]}: {self.players[1].points}""" + print(board) + + +class Server(Socket): + def __init__(self, host: str = "localhost", port: int = 3621): + super().__init__() + self.socket.bind((host, port)) + self.socket.listen(2) + self.board = Board() + + for p in self.board.players: + self.connexion(p) + + def connexion(self, p): + while True: + print("Await for a player connexion") + p.connexion = self.connect_client(self.socket, 1) + print("Got a connexion, wait or check") + + self.send(p.connexion, "pawn") + p.paws = self.receive(p.connexion) + self.send(p.connexion, "name") + p.name = self.receive(p.connexion) + print(f"Player {p.name} online") + return True + + +class Client(Socket): + def __init__(self, host: str, port: int, pawn: str, name: str): + super().__init__() + self.connect_server(self.socket, host, port, 1) + + if self.receive(self.socket) == "pawn": + self.send(self.socket, pawn) + else: + raise ConnectionError("Bad pawn demand") + if self.receive(self.socket) == "name": + self.send(self.socket, name) + else: + raise ConnectionError("Bad name demand") diff --git a/README.md b/README.md new file mode 100644 index 0000000..086dc4d --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +

Morpion

+ +A Moprion make with Python + + +

How to use it ?

+ +Look at the [wiki](https://github.com/flifloo/Morpion/wiki) ;3 + + +

License

+ +This project is on the [MIT license](https://github.com/flifloo/Morpion/blob/master/LICENSE) + + +

Dependencies

+ +* [SecureSocketService](https://github.com/flifloo/SecureSocketService) diff --git a/gui.py b/gui.py index 9ae9785..97ce040 100644 --- a/gui.py +++ b/gui.py @@ -25,7 +25,7 @@ def result(r: int): showwarning("Turn end", text) # Announce the result for b in range(9): # Reset all the buttons buttons[b].config(state="normal", text=DEFAULT_BUTTON) - scoreboard.set(f"{board.player1}: {board.player1.points}/{board.player2}: {board.player2.points}") # Scoreboard + scoreboard.set(f"{board.players[0]}: {board.players[0].points}/{board.players[1]}: {board.players[1].points}") # Scoreboard def case(posi: int): @@ -93,7 +93,7 @@ f.pack() turn = StringVar() scoreboard = StringVar() turn.set(f"{board.curr_turn[0]} turn !") -scoreboard.set(f"{board.player1}: {board.player1.points}/{board.player2}: {board.player2.points}") +scoreboard.set(f"{board.players[0]}: {board.players[0].points}/{board.players[1]}: {board.players[1].points}") Label(windows, textvariable=turn).pack() Label(windows, textvariable=scoreboard).pack()