Untitled
raw download clone
PYTHON
views 10
,
size 6592 b
import socket
import os
import json
from _thread import *
import sys
# Silas go fuck yourself

def line_win(board):
    rng = range(3)
    # Check rows
    for row in board:
        chk = set(row)
        if len(chk) == 1 and "0" not in row:
            return row[0]

    for x in rng:
        col = set([board[y][x] for y in rng])
        if ("0" not in col) and len(col) == 1:
            #print("Player {} Wins!".format(board[x][0]))
            return board[x][0]
    return "False"


def diag_win(matrix):
    l_to_r = list(zip(range(3), range(3)))
    r_to_l = list(zip(range(3), range(-1, -4, -1)))
    diags = [l_to_r, r_to_l]
    # print(l_to_r, r_to_l)
    for line in diags:
        nums = []
        for coord in line:
            x, y = coord
            nums.append(matrix[x][y])
        test = set(nums)
        if "0" not in test and len(test) == 1:
            #print("Player {} Wins!".format(nums[0]))
            return nums[0]
    return False

def check_for_even(matrix):
    for i in matrix:
        for i1 in i:
            if(i1 == "0"):
                return False
    return True

def parse_json(jsons,object1):
    x = json.loads(jsons)
    try:
        return x[object1]
    except:
        return "Error"
#converts a string send my the socket server to a matrix 
def convert_to_matrix(string_input):
    uwu = string_input.split("$")
    for i in range(3):
        uwu[i] = uwu[i].split("/")
    return uwu

#writes to the matrix and then returns it duh 
def write_to_matrix(matrix,x,y,setter):
    if matrix[x][y] == "0":
        matrix[x][y] = setter
        return matrix 
    else:
        return matrix 

def convert_to_string(matrix):
    string_output = ""

    for i in matrix:
        for ii in i:
            string_output = string_output + ii + "/"
        string_output = string_output + "$"

    string_output = string_output[:-1]
    strUwU = string_output.split("$")
    strfinal = ""
    for i in range(3):
        tmpstr = strUwU[i]
        strUwU[i] = tmpstr[:-1]
        strfinal = strfinal + strUwU[i] + "$"
        
    strfinal = strfinal[:-1]
    return strfinal

def start_as_server(port):
    ServerSideSocket = socket.socket()
    host = '127.0.0.1'
    ThreadCount = 0
    try:
        ServerSideSocket.bind((host, port))
    except socket.error as e:
        print("Something failed :(")
    print('Socket is listening..')
    ServerSideSocket.listen(5)    
    while True:
        if ThreadCount == 0:
            Client, address = ServerSideSocket.accept()
            print('Connected to: ' + address[0] + ':' + str(address[1]))
            start_new_thread(multi_threaded_client, (Client, ))
            ThreadCount += 1
            print('Thread has reached limit RTW')
        else:
            ServerSideSocket.close()
    ServerSideSocket.close()

def print_matrix(matrix):
    for i in matrix:
        print(i)

def multi_threaded_client(connection):
    matrix = [["0","0","0"],["0","0","0"],["0","0","0"]]
    connection.send(str.encode(convert_to_string(matrix)))
    print("Welcome to the shitshow youre the % your enemy/opposing force is §")
    print_matrix(matrix)
    while True:
        data = connection.recv(2048)
        response = 'Server message: ' + data.decode('utf-8')
        #print(parse_json(data.decode('utf-8'),"head"))
        if(parse_json(data.decode('utf-8'),"head") == "102"):
            x = parse_json(data.decode('utf-8'),"Body").split("/")
            try:
                matrix = write_to_matrix(matrix,int(x[0]),int(x[1]),"§")
            except:
                print("The oposing force has made a fucking mistake")
        if not data:
            break
        #im sleepy 
        print_matrix(matrix)
        if(check_for_even(matrix)):
            print("you even cunt")
            connection.sendall(str.encode('{"head":"'+"300" +'","Body":"You even"}'))
            connection.close()
            break
        if(line_win(matrix) != "False"):
            connection.sendall(str.encode('{"head":"'+"300" +'","Body":"yeah someone won the game: '+line_win(matrix)+'"}'))
            connection.close()
            print("so like someone won: "+line_win(matrix))
            break
        if(diag_win(matrix) != False):
            connection.sendall(str.encode('{"head":"'+"300" +'","Body":"yeah someone won the game: '+diag_win(matrix)+'"}'))
            connection.close()
            print("so like someone won: "+diag_win(matrix))
            break
        Inputy = input('Enter your y thing')
        Inputz = input('Enter your z thing')
        try:
            matrix = write_to_matrix(matrix,int(Inputz),int(Inputy),"%")
        except:
            print("Well that didnt really work did it?")
        connection.sendall(str.encode(convert_to_string(matrix)))
    connection.close()
    sys.exit()

def start_client(port,host):
    ClientMultiSocket = socket.socket()
    print('Waiting for connection response')
    try:
        ClientMultiSocket.connect((host, port))
    except socket.error as e:
        print("lOoKs lIkE YoU FuCkED uP ")

    try: 
        res = ClientMultiSocket.recv(1024)
        #print(res.decode('utf-8'))
        matrix = convert_to_matrix(res.decode('utf-8'))
        print_matrix(matrix)
        while True:
            Inputy = input('Enter your y thing ')
            Inputz = input('Enter your z thing ')
            ClientMultiSocket.send(str.encode('{"head":"'+"102" +'","Body":"'+ Inputz + "/" + Inputy +'"}'))
            res = ClientMultiSocket.recv(1024)
            try:
                if(parse_json(res.decode('utf-8'),"head") == "300"):
                    print(parse_json(res.decode('utf-8'),"Body"))
                    break
            except:
                pass
            matrix = convert_to_matrix(res.decode('utf-8'))
            print_matrix(matrix)
    except ConnectionResetError as e:
        print("Looks like you fucked up ")
    ClientMultiSocket.close()
while True:
    try:
        input1 = input("Hello start as server of client ? ")
        if(input1 == "client"):
            start_client(int(input("Enter port of target ")),input("Enter host "))
        elif(input1 == "server"):
            start_as_server(int(input("Enter your port number make sure its not taken " )))
        else:
            print("Du saft sack")
    except KeyboardInterrupt:
        sys.exit()
    except:
        print("Du saft sack")
close fullscreen
Login or Register to edit or fork this paste. It's free.