Skip to content

Commit 1846ac0

Browse files
committed
added client functions
1 parent 8a96e94 commit 1846ac0

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

client.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ def get_version(self):
4141
self.server_conn.sendall(TicTacToe.GET_VERSION_PKT)
4242
return self.read_response()
4343

44+
#caller guarantees turn is a single char, X or O
45+
def place(self, turn):
46+
user_coords = input("Please enter the coordinates you want to mark, eg: 0 0").split()
47+
while len(user_coords) != 2:
48+
input = ("You made me almost seg fault! Give me two coordinates!")
49+
50+
packet = turn + user_coords[0] + user_coords[1]
51+
self.server_conn.send(packet) #send which player, and coordinates
52+
53+
#expects a response that is a 9 byte string representing the board
54+
def read_board(self):
55+
response = self.read_response(self)
56+
57+
#print the board
58+
print(' 0 1 2')
59+
print('0 %s | %s | %s', response[0], response[1], response[2])
60+
print(' ----------------')
61+
print('1 %s | %s | %s', response[4], response[5], response[6])
62+
print(' ----------------')
63+
print('2 %s | %s | %s', response[7], response[8], response[9])
64+
65+
#expects 4 bytes repr. winner
66+
def get_winner(self):
67+
response = self.read_response(self)
68+
69+
70+
4471
def play(connect_tuple):
4572
game = TicTacToe(connect_tuple)
4673
print("connecting")
@@ -49,7 +76,6 @@ def play(connect_tuple):
4976
print(game.get_version())
5077

5178

52-
5379
def main():
5480
if len(sys.argv) != 3:
5581
print("Need <tictactoe host ip> <tictactoe host port>")

tictactoe.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ enum Player board[__BOARD_SIZE__][__BOARD_SIZE__] = { //(0, 0) top left, (2, 2)
1919
{E, E, E}
2020
};
2121

22+
//expects 3 * 4 bytes = 12
2223
bool handle_place(int client_fd, char* client_str) {
2324
enum Player player; //first four bytes of packet are the player
2425
while ( (player = get_int_from_client(client_fd)) == -1) { }
@@ -43,13 +44,14 @@ bool handle_place(int client_fd, char* client_str) {
4344
// return 0;
4445
// }
4546

46-
// sends the board as a 36-byte (9 ints) char*
47+
// sends the board as a 9-byte char*
4748
bool handle_read_board(int client_fd, char* client_str) {
4849
char board_pkt[9];
4950
int i = 0;
5051
for (int x = 0; x < __BOARD_SIZE__; x++) {
5152
for (int y = 0; y < __BOARD_SIZE__; y++) {
52-
board_pkt[i] = board[x][y];
53+
char player = 'X' ? board[x][y] == X : 'Y' ? board_pkt[x][y] == Y : 'E';
54+
board_pkt[i] = player;
5355
i++;
5456
}
5557
}
@@ -60,7 +62,7 @@ bool handle_read_board(int client_fd, char* client_str) {
6062
}
6163

6264

63-
65+
//sends 4 bytes
6466
bool handle_get_winner(int client_fd, char* client_str) {
6567
//implement a more effecient check if needed
6668

0 commit comments

Comments
 (0)