principleMinor
Player VS player Tic-Tac-Toe game
Viewed 0 times
toeplayertictacgame
Problem
This program is supposed to be a player VS player Tic-Tac-Toe game. The computer then checks whether you've won, lost, or tied. The code does work, but I don't want it to work through hacking; I want it to be done the right way.
```
Sub Main()
'Initialise variables
Dim StrCoordinate(3, 3) As String
Dim BooGameOver As Boolean = False
Dim BooIsNextANaught As Boolean = False
Dim IntLoopCounterX As Integer = 0
Dim IntLoopCounterY As Integer = 0
Dim IntTempStorage As Integer = 1
Dim StrPlayAgain As String
Dim IntTempStorage2 As Integer = 0
Dim BooIsInputValid As Boolean = False
Do
'Set all coordinates' contents to "-"
For IntLoopCounterY = 0 To 2
For IntLoopCounterX = 0 To 2
StrCoordinate(IntLoopCounterX, IntLoopCounterY) = ("-")
Next
Next
'Display Instructions
Console.WriteLine("╔═════════════════════════════════════════════╗")
Console.WriteLine("║Welcome to this tic-tac-toe game! ║")
Console.WriteLine("║Please enter the coordinates as you are told ║")
Console.WriteLine("║in order to place your X or 0 there. ║")
Console.WriteLine("╚═════════════════════════════════════════════╝")
Console.WriteLine()
Console.WriteLine("Coordinates are as follows:")
Console.WriteLine(vbTab & "╔═══╦═══╦═══╗")
Console.WriteLine(vbTab & "║1,1║2,1║3,1║")
Console.WriteLine(vbTab & "╠═══╬═══╬═══╣")
Console.WriteLine(vbTab & "║1,2║2,2║3,2║")
Console.WriteLine(vbTab & "╠═══╬═══╬═══╣")
Console.WriteLine(vbTab & "║1,3║2,3║3,3║")
Console.WriteLine(vbTab & "╚═══╩═══╩═══╝")
Console.ReadKey()
Console.WriteLine("Press any key once you have understood the instructions.")
Console.Clear()
Do
'Display Empty Table
Console.WriteLine()
Console.WriteLine(vbTab & "╔═══╦═══╦═══╗")
For IntLoopCounterY = 0 To 2
Console.Write(vbTab & "║ ")
For IntLoopCounterX = 0 To 2
Console.Write(StrCoordinate(IntLoopCou
```
Sub Main()
'Initialise variables
Dim StrCoordinate(3, 3) As String
Dim BooGameOver As Boolean = False
Dim BooIsNextANaught As Boolean = False
Dim IntLoopCounterX As Integer = 0
Dim IntLoopCounterY As Integer = 0
Dim IntTempStorage As Integer = 1
Dim StrPlayAgain As String
Dim IntTempStorage2 As Integer = 0
Dim BooIsInputValid As Boolean = False
Do
'Set all coordinates' contents to "-"
For IntLoopCounterY = 0 To 2
For IntLoopCounterX = 0 To 2
StrCoordinate(IntLoopCounterX, IntLoopCounterY) = ("-")
Next
Next
'Display Instructions
Console.WriteLine("╔═════════════════════════════════════════════╗")
Console.WriteLine("║Welcome to this tic-tac-toe game! ║")
Console.WriteLine("║Please enter the coordinates as you are told ║")
Console.WriteLine("║in order to place your X or 0 there. ║")
Console.WriteLine("╚═════════════════════════════════════════════╝")
Console.WriteLine()
Console.WriteLine("Coordinates are as follows:")
Console.WriteLine(vbTab & "╔═══╦═══╦═══╗")
Console.WriteLine(vbTab & "║1,1║2,1║3,1║")
Console.WriteLine(vbTab & "╠═══╬═══╬═══╣")
Console.WriteLine(vbTab & "║1,2║2,2║3,2║")
Console.WriteLine(vbTab & "╠═══╬═══╬═══╣")
Console.WriteLine(vbTab & "║1,3║2,3║3,3║")
Console.WriteLine(vbTab & "╚═══╩═══╩═══╝")
Console.ReadKey()
Console.WriteLine("Press any key once you have understood the instructions.")
Console.Clear()
Do
'Display Empty Table
Console.WriteLine()
Console.WriteLine(vbTab & "╔═══╦═══╦═══╗")
For IntLoopCounterY = 0 To 2
Console.Write(vbTab & "║ ")
For IntLoopCounterX = 0 To 2
Console.Write(StrCoordinate(IntLoopCou
Solution
You have a big bad block of monolithic code here, stuffed in a does-it-all
You need to first identify concerns:
Writing to the console involves several distinct operations:
Reading from the console also involves several distinct operations:
Create classes to encapsulate each concern, and write methods for each operation. As far as best practices are concerned, I'm biased with IoC and dependency-injection, so my
The
Your naming style is - I'll be gentle - from another era. Hungarian Notation is outdated and doesn't contribute in any way to make your code more readable: drop those type prefixes!
Main() procedure that is begging to be broken down into smaller, more focused procedures and functions.You need to first identify concerns:
- Writing to console
- Reading from console
- Evaluating game state
Writing to the console involves several distinct operations:
- Rendering the game title/instructions screen.
- Rendering the game screen (the 3x3 grid).
- Rendering the game over screen.
Reading from the console also involves several distinct operations:
- Displaying a message and waiting for Enter to be pressed.
- Displaying a message and getting valid coordinates from the user.
- Displaying a message and getting a Y or N answer from the user.
Create classes to encapsulate each concern, and write methods for each operation. As far as best practices are concerned, I'm biased with IoC and dependency-injection, so my
Sub Main() would probably look something like this:Public Class Program
Sub Main()
Dim ConsoleReader As New ConsoleUserInputProvider()
Dim Game As New TicTacToeGame(ConsoleReader)
Game.Run()
End Sub
End ClassThe
Game object encapsulates the game's logic; the ConsoleUserInputProvider exposes methods that TicTacToeGame uses to get the user's input. It implements some IUserInputProvider interface, which could just as well be implemented by some WinFormsUserInputProvider or WpfUserInputProvider if you wanted some fancypants GUI instead of just a console; the way you have your code, the concerns of game logic and console input/output are so tightly coupled it could very well be simpler to just rewrite the application from scratch to give it a new UI.Your naming style is - I'll be gentle - from another era. Hungarian Notation is outdated and doesn't contribute in any way to make your code more readable: drop those type prefixes!
Code Snippets
Public Class Program
Sub Main()
Dim ConsoleReader As New ConsoleUserInputProvider()
Dim Game As New TicTacToeGame(ConsoleReader)
Game.Run()
End Sub
End ClassContext
StackExchange Code Review Q#36361, answer score: 7
Revisions (0)
No revisions yet.