Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*** C#*** Design an AI for playing multiplayer battleships. The game is played o

ID: 3771127 • Letter: #

Question

*** C#***

Design an AI for playing multiplayer battleships. The game is played on an NxN grid Each player will place a specified set of ships The ships will vary in length from 2 to 7. There can be any number or any size ship including 0 EXCEPT the length 7 ship – the battleship – which there will always be 1 Player order will be random but fixed at the start of the game Each round consists of each player making a grid selection in turn Each grid selection will be played into all player’s grid. Including the current players grid Each player will respond with HIT, MISS or SANK {ship name} If a player’s battleship is sunk that player is removed from the game Repeat from #4 until 1 player remains That player is the winner Deliverables In a word document provide the following: A high level narrative description of the algorithms you will use to determine the grid selection when it is your turn (step #4 above) More detailed pseudocode that describes specific parts of the algorithm – note that all players are informed of all guesses and results (step #5 and #6 above) and you may (should?) store and use that information as part of your algorithm. Data Structures you will need to store any data as part of those algorithms A high level OO design for the classes you will need to incorporate the algorithms and data structures. A class diagram will be acceptable as will an outline of the class definitions in a Visual Studio project (i.e. empty classes with no code inside the methods) Define the APIs/Entry points for the code in specification 4/5/6 i.e. what information would you expect to be passed, returned for you to be able to implement your algorithm. Note that this may require you to add some data structures to deliverable #2. Justify why you need any parameters passed in and why you think the return values would be needed. You cannot expect to have direct access to any information about other players other than what is provided by steps #5 and #6 i.e. You cannot query a player for the state of their grid. Clearly this would be cheating! You are not expected to write C# code for this assignment and any code provided will not be graded other than an API outline The level of detail for this assignment should be enough to give to another developer to implement. This means a lot of detail. You do NOT have to specify the code that will run the game, determine if you have won or any other plumbing code.

Explanation / Answer

#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h> //for rand
#include <stdio.h> //for printf
#include <time.h>   //for time
void showplayerscreen();
void shipplace();
void computerplace();
void computerchoice(int size1, int left1, int up1, int char11);
void compscreen();
void placechoice(int size, int left, int up, int char1);
char shipname[10], compname[10];
int x,intcolumn, introw,intup,intleft,compcolumn, comprow,compup,compleft;
char playerboard [10][10]=
{
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
};
char hiddencomp [10][10]=
{
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
};
char showncomp [10][10]=
{
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
    {46,46,46,46,46,46,46,46,46,46},
};
/*
this is just random random number stuff

    srand(time(NULL));
    printf("A random number from 0 to 99: %d", rand() % 100);
    return 0;

int rand_mid(int low, int high)
{
   return low+rand()%(high-low+1);
}

*/
main()
{
    HANDLE hStdout;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    cout<<"Loading"<<endl;
    Sleep(1000);
    system("CLS");
    cout<<"Loading."<<endl;
    Sleep(1000);
    system("CLS");
    cout<<"Loading.."<<endl;
    Sleep(1000);
    system("CLS");
    cout<<"Loading..."<<endl;
    Sleep(1000);
    system("CLS");
    //this neat loading screen ©2001 JTech Enterprises.
    SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE|BACKGROUND_RED);
    cout<<"Welcome to BATTLESHIP..."<<endl;
    SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
// showplayerscreen();
// compscreen();
// shipplace();
    computerplace();
    compscreen();
    return 0;
}

void compscreen()
{
    
    comprow=0;
    compcolumn=0;
    cout << "" << endl;
    cout << "    Computer's Board (debug)" << endl;
    cout << "======================" << endl;
    cout << " |0|1|2|3|4|5|6|7|8|9|" ;
    cout<<' ';
    cout << comprow << "|";
    do
    {
        if(comprow == 9 && compcolumn == 10)
        {break;}
        if(compcolumn > 9 && comprow < 9)
        {
            cout << "|"<< endl;
            comprow = comprow + 1;
            cout << (comprow) << "|";
            compcolumn = 0;
        }
        if(comprow < 10)
        {
            if(compcolumn < 9)
            {
                cout << hiddencomp[comprow] [compcolumn] << " ";
            }
            if(compcolumn > 8)
            {
                cout << hiddencomp[comprow] [compcolumn];
            }
        }
        compcolumn = compcolumn + 1;
    }
    while(comprow < 10);
    comprow = 0;
    compcolumn = 0;
    cout << "|" << endl;
    cout << "======================" << endl;
    cout << "" << endl;
}

void computerplace()
{
    srand(time(NULL));
    for (x=5;x>=2;x--)
    {
        switch(x)
        {
            case 5:
                strcpy(compname,"Carrier");
            break;
            case 4:
                strcpy(compname,"Battleship");
            break;
            case 3:
                strcpy(compname,"Kruiser");
            break;
            case 2:
                strcpy(compname,"Destroyer");
            break;
        }
        do
        {
            compleft=rand()%(10);
            compup=rand()%(10);
        }
        while((hiddencomp[compleft][compup]!=46));
        hiddencomp[compleft][compup]=int(compname[0]);
        computerchoice(x,compleft,compup, (int(compname[0])));
    }
    do
    {
        compleft=rand()%(10);
        compup=rand()%(10);
    }
    while((hiddencomp[compleft][compup]!=46));
    hiddencomp[compleft][compup]=83;
    computerchoice(3,compleft,compup, 83);
}

void computerchoice(int size1, int left1, int up1, int char11)
{
    srand(time(NULL));
    int choice,loop;
    choice=1+rand()%(4);
    switch (choice)
    {
        case 1:
            for(loop=1;loop<size1;loop++)
            {
                if(hiddencomp[left1-loop][up1]!=46 || int(left1-(size1-1))<(0))
                {
                    hiddencomp[left1][up1]=46;
                    x=x+1;
                    break;
                }
                if(int(left1-(size1-1)) >= (0) && x==size1)
                {
                    for(loop=1;loop<size1;loop++)
                    {hiddencomp[left1-loop][up1]=char11;}
                    compscreen();
                }
            }
        break;
        case 2:
            for(loop=1;loop<size1;loop++)
            {
                if(hiddencomp[left1+loop][up1]!=46 || int(left1-(size1-1))>(9))
                {
                    hiddencomp[left1][up1]=46;
                    x=x+1;
                    break;
                }
                if(int(left1-(size1-1)) <= (9) && x==size1)
                {
                    for(loop=1;loop<size1;loop++)
                    {hiddencomp[left1+loop][up1]=char11;}
                    compscreen();
                }
            }
        break;
        case 3:
            for(loop=1;loop<size1;loop++)
            {
                if(hiddencomp[left1-loop][up1]!=46 || int(left1-(size1-1))>(9))
                {
                    hiddencomp[left1][up1]=46;
                    x=x+1;
                    break;
                }
                if(int(up1+(size1-1)) <= (9) && x==size1)
                {
                    for(loop=1;loop<size1;loop++)
                    {hiddencomp[left1][up1+ loop]=char11;}
                    compscreen();
                }
            }
        break;
        case 4:
            for(loop=1;loop<size1;loop++)
            {
                if(hiddencomp[left1-loop][up1]!=46 || int(up1-(size1-1))<(0))
                {
                    hiddencomp[left1][up1]=46;
                    x=x+1;
                    break;
                }
                if(int(up1-(size1-1)) >= (0) && x==size1)
                {
                    for(loop=1;loop<size1;loop++)
                    {hiddencomp[left1][up1-loop]=char11;}
                    compscreen();
                }
            }
        break;
    }
}

void showplayerscreen()
{
    introw=0;
    intcolumn=0;
    cout << "" << endl;
    cout << "    Player's Board" << endl;
    cout << "======================" << endl;
    cout << " |0|1|2|3|4|5|6|7|8|9|" ;
    cout<<' ';
    cout << introw << "|";
    do
    {
        if(introw == 9 && intcolumn == 10)
        {break;}
        if(intcolumn > 9 && introw < 9)
        {
            cout << "|"<< endl;
            introw = introw + 1;
            cout << (introw) << "|";
            intcolumn = 0;
        }
        if(introw < 10)
        {
            if(intcolumn < 9)
            {
                cout << playerboard[introw] [intcolumn] << " ";
            }
            if(intcolumn > 8)
            {
                cout << playerboard[introw] [intcolumn]<<flush;
            }
        }
        intcolumn = intcolumn + 1;
    }
    while(introw < 10);
    introw = 0;
    intcolumn = 0;
    cout << "|" << endl;
    cout << "======================" << endl;
    cout << "" << endl;
}

void placechoice(int size,int left,int up,int char1)
{
    int choice,loop;
    cout<<"Do you want your ship to face: "<< ' ' << "1. UP   2.DOWN   3.RIGHT   4.LEFT"<<' ';
    cin>>choice;
    switch (choice)
    {
        case 1:
            for(loop=1;loop<size;loop++)
            {
                if(playerboard[left-loop][up]!=46 || int(left-(size-1))<(0))
                {
                    playerboard[left][up]=46;
                    cout<<"Don't place ships illegally..."<<endl;
                    x=x+1;
                    break;
                }
                if(int(left-(size-1)) >= (0) && x==size)
                {
                    for(loop=1;loop<size;loop++)
                    {playerboard[left-loop][up]=char1;}
                    showplayerscreen();
                }
            }
        break;
        case 2:
            for(loop=1;loop<size;loop++)
            {
                if(playerboard[left+loop][up]!=46 || int(left-(size-1))>(9))
                {
                    playerboard[left][up]=46;
                    cout<<"Don't place ships illegally..."<<endl;
                    x=x+1;
                    break;
                }
                if(int(left-(size-1)) <= (9) && x==size)
                {
                    for(loop=1;loop<size;loop++)
                    {playerboard[left+loop][up]=char1;}
                    showplayerscreen();
                }
            }
        break;
        case 3:
            for(loop=1;loop<size;loop++)
            {
                if(playerboard[left-loop][up]!=46 || int(left-(size-1))>(9))
                {
                    playerboard[left][up]=46;
                    cout<<"Don't place ships illegally..."<<endl;
                    x=x+1;
                    break;
                }
                if(int(up+(size-1)) <= (9) && x==size)
                {
                    for(loop=1;loop<size;loop++)
                    {playerboard[left][up+ loop]=char1;}
                    showplayerscreen();
                }
            }
        break;
        case 4:
            for(loop=1;loop<size;loop++)
            {
                if(playerboard[left-loop][up]!=46 || int(up-(size-1))<(0))
                {
                    playerboard[left][up]=46;
                    cout<<"Don't place ships illegally..."<<endl;
                    x=x+1;
                    break;
                }
                if(int(up-(size-1)) >= (0) && x==size)
                {
                    for(loop=1;loop<size;loop++)
                    {playerboard[left][up-loop]=char1;}
                    showplayerscreen();
                }
            }
        break;
    }
}

void shipplace()
{
    for (x=5;x>=2;x--)
    {
        switch(x)
        {
            case 5:
                strcpy(shipname,"Carrier");
            break;
            case 4:
                strcpy(shipname,"Battleship");
            break;
            case 3:
                strcpy(shipname,"Kruiser");
            break;
            case 2:
                strcpy(shipname,"Destroyer");
            break;
        }
        do
        {
            cout<<"Plot one end of the "<<shipname<<"."<<' ';
            cout<<"Please put the left side coordinate, then the top..."<< ' ';
            cin>>intleft;
            cin>>intup;
        }
        while(intleft<0||intleft>9||intup<0||intup>9||(playerboard[intleft][intup]!=46));
        playerboard[intleft][intup]=int(shipname[0]);
        showplayerscreen();
        placechoice(x,intleft,intup, (int(shipname[0])));
    }
    do
    {
        cout<<"Plot one end of the Submarine."<<' ';
        cout<<"Please put the left side coordinate, then the top..."<< ' ';
        cin>>intleft;
        cin>>intup;
    }
    while(intleft<0||intleft>9||intup<0||intup>9||(playerboard[intleft][intup]!=46));
    playerboard[intleft][intup]=83;
    showplayerscreen();
    placechoice(3,intleft,intup, 83);
}