-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_sending.c
174 lines (163 loc) · 5.67 KB
/
data_sending.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "data_sending.h"
// Wojciech Sniady, nr indeksu: 322993
int TOTALLY_PLACED_MEN_PLAYER_1;
int TOTALLY_PLACED_MEN_PLAYER_2;
int MEN_NUMBER_P_1;
int MEN_NUMBER_P_2;
int NUMBER_OF_SQUARES_ARG;
GtkWidget* MAIN_WINDOW;
GtkWidget* WHOSE_TURN_LABEL;
void send_move_information(int current_square_number, int current_field_number, int chosen_square_number, int chosen_field_number, bool remove)
{
long move_information_int = value_to_send(current_square_number, current_field_number, chosen_square_number, chosen_field_number, remove);
char move_information[MAX_TEXT_LENGHT];
char* move_information_char = long_int_to_char(move_information_int);
strcpy(move_information, move_information_char);
free(move_information_char);
sendStringToPipe(potoki, move_information);
}
gboolean receive_move_information(gpointer data)
{
gchar wejscie[MAX_TEXT_LENGHT];
if (getStringFromPipe(potoki,wejscie,MAX_TEXT_LENGHT))
{
char move_information[MAX_TEXT_LENGHT];
strcpy(move_information, wejscie);
long* move_information_arr = received_value(move_information);
int *totally_placed_men_current_player = (P_1_TURN) ? (&TOTALLY_PLACED_MEN_PLAYER_1) : (&TOTALLY_PLACED_MEN_PLAYER_2);
int sqr_number_pla = move_information_arr[0];
int fie_number_pla = move_information_arr[1];
int chosen_sqr_number_pla = move_information_arr[2];
int chosen_fie_number_pla = move_information_arr[3];
bool remove_rec = move_information_arr[4];
//printf("RECEIVED VALUE: sqr : %d, fie: %d, ch_sqr: %d, ch_fie : %d, remove : %d\n", sqr_number_pla, fie_number_pla, chosen_sqr_number_pla, chosen_fie_number_pla, remove_rec);
free(move_information_arr);
int maximal_number_of_men = (BUTTON_BOARD->number_of_squares == 3) ? (9) : (6);
if (sqr_number_pla == 10)
{
if (fie_number_pla != NUMBER_OF_SQUARES_ARG)
{
printf("Both copies of the program must be called with the same number of squares!\n");
return FALSE;
}
else
{
return TRUE;
}
}
if (sqr_number_pla == 13)
{
reset_all_global_variables();
return TRUE;
}
if (sqr_number_pla == 11)
{
show_winner(P_1_TURN, true, MAIN_WINDOW);
disable_all_your_buttons(BUTTON_BOARD);
return TRUE;
}
if (YOUR_TURN == true)
{
return TRUE;
}
if (remove_rec)
{
remove_men_received(BOARD, BUTTON_BOARD, P_1_TURN, sqr_number_pla, fie_number_pla, &MEN_NUMBER_P_1, &MEN_NUMBER_P_2);
remove_rec = false;
P_1_TURN = !P_1_TURN;
update_label_whose_turn(P_1_TURN, WHOSE_TURN_LABEL);
YOUR_TURN = !YOUR_TURN;
enable_all_your_buttons(BUTTON_BOARD);
}
else if ((*totally_placed_men_current_player) < maximal_number_of_men)
{
place_men_received(BOARD, BUTTON_BOARD, P_1_TURN, sqr_number_pla, fie_number_pla, &MEN_NUMBER_P_1, &MEN_NUMBER_P_2);
(*totally_placed_men_current_player)++;
if (mill_achieved(BOARD, sqr_number_pla, fie_number_pla))
{
remove_rec = true;
}
else
{
remove_rec = false;
P_1_TURN = !P_1_TURN;
update_label_whose_turn(P_1_TURN, WHOSE_TURN_LABEL);
YOUR_TURN = !YOUR_TURN;
enable_all_your_buttons(BUTTON_BOARD);
}
}
else
{
move_men_received(BOARD, BUTTON_BOARD, P_1_TURN, sqr_number_pla, fie_number_pla, chosen_sqr_number_pla, chosen_fie_number_pla);
if (mill_achieved(BOARD, chosen_sqr_number_pla, chosen_fie_number_pla))
{
remove_rec = true;
}
else
{
remove_rec = false;
P_1_TURN = !P_1_TURN;
update_label_whose_turn(P_1_TURN, WHOSE_TURN_LABEL);
YOUR_TURN = !YOUR_TURN;
enable_all_your_buttons(BUTTON_BOARD);
}
}
}
return TRUE;
}
int compute_value_to_send(int position_number, int characterstic_value)
{
if (position_number == -1)
{
return 1;
}
return pow(characterstic_value, position_number+1);
}
long value_to_send(int current_square_number, int current_field_number, int chosen_square_number, int chosen_field_number, bool remove)
{
long result = 1;
result *= compute_value_to_send(current_square_number, 2);
result *= compute_value_to_send(current_field_number, 3);
result *= compute_value_to_send(chosen_square_number, 5);
result *= compute_value_to_send(chosen_field_number, 7);
if (remove)
{
result *= -1;
}
//printf("VALUE TO SEND: sqr : %d, fie: %d, ch_sqr: %d, ch_fie : %d, remove : %d\n", current_square_number, current_field_number, chosen_square_number, chosen_field_number, remove);
return result;
}
char* long_int_to_char(long value)
{
char *value_string = malloc(11*sizeof(char));
sprintf(value_string, "%ld", value);
return value_string;
}
long* received_value(char* value_char)
{
long value_int = atol(value_char);
long* move_information_int = malloc(5*sizeof(long));
move_information_int[0] = compute_received_value(2, value_int);
move_information_int[1] = compute_received_value(3, value_int);
move_information_int[2] = compute_received_value(5, value_int);
move_information_int[3] = compute_received_value(7, value_int);
if (value_int < 0)
{
move_information_int[4] = 1;
}
else
{
move_information_int[4] = 0;
}
return move_information_int;
}
long compute_received_value(int position_number, long received_value)
{
int result = -1;
while (received_value % position_number == 0)
{
received_value = received_value / position_number;
result++;
}
return result;
}