Question

(Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the...

(Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won and whether it’s a draw.

I want this in C#

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

There are 2 files required for this game to run. 1st file is TicTacToe.cs and 2nd is the driver file. Both the file codes are posted below in their respective tables. You are required to create a Windows Console C# Project in visual studio and then create 2 class files named TicTacToe.cs and another as Program.cs and then copy the below codes and paste into these files one by one.

TicTacToe.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tic_Tac_Toe
{
public class TicTacToe
{
private int[,] board;
private bool isFirstPlayer;
private int numMoves;

public TicTacToe()
{
board = new int[,] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
isFirstPlayer = true;
numMoves = 0;
}

public bool isFirstPlayerTurn()
{
return isFirstPlayer;
}

public void printInstructions()
{
Console.WriteLine("--- Welcome to the game of Tic-Tac-Toe ---");
Console.WriteLine();
Console.WriteLine("Instructions.");
Console.WriteLine("-------------------------------------------------");
Console.WriteLine("1) The game will be played between 2 human players.");
Console.WriteLine("2) The 1st Player would be represented by 1's and the");
Console.WriteLine(" 2nd player will be represented by 2's on the board");
Console.WriteLine("3) Each player will have to input a Row number and");
Console.WriteLine(" and column number to mark his position on the board");
Console.WriteLine("4) The game will continue untill one of the player");
Console.WriteLine(" wins or there happens a tie.");
Console.WriteLine("-------------------------------------------------");
Console.WriteLine();
Console.Write("Press any key to begin the game ...");
Console.ReadKey();
Console.WriteLine(" ");
}

public void printBoard()
{
  

Console.WriteLine("  " + board[0, 0] + "  |  " + board[0, 1] + "  | " + board[0, 2]);
            Console.WriteLine("_____|_____|_____");
            Console.WriteLine("     |     |");
            Console.WriteLine("  " + board[1, 0] + "  |  " + board[1, 1] + "  | " + board[1, 2]);
            Console.WriteLine("_____|_____|_____");
            Console.WriteLine("     |     |");
            Console.WriteLine("  " + board[2, 0] + "  |  " + board[2, 1] + "  | " + board[2, 2]);

}

public bool selectCell(int row, int col)
{
if(isValidMove(row, col))
{
if (board[row - 1, col - 1] == 0)
{
if (isFirstPlayer)
{
board[row - 1, col - 1] = 1;
isFirstPlayer = false;
}
  
else
{
board[row - 1, col - 1] = 2;
isFirstPlayer = true;
}

numMoves++;
return true;

}
else
{
Console.WriteLine("Sorry! That cell is already occupied!");
return false;
}
  
}
return false;

}

private bool isValidMove(int row, int col)
{
if ((row > 0 && row <= 3) && (col > 0 && col <= 3))
return true;
else
{
Console.WriteLine("That was an invalid move! Row and column must be selected in range 1 to 3");
return false;
}
  
}

public int checkWinner()
{
if( (board[0,0] == board[0,1]) && (board[0,0] == board[0,2]) && (board[0, 0] != 0) )
return board[0, 0];

else if((board[1, 0] == board[1, 1]) && (board[1, 0] == board[1, 2]) && (board[1, 0] != 0))
return board[1, 0];

else if ((board[2, 0] == board[2, 1]) && (board[2, 0] == board[2, 2]) && (board[2, 0] != 0))
return board[2, 0];

else if ((board[0, 0] == board[1, 0]) && (board[0, 0] == board[2, 0]) && (board[0, 0] != 0))
return board[0, 0];

else if ((board[0, 1] == board[1, 1]) && (board[0, 1] == board[2, 1]) && (board[0, 1] != 0))
return board[0, 1];

else if ((board[0, 2] == board[1, 2]) && (board[0, 2] == board[2, 2]) && (board[0, 2] != 0))
return board[0, 2];

else if ((board[0, 0] == board[1, 1]) && (board[0, 0] == board[2, 2]) && (board[0, 0] != 0))
return board[0, 0];

else if ((board[2, 2] == board[1, 1]) && (board[2, 2] == board[2, 0]) && (board[2, 2] != 0))
return board[2, 2];
else
{
if (numMoves == 9)
return -1;
else
return 0;
}

}
}
}


Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tic_Tac_Toe
{
class Program
{
static void Main(string[] args)
{
TicTacToe game = new TicTacToe();
game.printInstructions();
int row = 0, col = 0;
int winner = 0;
while(winner == 0)
{
game.printBoard();
Console.WriteLine();

if(game.isFirstPlayerTurn())
Console.WriteLine("** Player 1 Turn **");
else
Console.WriteLine("** Player 2 Turn **");

Console.WriteLine();
Console.Write("Enter the Row #: ");
int.TryParse(Console.ReadLine(), out row);
Console.Write("Enter the Column #: ");
int.TryParse(Console.ReadLine(), out col);

bool validMove = game.selectCell(row, col);
if (!validMove)
Console.WriteLine("Please re-try.");
else
{
winner = game.checkWinner();

if (winner == 1)
{
game.printBoard();
Console.WriteLine(" ******* Player 1 wins the Game *******");
}
  
else if(winner == 2)
{
game.printBoard();
Console.WriteLine(" ******* Player 2 wins the Game *******");
}
  
else if(winner == -1)
{
game.printBoard();
Console.WriteLine(" Oops! It was a tie.");
}
  
}
  
Console.WriteLine();

}

Console.ReadKey();
}
}
}

OUTPUT

CAProgram Workspace Visual Studio ProjectsC Sharp ProjectsTic-Tac-ToeTic-Tac-Toelbin -Welcome to the game of Tic-Tac-Toe nstructions, ) The game will be played between 2 human players. ) The 1st Player would be represented by 1s and the ) Each player will have to input a Row number and ) The game will continue untill one of the player 2nd player will be represented by 2s on the board and column number to mark his position on the board wins or there happens a tie ress any key to begin the game * Player 1 Turn ** nter the Row #: 2 nter the Column #: 3 * Player 2 Turn **

Enter the Row #: 3 Enter the Column #: 3 1 ** Player 1 Turn ** Enter the Row #: 2 Enter the Column #: 1 e 11 ** Player 2 Turn ** Enter the Row #: 1 Enter the Column #: 1 2 e le | 2 ** Player 1 Turn **

2 1 e 1 e 2 * Player 1 Turn** Enter the Row #: 2 Enter the Column #: 2 2 1 1 1 e 2 Player 1 wins the Game

NOTE: If there is some issue with slash n and slash t (escape sequences) i.e., any output format related issues then please comment in the comment box and I will post the code screenshots.

Add a comment
Know the answer?
Add Answer to:
(Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the...
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) 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...

  • Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth...

    Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when...

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

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

  • PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1....

    PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...

  • Write a c program that will allow two users to play a tic-tac-toe game. You should...

    Write a c program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assue they know how to play the game). Prompt first player(X) to enter their first move. .Then draw the gameboard showing the move. .Prompt the second player(O) to enter their first move. . Then draw the gameboard showing both moves. And so on...through 9 moves. You will need to:...

  • 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 :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():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)    ...

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

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

  • I need help developing this app using java in android studio Assignment: Tic-Tac-Toe app User interface...

    I need help developing this app using java in android studio Assignment: Tic-Tac-Toe app User interface Operation The app allows the user to play a game of Tic-Tac-Toe. The user can click the New Game button at any time to start a new game. The app displays other messages to the user as the game progresses such as (1) whose turn it is, (2) if a player wins, and (3) if the game ends in a tie. Specifications The app...

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