commit
72e44e7921
@ -0,0 +1,32 @@
|
||||
import socket
|
||||
|
||||
class OcaAOcaClient:
|
||||
def __init__(self, host, port):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.client_socket.connect((self.host, self.port))
|
||||
|
||||
def receive_message(self):
|
||||
return self.client_socket.recv(1024).decode()
|
||||
|
||||
if __name__ == "__main__":
|
||||
HOST = '192.168.100.252'
|
||||
PORT = 2000
|
||||
|
||||
client = OcaAOcaClient(HOST, PORT)
|
||||
current_turn = False
|
||||
|
||||
while True:
|
||||
while current_turn == False:
|
||||
msg = client.receive_message()
|
||||
if msg == "your_turn":
|
||||
current_turn = True
|
||||
else:
|
||||
print(msg)
|
||||
while current_turn == True:
|
||||
message = input("Type 'roll': ")
|
||||
client.client_socket.sendall(message.encode('utf-8'))
|
||||
if message.lower() == 'roll':
|
||||
client.client_socket.sendall(message.encode('utf-8'))
|
||||
current_turn = False
|
@ -0,0 +1,185 @@
|
||||
import socket
|
||||
import threading
|
||||
import random
|
||||
import time
|
||||
|
||||
class OcaAOcaGame:
|
||||
def __init__(self):
|
||||
self.current_turn = 1
|
||||
self.player1 = None
|
||||
self.player2 = {}
|
||||
self.player1_pos = 1
|
||||
self.player2_pos = 1
|
||||
self.oca_positions = [5, 9, 14, 18, 23, 27, 32, 36, 41, 45, 50, 54, 59]
|
||||
|
||||
# Asigns player1 and player2 to sockets
|
||||
def add_player(self, player):
|
||||
if self.player1 == None:
|
||||
self.player1 = player
|
||||
print("ADDED PLAYER 1")
|
||||
return
|
||||
else:
|
||||
self.player2 = player
|
||||
print("ADDED PLAYER 2")
|
||||
return
|
||||
|
||||
def start_game(self):
|
||||
msg_turn = "your_turn"
|
||||
msg_started = "The game has started!"
|
||||
msg_player1_oca = "Player 1 has landed on an Oca!"
|
||||
msg_player1_win = "Player 1 is the Winner!"
|
||||
msg_player2_oca = "Player 2 has landed on an Oca!"
|
||||
msg_player2_win = "Player 2 is the Winner!"
|
||||
self.player1[0].sendall(msg_started.encode('utf-8'))
|
||||
self.player2[0].sendall(msg_started.encode('utf-8'))
|
||||
# Used to prevent the game from locking up
|
||||
sleep_time = 1
|
||||
|
||||
while True:
|
||||
# Determine player turn
|
||||
if self.current_turn % 2 != 0:
|
||||
|
||||
# Player 1 turn
|
||||
time.sleep(sleep_time)
|
||||
self.player1[0].sendall(msg_turn.encode('utf-8'))
|
||||
time.sleep(sleep_time)
|
||||
player1_turn = True
|
||||
while player1_turn == True:
|
||||
|
||||
# Player rolls dice
|
||||
data = self.player1[0].recv(1024)
|
||||
print(data.decode('utf-8'))
|
||||
if data.decode('utf-8') == "roll":
|
||||
roll = random.randint(1, 6)
|
||||
print(f"Player 1 Roll: {roll}")
|
||||
self.player1_pos += roll
|
||||
|
||||
# Check position of player, if lands on oca -> go to next oca
|
||||
if self.player1_pos != self.check_next_oca(self.player1_pos):
|
||||
time.sleep(sleep_time)
|
||||
self.player1[0].sendall(msg_player1_oca.encode('utf-8'))
|
||||
self.player2[0].sendall(msg_player1_oca.encode('utf-8'))
|
||||
time.sleep(sleep_time)
|
||||
self.player1_pos = self.check_next_oca(self.player1_pos)
|
||||
|
||||
# Checks if player is over 63, if so -> return position after going back the required spaces
|
||||
self.player1_pos = self.check_if_over_63(self.player1_pos)
|
||||
|
||||
# Checks position of player, if lands of 63 -> win
|
||||
if self.player1_pos == 63:
|
||||
time.sleep(sleep_time)
|
||||
self.player1[0].sendall(msg_player1_win.encode('utf-8'))
|
||||
self.player2[0].sendall(msg_player1_win.encode('utf-8'))
|
||||
time.sleep(sleep_time)
|
||||
|
||||
# Updates player position message
|
||||
msg_player1_pos = "Player 1 Position: " + str(self.player1_pos)
|
||||
print(msg_player1_pos)
|
||||
time.sleep(sleep_time)
|
||||
self.player1[0].sendall(msg_player1_pos.encode('utf-8'))
|
||||
self.player2[0].sendall(msg_player1_pos.encode('utf-8'))
|
||||
time.sleep(sleep_time)
|
||||
|
||||
self.current_turn += 1
|
||||
player1_turn = False
|
||||
else:
|
||||
continue
|
||||
else:
|
||||
|
||||
# Player 2 turn
|
||||
time.sleep(sleep_time)
|
||||
self.player2[0].sendall(msg_turn.encode('utf-8'))
|
||||
time.sleep(sleep_time)
|
||||
player2_turn = True
|
||||
while player2_turn == True:
|
||||
|
||||
# Player rolls dice
|
||||
data = self.player2[0].recv(1024)
|
||||
print(data.decode('utf-8'))
|
||||
if data.decode('utf-8') == "roll":
|
||||
roll = random.randint(1, 6)
|
||||
print(f"Player 2 Roll: {roll}")
|
||||
self.player2_pos += roll
|
||||
|
||||
# Check position of player, if lands on oca -> go to next oca
|
||||
if self.player2_pos != self.check_next_oca(self.player2_pos):
|
||||
time.sleep(sleep_time)
|
||||
self.player1[0].sendall(msg_player2_oca.encode('utf-8'))
|
||||
self.player2[0].sendall(msg_player2_oca.encode('utf-8'))
|
||||
time.sleep(sleep_time)
|
||||
self.player2_pos = self.check_next_oca(self.player2_pos)
|
||||
|
||||
# Checks if player is over 63, if so -> return position after going back the required spaces
|
||||
self.player2_pos = self.check_if_over_63(self.player2_pos)
|
||||
|
||||
# Checks position of player, if lands of 63 -> win
|
||||
if self.player2_pos == 63:
|
||||
time.sleep(sleep_time)
|
||||
self.player1[0].sendall(msg_player2_win.encode('utf-8'))
|
||||
self.player2[0].sendall(msg_player2_win.encode('utf-8'))
|
||||
time.sleep(sleep_time)
|
||||
|
||||
# Updates player position message
|
||||
msg_player2_pos = "Player 2 Position: " + str(self.player2_pos)
|
||||
print(msg_player2_pos)
|
||||
time.sleep(sleep_time)
|
||||
self.player1[0].sendall(msg_player2_pos.encode('utf-8'))
|
||||
self.player2[0].sendall(msg_player2_pos.encode('utf-8'))
|
||||
time.sleep(sleep_time)
|
||||
|
||||
self.current_turn += 1
|
||||
player2_turn = False
|
||||
else:
|
||||
continue
|
||||
|
||||
# Check what oca is the next one in the array
|
||||
def check_next_oca(self, pos):
|
||||
if pos in self.oca_positions:
|
||||
index = self.oca_positions.index(pos)
|
||||
if index < len(self.oca_positions) - 1:
|
||||
return self.oca_positions[index + 1]
|
||||
else:
|
||||
return pos
|
||||
|
||||
def check_if_over_63(self, pos):
|
||||
if pos >= 63:
|
||||
# Returns the pos the player should be after getting more than the max
|
||||
return 63 - (pos - 63)
|
||||
else:
|
||||
return pos
|
||||
|
||||
class OcaAOcaServer:
|
||||
def __init__(self, host, port):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.game = OcaAOcaGame()
|
||||
self.clients = []
|
||||
self.player1 = {}
|
||||
self.player2 = {}
|
||||
|
||||
# Waits for players to join and starts the game when 2 have joined
|
||||
def start(self):
|
||||
self.server_socket.bind((self.host, self.port))
|
||||
self.server_socket.listen(2)
|
||||
print(f"Server is listening on {self.host}:{self.port}")
|
||||
|
||||
self.player1 = self.server_socket.accept()
|
||||
self.game.add_player(self.player1)
|
||||
print(f"Connection from {self.player1} has been established for Player 1.")
|
||||
|
||||
self.player2 = self.server_socket.accept()
|
||||
self.game.add_player(self.player2)
|
||||
print(f"Connection from {self.player2} has been established for Player 2.")
|
||||
|
||||
self.game.start_game()
|
||||
|
||||
if __name__ == "__main__":
|
||||
HOST = '192.168.100.252'
|
||||
PORT = 2000
|
||||
|
||||
server = OcaAOcaServer(HOST, PORT)
|
||||
server.start()
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue