# Determine if 1 Queen can take King

This challenge was pretty simple - be given the coordinates of a king and a queen on a chess board and determine if the king is in check.

Input is the number of test cases with each test case on a new line, king coordinates first. I read in via text file.

Output "Y" or "N" as the result for each test case.

e.g.

input data:
8
b4 b8
b4 e7
b4 d2
b4 g4
f2 b1
f2 c4
f2 d5
f2 g7

Y Y Y Y N N N N


I tried a bunch of stuff using System.Text.Encoding to try to figure out how to get the ascii char code from my letters, but ended up falling back on to what I know (Microsoft.VisualBasic).

Option Explicit On
Option Strict On
Option Infer On
Option Compare Text

Imports System.IO
Module Module1

Sub Main()
Const PATH = "C:\Temp\kingqueen.txt"
Dim fileContents As String() = System.IO.File.ReadAllLines(PATH)
Dim numberOfScenarios As Integer = Convert.ToInt32(fileContents(0))
Dim result As String
Dim king As String
Dim queen As String
For game As Integer = 1 To numberOfScenarios
king = fileContents(game).Substring(0, 2)
queen = fileContents(game).Substring(fileContents(game).Length - 2, 2)
result = result & TestLines(king, queen) & " "
Next
result.Trim()
Console.WriteLine(result)
End Sub

Private Function TestLines(ByVal king As String, ByVal queen As String) As String
Dim kingsub As String = king.Substring(0, 1)
If king.Substring(0, 1) = queen.Substring(0, 1) Or king.Substring(king.Length - 1, 1) = queen.Substring(queen.Length - 1, 1) Then
Return "Y"
Else
Return TestDiagonals(king, queen)
End If
End Function

Private Function TestDiagonals(ByVal king As String, ByVal queen As String) As String
Dim kingRank As Integer = Microsoft.VisualBasic.Strings.AscW(king.Substring(0, 1))
Dim kingFile As Integer = Convert.ToInt32(king.Substring(1, 1))
Dim queenRank As Integer = Microsoft.VisualBasic.Strings.AscW(queen.Substring(0, 1))
Dim queenFile As Integer = Convert.ToInt32(queen.Substring(1, 1))

If (kingRank + kingFile = queenRank + queenFile) Or (kingRank - kingFile = queenRank - queenFile) Then
Return "Y"
Else
Return "N"
End If
End Function
End Module

• Why is there a close vote? What's unclear? Do I need to remove some information? – Raystafarian May 4 '18 at 0:44
• I suspect the close vote was because you didn't specify what your problem was. However - I'm assuming that you just want cleaner code - Have a look at my answer below - in a few mins when I post it :-) – David Wilson Jun 13 '18 at 10:28

I would suggest that rather than try parsing strings all through your code, try this.

The code below creates a couple of new things. a numeric coordinate structure, a scenario, which is a pair of coordinates, and a list of scenarios.

There are also a couple of functions to parse the text file and each chess location into coordinates. Further down the line, it makes thing easier when you come to checking the queen and king locations.

Module Module1
Structure Coord
Dim Rank As Integer
Dim File As Integer
End Structure

Structure Scenario
Dim King As Coord
Dim Queen As Coord
End Structure

Dim Scenarios As New List(Of Scenario)
Dim fileContents() As String
Sub Main()
Const PATH = "C:\Temp\kingqueen.txt"
ParseTextFile()
Dim result As String = ""
For Each testScenario As Scenario In Scenarios
result = result & TestLines(testScenario) & " "
Next
result.Trim()
Console.WriteLine(result)
End Sub

Private Function ParsePieceLocationAsCoord(chessCoord As String) As Coord
Dim tempPoint As New Coord
tempPoint.File = Asc(chessCoord(0)) - 96
tempPoint.Rank = Val(chessCoord(1))
Return tempPoint
End Function

Private Sub ParseTextFile()
Scenarios.Clear()
For Each line As String In fileContents
Dim newScenario As Scenario
newScenario.King = ParsePieceLocationAsCoord(line.Substring(0, 2))
newScenario.Queen = ParsePieceLocationAsCoord(line.Substring(3, 2))
Next
End Sub

Private Function TestLines(testScenario As Scenario) As String
With testScenario
If .King.Rank = .Queen.Rank Or .King.File = .Queen.File Then
Return "Y"
Else
Return TestDiagonals(testScenario)
End If
End With
End Function

Private Function TestDiagonals(testScenario As Scenario) As String
With testScenario
If (.King.Rank + .King.File = .Queen.Rank + .Queen.File) Or (.King.Rank - .King.File = .Queen.Rank - .Queen.File) Then
Return "Y"
Else
Return "N"
End If
End With
End Function

• Hm, didn't even know what structures were. I'll have to comb through this - thanks! – Raystafarian Jun 13 '18 at 21:18