I'm working on creating a Tic Tac Toe game in Visual Basic. I'm having lots of problems, but the one I'm focusing on right now is this:
我正在使用Visual Basic创建一个Tic Tac Toe游戏。我遇到了很多问题,但我现在关注的问题是:
There are 2 players, one human, and the other is the computer. What would be the simplest way to determine whose turn it is? I mean, clearly one player will be X and the other will be O, so I need to figure out how to determine whose turn it is so the program knows what letter to set as the text of the button.
有2个玩家,一个是人,另一个是电脑。确定轮到谁的最简单方法是什么?我的意思是,显然一个玩家将是X而另一个将是O,所以我需要弄清楚如何确定轮到它,以便程序知道要设置哪个字母作为按钮的文本。
Here is what I have for this portion of the code, but it's missing the part that would tell whether it places an X or an O since that's what my question is about.
以下是我对这部分代码所拥有的内容,但它缺少可以判断它是放置X还是O的部分,因为这就是我的问题所在。
Public Class Form1
Private _currentPlayer As String = "O"
Private _gameArray As String
Dim turn As Integer = 1
Private _gameArray1 As Object
Private Property gameArray(p1 As Integer, p2 As Integer) As String
Get
Return _gameArray
End Get
Set(value As String)
_gameArray = value
End Set
End Property
Private Property gameArray(i As Integer) As Object
Get
Return _gameArray1
End Get
Set(value As Object)
_gameArray1 = value
End Set
End Property
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Welcome to Tic Tac Toe! Would you like to play a game?")
End Sub
Private Function nextPlayer() As String
If _currentPlayer.Equals("O") Then
_currentPlayer = "X"
Return "O"
Else
_currentPlayer = "O"
Return "X"
End If
End Function
Private Sub printView()
Console.WriteLine(btn1.Text & "|" & btn2.Text & "|" & btn3.Text)
Console.WriteLine("----------------")
Console.WriteLine(btn4.Text & "|" & btn5.Text & "|" & btn6.Text)
Console.WriteLine("----------------")
Console.WriteLine(btn7.Text & "|" & btn8.Text & "|" & btn9.Text)
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
Console.WriteLine(e.KeyChar)
Select Case e.KeyChar
Case "1"
btn1.Text = "X"
Case "2"
btn2.Text = "X"
Case "3"
btn3.Text = "X"
Case "4"
btn4.Text = "X"
Case "5"
btn5.Text = "X"
Case "6"
btn6.Text = "X"
Case "7"
btn7.Text = "X"
Case "8"
btn8.Text = "X"
Case "9"
btn9.Text = "X"
End Select
printView()
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
If turn = 1 Or 3 Or 5 Or 7 Or 9 Then
btn1.Text = "X"
gameArray(0, 0) = "X"
checkwin()
ElseIf (Not String.IsNullOrEmpty(btn1.Text)) Then
MessageBox.Show("This position has already been taken, please select an empty square.")
Else
btn1.Text = "O"
End If
turn += 1
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If turn = 1 Or 3 Or 5 Or 7 Or 9 Then
btn2.Text = "X"
gameArray(0, 2) = "X"
checkwin()
ElseIf (Not String.IsNullOrEmpty(btn1.Text)) Then
MessageBox.Show("This position has already been taken, please select an empty square.")
Else
btn2.Text = "O"
End If
turn += 1
End Sub
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
If turn = 1 Or 3 Or 5 Or 7 Or 9 Then
turn = 3
btn3.Text = "X"
gameArray(1, 0) = "X"
checkwin()
ElseIf (Not String.IsNullOrEmpty(btn1.Text)) Then
MessageBox.Show("This position has already been taken, please select an empty square.")
Else
btn3.Text = "O"
End If
End Sub
Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
If Not String.IsNullOrEmpty(btn4.Text) Then
MessageBox.Show("This position has already been taken, please select an empty square.")
Else
btn4.Text = nextPlayer()
gameArray(1, 0) = nextPlayer()
checkwin()
End If
End Sub
Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
If Not String.IsNullOrEmpty(btn5.Text) Then
MessageBox.Show("This position has already been taken, please select an empty square.")
Else
btn5.Text = nextPlayer()
gameArray(1, 1) = nextPlayer()
checkwin()
End If
End Sub
Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
If Not String.IsNullOrEmpty(btn6.Text) Then
MessageBox.Show("This position has already been taken, please select an empty square.")
Else
btn6.Text = nextPlayer()
gameArray(1, 2) = nextPlayer()
checkwin()
End If
End Sub
Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
If Not String.IsNullOrEmpty(btn7.Text) Then
MessageBox.Show("This position has already been taken, please select an empty square.")
Else
btn7.Text = nextPlayer()
gameArray(2, 0) = nextPlayer()
checkwin()
End If
End Sub
Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
If Not String.IsNullOrEmpty(btn8.Text) Then
MessageBox.Show("This position has already been taken, please select an empty square.")
Else
btn8.Text = nextPlayer()
gameArray(2, 1) = nextPlayer()
checkwin()
End If
End Sub
Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
If Not String.IsNullOrEmpty(btn9.Text) Then
MessageBox.Show("This position has already been taken, please select an empty square.")
Else
btn9.Text = nextPlayer()
gameArray(2, 2) = nextPlayer()
checkwin()
End If
End Sub
Private Sub checkwin()
If (btn1.Text = "X") And (btn2.Text = "X") And (btn3.Text = "X") Then MsgBox("X WINS!")
If (btn4.Text = "X") And (btn5.Text = "X") And (btn6.Text = "X") Then MsgBox("X WINS!")
If (btn7.Text = "X") And (btn8.Text = "X") And (btn9.Text = "X") Then MsgBox("X WINS!")
If (btn1.Text = "X") And (btn4.Text = "X") And (btn7.Text = "X") Then MsgBox("X WINS!")
If (btn2.Text = "X") And (btn5.Text = "X") And (btn8.Text = "X") Then MsgBox("X WINS!")
If (btn3.Text = "X") And (btn6.Text = "X") And (btn9.Text = "X") Then MsgBox("X WINS!")
If (btn1.Text = "X") And (btn5.Text = "X") And (btn9.Text = "X") Then MsgBox("X WINS!")
If (btn3.Text = "X") And (btn5.Text = "X") And (btn7.Text = "X") Then MsgBox("X WINS!")
If (btn1.Text = "O") And (btn2.Text = "O") And (btn3.Text = "O") Then MsgBox("O WINS!")
If (btn4.Text = "O") And (btn5.Text = "O") And (btn6.Text = "O") Then MsgBox("O WINS!")
If (btn7.Text = "O") And (btn8.Text = "O") And (btn9.Text = "O") Then MsgBox("O WINS!")
If (btn1.Text = "O") And (btn4.Text = "O") And (btn7.Text = "O") Then MsgBox("O WINS!")
If (btn2.Text = "O") And (btn5.Text = "O") And (btn8.Text = "O") Then MsgBox("O WINS!")
If (btn3.Text = "O") And (btn6.Text = "O") And (btn9.Text = "O") Then MsgBox("O WINS!")
If (btn1.Text = "O") And (btn5.Text = "O") And (btn9.Text = "O") Then MsgBox("O WINS!")
If (btn3.Text = "O") And (btn5.Text = "O") And (btn7.Text = "O") Then MsgBox("O WINS!")
End Sub
End Class
2 个解决方案
#1
1
Have a private variable in the main class:
在主类中有一个私有变量:
Private _currentPlayer as String = "O"
This sets the initial player as O
这将初始玩家设置为O.
Then have a simple function that flips between X
and O
output:
然后有一个在X和O输出之间翻转的简单函数:
Private Function nextPlayer() As String
If _currentPlayer.Equals("O") Then
_currentPlayer = "X"
Return "O"
Else
_currentPlayer = "O"
Return "X"
End If
End Function
Then change your function to use this new helper:
然后更改您的函数以使用此新助手:
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
If btn1.Text <> Nothing Then
btn1.Text = nextPlayer()
gameArray(0, 0) = nextPlayer()
Else
MessageBox.Show("This position has already been taken, please select an empty square.")
End If
End Sub
#2
0
I would suggest storing the players in an array, and having a reference to the first player.
我建议将玩家存储在一个数组中,并引用第一个玩家。
Sorry I can't remember VB syntax very well:
对不起,我不记得VB语法很好:
Private _players As List Of Player
私人_players作为球员名单
When you initialise
初始化时
_players.Add(new HumanPlayer(...))
_players.Add(new ComputerPlayer(...))
_players.Add(new HumanPlayer(...))_ players.Add(new ComputerPlayer(...))
then store a reference to whichever one is currently playing, possibly with a EndTurn()
function that advances the turn.
然后存储对当前正在播放的任何一个的引用,可能还有一个提前转弯的EndTurn()函数。
Private currentPlayer as IPlayer = _players.First()
私有currentPlayer为IPlayer = _players.First()
Then a little function to advance it to the next player.
然后是一个小功能,将它推进到下一个玩家。
Apologies for my code being invalid VB I haven't done any VB for years.
我的代码无效的道歉VB多年来我没有做任何VB。
This pattern makes your program a bit more flexible and easier to read, it also addresses the separation of concerns if you classes and interfaces correctly.
这种模式使您的程序更灵活,更易于阅读,如果类和接口正确,它还可以解决问题的分离。
#1
1
Have a private variable in the main class:
在主类中有一个私有变量:
Private _currentPlayer as String = "O"
This sets the initial player as O
这将初始玩家设置为O.
Then have a simple function that flips between X
and O
output:
然后有一个在X和O输出之间翻转的简单函数:
Private Function nextPlayer() As String
If _currentPlayer.Equals("O") Then
_currentPlayer = "X"
Return "O"
Else
_currentPlayer = "O"
Return "X"
End If
End Function
Then change your function to use this new helper:
然后更改您的函数以使用此新助手:
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
If btn1.Text <> Nothing Then
btn1.Text = nextPlayer()
gameArray(0, 0) = nextPlayer()
Else
MessageBox.Show("This position has already been taken, please select an empty square.")
End If
End Sub
#2
0
I would suggest storing the players in an array, and having a reference to the first player.
我建议将玩家存储在一个数组中,并引用第一个玩家。
Sorry I can't remember VB syntax very well:
对不起,我不记得VB语法很好:
Private _players As List Of Player
私人_players作为球员名单
When you initialise
初始化时
_players.Add(new HumanPlayer(...))
_players.Add(new ComputerPlayer(...))
_players.Add(new HumanPlayer(...))_ players.Add(new ComputerPlayer(...))
then store a reference to whichever one is currently playing, possibly with a EndTurn()
function that advances the turn.
然后存储对当前正在播放的任何一个的引用,可能还有一个提前转弯的EndTurn()函数。
Private currentPlayer as IPlayer = _players.First()
私有currentPlayer为IPlayer = _players.First()
Then a little function to advance it to the next player.
然后是一个小功能,将它推进到下一个玩家。
Apologies for my code being invalid VB I haven't done any VB for years.
我的代码无效的道歉VB多年来我没有做任何VB。
This pattern makes your program a bit more flexible and easier to read, it also addresses the separation of concerns if you classes and interfaces correctly.
这种模式使您的程序更灵活,更易于阅读,如果类和接口正确,它还可以解决问题的分离。