Skip to content

Commit

Permalink
WIN and LOSE messages added.
Browse files Browse the repository at this point in the history
  • Loading branch information
YasamanJafari committed Oct 26, 2018
1 parent 7e0639b commit b70a592
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
Binary file modified client
Binary file not shown.
35 changes: 31 additions & 4 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ void createAPortForClientsGame()
// puts(IPandPort);
}

int getRemainingShipsCount()
{
int shipCount = 0;
for(int i = 0; i < MAP_ROW * MAP_COLUMN * 2 - 1; i++)
if(map[i] == '1')
shipCount++;
return shipCount;
}

int checkIfFull(char *place)
{
int y, x;
Expand All @@ -98,7 +107,10 @@ int checkIfFull(char *place)
if(map[20 * y + x] == '1')
{
map[20 * y + x] = '2';
return 1;
if(getRemainingShipsCount() == 0)
return 3;
else
return 1;
}

//already chose
Expand Down Expand Up @@ -141,7 +153,9 @@ void playGame(int playerTurn, int playerNum)
}
write(1, buffer, strlen(buffer));
write(1, "\n", 1);
if(strcmp(buffer, FULL_BLOCK) == 0 || strcmp(buffer, REPEATED_MOVE) == 0 )
if(strcmp(buffer, WIN_MESSAGE) == 0)
exit(1);
else if(strcmp(buffer, FULL_BLOCK) == 0 || strcmp(buffer, REPEATED_MOVE) == 0 )
playGame(1, playerNum);
else if(strcmp(buffer, EMPTY_BLOCK) == 0)
playGame(0, playerNum);
Expand All @@ -168,7 +182,19 @@ void playGame(int playerTurn, int playerNum)

int check = checkIfFull(moveInfo);

if(check == 2)
if(check == 3)
{
write(1, LOSE_MESSAGE, strlen(LOSE_MESSAGE));
write(1, "\n", 1);
if(send(fd, WIN_MESSAGE, strlen(WIN_MESSAGE), 0) < 0)
{
write(2, "Sending info to other player failed.\n", 37);
exit(1);
}
exit(1);
}

else if(check == 2)
{
write(1, "Your opponent repeated a move. Wait for another move...\n", 56);
if(send(fd, REPEATED_MOVE, strlen(REPEATED_MOVE), 0) < 0)
Expand Down Expand Up @@ -212,8 +238,9 @@ void readMap(char* fileName)
}

bytesRead = read(fd, map, MAP_ROW * 2 * MAP_COLUMN);
write(1, "This is how your map looks like: \n", 34);
write(1, "This is how your map looks like: \n\n", 35);
write(1, map, strlen(map));
write(1, "\n", 1);
close(fd);
}

Expand Down
10 changes: 10 additions & 0 deletions map1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
2 changes: 1 addition & 1 deletion map.txt → map2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
1 1 1 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 0
0 0 1 1 1 0 0 0 1 0
0 0 1 1 1 0 0 0 1 0

0 comments on commit b70a592

Please sign in to comment.