Epic Systems Interview Question for Software Architects
- -6of 6 votes
AnswersThis code is written in vb.net language for XO game between two players , TIC TOE
- Eliana December 23, 2013 in United States for personnel interview
I WANT A LITTLE EXPLANATION FOR EACH MAIN LINE IN THE CODE ..
Here is the cod>>
>
>
Public Class Form1
Dim player1, player2 As String
Dim CurrPlayer As Char
Dim winner As Char
Dim win As Boolean = False
Dim Endgame As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CurrPlayer = "O"
player1 = InputBox("First Player Name:")
player2 = InputBox("Second Player Name:")
lblPlayer1.Text = lblPlayer1.Text + player1
lblPlayer2.Text = lblPlayer2.Text + player2
End Sub
Public Function turn() As Char
If CurrPlayer = "X" Then
CurrPlayer = "O"
ElseIf CurrPlayer = "O" Then
CurrPlayer = "X"
End If
Return CurrPlayer
End Function
Public Function CheckWin() As Boolean
If btn1.Text <> "" And btn1.Text = btn2.Text And btn2.Text = btn3.Text Then
winner = CurrPlayer
win = True
ElseIf btn1.Text <> "" And btn1.Text = btn5.Text And btn5.Text = btn9.Text Then
winner = CurrPlayer
win = True
ElseIf btn1.Text <> "" And btn1.Text = btn4.Text And btn4.Text = btn7.Text Then
winner = CurrPlayer
win = True
ElseIf btn2.Text <> "" And btn2.Text = btn5.Text And btn5.Text = btn8.Text Then
winner = CurrPlayer
win = True
ElseIf btn3.Text <> "" And btn3.Text = btn5.Text And btn5.Text = btn7.Text Then
winner = CurrPlayer
win = True
ElseIf btn3.Text <> "" And btn3.Text = btn6.Text And btn6.Text = btn9.Text Then
winner = CurrPlayer
win = True
ElseIf btn4.Text <> "" And btn4.Text = btn5.Text And btn5.Text = btn6.Text Then
winner = CurrPlayer
win = True
ElseIf btn7.Text <> "" And btn7.Text = btn8.Text And btn8.Text = btn9.Text Then
winner = CurrPlayer
win = True
End If
Return win
End Function
Public Sub newGame()
For Each c As Control In Me.Controls
If c.GetType Is GetType(Button) Then
c.Text = ""
End If
Next
CurrPlayer = "O"
win = False
End Sub
Public Sub displayWinner()
If CheckWin() Then
If CurrPlayer = "X" Then
MessageBox.Show("Congratulations, the winner is " + player1, "Congratulations", MessageBoxButtons.OK, MessageBoxIcon.Information)
newGame()
Else
MessageBox.Show("Congratulations, the winner is " + player2, "Congratulations", MessageBoxButtons.OK, MessageBoxIcon.Information)
newGame()
End If
End If
End Sub
Public Sub CheckEven()
Dim empty As Boolean = False
For Each c As Control In Me.Controls
If c.GetType Is GetType(Button) Then
If c.Text = "" Then
empty = True
End If
End If
Next
If empty <> True Then
MessageBox.Show("Players are Even", "Neutral Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
newGame()
End If
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
If btn1.Text = "" Then
btn1.Text = turn()
displayWinner()
CheckEven()
End If
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
If btn2.Text = "" Then
btn2.Text = turn()
displayWinner()
CheckEven()
End If
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
If btn3.Text = "" Then
btn3.Text = turn()
displayWinner()
CheckEven()
End If
End Sub
Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
If btn4.Text = "" Then
btn4.Text = turn()
displayWinner()
CheckEven()
End If
End Sub
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
If btn5.Text = "" Then
btn5.Text = turn()
displayWinner()
CheckEven()
End If
End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
If btn6.Text = "" Then
btn6.Text = turn()
displayWinner()
End If
End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
If btn7.Text = "" Then
btn7.Text = turn()
displayWinner()
CheckEven()
End If
End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
If btn8.Text = "" Then
btn8.Text = turn()
displayWinner()
CheckEven()
End If
End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
If btn9.Text = "" Then
btn9.Text = turn()
displayWinner()
CheckEven()
End If
End Sub
End Class| Report Duplicate | Flag | PURGE
Epic Systems Software Architect
Country: United States
Interview Type: Written Test
On clicking of any of the numbers (1 to 9) buttons, checking if the current user is winner of the tic tac toe game (either by making horizontal or vertical or diagonally).
- Apparao October 22, 2014suggesting few improvements:
1) The common code (checking Even; checking winner) can be moved to base class to avoid code redundancy.
2) Can display a label who should play next (based on the current user variable).
Nice questions, pls continue posting the questions.