Skip to content

Commit 89ac7cf

Browse files
committed
working client/server gameplay
1 parent 548f35d commit 89ac7cf

File tree

4 files changed

+65
-40
lines changed

4 files changed

+65
-40
lines changed

Diff for: client.py

+27-21
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ def __init__(self, connect_tuple, debug = True):
4242
def read_response(self):
4343
resp_len = self.server_conn.recv(4)
4444
if len(resp_len) < 4:
45-
client_error_wrapper("unable to read resposne length")
46-
raise Exception("need 4 bytes in length resposne, you have " + str(len(resp_len)))
45+
client_error_wrapper("unable to read response length")
46+
raise Exception("need 4 bytes in length response, you have " + str(len(resp_len)))
4747
elif len(resp_len) > 4:
48-
client_error_wrapper("unable to read resposne length")
49-
raise Exception("need 4 bytes in length resposne, you have " + str(len(resp_len)))
48+
client_error_wrapper("unable to read response length")
49+
raise Exception("need 4 bytes in length response, you have " + str(len(resp_len)))
5050
resp_len = int.from_bytes(resp_len, NET_ORDER)
5151
if self.debug:
52-
print("getting msg resposne len:", resp_len)
52+
print("getting msg response len:", resp_len)
5353

5454
resp_data = self.server_conn.recv(resp_len)
5555
return resp_data
@@ -68,30 +68,36 @@ def get_version(self):
6868

6969
#caller guarantees turn is a single char, X or O
7070
def place(self, turn):
71-
user_coords = input("Please enter the coordinates you want to mark, eg: 0 0").split()
71+
user_coords = input("Please enter the coordinates you want to mark in x-y order, eg: 0 0\n>").split(' ')
7272
while len(user_coords) != 2:
73-
input = ("You made me almost seg fault! Give me two coordinates!")
73+
user_coords = input("Watch it! you made me almost seg fault! Give me two coordinates!").split(' ')
7474

7575
turn_enum = 1
7676
if turn == 'O':
7777
turn_enum = 2
78-
packet = int.to_bytes(turn_enum, 4, NET_ORDER) + user_coords[0].encode("utf-8") + user_coords[1].encode("utf-8")
78+
79+
packet = int.to_bytes(turn_enum, 4, NET_ORDER) + int.to_bytes(int(user_coords[0]), 4, NET_ORDER) + int.to_bytes(int(user_coords[1]), 4, NET_ORDER)
7980
self.server_conn.sendall(int.to_bytes(7, 4, NET_ORDER))
8081
self.server_conn.sendall(packet) #send which player, and coordinates
8182

83+
8284
#expects a response that is a 9 byte string representing the board
8385
def read_board(self):
8486
self.server_conn.sendall(int.to_bytes(8, 4, NET_ORDER))
8587
response = self.read_response()
86-
print(response)
8788

8889
#print the board //TODO convert ascii code from stuff
89-
print(' 0 1 2')
90-
print(f'0 {response[0]} | {response[1]} | {response[2]}')
91-
print(' --------------')
92-
print(f'1 {response[3]} | {response[4]} | {response[5]}')
93-
print(' --------------')
94-
print(f'2 {response[6]} | {response[7]} | {response[8]}')
90+
print()
91+
print()
92+
print(' 0 1 2')
93+
print()
94+
print(f'0 {chr(response[0])} | {chr(response[1])} | {chr(response[2])}')
95+
print(' ---------------')
96+
print(f'1 {chr(response[3])} | {chr(response[4])} | {chr(response[5])}')
97+
print(' ---------------')
98+
print(f'2 {chr(response[6])} | {chr(response[7])} | {chr(response[8])}')
99+
print()
100+
print()
95101

96102
#expects 4 bytes repr. winner, 0 = no one, 1 = X, 2 = O
97103
def get_winner(self):
@@ -109,20 +115,20 @@ def play(connect_tuple):
109115
print("WELCOME TO ZERO TICTACTOE. 100% VULNERABILITY-FREE CODE GUARANTEED")
110116
game.read_board()
111117

112-
time.sleep(1)
113118
#gameplay
114119
turn = 'X'
115120
while game.get_winner() == 0:
116-
print('Player %s\'s')
117-
game.place(game, turn)
121+
print('Player %s\'s turn' % turn)
122+
game.place(turn)
118123
print('Good move. I think...')
119-
time.sleep(1)
124+
print('-'*20)
120125
game.read_board()
121-
turn = turn == 'X' if turn == 'Y' else 'Y'
126+
turn = 'X' if turn == 'O' else 'O'
122127

123128
#game ended
124129
name = random.choice(names) # fingers crossed
125-
print('CONGRATS %s YOU WIN!' % name)
130+
print('Congrats %s, YOU WIN!' % name)
131+
print('hope I guessed that right :^)')
126132

127133

128134
def main():

Diff for: helper.c

+6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66

77
#include "helper.h"
88
#include "globals.h"
9+
#include "tictactoe.h"
910

1011
bool handle_get_version(int client_fd, char* client_str) {
1112
char version_str[0x100] = {0};
1213
sprintf(version_str, "Stanford Zero TicTacToe vesion %s", TICTACTOE_VERSION_STR);
1314
respond_str_to_client(client_fd, version_str);
15+
16+
//reset the board here in case a previous game was cut off before winning
17+
18+
reset_board();
19+
1420
return true;
1521
}
1622

Diff for: tictactoe.c

+29-18
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,40 @@
1010
#include "high_score.h"
1111
#include "helper.h"
1212

13-
//TODO make sure each game has their own board, client_fd etc
14-
//this is just a visualization of what it'd look like for each player
15-
13+
// from tictactoe.h:
14+
// enum Player {
15+
// E, //E means empty and happens to be null too
16+
// X,
17+
// O
18+
// };
1619
enum Player board[__BOARD_SIZE__][__BOARD_SIZE__] = { //(0, 0) top left, (2, 2) bottom right
1720
{E, E, E},
1821
{E, E, E},
1922
{E, E, E}
2023
};
2124

25+
void reset_board(){
26+
for (int x = 0; x < __BOARD_SIZE__; x++) {
27+
for (int y = 0; y < __BOARD_SIZE__; y++) {
28+
board[x][y] = E;
29+
}
30+
}
31+
}
32+
2233
//expects 3 * 4 bytes = 12
2334
bool handle_place(int client_fd, char* client_str) {
24-
enum Player player; //first four bytes of packet are the player
25-
while ( (player = get_int_from_client(client_fd)) == -1) { }
26-
int x_coord; //next four bytes are the x
27-
while ( (x_coord = get_int_from_client(client_fd)) == -1) { }
28-
int y_coord; //last 4 are the y
29-
while ( (y_coord = get_int_from_client(client_fd)) == -1) { }
35+
enum Player player; //first four bytes of packet are the player
3036

31-
board[x_coord][y_coord] = player;
37+
//flipped because of array indexing vs x-y order
38+
int y_coord; //next four bytes are the y
39+
int x_coord; //last 4 are the x
3240

33-
return true;
41+
while ( (player = get_int_from_client(client_fd)) == -1) { }
42+
while ( (y_coord = get_int_from_client(client_fd)) == -1) { }
43+
while ( (x_coord = get_int_from_client(client_fd)) == -1) { }
44+
45+
board[x_coord][y_coord] = player;
46+
return true;
3447
}
3548

3649
//should we send the whole board as a (9 * 4) byte string instead? modify client/how we send this
@@ -66,8 +79,6 @@ bool handle_read_board(int client_fd, char* client_str) {
6679
bool handle_get_winner(int client_fd, char* client_str) {
6780
//implement a more effecient check if needed
6881

69-
printf("entered get winner\n");
70-
7182
enum Player winner = E;
7283

7384
for (int i = 0; i < 3; i++) {
@@ -78,19 +89,19 @@ bool handle_get_winner(int client_fd, char* client_str) {
7889
if ((board[0][i] != E) && (board[0][i] == board[1][i]) && (board[0][i] == board[2][i]))
7990
winner = board[0][i];
8091
}
81-
printf("sdfadafa\n");
8292

8393
// check diagonals
8494
if ((board[0][0] != E) && (board[0][0] == board[1][1]) && (board[0][0] == board[2][2]))
8595
winner = board[0][0];
86-
printf("sdfadafa\n");
8796

8897
if ((board[2][0] != E) && (board[2][0] == board[1][1]) && (board[0][0] == board[0][2]))
8998
winner = board[2][0];
9099

91-
//send winner to client
92-
93-
respond_buff_to_client(client_fd, winner, sizeof(E));
100+
//clear board and send winner to client
101+
if (winner != E) {
102+
reset_board();
103+
}
104+
respond_buff_to_client(client_fd, (char*)(&winner), sizeof(E));
94105

95106
return true;//what to return for status?
96107

Diff for: tictactoe.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <stdbool.h>
66

77
enum Player {
8-
E, X, O //E is empty and happens to be null too, 0, 1, 2 default values
8+
E, X, O //E means empty and happens to be null too, 0, 1, 2 default values
99
};
1010

1111
bool handle_place(int client_fd, char* client_str);
@@ -14,4 +14,6 @@ bool handle_read_board(int client_fd, char* client_str);
1414

1515
bool handle_get_winner(int client_fd, char* client_str);
1616

17+
void reset_board();
18+
1719
#endif

0 commit comments

Comments
 (0)