我一直在为大学工作。这是一个多人游戏的类型,你有自己的战舰,需要编程射击其他战舰。一旦战列舰死亡,它会重新产卵,它们也可以移动,并有能见度和射程。
问题是,我一直在试图找到最接近的战舰射击,但我有阵列的问题。
我的代码在你的战术中。我被卡住的一点是变量close_shipX/Y。它总是被设置为0,而不是位置是一艘船。
// 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;
}
游戏图片:
最佳答案
你的条件if (shipX[i] + shipY[i] - myX + myY < 100)
没有给你任何关于船[i]和你的船之间距离的信息。笛卡尔坐标系中的实际距离可以通过毕达哥拉斯定理找到。if(sqrt((shipX[i] - myX)*(shipX[i] - myX) + (shipY[i] - myY)*(shipY[i]-myY)) < 100)
但是,sqrt()函数的计算代价很高,而且经常被忽略(您可以检查距离^2而不是距离:if(((shipX[i] - myX)*(shipX[i] - myX) + (shipY[i] - myY)*(shipY[i]-myY)) < 100)
关于c - C-战舰阵列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21815915/