Moin,
In diesem Tutorial zeig ich euch wie man ganz leicht mit vb.net ein Tic-Tac-Toe Spiel erstellt.
Was ihr braucht:
Fangen wir an:
Als erstes legen wir auf der Form an:
- 9 Label (Fixed3D(Eigenschaftsfenster -> Border-Style -> Fixed3D))
- 1 Menustrip
Im Menüstript legt ihr folgenes an:
- Game
- -New
- -Exit
- Scores
- -View Scores
- -Reset Scores
Die Labels benennt ihr wiefolgt:
F1
F2
F3
F4
F5
F6
F7
F8
F9
Also ihr nennt das um bei "name".
So das kann dann so aussehen:
So nun zu den Codes:
Als erstes Deklaieren wir ein paar Sachen (unter Public Class Form1)
Code:
Dim GW As Boolean
Dim Player As String = "X"
Dim X As Integer = 0
Dim O As Integer = 0
Danach fügen wir zwei Funktionen hinzu(unter den Deklarationen):
Code:
Private Function GWN()
If Player = "X" Then
Player = "O"
O = O + 1
Else
Player = "X"
X = X + 1
End If
Dim msg As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Player-" & Player & " hat gewonnen!" & vbCr & "Möchtest du ein neues Spiel starten?"
style = MsgBoxStyle.DefaultButton1 Or _
MsgBoxStyle.Question Or MsgBoxStyle.YesNoCancel
title = "Player-" & Player & " hat gewonnen!"
response = MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then
F1.Text = ""
F2.Text = ""
F3.Text = ""
F4.Text = ""
F5.Text = ""
F6.Text = ""
F7.Text = ""
F8.Text = ""
F9.Text = ""
ElseIf response = MsgBoxResult.Cancel Then
F1.Text = ""
F2.Text = ""
F3.Text = ""
F4.Text = ""
F5.Text = ""
F6.Text = ""
F7.Text = ""
F8.Text = ""
F9.Text = ""
End If
GW = False
End Function
Und
Code:
Public Function GWE()
If F1.Text = "O" And F4.Text = "O" And F7.Text = "O" Then
GW = True
ElseIf F1.Text = "O" And F2.Text = "O" And F3.Text = "O" Then
GW = True
ElseIf F2.Text = "O" And F5.Text = "O" And F8.Text = "O" Then
GW = True
ElseIf F3.Text = "O" And F6.Text = "O" And F9.Text = "O" Then
GW = True
ElseIf F4.Text = "O" And F5.Text = "O" And F6.Text = "O" Then
GW = True
ElseIf F7.Text = "O" And F8.Text = "O" And F9.Text = "O" Then
GW = True
ElseIf F7.Text = "O" And F5.Text = "O" And F3.Text = "O" Then
GW = True
ElseIf F1.Text = "O" And F5.Text = "O" And F9.Text = "O" Then
GW = True
ElseIf F1.Text = "X" And F4.Text = "X" And F7.Text = "X" Then
GW = True
ElseIf F1.Text = "X" And F2.Text = "X" And F3.Text = "X" Then
GW = True
ElseIf F2.Text = "X" And F5.Text = "X" And F8.Text = "X" Then
GW = True
ElseIf F3.Text = "X" And F6.Text = "X" And F9.Text = "X" Then
GW = True
ElseIf F4.Text = "X" And F5.Text = "X" And F6.Text = "X" Then
GW = True
ElseIf F7.Text = "X" And F8.Text = "X" And F9.Text = "X" Then
GW = True
ElseIf F7.Text = "X" And F5.Text = "X" And F3.Text = "X" Then
GW = True
ElseIf F1.Text = "X" And F5.Text = "X" And F9.Text = "X" Then
GW = True
Else
End If
If GW = True Then
GWN()
End If
End Function
Doppelklick auf F1:
Code:
If F1.Text = "" Then
If Player = "X" Then
F1.Text = "X"
Player = "O"
ElseIf Player = "O" Then
F1.Text = "O"
Player = "X"
End If
End If
GWE()
Und so dann immer weiter (bis F9)
Einfach immer F1 mit F2-F9 austauschen.
Game -> New
Code:
F1.Text = ""
F2.Text = ""
F3.Text = ""
F4.Text = ""
F5.Text = ""
F6.Text = ""
F7.Text = ""
F8.Text = ""
F9.Text = ""
Player = "X"
GW = False
Game -> Exit
Scores -> View Scores:
Code:
MsgBox("Player-X: " & X & vbCr & vbCr & "Player-O: " & O)
Scores -> Reset Scores:
Nun Debuggen & Fertig
Beispiel: Download (Das ganze project (Source Code))