Determine if 1 Queen can take King
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
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:Tempkingqueen.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
programming-challenge vb.net chess
add a comment |Â
up vote
2
down vote
favorite
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:Tempkingqueen.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
programming-challenge vb.net chess
Why is there a close vote? What's unclear? Do I need to remove some information?
â Raystafarian
May 4 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 at 10:28
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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:Tempkingqueen.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
programming-challenge vb.net chess
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:Tempkingqueen.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
programming-challenge vb.net chess
edited May 4 at 0:45
asked Apr 30 at 5:54
Raystafarian
5,4331046
5,4331046
Why is there a close vote? What's unclear? Do I need to remove some information?
â Raystafarian
May 4 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 at 10:28
add a comment |Â
Why is there a close vote? What's unclear? Do I need to remove some information?
â Raystafarian
May 4 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 at 10:28
Why is there a close vote? What's unclear? Do I need to remove some information?
â Raystafarian
May 4 at 0:44
Why is there a close vote? What's unclear? Do I need to remove some information?
â Raystafarian
May 4 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 at 10:28
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 at 10:28
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
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
<STAThread>
Sub Main()
Const PATH = "C:Tempkingqueen.txt"
fileContents = System.IO.File.ReadAllLines(PATH)
ParseTextFile()
Dim result As String = ""
For Each testScenario As Scenario In Scenarios
result = result & TestLines(testScenario) & " "
Next
result.Trim()
Console.WriteLine(result)
Console.ReadLine()
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))
Scenarios.Add(newScenario)
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 whatstructures
were. I'll have to comb through this - thanks!
â Raystafarian
Jun 13 at 21:18
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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
<STAThread>
Sub Main()
Const PATH = "C:Tempkingqueen.txt"
fileContents = System.IO.File.ReadAllLines(PATH)
ParseTextFile()
Dim result As String = ""
For Each testScenario As Scenario In Scenarios
result = result & TestLines(testScenario) & " "
Next
result.Trim()
Console.WriteLine(result)
Console.ReadLine()
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))
Scenarios.Add(newScenario)
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 whatstructures
were. I'll have to comb through this - thanks!
â Raystafarian
Jun 13 at 21:18
add a comment |Â
up vote
2
down vote
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
<STAThread>
Sub Main()
Const PATH = "C:Tempkingqueen.txt"
fileContents = System.IO.File.ReadAllLines(PATH)
ParseTextFile()
Dim result As String = ""
For Each testScenario As Scenario In Scenarios
result = result & TestLines(testScenario) & " "
Next
result.Trim()
Console.WriteLine(result)
Console.ReadLine()
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))
Scenarios.Add(newScenario)
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 whatstructures
were. I'll have to comb through this - thanks!
â Raystafarian
Jun 13 at 21:18
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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
<STAThread>
Sub Main()
Const PATH = "C:Tempkingqueen.txt"
fileContents = System.IO.File.ReadAllLines(PATH)
ParseTextFile()
Dim result As String = ""
For Each testScenario As Scenario In Scenarios
result = result & TestLines(testScenario) & " "
Next
result.Trim()
Console.WriteLine(result)
Console.ReadLine()
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))
Scenarios.Add(newScenario)
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
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
<STAThread>
Sub Main()
Const PATH = "C:Tempkingqueen.txt"
fileContents = System.IO.File.ReadAllLines(PATH)
ParseTextFile()
Dim result As String = ""
For Each testScenario As Scenario In Scenarios
result = result & TestLines(testScenario) & " "
Next
result.Trim()
Console.WriteLine(result)
Console.ReadLine()
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))
Scenarios.Add(newScenario)
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
answered Jun 13 at 10:35
David Wilson
1313
1313
Hm, didn't even know whatstructures
were. I'll have to comb through this - thanks!
â Raystafarian
Jun 13 at 21:18
add a comment |Â
Hm, didn't even know whatstructures
were. I'll have to comb through this - thanks!
â Raystafarian
Jun 13 at 21:18
Hm, didn't even know what
structures
were. I'll have to comb through this - thanks!â Raystafarian
Jun 13 at 21:18
Hm, didn't even know what
structures
were. I'll have to comb through this - thanks!â Raystafarian
Jun 13 at 21:18
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193241%2fdetermine-if-1-queen-can-take-king%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Why is there a close vote? What's unclear? Do I need to remove some information?
â Raystafarian
May 4 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 at 10:28