Question

Visual Basic: Create an application that simulates a tic tac toe game. The form uses 9...

Visual Basic: Create an application that simulates a tic tac toe game. The form uses 9 large labels to display the x's and o's. The application should use a two dimensional integer array to simulate the game board in memory. When the user clicks the "New Game" button the application should step throguh the array storing a random number of 0 to 1 in each element. The numer 0 represent the letter o and the number 1 reprsents the letter x. The form should then be updated to display the gameboard. The application should display a message indicating whether "x wins" or "o wins" or "tie".

Note: Since it is a simulation and random x's and o's fill the board there could be two winners or no winner both which would be considered a tie.

This is the code I have so far but I am not sure if the logic is correct. I think the problem lies in the checkForWin function as I am getting false conditons:

Public Class Form1
Dim game(2, 2) As Integer
Const intMAX_ROW As Integer = 2
Const intMAX_COL As Integer = 2
Dim rand As New Random
Dim intRow As Integer
Dim intCol As Integer
'CheckForWin function to check for a valid winner.
Public Function CheckForWin(value As Integer) As Boolean
Dim hasWin As Boolean

'Check each row

For intRow As Integer = 0 To intMAX_ROW
hasWin = True
For intCol As Integer = 0 To intMAX_COL
If game(intRow, intCol) <> value Then
hasWin = False
End If
Next
Next

'Check each column
For intCol As Integer = 0 To intMAX_COL
hasWin = True
For intRow As Integer = 0 To intMAX_ROW
If game(intCol, intRow) <> value Then
hasWin = False
End If
Next
Next

'Check diagonal top right to bottom left
For intRow As Integer = 0 To intMAX_ROW
hasWin = True
If game(intRow, intRow) <> value Then
hasWin = False
End If
Next

'Check diagonal top left to bottom right
For intRow As Integer = 0 To intMAX_ROW
hasWin = True
If game(intRow, intRow) <> value Then
hasWin = False
End If
Next
If hasWin = True Then Return True
Return False
End Function

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close the form
Me.Close()
End Sub

Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click
'Randomly fill array with a value of 0 or 1
For intRow As Integer = 0 To 2
For intCol As Integer = 0 To 2
Dim n As Integer = rand.Next(2)
game(intRow, intCol) = n
Next
Next
'Determine if an X or O will be the text by the random value placed in the subscript.
If game(0, 0) = 1 Then
lbl1.Text = "X"
Else
lbl1.Text = "O"
End If
If game(0, 1) = 1 Then
lbl2.Text = "X"
Else
lbl2.Text = "O"
End If
If game(0, 2) = 1 Then
lbl3.Text = "X"
Else
lbl3.Text = "O"
End If
If game(1, 0) = 1 Then
lbl4.Text = "X"
Else
lbl4.Text = "O"
End If
If game(1, 1) = 1 Then
lbl5.Text = "X"
Else
lbl5.Text = "O"
End If
If game(1, 2) = 1 Then
lbl6.Text = "X"
Else
lbl6.Text = "O"
End If
If game(2, 0) = 1 Then
lbl7.Text = "X"
Else
lbl7.Text = "O"
End If
If game(2, 1) = 1 Then
lbl8.Text = "X"
Else
lbl8.Text = "O"
End If
If game(2, 2) = 1 Then
lbl9.Text = "X"
Else
lbl9.Text = "O"
End If
'Call CheckForWin function to determine if game is a tie or a player won.
Dim winnerO As Boolean = CheckForWin(0) 'check for an O's winner by value 0
Dim winnerX As Boolean = CheckForWin(3) 'check for an X's winner by value 3
If winnerO = True And winnerX = True Then
lblMessage.Text = "Tie game!"
ElseIf winnerO = False And winnerX = False Then
lblMessage.Text = "Tie game!"
ElseIf winnerO = True And winnerX = False Then
lblMessage.Text = "O wins!"
ElseIf winnerX = True And winnerO = False Then
lblMessage.Text = "X wins!"
End If
End Sub


End Class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;

using System.Threading;

namespace TIC_TAC_TOE

{

    class Program

    {

        //making array and   

        //by default I am providing 0-9 where no use of zero  

        static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        static int player = 1; //By default player 1 is set  

        static int choice; //This holds the choice at which position user want to mark   

        // The flag veriable checks who has won if it's value is 1 then some one has won the match if -1 then Match has Draw if 0 then match is still running  

        static int flag = 0;

        static void Main(string[] args)

        {

            do

            {

                Console.Clear();// whenever loop will be again start then screen will be clear  

                Console.WriteLine("Player1:X and Player2:O");

                Console.WriteLine("\n");

                if (player % 2 == 0)//checking the chance of the player  

                {

                    Console.WriteLine("Player 2 Chance");

                }

                else

                {

                    Console.WriteLine("Player 1 Chance");

                }

                Console.WriteLine("\n");

                Board();// calling the board Function  

               choice = int.Parse(Console.ReadLine());//Taking users choice   

                // checking that position where user want to run is marked (with X or O) or not  

                if (arr[choice] != 'X' && arr[choice] != 'O')

                {

                    if (player % 2 == 0) //if chance is of player 2 then mark O else mark X  

                    {

                        arr[choice] = 'O';

                        player++;

                    }

                    else

                    {

                        arr[choice] = 'X';

                        player++;

                    }

                }

                else //If there is any possition where user want to run and that is already marked then show message and load board again  

                {

                    Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, arr[choice]);

                    Console.WriteLine("\n");

                    Console.WriteLine("Please wait 2 second board is loading again.....");

                    Thread.Sleep(2000);

                }

                flag = CheckWin();// calling of check win  

            } while (flag != 1 && flag != -1);// This loof will be run until all cell of the grid is not marked with X and O or some player is not win  

            Console.Clear();// clearing the console  

            Board();// getting filled board again  

            if (flag == 1)// if flag value is 1 then some one has win or means who played marked last time which has win  

            {

                Console.WriteLine("Player {0} has won", (player % 2) + 1);

            }

            else// if flag value is -1 the match will be draw and no one is winner  

            {

                Console.WriteLine("Draw");

            }

            Console.ReadLine();

        }

        // Board method which creats board  

        private static void Board()

        {

            Console.WriteLine("     |     |      ");

            Console.WriteLine("  {0}  |  {1}  |  {2}", arr[1], arr[2], arr[3]);

            Console.WriteLine("_____|_____|_____ ");

            Console.WriteLine("     |     |      ");

            Console.WriteLine("  {0}  |  {1}  |  {2}", arr[4], arr[5], arr[6]);

            Console.WriteLine("_____|_____|_____ ");

            Console.WriteLine("     |     |      ");

            Console.WriteLine("  {0}  |  {1}  |  {2}", arr[7], arr[8], arr[9]);

            Console.WriteLine("     |     |      ");

        }

        //Checking that any player has won or not  

       private static int CheckWin()

        {

            #region Horzontal Winning Condtion

            //Winning Condition For First Row   

            if (arr[1] == arr[2] && arr[2] == arr[3])

            {

                return 1;

            }

           //Winning Condition For Second Row   

            else if (arr[4] == arr[5] && arr[5] == arr[6])

            {

                return 1;

            }

            //Winning Condition For Third Row   

            else if (arr[6] == arr[7] && arr[7] == arr[8])

            {

                return 1;

            }

            #endregion

            #region vertical Winning Condtion

            //Winning Condition For First Column       

            else if (arr[1] == arr[4] && arr[4] == arr[7])

            {

                return 1;

            }

            //Winning Condition For Second Column  

            else if (arr[2] == arr[5] && arr[5] == arr[8])

            {

                return 1;

            }

            //Winning Condition For Third Column  

            else if (arr[3] == arr[6] && arr[6] == arr[9])

            {

                return 1;

            }

            #endregion

            #region Diagonal Winning Condition

            else if (arr[1] == arr[5] && arr[5] == arr[9])

            {

                return 1;

            }

            else if (arr[3] == arr[5] && arr[5] == arr[7])

            {

                return 1;

            }

            #endregion

            #region Checking For Draw

            // If all the cells or values filled with X or O then any player has won the match  

            else if (arr[1] != '1' && arr[2] != '2' && arr[3] != '3' && arr[4] != '4' && arr[5] != '5' && arr[6] != '6' && arr[7] != '7' && arr[8] != '8' && arr[9] != '9')

            {

                return -1;

            }

            #endregion

            else

            {

                return 0;

            }

        }

    }

}  

Add a comment
Know the answer?
Add Answer to:
Visual Basic: Create an application that simulates a tic tac toe game. The form uses 9...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...

  • Project 2 – Memory Match Game Purpose This Windows Classic Desktop application plays a simple matching game. The game si...

    Project 2 – Memory Match Game Purpose This Windows Classic Desktop application plays a simple matching game. The game simulates a card game where the cards a placed face down and the player flips over pairs of cards in an attempt to find matching cards.   Program Procedure Display a 4x4 grid of “face down” cards. Assign the letters A through H randomly to the cards in pairs. Allow the user to click on a card to “flip” it over and...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

  • You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people....

    You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...

  • TIC TAC TOE in openGL/ C++

    I just cannot figure out how to get the game to alternate turns. it is designed to be a 2 player game. the problem is once either an x or an o is placed it will onlycontinue to place the x or the o.please show me how to do the rest here is my code://#include <windows.h> // This header file will be needed for some windows compilers//#include <GL/gl.h> // gl.h and glu.h also maybe needed for some compilers//#include <GL/glu.h>#include <GL/glut.h>...

  • The Murach's Visual Basic 2015 Book And Visual Studio Exercise 7-2 Enhance the Invoice Total application...

    The Murach's Visual Basic 2015 Book And Visual Studio Exercise 7-2 Enhance the Invoice Total application If you did exercise 6-1 in the last chapter, this exercise asks you to add data validation and exception handling to it. 1. Copy the Invoice Total application from your C:\VB 2015\Chapter 06 directory to your Chapter 07 directory. This should be your solution to exercise 6-1 of the last chapter. 2. If your application has both a Sub procedure and a Function procedure...

  • I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program...

    I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: a. Displays the contents of the board array. b. Allows player 1 to select a location...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • Hi, I am a student in college who is trying to make a tic tac toe...

    Hi, I am a student in college who is trying to make a tic tac toe game on Java. Basically, we have to make our own game, even ones that already exist, on Java. We also have to implement an interface, that I have no idea exactly. The professor also stated we need at least 2 different data structures and algorithms for the code. The professor said we could use code online, we'd just have to make some of our...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT