Question

In: Computer Science

In C# A Tic Tac Toe program that uses functions and arrays. The program should start...

In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!

Solutions

Expert Solution

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication7
{
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)
{
string PlayerOneName, PlayerTwoName;
string playAgain;
Console.Write("What is the name of player one? ");
PlayerOneName = Console.ReadLine();
Console.Write("What is the name of player two? ");
PlayerTwoName = Console.ReadLine();
do
{
Console.WriteLine("\n{0}'s Symbol : X and {1}'s Symbol : O", PlayerOneName, PlayerTwoName);
Console.WriteLine("\n");
do
{
  
  
Board();// calling the board Function
if (player % 2 == 0)//checking the chance of the player
{
Console.WriteLine("\n{0} Chance", PlayerTwoName);
}
else
{
Console.WriteLine("\n{0} Chance", PlayerOneName);
}
Console.Write("\nEnter you move from 1 to 9: ");
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
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
{
if (((player % 2) + 1) == 1)
Console.WriteLine("{0} has won", PlayerOneName);
else
Console.WriteLine("{0} has won", PlayerTwoName);
}
else// if flag value is -1 the match will be draw and no one is winner
{
Console.WriteLine(" Game is Draw");
}
Console.Write("Do you want to play again? y/n: ");
playAgain = Console.ReadLine();
if (playAgain == "y" || playAgain == "Y")
{
clearBoard();
Console.Clear();// whenever loop will be again start then screen will be clear
}
} while (playAgain != "n");
Console.ReadLine();
}
public static void clearBoard()
{
arr[0] = '1';
arr[1] = '2';
arr[2] = '3';
arr[3] = '4';
arr[4] = '5';
arr[5] = '6';
arr[6] = '7';
arr[7] = '8';
arr[8] = '9';
}
// 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;
}
}
}
  
}

outputs

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


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...
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...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications...
c++ //Tic tac toe lab. Topics: 2D arrays, classes. // Part 1 //Implement the following specifications for a tic tac toe game object: class tictactoeGame { public: char boardConfig[3][3]; // two dimensional array stores current board configuration // Constructor: // set boardConfig[i][j] to be ' ' (the space character) // for 0<= i <= 2, 0<= j <= 2 tictactoeGame() { //fill this in } //put an 'X' character at the given location bool placeX(int x, int y) { //fill...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
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 (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
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
I am having an issue with the Java program with a tic tac toe. it isn't...
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of...
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O)...
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O) 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 players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that: Displays the contents...
Perk Company produces three products: Tic, Tac, and Toe. Tic requires 90 machine setups, Tac requires...
Perk Company produces three products: Tic, Tac, and Toe. Tic requires 90 machine setups, Tac requires 80 setups, and Toe requires 330 setups. Berk has identified an activity cost pool with allocated overhead of $18,000 for which the cost driver is machine setups. How much overhead is assigned to the Tic product?
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT