VB.Net - Split Decisions
Overview
Split Decisions is a word game played in a grid with twenty one split cells. Each cell presents you with a choice of 2 letters. To win the game, you need to select the correct letter in each split cell, spelling out the six hidden words.
The Form code
Most of the code used in this application is located within the main Form, as this game uses mainly graphical elements...
Public Class Form1
Private wordGrid(4, 4) As String
Private flipLetter(4, 4) As reveal
Private answers(4, 4) As reveal
Private r As New Random
Private game As New Game
Private Enum reveal
unset
[true]
[false]
End Enum
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 1 To 10
dgvGrid.Columns.Add(New DataGridViewTextBoxColumn())
Next
dgvGrid.Rows.Add(10)
For x As Integer = 1 To 10
dgvGrid.Columns(x - 1).Width = dgvGrid.Rows(0).Height
Next
dgvGrid.Size = New Size(dgvGrid.Rows(0).Height * 10 + 1, dgvGrid.Rows(0).Height * 10 + 1)
dgvGrid.ReadOnly = True
dgvGrid.DefaultCellStyle.SelectionBackColor = Color.Transparent
For x As Integer = 0 To 4
For y As Integer = 0 To 4
flipLetter(x, y) = reveal.unset
Next
Next
dgvGrid.ClearSelection()
btnNewGame.PerformClick()
End Sub
Private Sub dgvGrid_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvGrid.CellClick
Dim c As Integer = CInt(Math.Floor(e.ColumnIndex / 2))
Dim r As Integer = CInt(Math.Floor(e.RowIndex / 2))
If e.RowIndex / 2 > r Then
flipLetter(c, r) = reveal.true
Else
flipLetter(c, r) = reveal.false
End If
dgvGrid.ClearSelection()
dgvGrid.Refresh()
End Sub
Private Sub dgvGrid_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles dgvGrid.Paint
For x As Integer = 0 To 8 Step 2
For y As Integer = 2 To 10 Step 2
e.Graphics.DrawLine(Pens.Black, dgvGrid.Rows(0).Height * x, dgvGrid.Rows(0).Height * y, dgvGrid.Rows(0).Height * (x + 2), dgvGrid.Rows(0).Height * (y - 2))
Next
Next
Dim n As Integer = dgvGrid.Rows(0).Height * 2
For x As Integer = 0 To 4
For y As Integer = 0 To 4
If If(chkCheckWords.Checked, answers(x, y), flipLetter(x, y)) = reveal.true Then
e.Graphics.FillPolygon(Brushes.Black, New Point() _
{New Point(x * n, y * n), _
New Point((x + 1) * n, y * n), _
New Point(x * n, (y + 1) * n)})
ElseIf If(chkCheckWords.Checked, answers(x, y), flipLetter(x, y)) = reveal.false Then
e.Graphics.FillPolygon(Brushes.Black, New Point() _
{New Point((x + 1) * n, y * n), _
New Point((x + 1) * n, (y + 1) * n), _
New Point(x * n, (y + 1) * n)})
End If
Next
Next
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), New Rectangle(n, n, n, n))
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), New Rectangle(n * 3, n, n, n))
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), New Rectangle(n, n * 3, n, n))
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), New Rectangle(n * 3, n * 3, n, n))
For x As Integer = 0 To 10 Step 2
e.Graphics.DrawLine(Pens.Black, dgvGrid.Rows(0).Height * x, 0, dgvGrid.Rows(0).Height * x, dgvGrid.Height)
Next
For y As Integer = 0 To 10 Step 2
e.Graphics.DrawLine(Pens.Black, 0, dgvGrid.Rows(0).Height * y, dgvGrid.Width, dgvGrid.Rows(0).Height * y)
Next
End Sub
Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
chkCheckWords.Checked = False
wordGrid = game.createArray
For x As Integer = 0 To 4
For y As Integer = 0 To 4
Dim offset As Integer = r.Next(0, 2)
answers(x, y) = If(offset = 0, reveal.false, reveal.true)
flipLetter(x, y) = reveal.unset
If offset = 0 Then
dgvGrid.Rows(y * 2).Cells(x * 2).Value = wordGrid(x, y)
dgvGrid.Rows(y * 2 + 1).Cells(x * 2 + 1).Value = Chr(65 + r.Next(0, 26))
Else
dgvGrid.Rows(y * 2).Cells(x * 2).Value = Chr(65 + r.Next(0, 26))
dgvGrid.Rows(y * 2 + 1).Cells(x * 2 + 1).Value = wordGrid(x, y)
End If
Next
Next
dgvGrid.Refresh()
End Sub
Private Sub chkCheckWords_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCheckWords.CheckedChanged
dgvGrid.Refresh()
End Sub
End Class
The Game Class
There's just one Public Function used in this Class. This Function is used to create a new random game...
Public Class Game
Private words() As String
Private r As New Random
Public Sub New()
words = My.Resources.words05.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
End Sub
Public Function createArray() As String(,)
Dim wordGrid(4, 4) As String
Do
For y As Integer = 0 To 4 Step 2
Dim word As String = words(r.Next(0, words.Count))
For x As Integer = 0 To 4
wordGrid(x, y) = word(x).ToString
Next
Next
For x As Integer = 0 To 4 Step 2
Dim matches() As String = words.Where(Function(w) wordGrid(x, 0) = w(0).ToString And wordGrid(x, 2) = w(2).ToString And wordGrid(x, 4) = w(4).ToString).ToArray
If matches.Count = 0 Then Continue Do
Dim word As String = matches(r.Next(0, matches.Count))
For y As Integer = 0 To 4
wordGrid(x, y) = word(y).ToString
Next
Next
Exit Do
Loop
Return wordGrid
End Function
End Class
Conclusion
This is another example of a simply implemented winforms game.
Articles related to game programming
VB.Net - WordSearch
VB.Net - Vertex
VB.Net - Perspective
VB.Net - MasterMind
VB.Net - OOP BlackJack
VB.Net - Numbers Game
VB.Net - HangMan
Console BlackJack - VB.Net | C#
TicTacToe - VB.Net | C#
OOP Sudoku - VB.Net | C#
OctoWords VB.Net | C#
OOP Buttons Guessing Game VB.Net | C#
OOP Tangram Shapes Game VB.Net | C#
VB.Net - Three-card Monte
VB.Net - Pascal's Pyramid
VB.Net - Random Maze Games
(Office) Wordsearch Creator
VB.Net - Event Driven Programming - LockWords Game
C# - Crack the Lock
VB.Net - Totris