You will create a problem that takes in a board with chess pieces from file. For
ID: 3702044 • Letter: Y
Question
You will create a problem that takes in a board with chess pieces from file. For this exercise, the board will only contain pawns and rooks. The dimensions of the board will be some MxN dimensions where both M and N are greater than 1.
Given a board, you will mark all pawns that can be attacked by a rook. Recall, a rook can move horizontally or vertically through empty spaces until it reaches an end of the board or another piece.
You job is to read in the file, find all the pawns that can be attacked, and print an updated board to the screen.
Piece and Board Representations
Rooks: 'R'
Pawns: 'P'
Empty spaces: '*'
When you find a pawn that can be attacked, change it from a 'P' to a '!'
Sample file
The files will contain the dimensions on the first line of the file then contain a board with a mix of empty spaces, pawns, and rooks.
Command-line argument:
Contents of sample.txt input file:
Output to terminal:
Notes:
The pawn at position (3,0) cannot be attacked because there is no rook in its row or column
The pawn at position (3,4) cannot be attacked because there is a piece between it and the rook in its column
Explanation / Answer
The following code is done in Microsoft visual studios (console C#)
Where the result is stored in 2d array
and this array is compared by itself with two for loops based on postion p in the similar row and col were R is present
the same is applied for Notes:
and this array is compared by itself with two for loops based on postion I in the similar row and col were P is present
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sampletest
{
class Program
{
static Int32 D1;
static Int32 D2;
static string[,] Position;
static void Main(string[] args)
{
string[] lines = System.IO.File.ReadAllLines(@"C:TestFolderWriteText.txt");
System.Console.WriteLine("Contents of WriteLines2.txt = ");
int c = 0;
foreach (string line in lines)
{
if (c == 0)
{
D1 = Convert.ToInt32(line.Substring(0, 1));
D2 = Convert.ToInt32(line.Substring(2, 1));
Position = new string[Convert.ToInt32(D1), Convert.ToInt32(D2)];
}
else
{
for (int i = 0; i < line.Length; i++)
{
Position[(c - 1), i] = line.Substring(i, 1);
Console.Write(Position[(c - 1), i] + " ");
}
}
Console.WriteLine(" ");
c++;
}
if (D1 > 1 && D2 > 1)
{
Console.WriteLine(" ");
int f = 0;
for (Int32 i = 0; i < D1; i++)
{
for (Int32 j = 0; j < D2; j++)
{
if (Position[i, j] == "R")
{
for (Int32 l = 0; l < D1; l++)
{
if (Position[l, j] == "P")
{
f = 1;
Position[l, j] = "!";
break;
}
}
for (Int32 k = 0; k < D2; k++)
{
if (f == 1)
break;
if (Position[i, k] == "P")
{
Position[i, k] = "!";
break;
}
}
}
//Console.Write(Position[i, j] + " ");
}
Console.WriteLine(" ");
}
for (Int32 i = 0; i < D1; i++)
{
for (Int32 j = 0; j < D2; j++)
{
Console.Write(Position[i, j] + " ");
}
Console.WriteLine(" ");
}
Console.WriteLine(" ");
int h = 0;
for (Int32 i = 0; i < D1; i++)
{
for (Int32 j = 0; j < D2; j++)
{
h = 0;
if (Position[i, j] == "P")
{
for (Int32 l = 0; l < D1; l++)
{
if (Position[l, j] == "!")
{
h = 1;
Console.WriteLine("The pawn at position (" + i.ToString() + "," + j.ToString() + ") cannot be attacked because there is a piece between it and the rook in its column");
break;
}
}
for (Int32 k = 0; k < D2; k++)
{
if (h == 1)
break;
if (Position[i, k] == "!")
{
h = 1;
Console.WriteLine("The pawn at position (" + i.ToString() + "," + j.ToString() + ") cannot be attacked because there is a piece between it and the rook in its column");
break;
}
}
if (h == 0)
{
Console.WriteLine("The pawn at position (" + i.ToString() + "," + j.ToString() + ") cannot be attacked because there is no rook in its row or column");
}
}
//Console.Write(Position[i,j] + " ");
}
Console.WriteLine(" ");
}
}
else
{
Console.WriteLine(" Dimentions must be greater than one");
}
System.Console.ReadKey();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.