-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_in_one.py
131 lines (113 loc) · 4.36 KB
/
all_in_one.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#Ranush Mithila
from http import client
import socket
import threading
class TicTacToe:
def __init__(self):
self.board = [[" ", " ", " "],[" ", " ", " "],[" ", " ", " "]]
self.turn = "X"
self.you = "X"
self.opponent = "O"
self.winner = None
self.gameover = False
self.counter = 0
def host_game(self, host, port):
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((host,port))
server.listen(1)
client, addr = server.accept()
self.you = "X"
self.opponent = "O"
threading.Thread(target=self.handle_connection, args=(client,)).start()
server.close()
def connect_to_game(self, host, port):
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host,port))
self.you = "O"
self.opponent = "X"
threading.Thread(target=self.handle_connection, args=(client,)).start()
def convert_to_array_num(self,move):
#move convert to real numbers
move = move.split(',')
for mv in range(2):
if mv == 0:
realMove = str(int(move[mv]) - 1) + ","
else:
realMove += str(int(move[mv]) - 1)
move = realMove
return move
def handle_connection(self, client):
while not self.gameover:
if self.turn == self.you:
move = input("Enter move (row,col) : ")
move = self.convert_to_array_num(move)
if self.check_valid_move(move.split(',')):
client.send(move.encode('utf-8'))
self.apply_move(move.split(','), self.you)
self.turn = self.opponent
else:
print("Invalid move!!")
else:
data = client.recv(1024)
if not data:
break
else:
self.apply_move(data.decode('utf-8').split(','), self.opponent)
self.turn = self.you
client.close()
def apply_move(self, move, player):
if self.gameover:
return
self.counter += 1
self.board[int(move[0])][int(move[1])] = player
self.print_board()
if self.check_if_won():
if self.winner == self.you:
print("you win!")
input("Press any key to exit: ")
exit()
elif self.winner == self.opponent:
print("You loose!")
input("Press any key to exit: ")
exit()
else:
if self.counter >= 9:
print("It is a tie")
exit()
def check_valid_move(self, move):
return self.board[int(move[0])][int(move[1])] == " "
def check_if_won(self):
for row in range(3):
if self.board[row][0] == self.board[row][1] == self.board[row][2] != " ":
self.winner = self.board[row][0]
self.gameover = True
return True
for col in range(3):
if self.board[0][col] == self.board[1][col] == self.board[2][col] != " ":
self.winner = self.board[0][col]
self.gameover = True
return True
if self.board[0][0] == self.board[1][1] == self.board[2][2] != " ":
self.winner = self.board[0][0]
self.gameover = True
return True
if self.board[0][2] == self.board[1][1] == self.board[2][0] != " ":
self.winner = self.board[0][2]
self.gameover = True
return True
return False
def print_board(self):
for row in range(3):
print(" | ".join(self.board[row]))
if row != 2:
print("------------")
def choose_type(self):
gameType = input("Do you want to host (y/n) : ")
ip = input("Enter IP (example: 192.168.1.2) : ")
port = int(input("Enter port (example: 1418) : "))
if gameType == "y" or gameType == "Y":
game.host_game(ip, port)
elif gameType == "n" or gameType == "N":
game.connect_to_game(ip, port)
game = TicTacToe()
game.choose_type()