Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEEDED IN C# Write a program to play Tic-Tac-Toe. The game is designed for two h

ID: 3695386 • Letter: N

Question

NEEDED IN C#

Write a program to play Tic-Tac-Toe. The game is designed for two human players (Player 1 and Player 2). When the game begins, initialize the empty 3 by 3 game board to all 0s. These 0s indicate that none of the 9 squares is taken. The two players take turns to pick squares. Wherever Player 1 picks, place a 1 in the specified square. Similarly, place a 2 wherever the second player picks. A player must pick a square that is empty. If the player picks a square that is already taken, the program should ask the player to pick another square. The player picks a square by entering the row number (1, 2 or 3) and the column number (1, 2 or 3) of the square he wants. You can assume that the user always enters valid row number and column number so you do not have to write code to handle row or column number less than 1 or greater than 3. Every time a square is picked, the computer should determine whether the game has been won and announce the winner if necessary. A player wins if he occupies all three squares in a row, a column, or a diagonal. If there is no winner after the whole game board is full, announce that the game is a draw.

Explanation / Answer

Tic-Tac-Toe Program:    Iam using c# console application to develop the Tic Tac Toe game as follows.

using System;

  using System.Collections.Generic;

  using System.Linq;

using System.Text;

namespace TicTacToe

{

class Class1

{

  public string[,] tictactoeArray = new string[3, 3] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };

  public void Display()

{

Console.WriteLine(" {0} {1} {2} ", tictactoeArray[0, 0], tictactoeArray[0, 1], tictactoeArray[0, 2]);

  Console.WriteLine("{0} {1} {2} ", tictactoeArray[1, 0], tictactoeArray[1, 1], tictactoeArray[1, 2]);

  Console.WriteLine("{0} {1} {2} ", tictactoeArray[2, 0], tictactoeArray[2, 1], tictactoeArray[2, 2]);

}

}

}

This is the main code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace TicTacToe

{

class Program

{

static void Main(string[] args)

{

  Console.WriteLine("Welcome to Tic Tac Toe, X goes first.");

  Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

  Class1 Obj1;

Obj1 = new Class1();

Obj1.Display();

bool isXToMove = true;

string input = Console.ReadLine();

do

{

if (input == "1" && isXToMove == true)

{

Obj1.tictactoeArray[0, 0] = "X";

Obj1.Display();

isXToMove = false;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "2" && isXToMove == true)

{

Obj1.tictactoeArray[0, 1] = "X";

Obj1.Display();

  isXToMove = false;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "3" && isXToMove == true)

{

  Obj1.tictactoeArray[0, 2] = "X";

  Obj1.Display();

  isXToMove = false;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

if (input == "4" && isXToMove == true)

{

  Obj1.tictactoeArray[1, 0] = "X";

  Obj1.Display();

  isXToMove = false;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "5" && isXToMove == true)

{

  Obj1.tictactoeArray[1, 1] = "X";

  Obj1.Display();

isXToMove = false;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "6" && isXToMove == true)

{

  Obj1.tictactoeArray[1, 2] = "X";

  Obj1.Display();

  isXToMove = false;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "7" && isXToMove == true)

{

Obj1.tictactoeArray[2, 0] = "X";

Obj1.Display();

isXToMove = false;

  Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "8" && isXToMove == true)

{

Obj1.tictactoeArray[2, 1] = "X";

Obj1.Display();

  isXToMove = false;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "9" && isXToMove == true)

{

Obj1.tictactoeArray[2, 2] = "X";

Obj1.Display();

isXToMove = false;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "1" && isXToMove == false)

{

  Obj1.tictactoeArray[0, 0] = "O";

Obj1.Display();

  isXToMove = true;

  Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "2" && isXToMove == false)

{

Obj1.tictactoeArray[0, 1] = "O";

Obj1.Display();

isXToMove = true;

  Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

if (input == "3" && isXToMove == false)

{

  Obj1.tictactoeArray[0, 2] = "O";

  Obj1.Display();

  isXToMove = true;

  Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

if (input == "4" && isXToMove == false)

{

  Obj1.tictactoeArray[1, 0] = "O";

  Obj1.Display();

  isXToMove = true;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "6" && isXToMove == false)

{

Obj1.tictactoeArray[1, 2] = "O";

Obj1.Display();

  isXToMove = true;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

if (input == "7" && isXToMove == false)

{

  Obj1.tictactoeArray[2, 0] = "O";

  Obj1.Display();

isXToMove = true;

Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

if (input == "8" && isXToMove == false)

{

  Obj1.tictactoeArray[2, 1] = "O";

  Obj1.Display();

isXToMove = true;

  Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

  if (input == "9" && isXToMove == false)

{

Obj1.tictactoeArray[2, 2] = "O";

Obj1.Display();

isXToMove = true;

  Console.WriteLine("Pick a number to select a space. To exit, type q or quit.");

}

if (input != "q" && input != "quit" && input != "1" && input != "2" && input != "3" && input != "4" && input != "5" && input != "6" && input != "7" && input != "8" && input != "9")

{

  Console.WriteLine("Please enter an unoccupied number, or press q or quit to exit");

Obj1.Display();

}

if (input == "quit" || input == "q")

{

  Environment.Exit(0);

}

input = Console.ReadLine();

}

  while (input != "q" || input != "quit");

Console.ReadLine();

}

bool BoardComplete = false;

}

}