Question

In: Computer Science

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

C# (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. If you feel ambitious, modify your app so that the computer makes the moves for one of the players. Also, allow the player to specify whether he or she wants to go first or second. If you feel exceptionally ambitious, develop an app that will play three-dimensional Tic-Tac-Toe on a 4-by-4-by-4 board. (Use Console Clear)

Solutions

Expert Solution

Code -

using System;

using System.Threading;

namespace TIC_TAC_TOE

{

class Program

{

//by default position array is define from 0-9 and no use of zero

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

static int player = 1; // player 1 is will play first so set it as default

static int positionChoice; //position where player have to mark

//variable to check win
static int checkWin = 0;

static void Main(string[] args)

{

do

{

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

Console.WriteLine("Player1 mark is : X and Player2 mark is : 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");

createBoard();// calling the board Function

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

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

if (position[positionChoice] != 'X' && position[positionChoice] != 'O')

{

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

{

position[positionChoice] = 'O';

player++;

}

else

{

position[positionChoice] = '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}", positionChoice, position[positionChoice]);

Console.WriteLine("\n");

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

Thread.Sleep(2000);

}

checkWin = checkPlayerWin();// calling of check win

} while (checkWin != 1 && checkWin != -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

createBoard();// getting filled board again

if (checkWin == 1)// if checkWin 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 checkWin value is -1 the match will be draw and no one is winner

{

Console.WriteLine("Draw");

}

Console.ReadLine();

}

// createBoard method which create board

private static void createBoard()

{

Console.WriteLine(" | | ");

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

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

Console.WriteLine(" | | ");

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

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

Console.WriteLine(" | | ");

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

Console.WriteLine(" | | ");

}

//method to check if any play have won or not

private static int checkPlayerWin()

{

#region Horzontal Winning Condtion

//check for first row win   

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

{

return 1;

}

//check for second row win

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

{

return 1;

}

//check for third row win
else if (position[6] == position[7] && position[7] == position[8])

{

return 1;

}

#endregion

#region check for vertical wining condition

//Winning Condition For First Column   

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

{

return 1;

}

//Winning Condition For Second Column

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

{

return 1;

}

//Winning Condition For Third Column

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

{

return 1;

}

#endregion

#region check for diagnol winning condition

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

{

return 1;

}

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

{

return 1;

}

#endregion

#region check for draw

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

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

{

return -1;

}

#endregion

else

{

return 0;

}

}

}

}

Screenshots -

pls do give a like , thanks


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board. This game should involve one human player vs....
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A...
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board. This game should involve one human player vs....
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
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 the row...
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
explain a pseudocode for tic tac toe in c++ between a computer and a player in...
explain a pseudocode for tic tac toe in c++ between a computer and a player in words
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT