Question

Write a program to Simulate a game of tic tac toe in c#


Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.

Player

-name: string

-symbol :char

Player (name:string,symbol:char)

getName():string

getSymbol():char

getInfo():string



The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’).

 

Program flow:

1)     Your program must ask for each player’s name and their special symbol or character (usually an ‘X’ or ‘O’).

2)     Display each player’s name and symbol.

3)     The program must then display the board (the 2 dimensional array).

4)     The player whose turn it is has their name displayed and is asked for a row and column number to place their symbol. They can enter -1 -1 as the row and column number to quit.

5)     Repeat 2 and 3 until one player enters -1 -1.

 

Your program does not have to detect if the game is won. It simply allows the players to place symbols on the board.


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

Program.cs:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Tic_Tac_Toe_Chegg

{

    class Program

    {

        public static void displayBoard(char [,] board)

        {

            for (int i = 0; i < 3; i++)

            {

                for (int j = 0; j < 3; j++)

                {

                    Console.Write(board[i,j] +  );

                }

 

                Console.WriteLine();

            }

        }

 

        //this function will check if x or o has won

        //and return that character

        //the character returned is checked with player

        //0 or player 1s character and displays who won

        //if its a tie, return T

        public static char checkBoard(char[,] board)

        {

            //first row is checked

            if (board[0, 0] == x && board[0, 1] == x && board[0, 2] == x)

            {

                return x;

            }

 

            //first row is checked

            if (board[0, 0] == o && board[0, 1] == o && board[0, 2] == o)

            {

                return o;

            }

 

            //second row is checked

            if (board[1, 0] == x && board[1, 1] == x && board[1, 2] == x)

            {

                return x;

            }

 

            //second row is checked

            if (board[1, 0] == o && board[1, 1] == o && board[1, 2] == o)

            {

                return o;

            }

 

            //third row is checked

            if (board[2, 0] == x && board[2, 1] == x && board[2, 2] == x)

            {

                return x;

            }

 

            //third row is checked

            if (board[2, 0] == o && board[2, 1] == o && board[2, 2] == o)

            {

                return o;

            }

 

            //first diagonal is checked

            if (board[0, 0] == x && board[1, 1] == x && board[2, 2] == x)

            {

                return x;

            }

 

            //first diagonal is checked

            if (board[0, 0] == o && board[1, 1] == o && board[2, 2] == o)

            {

                return o;

            }

 

            //second diagonal is checked

            if (board[0, 2] == x && board[1, 1] == x && board[2, 0] == x)

            {

                return x;

            }

 

            //second diagonal is checked

            if (board[0, 2] == o && board[1, 1] == o && board[2, 0] == o)

            {

                return o;

            }

 

            //first column is checked

            if (board[0, 0] == x && board[1, 0] == x && board[2, 0] == x)

            {

                return x;

            }

 

            //first column is checked

            if (board[0, 0] == o && board[1, 0] == o && board[2, 0] == o)

            {

                return o;

            }

 

            //second column is checked

            if (board[0, 1] == x && board[1, 1] == x && board[2, 1] == x)

            {

                return x;

            }

 

            //second column is checked

            if (board[0, 1] == o && board[1, 1] == o && board[2, 1] == o)

            {

                return o;

            }

 

            //third column is checked

            if (board[0, 2] == x && board[1, 2] == x && board[2, 2] == x)

            {

                return x;

            }

 

            //third column is checked

            if (board[0, 2] == o && board[1, 2] == o && board[2, 2] == o)

            {

                return o;

            }

 

            //checking if the board is full

            bool boardFull = true;

 

            for (int i = 0; i < 3; i++)

            {

                for (int j = 0; j < 3; j++)

                {

                    //if an empty character exists

                    //board is not full

                    if (board[i, j] == _)

                    {

                        boardFull = false;

                    }

                }

            }

 

            if (boardFull)

            {

                return T;

            }

 

            //return space if nothing matches

            return  ;

        }

 

        static void Main(string[] args)

        {

            char[,] board = { { _, _, _ }, { _, _, _ }, { _, _, _ } };

 

            Player [] players = new Player[2];

 

            for (int i = 0; i < players.Length; i++)

            {

                Console.WriteLine(Please enter your name: );

                string name = Console.ReadLine();

 

                Console.WriteLine(Please choose your symbol (X or O): );

                char symbol = Console.ReadLine()[0];

 

                players[i] = new Player(name, symbol);

            }

 

            Console.WriteLine(Welcome  + players[0].getName());

            Console.WriteLine(Welcome  + players[1].getName());

 

            while (true)

            {

                displayBoard(board);

 

                Console.WriteLine(Please enter your position  + players[0].getName() +  (row column): );

                string [] positions = Console.ReadLine().Split( );

 

                int[] pos = new int[2];

                pos[0] = Int32.Parse(positions[0]);

                pos[1] = Int32.Parse(positions[1]);

 

                if (pos[0] == -1 && pos[1] == -1)

                {

                    Console.WriteLine(Thank you for playing!!);

                    break;

                }

 

                board[pos[0], pos[1]] = players[0].getChar();

 

                displayBoard(board);

 

                char winningChar = checkBoard(board);

 

                if (winningChar !=  )

                {

                    if (players[0].getChar() == winningChar)

                    {

                        Console.WriteLine(Player  + players[0].getName() +  wins!!);

                        break;

                    }

 

                    else if (players[1].getChar() == winningChar)

                    {

                        Console.WriteLine(Player  + players[1].getName() +  wins!!);

                        break;

                    }

 

                    else if (winningChar == T)

                    {

                        Console.WriteLine(Its a tie!!);

                        break;

                    }

                }

 

                Console.WriteLine(Please enter your position  + players[1].getName() +  (row column): );

                string[] positions1 = Console.ReadLine().Split( );

 

                int[] pos1 = new int[2];

                pos1[0] = Int32.Parse(positions1[0]);

                pos1[1] = Int32.Parse(positions1[1]);

 

                if (pos[0] == -1 && pos[1] == -1)

                {

                    Console.WriteLine(Thank you for playing!!);

                    break;

                }

 

                board[pos1[0], pos1[1]] = players[1].getChar();

 

                char winningChar1 = checkBoard(board);

 

                if (winningChar1 !=  )

                {

                    if (players[0].getChar() == winningChar1)

                    {

                        Console.WriteLine(Player  + players[0].getName() +  wins!!);

                        break;

                    }

 

                    else if (players[1].getChar() == winningChar1)

                    {

                        Console.WriteLine(Player  + players[1].getName() +  wins!!);

                        break;

                    }

 

                    else if (winningChar1 == T)

                    {

                        Console.WriteLine(Its a tie!!);

                        break;

                    }

                }

 

                else

                {

                    continue;

                }

            }

        }

    }

}


answered by: anonymous
Add a comment
Know the answer?
Add Answer to:
Write a program to Simulate a game of tic tac toe in c#
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
  • 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...

  • 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...

  • 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...

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

  • Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players...

    Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. 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 Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should...

  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a 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 draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...

  • 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...

  • (C++) Please create a tic tac toe game. Must write a Player class to store each...

    (C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...

  • I need to create a Tic Tac Toe program in C++. These are the requirements Write...

    I need to create a Tic Tac Toe program in C++. These are the requirements Write a program that allows the computer to play TicTacToe against a human player or allow two human players to play one another. Implement the following conditions: The player that wins the current game goes first in the next round, and their symbol is X. The other player will be O. Keep track of the number of games played, wins, and draws for each player....

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