C - 战舰阵列

时间:2014-02-16 20:11:53

标签: c arrays variables

我一直在为大学工作。这是一个多人游戏,你有自己的战舰,需要编程射击其他战列舰。一旦战舰死亡,它就会重新产生并且它们也可以移动并拥有能见度和射程。 问题是我一直试图找到最接近的战舰射击但是我遇到阵列问题。 我的代码位于您的战术中,请转到此处。我坚持的是变量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;
}

游戏图片:

2 个答案:

答案 0 :(得分:2)

您的条件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)

答案 1 :(得分:0)

您可以计算最近的船只。在此基础上,您可以决定是否根据您的健康状况和船舶健康状况进行攻击或逃离。但健康有点,你应该自己出发。 :) 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);