License and README add
This commit is contained in:
parent
1c0af6ae85
commit
68932093f7
4 changed files with 95 additions and 14 deletions
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -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.
|
66
Morpion.py
66
Morpion.py
|
@ -1,7 +1,10 @@
|
||||||
|
from Socket import Socket
|
||||||
|
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
"""self, number (int), pawn (str), name (str)
|
"""self, number (int), pawn (str), name (str)
|
||||||
A player of the party"""
|
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)
|
"""self, number (int), pawn (str), name (str)
|
||||||
A player of the party"""
|
A player of the party"""
|
||||||
self.number = number # The number of the player
|
self.number = number # The number of the player
|
||||||
|
@ -9,6 +12,7 @@ class Player:
|
||||||
self.name = name # The name of the player
|
self.name = name # The name of the player
|
||||||
self.pawns = list() # All the pawns on the grid of the player
|
self.pawns = list() # All the pawns on the grid of the player
|
||||||
self.points = 0 # The points of the player
|
self.points = 0 # The points of the player
|
||||||
|
self.connexion = connexion
|
||||||
|
|
||||||
def add_pawn(self, enemy: list, pos: int):
|
def add_pawn(self, enemy: list, pos: int):
|
||||||
"""self, enemy (list), pos (int)
|
"""self, enemy (list), pos (int)
|
||||||
|
@ -67,7 +71,7 @@ class Player:
|
||||||
class Board:
|
class Board:
|
||||||
"""self, p1_pawn (str), p1_name (str), p2_pawn (str), p2_name (str)
|
"""self, p1_pawn (str), p1_name (str), p2_pawn (str), p2_name (str)
|
||||||
A party object"""
|
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)
|
"""self, p1_pawn (str), p1_name (str), p2_pawn (str), p2_name (str)
|
||||||
A party object"""
|
A party object"""
|
||||||
# Security if conflict with players pawns and name
|
# Security if conflict with players pawns and name
|
||||||
|
@ -76,19 +80,18 @@ class Board:
|
||||||
elif p1_name == p2_name:
|
elif p1_name == p2_name:
|
||||||
raise ValueError("The name are the same !")
|
raise ValueError("The name are the same !")
|
||||||
|
|
||||||
self.player1 = Player(1, p1_pawn, p1_name) # Set player 1
|
self.players = [Player(1, p1_pawn, p1_name), Player(2, p2_pawn, p2_name)] # Set players
|
||||||
self.player2 = Player(2, p2_pawn, p2_name) # Set player 2
|
self.curr_turn = self.players # Set turn order
|
||||||
self.curr_turn = [self.player1, self.player2] # Set turn order
|
|
||||||
|
|
||||||
def check(self):
|
def check(self):
|
||||||
"""self
|
"""self
|
||||||
Check if someone win the game"""
|
Check if someone win the game"""
|
||||||
if (len(self.player1) + len(self.player2) == 9)\
|
if (len(self.players[0]) + len(self.players[1]) == 9)\
|
||||||
and not(self.player1.check() or self.player2.check()): # Check equality
|
and not(self.players[0].check() or self.players[1].check()): # Check equality
|
||||||
return 3
|
return 3
|
||||||
elif self.player1.check(): # Check player 1
|
elif self.players[0].check(): # Check player 1
|
||||||
return 1
|
return 1
|
||||||
elif self.player2.check(): # Check player 2
|
elif self.players[1].check(): # Check player 2
|
||||||
return 2
|
return 2
|
||||||
else: # If nobody win
|
else: # If nobody win
|
||||||
return 0
|
return 0
|
||||||
|
@ -114,9 +117,48 @@ class Board:
|
||||||
for i in list(p):
|
for i in list(p):
|
||||||
pawns[i] = p.pawn
|
pawns[i] = p.pawn
|
||||||
# Format and print the grid from the dict
|
# 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[0]},{pawns[1]},{pawns[2]}]
|
||||||
[{pawns[3]},{pawns[4]},{pawns[5]}]
|
[{pawns[3]},{pawns[4]},{pawns[5]}]
|
||||||
[{pawns[6]},{pawns[7]},{pawns[8]}]
|
[{pawns[6]},{pawns[7]},{pawns[8]}]
|
||||||
{self.player1}: {self.player1.points}/{self.player2}: {self.player2.points}"""
|
{self.players[0]}: {self.players[0].points}/{self.players[1]}: {self.players[1].points}"""
|
||||||
print(board)
|
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")
|
||||||
|
|
18
README.md
Normal file
18
README.md
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<h1>Morpion</h1>
|
||||||
|
|
||||||
|
A Moprion make with Python
|
||||||
|
|
||||||
|
|
||||||
|
<h2>How to use it ?</h2>
|
||||||
|
|
||||||
|
Look at the [wiki](https://github.com/flifloo/Morpion/wiki) ;3
|
||||||
|
|
||||||
|
|
||||||
|
<h2>License</h2>
|
||||||
|
|
||||||
|
This project is on the [MIT license](https://github.com/flifloo/Morpion/blob/master/LICENSE)
|
||||||
|
|
||||||
|
|
||||||
|
<h2>Dependencies</h2>
|
||||||
|
|
||||||
|
* [SecureSocketService](https://github.com/flifloo/SecureSocketService)
|
4
gui.py
4
gui.py
|
@ -25,7 +25,7 @@ def result(r: int):
|
||||||
showwarning("Turn end", text) # Announce the result
|
showwarning("Turn end", text) # Announce the result
|
||||||
for b in range(9): # Reset all the buttons
|
for b in range(9): # Reset all the buttons
|
||||||
buttons[b].config(state="normal", text=DEFAULT_BUTTON)
|
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):
|
def case(posi: int):
|
||||||
|
@ -93,7 +93,7 @@ f.pack()
|
||||||
turn = StringVar()
|
turn = StringVar()
|
||||||
scoreboard = StringVar()
|
scoreboard = StringVar()
|
||||||
turn.set(f"{board.curr_turn[0]} turn !")
|
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=turn).pack()
|
||||||
Label(windows, textvariable=scoreboard).pack()
|
Label(windows, textvariable=scoreboard).pack()
|
||||||
|
|
Loading…
Reference in a new issue