Hello, i have tried everything i can imagine but i cant figure out whats going wrong here.
in the player struct , there is a 6x6 matrix, which i fill in the create_player function but when i print it i got 7 columns? then i try to place a '0' in the matrix with the place_ship function, but when the column is 5 it add 2 ceros?
if i reduce the j iterations then i got duplicated in the column 4? or 3 or 2...
is this really a duplicated column? or am i doing something wrong in the code? i searched and tried different things the hole day but cant get it
hope anyone can help me,
Thanks in advice!!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <pthread.h>
#include <signal.h>
#define SHMEM_SIZE 1024
#define SHM_KEY 1210
#define SHM_V_KEY 1012
typedef struct{
char board[6][6];
int thread_ret;
pthread_t thread;
}player;
player *shm;
int *shm_v;
void *shoot(void *target){
printf("under construction\n");
}
//shm[shm_v[1]]->thread_ret = pthread_create(&shm[shm_v[1]]->thread, NULL, shoot, );
void fill_matrix(){
for (size_t i = 0; i < 6; i++) {
for (size_t j = 0; j < 6; j++) {
shm[shm_v[1]].board[i][j] = '*';
}
}
}
void place_ship(){
int x, y; srand(time(NULL));
x = rand() & 5;
y = rand() & 5;
printf("x = %d\ny = %d\n", x,y);
shm[shm_v[1]]->board[3][5] = '0';
}
void print_player(player *p){
for (size_t i = 0; i < 6; i++) {
for (size_t j = 0; j < 6; j++) {
printf("|%c", shm[shm_v[1]].board[i][j]);
if (j == 5) {
printf("|%c|\n", shm[shm_v[1]].board[i][j]);
}
}
}
}
void new_player(){
for (size_t i = 0; i < 6; i++) {
for (size_t j = 0; j < 6; j++) {
shm[shm_v[1]].board[i][j] = '*';
}
}
}
int main(int argc, char *argv[]) {
int id_shm, id_shm_v;
id_shm = shmget(SHM_KEY, SHMEM_SIZE, 0777 | IPC_CREAT);
id_shm_v = shmget(SHM_V_KEY, SHMEM_SIZE, 0777 | IPC_CREAT);
if (id_shm == -1 || id_shm_v == -1) {
perror("No se pudo reservar la memoria");
exit(-1);
}
shm = (player *) shmat(id_shm, 0, 0);
shm_v = (int *) shmat(id_shm_v, 0, 0);
if(shm == -1 || shm_v == -1){
perror("No se pudo acceder a la memoria");
exit(-1);
}
new_player();
print_player(&shm[shm_v[1]]);
shm_v[1]++;
//fill_matrix();
//print_player(&shm[shm_v[1]]);
while (1) {
printf("Id del servidor: %d, cantida de jugadores actualmente %d\n", shm_v[0],shm_v[1]);
sleep(2);
}
//shmdt(shm); //
//shmctl(id_shm, IPC_RMID, 0);
}