Reputation: 3
I have been on working on a task for university. It is a multi player game of sort where you have your own battleship which needs to be programmed to shoot other battleships. Once a battleship is dead it re-spawns and they can also move and have a visibility and shooting range. The problem is that I have been trying to find the closest battleship to shoot however I am having problems with arrays. My code is located In the your tactics go here section. The bit I am stuck on is that the variable close_shipX/Y. It is always set to 0 and never the position is a ship.
// BattleshipBot.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#pragma comment(lib, "wsock32.lib")
#define STUDENT_NUMBER "13012792"
#define STUDENT_FIRSTNAME "Joseph"
#define STUDENT_FAMILYNAME "Kenny"
//#define IP_ADDRESS_SERVER "127.0.0.1"
#define IP_ADDRESS_SERVER "192.168.154.1"
#define PORT_SEND 1924 // We define a port that we are going to use.
#define PORT_RECEIVE 1925 // We define a port that we are going to use.
#define MAX_BUFFER_SIZE 500
#define MAX_SHIPS 200
#define FIRING_RANGE 100
#define MOVE_LEFT -1
#define MOVE_RIGHT 1
#define MOVE_UP 1
#define MOVE_DOWN -1
#define MOVE_FAST 2
#define MOVE_SLOW 1
SOCKADDR_IN sendto_addr;
SOCKADDR_IN receive_addr;
SOCKET sock_send; // This is our socket, it is the handle to the IO address to read/write packets
SOCKET sock_recv; // This is our socket, it is the handle to the IO address to read/write packets
WSADATA data;
char InputBuffer [MAX_BUFFER_SIZE];
int myX;
int myY;
int myHealth;
int myFlag;
int number_of_ships;
int shipX[MAX_SHIPS];
int shipY[MAX_SHIPS];
int shipHealth[MAX_SHIPS];
int shipFlag[MAX_SHIPS];
bool fire = false;
int fireX;
int fireY;
bool moveShip = false;
int moveX;
int moveY;
bool setFlag = true;
int new_flag = 0;
void fire_at_ship(int X, int Y);
void move_in_direction(int left_right, int up_down);
void set_new_flag(int newFlag);
/*************************************************************/
/********* Your tactics code starts here *********************/
/*************************************************************/
int up_down = MOVE_LEFT*MOVE_SLOW;
int left_right = MOVE_UP*MOVE_FAST;
int close_shipX; //closest ship's x posistion to my ship
int close_shipY;//closest ship's y posistion to my ship
void tactics()
{
if ( myY > 900)
{
up_down = MOVE_DOWN*MOVE_FAST;
}
if (myX < 200)
{
left_right = MOVE_RIGHT*MOVE_FAST;
}
if ( myY < 100)
{
up_down = MOVE_UP*MOVE_FAST;
}
if (myX > 800)
{
left_right = MOVE_LEFT*MOVE_FAST;
}
move_in_direction(left_right, up_down);
for (int i = 0; i < MAX_SHIPS; i++) //cycles through finding closest ship.
{
if (shipX[i] + shipY[i] - myX + myY < 100)
{
close_shipX = shipX[i]; //This is the part that doesnt work.
close_shipY = shipY[i]; // close_shipX and Y and always 0 and are never to set a ships posisition.
}
}
if (number_of_ships > 1)
{
fire_at_ship(close_shipX, close_shipY); //fires at closest ship
printf("X: %d\n",close_shipX);
printf("Y: %d\n",close_shipY);
}
}
/*************************************************************/
/********* Your tactics code ends here ***********************/
/*************************************************************/
void communicate_with_server()
{
char buffer[4096];
int len = sizeof(SOCKADDR);
char chr;
bool finished;
int i;
int j;
int rc;
sprintf_s(buffer, "Register %s,%s,%s", STUDENT_NUMBER, STUDENT_FIRSTNAME, STUDENT_FAMILYNAME);
sendto(sock_send, buffer, strlen(buffer), 0, (SOCKADDR *)&sendto_addr, sizeof(SOCKADDR));
while (true)
{
if (recvfrom(sock_recv, buffer, sizeof(buffer)-1, 0, (SOCKADDR *)&receive_addr, &len) != SOCKET_ERROR)
{
i = 0;
j = 0;
finished = false;
number_of_ships = 0;
while (!finished)
{
chr = buffer[i];
switch (chr)
{
case '|':
InputBuffer[j] = '\0';
j = 0;
sscanf_s(InputBuffer,"%d,%d,%d,%d", &shipFlag[number_of_ships]);
number_of_ships++;
break;
case '\0':
InputBuffer[j] = '\0';
sscanf_s(InputBuffer,"%d,%d,%d,%d", &shipX[number_of_ships], &shipY[number_of_ships], &shipHealth[number_of_ships], &shipFlag[number_of_ships]);
number_of_ships++;
finished = true;
break;
default:
InputBuffer[j] = chr;
j++;
break;
}
i++;
}
myX = shipX[0];
myY = shipY[0];
myHealth = shipHealth[0];
myFlag = shipFlag[0];
tactics();
if (fire)
{
sprintf_s(buffer, "Fire %s,%d,%d", STUDENT_NUMBER, fireX, fireY);
sendto(sock_send, buffer, strlen(buffer), 0, (SOCKADDR *)&sendto_addr, sizeof(SOCKADDR));
fire = false;
}
if (moveShip)
{
sprintf_s(buffer, "Move %s,%d,%d", STUDENT_NUMBER, moveX, moveY);
rc = sendto(sock_send, buffer, strlen(buffer), 0, (SOCKADDR *)&sendto_addr, sizeof(SOCKADDR));
moveShip = false;
}
if (setFlag)
{
sprintf_s(buffer, "Flag %s,%d", STUDENT_NUMBER, new_flag);
sendto(sock_send, buffer, strlen(buffer), 0, (SOCKADDR *)&sendto_addr, sizeof(SOCKADDR));
setFlag = false;
}
}
else
{
printf_s("recvfrom error = %d\n", WSAGetLastError());
}
}
printf_s("Student %s\n", STUDENT_NUMBER);
}
void fire_at_ship(int X, int Y)
{
fire = true;
fireX = X;
fireY = Y;
}
void move_in_direction(int X, int Y)
{
if (X < -2) X = -2;
if (X > 2) X = 2;
if (Y < -2) Y = -2;
if (Y > 2) Y = 2;
moveShip = true;
moveX = X;
moveY = Y;
}
void set_new_flag(int newFlag)
{
setFlag = true;
new_flag = newFlag;
}
int _tmain(int argc, _TCHAR* argv[])
{
char chr = '\0';
printf("\n");
printf("Battleship Bots\n");
printf("UWE Computer and Network Systems Assignment 2 (2013-14)\n");
printf("\n");
if (WSAStartup(MAKEWORD(2, 2), &data) != 0) return(0);
//sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Here we create our socket, which will be a UDP socket (SOCK_DGRAM).
//if (!sock)
//{
// printf("Socket creation failed!\n");
//}
sock_send = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Here we create our socket, which will be a UDP socket (SOCK_DGRAM).
if (!sock_send)
{
printf("Socket creation failed!\n");
}
sock_recv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // Here we create our socket, which will be a UDP socket (SOCK_DGRAM).
if (!sock_recv)
{
printf("Socket creation failed!\n");
}
memset(&sendto_addr, 0, sizeof(SOCKADDR_IN));
sendto_addr.sin_family = AF_INET;
sendto_addr.sin_addr.s_addr = inet_addr(IP_ADDRESS_SERVER);
sendto_addr.sin_port = htons(PORT_SEND);
memset(&receive_addr, 0, sizeof(SOCKADDR_IN));
receive_addr.sin_family = AF_INET;
// receive_addr.sin_addr.s_addr = inet_addr(IP_ADDRESS_SERVER);
receive_addr.sin_addr.s_addr = INADDR_ANY;
receive_addr.sin_port = htons(PORT_RECEIVE);
int ret = bind(sock_recv, (SOCKADDR *)&receive_addr, sizeof(SOCKADDR));
// int ret = bind(sock_send, (SOCKADDR *)&receive_addr, sizeof(SOCKADDR));
if (ret)
{
printf("Bind failed! %d\n", WSAGetLastError());
}
communicate_with_server();
closesocket(sock_send);
closesocket(sock_recv);
WSACleanup();
while (chr != '\n')
{
chr = getchar();
}
return 0;
}
Picture of the game:
Upvotes: 0
Views: 1414
Reputation: 1
You could calculate the closest ship to you. Based on that, you could decide whether u would to attack or flee base on ur health and the ships health. But the health bit, you should come up on your own. :) UWE
//calculate the closest i
for(i=1; i<number_of_ships;`<number_of_ships;` i++)
{
if (shipDistance[closest] > shipDistance[i] )
{
closest = i;
}
}
fire_at_ship(shipX[closest], shipY[closest]);
move_in_direction(left_right, up_down);
Upvotes: 0
Reputation: 2883
Your conditional if (shipX[i] + shipY[i] - myX + myY < 100)
doesn't give you anything related to the distance between ship[i] and your ship. The actual distance in your Cartesian coordinate playing field would be found via the Pythagorean Theorem.
if(sqrt((shipX[i] - myX)*(shipX[i] - myX) + (shipY[i] - myY)*(shipY[i]-myY)) < 100)
The sqrt() function, however, is computationally expensive and frequently omitted (you check distance^2 instead of distance:
if(((shipX[i] - myX)*(shipX[i] - myX) + (shipY[i] - myY)*(shipY[i]-myY)) < 100)
Upvotes: 2