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 answer: 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
<STAThread>
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