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

Module 6 Programming Assignment – Multiplayer Battleship AI (80 points) Design t

ID: 3913453 • Letter: M

Question

Module 6 Programming Assignment – Multiplayer Battleship AI (80 points)


Design the AI for playing multiplayer Battleship.


Specification


1. The game is played on an NxN grid.


2. Each player will place a specified set of ships


a. The ships will vary in length from 2 to 7.


b. There can be any number or any size ship including 0.


c. EXCEPT the length 7 ship – the battleship – which there will always be 1.


3. Player order will be random but fixed at the start of the game.


4. Each round consists of each player making a grid selection in turn.


5. Each grid selection will be played into all player’s grid. Including the current players grid.


6. Each player will respond with HIT, MISS or SANK {ship name}.


7. If a player’s battleship is sunk that player is removed from the game.


8. Repeat from #4 until 1 player remains.


9. That player is the winner.


Deliverables


In a word document provide the following:


1. A high level narrative description of the algorithms you will use to determine the grid selection when it is your turn (step #4 above).


2. 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.


3. Data Structures you will need to store any data as part of those algorithms.


4. 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).


5. Define the APIs/Entry points for the code in specification 4/5/6. Specifically, 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. This mean that 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

Code:

using System;
using System.Text;
using System.Collections.Generic;


namespace School_Projects_C_Sharp
{
class battle_ship_assignment
{
static List<int> yPositions = new List<int>();
static List<int> xPositions = new List<int>();
static public Random generator = new Random();
static public int[,] player = new int[12, 12];
static public int[,] comp = new int[12, 12];
static public string[] numbers = new string[13] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
static public string[] lowwer = new string[12] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l" };
static public string[] upper = new string[12] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L" };
static public int playerhit;
static public int pcshot;
static public int pchit;
static public int playershot;
static public string name;
static public bool kp = false;

static void Main(string[] args)
{
bool winr = false;

Console.WriteLine("Type your Name Here");
name = Console.ReadLine();
comp_shps();
display();
player_shps();
do
{
pc_shot();
player_shot();

if ((pchit == 20) || (playerhit == 20))
{
winner = true;
}
} while (winner == false);

if (pchit == 20) Console.WriteLine("Computer Wins");
else Console.WriteLine("Player Wins");
}
static void display()
{

Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(" A B C D E F G H I J K L A B C D E F G H I J K L");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(" ----------------------- -------------------");

for (int x = 0; x < 12; x++)
{
for (int y = 0; y < 12; y++)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("|");
Console.ForegroundColor = ConsoleColor.Green;
if (player[x, y] == 0) Console.Write(" ");
else if (player[x, y] == 1) Console.Write(Convert.ToChar(30));
else if (player[x, y] == 10) Console.Write(Convert.ToChar(15));
else Console.Write(Convert.ToChar(21));
}
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("|");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" " + (x + 1) + " ");

for (int y = 0; y < 12; y++)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("|");
Console.ForegroundColor = ConsoleColor.Green;
if (comp[x, y] == 0) Console.Write(" ");
else if (comp[x, y] == 1) Console.Write(" ");
else if (comp[x, y] == 10) Console.Write(Convert.ToChar(15));
else Console.Write(Convert.ToChar(2));
}
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("|");

Console.Write(" ");
Console.WriteLine("------------------------ -----------------------");
}

Console.WriteLine("Name: " + name + " PC");
Console.WriteLine("Hits: " + playerhit + " Hits: " + pchit);
Console.WriteLine("Shots: " + playershot + " Shots: " + pcshot + " ");
}
static void comp_shps()
{

int co1, co2;


for (int i = 0; i < 2; i++)
{

co1 = rnumber(12);

co2 = rnumber(12);


if (comp[co1, co2] == 0)
{
comp[co1, co2] = 1;

}

else i--;

}

for (int counter = 3; counter < 7; counter++)
{

co1 = rnumber(12 - counter);
co2 = rnumber(12 - counter);
int direction = rnumber(2);
bool ready = false;

if (direction == 0)
{
co2 = rnumber(12);

do
{
if ((counter == 3) && ((comp[co1, co2] == 0) && (comp[co1 + 1, co2] == 0) && (comp[co1 + 2, co2] == 0)) || (counter == 4) && ((comp[co1, co2] == 0) && (comp[co1 + 1, co2] == 0) && (comp[co1 + 2, co2] == 0) && (comp[co1 + 3, co2] == 0))
|| (counter == 5) && ((comp[co1, co2] == 0) && (comp[co1 + 1, co2] == 0) && (comp[co1 + 2, co2] == 0) && (comp[co1 + 3, co2] == 0) && (comp[co1 + 4, co2] == 0))
|| (counter == 6) && ((comp[co1, co2] == 0) && (comp[co1 + 1, co2] == 0) && (comp[co1 + 2, co2] == 0) && (comp[co1 + 3, co2] == 0) && (comp[co1 + 4, co2] == 0)
&& (comp[co1 + 5, co2] == 0)))
{

for (int i = 0; i < counter; i++)
{
comp[co1 + i, co2] = 1;
}
ready = true;
}
else
{
co1 = rnumber(12 - counter);
co2 = rnumber(12 - counter);
ready = false;
}
}
while (ready == false);
}

if (direction == 1)
{
co1 = rnumber(12);

do
{
if ((counter == 3) && ((comp[co1, co2] == 0) && (comp[co1, co2 + 1] == 0) && (comp[co1, co2 + 2] == 0)) || (counter == 4) && ((comp[co1, co2] == 0) && (comp[co1, co2 + 1] == 0) && (comp[co1, co2 + 2] == 0) && (comp[co1, co2 + 3] == 0))
|| (counter == 5) && ((comp[co1, co2] == 0) && (comp[co1, co2 + 1] == 0) && (comp[co1, co2 + 2] == 0) && (comp[co1, co2 + 3] == 0) && (comp[co1, co2 + 4] == 0))
|| (counter == 6) && ((comp[co1, co2] == 0) && (comp[co1, co2 + 1] == 0) && (comp[co1, co2 + 2] == 0) && (comp[co1, co2 + 3] == 0) && (comp[co1, co2 + 4] == 0) && (comp[co1, co2 + 5] == 0)))
{
for (int i = 0; i < counter; i++)
{
comp[co1, co2 + i] = 1;
}
ready = true;
}

else
{
co1 = rnumber(12 - counter);
co2 = rnumber(12 - counter);
ready = false;
}
}
while (ready == false);
}
}
}
static void player_shps()
{
int direction, position1, position2;
string getd, getx, gety;
bool flag1 = false, ready = false;

for (int s = 0; s < 2; s++)
{
do
{
do
{
Console.WriteLine("Enter horizontal position");
getx = Console.ReadLine();
getx = letpress(getx);
} while (kp != false);
position2 = hposition(getx);
kp = false;
do
{
Console.WriteLine("Enter vertical position");
gety = Console.ReadLine();
gety = numpress(gety);
} while (kp != false);
position1 = Convert.ToInt16(gety);
position1--;
kp = false;
  
if ((position1 > -1) && (position1 < 12) && (position2 > -1) && (position2 < 12) && (player[position1, position2] == 0))
{
player[position1, position2] = 1;
flag1 = true;
}
else
{
Console.WriteLine("Invalid inputs please try again");
flag1 = false;
}
}
while (flag1 == false);
flag1 = false;
display();
}
//================================ fill up boat with 3
do
{
do
{
do
{
Console.WriteLine("1. Horizontal :: 0. Vertial");
getd = Console.ReadLine();
getd = numpress(getd);
} while (kp != false);
direction = Convert.ToInt16(getd);
do
{
Console.WriteLine("Enter horizontal position");
getx = Console.ReadLine();
getx = letpress(getx);
} while (kp != false);
position2 = hposition(getx);
kp = false;
do
{
Console.WriteLine("Enter vertical position");
gety = Console.ReadLine();
gety = numpress(gety);
} while (kp != false);
position1 = Convert.ToInt16(gety);
position1--;
kp = false;
if (((direction == 1) && (position1 > -1) && (position1 < 10) && (position2 > -1) && (position2 < 10))
|| ((direction == 0) && (position1 > -1) && (position1 < 10) && (position2 > -1) && (position2 < 10)))
flag1 = true;
else
Console.WriteLine("Invalid inputs please try again");
} while (flag1 == false);
if (direction == 0)
{
if ((player[position1, position2] == 0) && (player[position1 + 1, position2] == 0)
&& (player[position1 + 2, position2] == 0))
{
for (int fill = 0; fill < 3; fill++)
{
player[position1 + fill, position2] = 1;
}
ready = true;
}
else Console.WriteLine("Position contains a ship");
}
if (direction == 1)
{
if ((player[position1, position2] == 0) && (player[position1, position2 + 1] == 0)
&& (player[position1, position2 + 2] == 0))
{
for (int fill = 0; fill < 3; fill++)
{
player[position1, position2 + fill] = 1;
}
ready = true;
}
else Console.WriteLine("Position contains a ship");
}
}
while (ready == false);
display();

ready = false;


  
//================================ fill up boat with 4
do
{
do
{
do
{
Console.WriteLine("1. Horizontal :: 0. Vertial");
getd = Console.ReadLine();
getd = numpress(getd);
} while (kp != false);
direction = Convert.ToInt16(getd);
do
{
Console.WriteLine("Enter horizontal position");
getx = Console.ReadLine();
getx = letpress(getx);
} while (kp != false);
position2 = hposition(getx);
kp = false;
do
{
Console.WriteLine("Enter vertical position");
gety = Console.ReadLine();
gety = numpress(gety);
} while (kp != false);
position1 = Convert.ToInt16(gety);
position1--;
kp = false;
if (((direction == 1) && (position1 > -1) && (position1 < 12) && (position2 > -1) && (position2 < 9))
|| ((direction == 0) && (position1 > -1) && (position1 < 9) && (position2 > -1) && (position2 < 12)))
flag1 = true;
else
{
Console.WriteLine("Invalid inputs please try again");
flag1 = false;
}


} while (flag1 == false);

if (direction == 0)
{
if ((player[position1, position2] == 0) && (player[(position1 + 1), position2] == 0)
&& (player[(position1 + 2), position2] == 0) && (player[(position1 + 3), position2] == 0))
{
for (int fill = 0; fill < 4; fill++)
{
player[position1 + fill, position2] = 1;
}
ready = true;
}
else
Console.WriteLine("Position contains a ship");
}

if (direction == 1)
{
if ((player[position1, position2] == 0) && (player[position1, position2 + 1] == 0)
&& (player[position1, position2 + 2] == 0) && (player[position1, position2 + 3] == 0))
{
for (int fill = 0; fill < 4; fill++)
{
player[position1, position2 + fill] = 1;
}
ready = true;
}
else Console.WriteLine("Position contains a ship");
}
}
while (ready == false);
display();

ready = false;

  
//===================================== 5 box boat

do
{
do
{
do
{
Console.WriteLine("1. Horizontal :: 0. Vertial");
getd = Console.ReadLine();
getd = numpress(getd);
} while (kp != false);
direction = Convert.ToInt16(getd);
do
{
Console.WriteLine("Enter horizontal position");
getx = Console.ReadLine();
getx = letpress(getx);
} while (kp != false);
position2 = hposition(getx);
kp = false;
do
{
Console.WriteLine("Enter vertical position");
gety = Console.ReadLine();
gety = numpress(gety);
} while (kp != false);
position1 = Convert.ToInt16(gety);
position1--;
kp = false;
if (((direction == 1) && (position1 > -1) && (position1 < 12) && (position2 > -1) && (position2 < 8))
|| ((direction == 0) && (position1 > -1) && (position1 < 8) && (position2 > -1) && (position2 < 12)))
flag1 = true;
else
{
Console.WriteLine("Invalid inputs please try again");
flag1 = false;
}

} while (flag1 == false);

if (direction == 0)
{
if ((player[position1, position2] == 0) && (player[(position1 + 1), position2] == 0)
&& (player[(position1 + 2), position2] == 0) && (player[(position1 + 3), position2] == 0) && (player[(position1 + 4), position2] == 0))
{
for (int fill = 0; fill < 5; fill++)
{
player[position1 + fill, position2] = 1;
}
ready = true;
}
else
Console.WriteLine("Position contains a ship");
}

if (direction == 1)
{
if ((player[position1, position2] == 0) && (player[position1, position2 + 1] == 0)
&& (player[position1, position2 + 2] == 0) && (player[position1, position2 + 3] == 0) && (player[position1, position2 + 4] == 0))
{
for (int fill = 0; fill < 5; fill++)
{
player[position1, position2 + fill] = 1;
}
ready = true;
}
else Console.WriteLine("Position contains a ship");
}
}
while (ready == false);
display();

ready = false;

//===================================== 6 box boat

do
{
do
{
do
{
Console.WriteLine("1. Horizontal :: 0. Vertial");
getd = Console.ReadLine();
getd = numpress(getd);
} while (kp != false);
direction = Convert.ToInt16(getd);
do
{
Console.WriteLine("Enter horizontal position");
getx = Console.ReadLine();
getx = letpress(getx);
} while (kp != false);
position2 = hposition(getx);
kp = false;
do
{
Console.WriteLine("Enter vertical position");
gety = Console.ReadLine();
gety = numpress(gety);
} while (kp != false);
position1 = Convert.ToInt16(gety);
position1--;
kp = false;
if (((direction == 1) && (position1 > -1) && (position1 < 12) && (position2 > -1) && (position2 < 7))
|| ((direction == 0) && (position1 > -1) && (position1 < 7) && (position2 > -1) && (position2 < 12)))
flag1 = true;
else
{
Console.WriteLine("Invalid inputs please try again");
flag1 = false;
}

} while (flag1 == false);

if (direction == 0)
{
if ((player[position1, position2] == 0) && (player[(position1 + 1), position2] == 0)
&& (player[(position1 + 2), position2] == 0) && (player[(position1 + 3), position2] == 0) && (player[(position1 + 4), position2] == 0) && (player[(position1 + 5), position2] == 0))
{
for (int fill = 0; fill < 6; fill++)
{
player[position1 + fill, position2] = 1;
}
ready = true;
}
else
Console.WriteLine("Position contains a ship");
}

if (direction == 1)
{
if ((player[position1, position2] == 0) && (player[position1, position2 + 1] == 0)
&& (player[position1, position2 + 2] == 0) && (player[position1, position2 + 3] == 0) && (player[position1, position2 + 4] == 0) && (player[position1, position2 + 5] == 0))
{
for (int fill = 0; fill < 6; fill++)
{
player[position1, position2 + fill] = 1;
}
ready = true;
}
else Console.WriteLine("Position contains a ship");
}
}
while (ready == false);
display();

ready = false;

}

static void pc_shot()
{
int xshot, yshot;
bool hit = false;

xshot = rnumber(12);
yshot = rnumber(12);
if (xPositions.Count > 0 && yPositions.Count > 0)
{
xshot = xPositions[0];
yshot = yPositions[0];
xPositions.Remove(0);
yPositions.Remove(0);
}

if (player[xshot, yshot] == 0)
{
player[xshot, yshot] = 10;
}
else if (player[xshot, yshot] == 1)
{
player[xshot, yshot] = 11;
xPositions.Add(xshot + 1);
xPositions.Add(xshot - 1);
xPositions.Add(xshot + 1);
xPositions.Add(xshot - 1);

yPositions.Add(yshot + 1);
yPositions.Add(yshot - 1);
yPositions.Add(yshot - 1);
yPositions.Add(yshot + 1);
pchit++;
hit = true;
}
display();
}
static void player_shot()
{
int xshot, yshot;
string getx, gety;
bool hit = false;
bool planex = false;
bool planey = false;

do
{
do
{
Console.Write("Enter X-Coordinate ");
getx = Console.ReadLine();
//getx = keypress(getx);
yshot = hposition(getx);
if ((yshot > -1) && (yshot < 12))
{
planex = true;
}
else Console.Write(" Error: Invalid I/P ");
} while (planex == false);
do
{
Console.Write("Enter Y-Coordinate ");
gety = Console.ReadLine();
xshot = Convert.ToInt16(gety);
xshot--;
if ((xshot > -1) && (xshot < 12))
{
planey = true;
}
else Console.Write(" ERROR: Invalid I/P ");
} while (planey == false);


if ((comp[xshot, yshot] == 0) || (comp[xshot, yshot] == 1))
{
if (comp[xshot, yshot] == 0)
{
comp[xshot, yshot] = 10;
}
else
{
comp[xshot, yshot] = 11;
playerhit++;
}

hit = true;
}
else Console.Write(" Already hit is there . ");
} while (hit == false);
playershot++;
display();
}
static int rnumber(int number)
{
int z;

z = (int)(generator.NextDouble() * number);
return (z);
}
static int hposition(string hchar)
{
int hpos;
int x = System.Convert.ToInt16(hchar[0]);

switch (x)
{
case 97:
case 65:
hpos = 0;
break;
case 98:
case 66:
hpos = 1;
break;
case 99:
case 67:
hpos = 2;
break;
case 100:
case 68:
hpos = 3;
break;
case 101:
case 69:
hpos = 4;
break;
case 102:
case 70:
hpos = 5;
break;
case 103:
case 71:
hpos = 6;
break;
case 104:
case 72:
hpos = 7;
break;
case 105:
case 73:
hpos = 8;
break;
case 106:
case 74:
hpos = 9;
break;
case 107:
case 75:
hpos = 10;
break;
case 108:
case 76:
hpos = 11;
break;
default:
hpos = 20;
break;
}
return hpos;
}
static string numpress(string key)
{
for (int z = 0; z < 13; z++)
{
if (key == numbers[z])
{
return key;
kp = true;
break;
}
}
kp = false;

return "200";

}
static string letpress(string key)
{
for (int z = 0; z < 12; z++)
{
if (key == lowwer[z])
{
return key;
kp = true;
break;
}
if (key == upper[z])
{
return key;
kp = true;
break;
}}
kp = false;

return "Z";
}}}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote