This post contains a total of 12+ Hand-Picked C# Tic Tac Toe Examples with Source Code. All the Tic Tac Toe programs are made using C Sharp Programming Language.
You can use the source code of these programs for educational use with credits to the original owner.
Related Posts
Click a Code to Copy it.
1. By Fullhurt
Made by Fullhurt. AI C# Tic Tac Toe game, the program outputs different outcomes every time you run it. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[] cell = {1,2,3,4,5,6,7,8,9};
char[] output = {'1','2','3','4','5','6','7','8','9'};
int go = 0;
char xo;
for (int a = 1;go < 1;)
{
if ((output[0] == output[1] && output[1] == output[2]) || (output[3] == output[4] && output[4] == output[5]) || (output[6] == output[7] && output[7] == output[8]) || (output[0] == output[3] && output[3] == output[6]) || (output[1] == output[4] && output[4] == output[7]) || (output[2] == output[5] && output[5] == output[8]) || (output[0] == output[4] && output[4] == output[8]) || (output[2] == output[4] && output[4] == output[6]))
{
if (a % 2 == 0)
{
Console.WriteLine("X Win");
}
else
{
Console.WriteLine("O Win");
}
break;
}
Random hodr = new Random();
int hod = hodr.Next(0,9);
if (a % 2 == 0)
{
xo = 'O';
}
else
{
xo = 'X';
}
for (int i = 1; i < 10;)
{
if (cell[hod] == i)
{
output[hod] = xo;
cell[hod] = 0;
Console.WriteLine(" {0} | {1} | {2} \n-----------\n {3} | {4} | {5} {9}={10}\n-----------\n {6} | {7} | {8} \n\n", output[0], output[1], output[2], output[3], output[4], output[5], output[6], output[7], output[8], xo, i);
a++;
}
else if (cell[hod] != i)
{
i++;
}
else if (cell[hod] == 0)
{
}
}
}
}
}
}
2. By Omar Hach
Made by Omar Hach. Simple Tic Tac Toe game for two players, To play please: Enter the location of X (1 or 2 or 3), then enter the location of y (1 or 2 or 3). ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
//Put the action of player(o OR x) in the matrix
static string OXinput(int OX, int OY, string[] cases, string symbole)
{
int location = OX + 3 * OY - 4;
cases[location] = symbole;
Console.WriteLine(" -1---2---3-X\n" +
"1|" + cases[0] + "|" + cases[1] + "|" + cases[2] + "|\n" +
"2|" + cases[3] + "|" + cases[4] + "|" + cases[5] + "|\n" +
"3|" + cases[6] + "|" + cases[7] + "|" + cases[8] + "|\n" +
"Y ----------- \n");
return "00";
}
//Verify that a player has won.
static Boolean Win(string[] cases, string symbole)
{
//verify the center case
int distance = 0;
if (cases[4] == symbole)
{
//verify the first half cases then the oposit case.
for (int i = 0; i < 4; i++)
{
distance = 8 - i;
if (cases[i] == cases[distance] && cases[i] == symbole)
{
return true;
}
}
}
else
{
//verify the corner cases than the middle one.
int[] corner = { 2, 8, 6, 0, 4};
int constant = 0;
foreach (int j in corner)
{
if (cases[j] == symbole && cases[(j+constant)/2] == cases[constant] && cases[constant] == cases[j])
{
return true;
}
else if(j == 4)
{
break;
}
constant = j;
}
}
return false;
}
static void Main(string[] args)
{
// Declare and initialize variables and erray.
int locationOX;
int locationOY;
int count = 0;
string[] active_case = new string[2] {" x ", " o " };
string[] matrix = new string[9] { " ", " ", " ", " ", " ", " ", " ", " ", " "};
//show the matrix.
Console.WriteLine("Hello, Welcome to the X/O game.\n" +
" -1---2---3-X\n" +
"1| | | |\n" +
"2| | | |\n" +
"3| | | |\n" +
"Y ----------- \n" +
"~~~~~~It is player O turn~~~~~~\n" +
"to play please:");
//Receve and Display the player action.
while (Array.Exists(matrix, element => element == " "))
{
//Recive and Exament the locations variables.
Console.WriteLine("entre the location of X");
locationOX = Convert.ToInt16(Console.ReadLine());
while (locationOX > 3 && locationOX < 1)
{
Console.WriteLine("Error please entre the location again\n");
locationOX = Convert.ToInt16(Console.ReadLine());
}
Console.WriteLine("and the location of Y");
locationOY = Convert.ToInt16(Console.ReadLine());
while (locationOY > 3 && locationOY < 1)
{
Console.WriteLine("Error please entre the location again\n");
locationOY = Convert.ToInt16(Console.ReadLine());
}
//Verify if the case is empty
if (matrix[locationOX + 3 * locationOY - 4] == " ")
{
//Put and Show players action in the the matrix.
if (count % 2 == 0)
{
OXinput(locationOX, locationOY, matrix, active_case[1]);
//Verify that player O won.
if (Win(matrix, active_case[1]))
{
System.Console.WriteLine("~~~~~~congratulation player O Win!!~~~~~~");
break;
}
Console.WriteLine("~~~~~~It is player X turn~~~~~~\n");
}
else
{
OXinput(locationOX, locationOY, matrix, active_case[0]);
//Verify that player X won.
if (Win(matrix, active_case[0]))
{
System.Console.WriteLine("~~~~~~congratulation player X Win!!~~~~~~");
break;
}
Console.WriteLine("~~~~~~It is player O turn~~~~~~\n");
}
}
//make sure that the is a location
else
{
Console.WriteLine("Error this case is allready full, please chose another one\n");
continue;
}
count++;
}
System.Console.WriteLine("Press any key to exit...");
System.Console.ReadKey();
}
}
}
3. By Sveiki Ata
Made by Sveiki Ata. This program has two options, you can play with computer or you can play with another player, choose with whom you want to play and after that enter your name and start the game. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TicTacToe
{
class Program
{
/*
Seems that the game does not work on this app since
SoloLearn does not allow an input to be made by the player when the application is running (-_-)
*/
static string gameStart;
static bool draw = false;
static bool inputCorrect = true;
static string playerOne;
static string playerTwo;
public static int firstPlayerInput;
public static int secondPlayerInput = 0;
static char[,] playField =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
static char[,] playFieldDefault =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
static string replay = "";
static bool win = false;
static void Main(string[] args)
{
Random botTurn = new Random();
Console.WriteLine("Welcome to Tic Tac Toe");
Console.WriteLine("Press 1 to play with Computer or press 2 to play 2 player game");
try
{
gameStart = Console.ReadLine();
}
catch (Exception)
{
Console.WriteLine("Input Incorrect, try again");
}
if (gameStart.Equals("2"))
{
Console.WriteLine("Enter first players name: ");
playerOne = Console.ReadLine();
Console.WriteLine("Enter second players name: ");
playerTwo = Console.ReadLine();
while (win == false && draw == false)
{
///////////////////////////////////////////////////////////
// Leaves "O's"
PlayFieldPrint();
Console.WriteLine("__________________________________________________");
Console.WriteLine(playerOne + "'s turn. Enter your number:");
try
{
firstPlayerInput = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("The input was incorrect, please enter a single digit");
}
CheckForSameValues();
while (!inputCorrect)
{
Console.WriteLine("The Field has already been taken, please repeat, type it again");
firstPlayerInput = Convert.ToInt32(Console.ReadLine());
CheckForSameValues();
}
if (inputCorrect)
{
PlayField();
WinCondition();
if (win == true)
{
FirstPlayerWinAnouncement();
}
DrawAnnouncement();
}
/***********************************************************************************/
PlayFieldPrint();
Console.WriteLine("__________________________________________________");
///// Leaves "X's"
Console.WriteLine(playerTwo + "'s turn. Enter your number:");
try
{
secondPlayerInput = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("The input was incorrect, please enter a single digit");
}
CheckForSameValues();
while (!inputCorrect)
{
Console.WriteLine("The Field has already been taken, please repeat, type it again");
secondPlayerInput = Convert.ToInt32(Console.ReadLine());
CheckForSameValues();
}
if (inputCorrect)
{
PlayField();
WinCondition();
if (win == true)
{
SecondPlayerWinAnouncement();
}
DrawAnnouncement();
}
}
}
else if(gameStart.Equals("1"))
{
playerTwo = "Computer";
Console.WriteLine("Enter your name: ");
playerOne = Console.ReadLine();
while (win == false && draw == false)
{
///////////////////////////////////////////////////////////
// Leaves "O's"
PlayFieldPrint();
Console.WriteLine("__________________________________________________");
if (secondPlayerInput == 0)
{
Console.WriteLine("It's your turn");
}
else
{
Console.WriteLine("Computer has chosen: {0}. It's your turn: ", secondPlayerInput);
}
try
{
firstPlayerInput = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("The input was incorrect, please enter a single digit");
}
CheckForSameValues();
while (!inputCorrect)
{
Console.WriteLine("The Field has already been taken, please repeat, type it again");
firstPlayerInput = Convert.ToInt32(Console.ReadLine());
CheckForSameValues();
}
if (inputCorrect)
{
PlayField();
WinCondition();
if (win == true)
{
FirstPlayerWinAnouncement();
}
DrawAnnouncement();
}
/***********************************************************************************/
PlayFieldPrint();
Console.WriteLine("__________________________________________________");
///// Leaves "X's"
secondPlayerInput = botTurn.Next(1, 9);
CheckForSameValues();
while (!inputCorrect)
{
secondPlayerInput = botTurn.Next(1, 9);
CheckForSameValues();
}
if (inputCorrect)
{
PlayField();
WinCondition();
if (win == true)
{
SecondPlayerWinAnouncement();
}
DrawAnnouncement();
}
}
}
else
{
Console.WriteLine("Input incorrect, try again");
}
}
static bool WinCondition()
{
if (WinningPositions() == true)
{
win = true;
}
return win;
}
static void FirstPlayerWinAnouncement()
{
if (win == true)
{
PlayFieldPrint();
Console.WriteLine("{0} has won the game", playerOne);
Console.WriteLine("Press enter to play again");
replay = Console.ReadLine();
if (replay.Equals(""))
{
win = false;
playField.Equals(DefaultPositions());
firstPlayerInput = 0;
secondPlayerInput = 0;
}
else
{
win = true;
}
}
}
static void SecondPlayerWinAnouncement()
{
if (win == true)
{
PlayFieldPrint();
Console.WriteLine("{0} has won the game", playerTwo);
Console.WriteLine("Press enter to play again");
replay = Console.ReadLine();
if (replay.Equals(""))
{
win = false;
playField.Equals(DefaultPositions());
firstPlayerInput = 0;
secondPlayerInput = 0;
}
else
{
win = true;
}
}
}
static void DrawAnnouncement()
{
if (DrawConditions() == true)
{
PlayFieldPrint();
Console.WriteLine("The game has ended with a draw");
Console.WriteLine("To play again press enter");
replay = Console.ReadLine();
if (replay.Equals(""))
{
draw = false;
playField.Equals(DefaultPositions());
firstPlayerInput = 0;
secondPlayerInput = 0;
}
else
{
draw = true;
}
}
}
static bool DrawConditions()
{
if (
playField[0, 0] != '1' &&
playField[0, 1] != '2' &&
playField[0, 2] != '3' &&
playField[1, 0] != '4' &&
playField[1, 1] != '5' &&
playField[1, 2] != '6' &&
playField[2, 0] != '7' &&
playField[2, 1] != '8' &&
playField[2, 2] != '9'
)
{
draw = true;
}
return draw;
}
static char[,] DefaultPositions()
{
playField[0, 0] = '1';
playField[0, 1] = '2';
playField[0, 2] = '3';
playField[1, 0] = '4';
playField[1, 1] = '5';
playField[1, 2] = '6';
playField[2, 0] = '7';
playField[2, 1] = '8';
playField[2, 2] = '9';
return playField;
}
static bool CheckForSameValues()
{
/************************************************************************************************************************************************************/
if ((firstPlayerInput == 1) && (playField[0, 0] == '1')) { inputCorrect = true; }
else if ((firstPlayerInput == 2) && (playField[0, 1] == '2')) { inputCorrect = true; }
else if ((firstPlayerInput == 3) && (playField[0, 2] == '3')) { inputCorrect = true; }
else if ((firstPlayerInput == 4) && (playField[1, 0] == '4')) { inputCorrect = true; }
else if ((firstPlayerInput == 5) && (playField[1, 1] == '5')) { inputCorrect = true; }
else if ((firstPlayerInput == 6) && (playField[1, 2] == '6')) { inputCorrect = true; }
else if ((firstPlayerInput == 7) && (playField[2, 0] == '7')) { inputCorrect = true; }
else if ((firstPlayerInput == 8) && (playField[2, 1] == '8')) { inputCorrect = true; }
else if ((firstPlayerInput == 9) && (playField[2, 2] == '9')) { inputCorrect = true; }
else if ((secondPlayerInput == 1) && (playField[0, 0] == '1')) { inputCorrect = true; }
else if ((secondPlayerInput == 2) && (playField[0, 1] == '2')) { inputCorrect = true; }
else if ((secondPlayerInput == 3) && (playField[0, 2] == '3')) { inputCorrect = true; }
else if ((secondPlayerInput == 4) && (playField[1, 0] == '4')) { inputCorrect = true; }
else if ((secondPlayerInput == 5) && (playField[1, 1] == '5')) { inputCorrect = true; }
else if ((secondPlayerInput == 6) && (playField[1, 2] == '6')) { inputCorrect = true; }
else if ((secondPlayerInput == 7) && (playField[2, 0] == '7')) { inputCorrect = true; }
else if ((secondPlayerInput == 8) && (playField[2, 1] == '8')) { inputCorrect = true; }
else if ((secondPlayerInput == 9) && (playField[2, 2] == '9')) { inputCorrect = true; }
else
{
inputCorrect = false;
}
return inputCorrect;
}
static void PlayField()
{
{
switch (firstPlayerInput)
{
case 1:
playField[0, 0] = 'O';
break;
case 2:
playField[0, 1] = 'O';
break;
case 3:
playField[0, 2] = 'O';
break;
case 4:
playField[1, 0] = 'O';
break;
case 5:
playField[1, 1] = 'O';
break;
case 6:
playField[1, 2] = 'O';
break;
case 7:
playField[2, 0] = 'O';
break;
case 8:
playField[2, 1] = 'O';
break;
case 9:
playField[2, 2] = 'O';
break;
default:
Console.WriteLine("");
break;
}
switch (secondPlayerInput)
{
case 1:
playField[0, 0] = 'X';
break;
case 2:
playField[0, 1] = 'X';
break;
case 3:
playField[0, 2] = 'X';
break;
case 4:
playField[1, 0] = 'X';
break;
case 5:
playField[1, 1] = 'X';
break;
case 6:
playField[1, 2] = 'X';
break;
case 7:
playField[2, 0] = 'X';
break;
case 8:
playField[2, 1] = 'X';
break;
case 9:
playField[2, 2] = 'X';
break;
default:
Console.WriteLine("");
break;
}
}
}
static bool WinningPositions()
{
//*************************************************************************************************
//top bottom middle
if (playField[0, 1].Equals('X') && playField[1, 1].Equals('X') && playField[2, 1].Equals('X'))
{
win = true;
}
//top bottom right
else if (playField[0, 2].Equals('X') && playField[1, 2].Equals('X') && playField[2, 2].Equals('X'))
{
win = true;
}
//top bottom left
else if (playField[0, 0].Equals('X') && playField[1, 0].Equals('X') && playField[2, 0].Equals('X'))
{
win = true;
}
//*************************************************************************************************
// left right upper line
else if (playField[0, 0].Equals('X') && playField[0, 1].Equals('X') && playField[0, 2].Equals('X'))
{
win = true;
}
// left right middle line
else if (playField[1, 0].Equals('X') && playField[1, 1].Equals('X') && playField[1, 2].Equals('X'))
{
win = true;
}
//left right bottom
else if (playField[2, 0].Equals('X') && playField[2, 1].Equals('X') && playField[2, 2].Equals('X'))
{
win = true;
}
//*************************************************************************************************
///diagonaly bottom left to right top
else if (playField[2, 0].Equals('X') && playField[1, 1].Equals('X') && playField[0, 2].Equals('X'))
{
win = true;
}
///diagonaly bottom right to left top
else if (playField[0, 0].Equals('X') && playField[1, 1].Equals('X') && playField[2, 2].Equals('X'))
{
win = true;
}
//*************************************************************************************************
//top bottom middle
else if (playField[0, 1].Equals('O') && playField[1, 1].Equals('O') && playField[2, 1].Equals('O'))
{
win = true;
}
//top bottom right
else if (playField[0, 2].Equals('O') && playField[1, 2].Equals('O') && playField[2, 2].Equals('O'))
{
win = true;
}
//top bottom left
else if (playField[0, 0].Equals('O') && playField[1, 0].Equals('O') && playField[2, 0].Equals('O'))
{
win = true;
}
//*************************************************************************************************
// left right upper line
else if (playField[0, 0].Equals('O') && playField[0, 1].Equals('O') && playField[0, 2].Equals('O'))
{
win = true;
}
// left right middle line
else if (playField[1, 0].Equals('O') && playField[1, 1].Equals('O') && playField[1, 2].Equals('O'))
{
win = true;
}
//left right bottom
else if (playField[2, 0].Equals('O') && playField[2, 1].Equals('O') && playField[2, 2].Equals('O'))
{
win = true;
}
//*************************************************************************************************
///diagonaly bottom left to right top
else if (playField[2, 0].Equals('O') && playField[1, 1].Equals('O') && playField[0, 2].Equals('O'))
{
win = true;
}
///diagonaly bottom right to left top
else if (playField[0, 0].Equals('O') && playField[1, 1].Equals('O') && playField[2, 2].Equals('O'))
{
win = true;
}
else
{
win = false;
}
return win;
}
static void PlayFieldPrint()
{
Console.Clear();
Console.WriteLine(" ");
Console.WriteLine(" {0} | {1} | {2}", playField[0, 0], playField[0, 1], playField[0, 2]);
Console.WriteLine("---|---|---");
Console.WriteLine(" {0} | {1} | {2}", playField[1, 0], playField[1, 1], playField[1, 2]);
Console.WriteLine("---|---|---");
Console.WriteLine(" {0} | {1} | {2}", playField[2, 0], playField[2, 1], playField[2, 2]);
Console.WriteLine(" ");
}
}
}
4. By Tj Moua
Made by Tj Moua. This is a 2 players, input dependent console Tic Tac Toe game. ( Source )
sing System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using static System.Console;
namespace TicTacToe
{
class Program
{
//Making a habit of declaring my variables right from the get-go.
static string[] mark = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", };
static int player = 1;
static long decision = 0;
static int checkWin = 0;
static bool game = true;
static void Main(string[] args)
{
while (game)
{
do
{
Clear();
Opening();
try
{
decision = Int64.Parse(ReadLine());
if (mark[decision] != "X" && mark[decision] != "O")
{
if (player % 2 == 0)
{
mark[decision] = "O";
player++;
Beep();
}
else
{
mark[decision] = "X";
player++;
Beep();
}
}
}
catch /*Had my fun with this try-catch deal, but not displaying my message when user put invalid inputs*/
{
WriteLine("Invalid input, follow the instructions, please.");
}
checkWin = CheckStatusOfGame();
} while (checkWin != 1 && checkWin != -1);
Clear();
DrawBoard();
if (checkWin == 1)
{
WriteLine("Player {0} has won!", (player % 2) + 1);
}
if (checkWin == -1)
{
WriteLine("!Draw!");
}
WriteLine("\n");
WriteLine("Type 'yes' to restart game or 'no' to end program. ");
var input = Convert.ToString(ReadLine().ToLower());
try
{
if (input == "yes")
{
Clear();
ResetWholeGame();
continue;
}
if (input == "no")
WriteLine("\n\n\n\nGOOD\nBYE");
break;
}
catch
{
}
}
}
static void Opening()
{
var builder = new StringBuilder();
builder.Append('-', 117);
WriteLine("Player 1 will be 'X' and Player 2 will be 'O'\n\nDuring each player's turn, they may enter the number/placement they would like to mark.");
WriteLine(builder);
if (player % 2 == 0)
{
WriteLine("Player 2's Turn. ");
}
else
{
WriteLine("Player 1's Turn. ");
}
DrawBoard();
WriteLine("\n");
WriteLine(builder);
}
static void DrawBoard()
{
WriteLine(" ---------------------");
WriteLine(" | | | |");
WriteLine(" | {0} | {1} | {2} |", mark[1], mark[2], mark[3]);
WriteLine(" |______|______|_____|");
WriteLine(" | | | |");
WriteLine(" | {0} | {1} | {2} |", mark[4], mark[5], mark[6]);
WriteLine(" |______|______|_____|");
WriteLine(" | | | |");
WriteLine(" | {0} | {1} | {2} |", mark[7], mark[8], mark[9]);
WriteLine(" | | | |");
WriteLine(" ---------------------");
}
static int CheckStatusOfGame() //Checking the win/draw.
{
if (mark[1] == mark[2] &&
mark[2] == mark[3])
{
return 1;
}
if (mark[4] == mark[5] &&
mark[5] == mark[6])
{
return 1;
}
if (mark[7] == mark[8] &&
mark[8] == mark[9])
{
return 1;
}
if (mark[1] == mark[4] &&
mark[4] == mark[7])
{
return 1;
}
if (mark[2] == mark[5] &&
mark[5] == mark[8])
{
return 1;
}
if (mark[3] == mark[6] &&
mark[6] == mark[9])
{
return 1;
}
if (mark[1] == mark[5] &&
mark[5] == mark[9])
{
return 1;
}
if (mark[3] == mark[5] &&
mark[5] == mark[7])
{
return 1;
}
if (mark[1] != "1" &&
mark[2] != "2" &&
mark[3] != "3" &&
mark[4] != "4" &&
mark[5] != "5" &&
mark[6] != "6" &&
mark[7] != "7" &&
mark[8] != "8" &&
mark[9] != "9")
{
return -1;
}
else
{
return 0;
}
}
private static void ResetWholeGame()//Took awhile to incorporate a reset/restart game feature.
{
string[] boardReset =
{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", };
mark = boardReset;
}
}
}
5. By Shreyan Narayan Chowdhury
Made by Shreyan Narayan Chowdhury. ( Source )
using System;
using System.Net;
namespace ConsoleApp1
{
class Program
{
public static char[,] matrix = new char[3, 3] { {' ',' ',' ' }, { ' ', ' ' , ' ' }, { ' ', ' ', ' ' } };
static bool winner = false;
public static void Display()
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine();
for (int j = 0; j < 3; j++)
{
if (j < 2)
Console.Write(matrix[i, j] + "\t |");
else
Console.Write(matrix[i, j]);
}
Console.WriteLine();
if (i < 2)
Console.Write("--------------------------------");
}
}
public static void Move(char c)
{
Console.Write("Choose your move (row [ 1 -3 ] and column [ 1 - 3 ] :- ");
int row = Convert.ToInt32(Console.ReadLine());
int column = Convert.ToInt32(Console.ReadLine());
row--;
column--;
if (matrix[row, column] == 'X' || matrix[row, column] == 'O')
{
Console.Write("Are you illiterate?????? Giving you another try");
Move(c);
}
else
{
matrix[row,column] = c;
Display();
}
}
public static void check_Move(char c)
{
int rowCount = 0, columnCount = 0, leftDiagonalCount = 0, rightDiagonalCount = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (matrix[i, j] == c)
rowCount++;
if (matrix[j,i] == c)
columnCount++;
if (i == j && matrix[i, j] == c)
leftDiagonalCount++;
if (i == 3 - j - 1 && matrix[i, j] == c)
rightDiagonalCount++;
}
if (rowCount == 3 || columnCount == 3)
winner = true;
else
rowCount = columnCount = 0;
}
if ( rightDiagonalCount == 3 || leftDiagonalCount == 3 )
winner = true;
if( winner )
Console.Write(c + " Won....");
}
public static void PlayGame()
{
matrix = new char[3, 3] { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
for (int i = 1; i <= 9; i++)
{
if (!winner)
{
if (i % 2 == 0)
{
Move('O');
check_Move('O');
}
else
{
Move('X');
check_Move('X');
}
}
else
{
Console.WriteLine("Game Over..........");
break;
}
}
if (!winner)
Console.Write("Match Draw.............");
}
public static void Main(string[] args)
{
//main
char response;
do
{
PlayGame();
winner = false;
Console.WriteLine("Do you want to play again? (y for yes / n for no)");
response = (char)Console.Read();
if (response == 'y' || response == 'Y')
PlayGame();
}while (response == 'y' || response == 'Y');
}
}
}
6. By Justin Lynn
Made by Justin Lynn. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TicTacToe
{
class Program
{
//Run this in Visual Studio, it will not compile on SoloLearn.
static void NewGame()
{
//Board Setup
string oWins = "O WINS!!";
string xWins = "X WINS!!";
string placeholder = "-";
int rowSize = 3;
string[] row1 = new string[rowSize];
string[] row2 = new string[rowSize];
string[] row3 = new string[rowSize];
Console.WriteLine("Welcome to Tic Tac Toe!");
Console.WriteLine("=======================");
for (int i = 0; i < rowSize; i++)
{
row1[i] = placeholder;
Console.Write(placeholder);
}
Console.WriteLine();
for (int i = 0; i < rowSize; i++)
{
row2[i] = placeholder;
Console.Write(placeholder);
}
Console.WriteLine();
for (int i = 0; i < rowSize; i++)
{
row3[i] = placeholder;
Console.Write(placeholder);
}
////////////////////////////////////////
string x = "x";
string o = "o";
bool xTurn = true;
bool oTurn = true;
bool xWinner = false;
bool oWinner = false;
int input = 0;
//////////////////////////////////////////
while (!(xWinner == true && oWinner == true))
{//the game loop, runs the whole time while there is not a winner
if (xWinner == true || oWinner == true)
{
xTurn = false;
oTurn = false;
break;
}
do
{//the x loop
Console.SetCursorPosition(0, 5);
Console.WriteLine("Press a number on the numpad corresponding to where you want to place your character!");
Console.WriteLine("Player X go!");
if (xWinner == true || oWinner == true)
{
xTurn = false;
oTurn = false;
break;
}
try
{
input = Convert.ToInt32(Console.ReadLine());//convert the input and then proceed with the game
if (input == 1 && row3[0] != x && row3[0] != o)
{
Console.SetCursorPosition(0, 4);
Console.WriteLine(x);
row3[0] = x;
Console.SetCursorPosition(0, 8);
xTurn = false;
if (row1[0] == x && row2[0] == x && row3[0] == x)
{
xWinner = true;
}
else if (row1[1] == x && row2[1] == x && row3[1] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[2] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row1[1] == x && row1[2] == x)
{
xWinner = true;
}
else if (row2[0] == x && row2[1] == x && row2[2] == x)
{
xWinner = true;
}
else if (row3[0] == x && row3[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row2[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[1] == x && row3[0] == x)
{
xWinner = true;
}
}
else if (input == 2 && row3[1] != x && row3[1] != o)
{
Console.SetCursorPosition(1, 4);
Console.WriteLine(x);
row3[1] = x;
Console.SetCursorPosition(0, 8);
xTurn = false;
if (row1[0] == x && row2[0] == x && row3[0] == x)
{
xWinner = true;
}
else if (row1[1] == x && row2[1] == x && row3[1] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[2] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row1[1] == x && row1[2] == x)
{
xWinner = true;
}
else if (row2[0] == x && row2[1] == x && row2[2] == x)
{
xWinner = true;
}
else if (row3[0] == x && row3[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row2[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[1] == x && row3[0] == x)
{
xWinner = true;
}
}
else if (input == 3 && row3[2] != x && row3[2] != o)
{
Console.SetCursorPosition(2, 4);
Console.WriteLine(x);
row3[2] = x;
Console.SetCursorPosition(0, 8);
xTurn = false;
if (row1[0] == x && row2[0] == x && row3[0] == x)
{
xWinner = true;
}
else if (row1[1] == x && row2[1] == x && row3[1] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[2] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row1[1] == x && row1[2] == x)
{
xWinner = true;
}
else if (row2[0] == x && row2[1] == x && row2[2] == x)
{
xWinner = true;
}
else if (row3[0] == x && row3[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row2[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[1] == x && row3[0] == x)
{
xWinner = true;
}
}
else if (input == 4 && row2[0] != x && row2[0] != o)
{
Console.SetCursorPosition(0, 3);
Console.WriteLine(x);
row2[0] = x;
Console.SetCursorPosition(0, 8);
xTurn = false;
if (row1[0] == x && row2[0] == x && row3[0] == x)
{
xWinner = true;
}
else if (row1[1] == x && row2[1] == x && row3[1] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[2] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row1[1] == x && row1[2] == x)
{
xWinner = true;
}
else if (row2[0] == x && row2[1] == x && row2[2] == x)
{
xWinner = true;
}
else if (row3[0] == x && row3[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row2[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[1] == x && row3[0] == x)
{
xWinner = true;
}
}
else if (input == 5 && row2[1] != x && row2[1] != o)
{
Console.SetCursorPosition(1, 3);
Console.WriteLine(x);
row2[1] = x;
Console.SetCursorPosition(0, 8);
xTurn = false;
if (row1[0] == x && row2[0] == x && row3[0] == x)
{
xWinner = true;
}
else if (row1[1] == x && row2[1] == x && row3[1] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[2] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row1[1] == x && row1[2] == x)
{
xWinner = true;
}
else if (row2[0] == x && row2[1] == x && row2[2] == x)
{
xWinner = true;
}
else if (row3[0] == x && row3[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row2[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[1] == x && row3[0] == x)
{
xWinner = true;
}
}
else if (input == 6 && row2[2] != x && row2[2] != o)
{
Console.SetCursorPosition(2, 3);
Console.WriteLine(x);
row2[2] = x;
Console.SetCursorPosition(0, 8);
xTurn = false;
if (row1[0] == x && row2[0] == x && row3[0] == x)
{
xWinner = true;
}
else if (row1[1] == x && row2[1] == x && row3[1] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[2] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row1[1] == x && row1[2] == x)
{
xWinner = true;
}
else if (row2[0] == x && row2[1] == x && row2[2] == x)
{
xWinner = true;
}
else if (row3[0] == x && row3[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row2[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[1] == x && row3[0] == x)
{
xWinner = true;
}
}
else if (input == 7 && row1[0] != x && row1[0] != o)
{
Console.SetCursorPosition(0, 2);
Console.WriteLine(x);
row1[0] = x;
Console.SetCursorPosition(0, 8);
xTurn = false;
if (row1[0] == x && row2[0] == x && row3[0] == x)
{
xWinner = true;
}
else if (row1[1] == x && row2[1] == x && row3[1] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[2] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row1[1] == x && row1[2] == x)
{
xWinner = true;
}
else if (row2[0] == x && row2[1] == x && row2[2] == x)
{
xWinner = true;
}
else if (row3[0] == x && row3[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row2[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[1] == x && row3[0] == x)
{
xWinner = true;
}
}
else if (input == 8 && row1[1] != x && row1[1] != o)
{
Console.SetCursorPosition(1, 2);
Console.WriteLine(x);
row1[1] = x;
Console.SetCursorPosition(0, 8);
xTurn = false;
if (row1[0] == x && row2[0] == x && row3[0] == x)
{
xWinner = true;
}
else if (row1[1] == x && row2[1] == x && row3[1] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[2] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row1[1] == x && row1[2] == x)
{
xWinner = true;
}
else if (row2[0] == x && row2[1] == x && row2[2] == x)
{
xWinner = true;
}
else if (row3[0] == x && row3[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row2[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[1] == x && row3[0] == x)
{
xWinner = true;
}
}
else if (input == 9 && row1[2] != x && row1[2] != o)
{
Console.SetCursorPosition(2, 2);
Console.WriteLine(x);
row1[2] = x;
Console.SetCursorPosition(0, 8);
xTurn = false;
if (row1[0] == x && row2[0] == x && row3[0] == x)
{
xWinner = true;
}
else if (row1[1] == x && row2[1] == x && row3[1] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[2] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row1[1] == x && row1[2] == x)
{
xWinner = true;
}
else if (row2[0] == x && row2[1] == x && row2[2] == x)
{
xWinner = true;
}
else if (row3[0] == x && row3[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[0] == x && row2[1] == x && row3[2] == x)
{
xWinner = true;
}
else if (row1[2] == x && row2[1] == x && row3[0] == x)
{
xWinner = true;
}
}
else
{
Console.SetCursorPosition(0, 8);
Console.WriteLine("You can't do that! Pick another spot!");
Console.WriteLine(" ");
Console.ReadLine();
Console.SetCursorPosition(0, 7);
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
continue;
}
oTurn = true;
}//end try
catch (Exception)
{
//Display nothing
}
} while (xTurn == true);//end xTurn do/While
do
{//the o loop
Console.SetCursorPosition(0, 5);
Console.WriteLine("Press a number on the numpad corresponding to where you want to place your character!");
Console.WriteLine("Player O go!");
if (xWinner == true || oWinner == true)
{
xTurn = false;
oTurn = false;
break;
}
try
{
input = Convert.ToInt32(Console.ReadLine());
if (input == 1 && row3[0] != x && row3[0] != o)
{
Console.SetCursorPosition(0, 4);
Console.WriteLine(o);
row3[0] = o;
Console.SetCursorPosition(0, 8);
oTurn = false;
if (row1[0] == o && row2[0] == o && row3[0] == o)
{
oWinner = true;
}
else if (row1[1] == o && row2[1] == o && row3[1] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[2] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row1[1] == o && row1[2] == o)
{
oWinner = true;
}
else if (row2[0] == o && row2[1] == o && row2[2] == o)
{
oWinner = true;
}
else if (row3[0] == o && row3[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row2[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[1] == o && row3[0] == o)
{
oWinner = true;
}
}
else if (input == 2 && row3[1] != x && row3[1] != o)
{
Console.SetCursorPosition(1, 4);
Console.WriteLine(o);
row3[1] = o;
Console.SetCursorPosition(0, 8);
oTurn = false;
if (row1[0] == o && row2[0] == o && row3[0] == o)
{
oWinner = true;
}
else if (row1[1] == o && row2[1] == o && row3[1] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[2] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row1[1] == o && row1[2] == o)
{
oWinner = true;
}
else if (row2[0] == o && row2[1] == o && row2[2] == o)
{
oWinner = true;
}
else if (row3[0] == o && row3[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row2[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[1] == o && row3[0] == o)
{
oWinner = true;
}
}
else if (input == 3 && row3[2] != x && row3[2] != o)
{
Console.SetCursorPosition(2, 4);
Console.WriteLine(o);
row3[2] = o;
Console.SetCursorPosition(0, 8);
oTurn = false;
if (row1[0] == o && row2[0] == o && row3[0] == o)
{
oWinner = true;
}
else if (row1[1] == o && row2[1] == o && row3[1] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[2] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row1[1] == o && row1[2] == o)
{
oWinner = true;
}
else if (row2[0] == o && row2[1] == o && row2[2] == o)
{
oWinner = true;
}
else if (row3[0] == o && row3[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row2[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[1] == o && row3[0] == o)
{
oWinner = true;
}
}
else if (input == 4 && row2[0] != x && row2[0] != o)
{
Console.SetCursorPosition(0, 3);
Console.WriteLine(o);
row2[0] = o;
Console.SetCursorPosition(0, 8);
oTurn = false;
if (row1[0] == o && row2[0] == o && row3[0] == o)
{
oWinner = true;
}
else if (row1[1] == o && row2[1] == o && row3[1] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[2] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row1[1] == o && row1[2] == o)
{
oWinner = true;
}
else if (row2[0] == o && row2[1] == o && row2[2] == o)
{
oWinner = true;
}
else if (row3[0] == o && row3[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row2[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[1] == o && row3[0] == o)
{
oWinner = true;
}
}
else if (input == 5 && row2[1] != x && row2[1] != o)
{
Console.SetCursorPosition(1, 3);
Console.WriteLine(o);
row2[1] = o;
Console.SetCursorPosition(0, 8);
oTurn = false;
if (row1[0] == o && row2[0] == o && row3[0] == o)
{
oWinner = true;
}
else if (row1[1] == o && row2[1] == o && row3[1] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[2] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row1[1] == o && row1[2] == o)
{
oWinner = true;
}
else if (row2[0] == o && row2[1] == o && row2[2] == o)
{
oWinner = true;
}
else if (row3[0] == o && row3[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row2[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[1] == o && row3[0] == o)
{
oWinner = true;
}
}
else if (input == 6 && row2[2] != x && row2[2] != o)
{
Console.SetCursorPosition(2, 3);
Console.WriteLine(o);
row2[2] = o;
Console.SetCursorPosition(0, 8);
oTurn = false;
if (row1[0] == o && row2[0] == o && row3[0] == o)
{
oWinner = true;
}
else if (row1[1] == o && row2[1] == o && row3[1] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[2] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row1[1] == o && row1[2] == o)
{
oWinner = true;
}
else if (row2[0] == o && row2[1] == o && row2[2] == o)
{
oWinner = true;
}
else if (row3[0] == o && row3[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row2[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[1] == o && row3[0] == o)
{
oWinner = true;
}
}
else if (input == 7 && row1[0] != x && row1[0] != o)
{
Console.SetCursorPosition(0, 2);
Console.WriteLine(o);
row1[0] = o;
Console.SetCursorPosition(0, 8);
oTurn = false;
if (row1[0] == o && row2[0] == o && row3[0] == o)
{
oWinner = true;
}
else if (row1[1] == o && row2[1] == o && row3[1] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[2] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row1[1] == o && row1[2] == o)
{
oWinner = true;
}
else if (row2[0] == o && row2[1] == o && row2[2] == o)
{
oWinner = true;
}
else if (row3[0] == o && row3[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row2[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[1] == o && row3[0] == o)
{
oWinner = true;
}
}
else if (input == 8 && row1[1] != x && row1[1] != o)
{
Console.SetCursorPosition(1, 2);
Console.WriteLine(o);
row1[1] = o;
Console.SetCursorPosition(0, 8);
oTurn = false;
if (row1[0] == o && row2[0] == o && row3[0] == o)
{
oWinner = true;
}
else if (row1[1] == o && row2[1] == o && row3[1] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[2] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row1[1] == o && row1[2] == o)
{
oWinner = true;
}
else if (row2[0] == o && row2[1] == o && row2[2] == o)
{
oWinner = true;
}
else if (row3[0] == o && row3[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row2[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[1] == o && row3[0] == o)
{
oWinner = true;
}
}
else if (input == 9 && row1[2] != x && row1[2] != o)
{
Console.SetCursorPosition(2, 2);
Console.WriteLine(o);
row1[2] = o;
Console.SetCursorPosition(0, 8);
oTurn = false;
if (row1[0] == o && row2[0] == o && row3[0] == o)
{
oWinner = true;
}
else if (row1[1] == o && row2[1] == o && row3[1] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[2] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row1[1] == o && row1[2] == o)
{
oWinner = true;
}
else if (row2[0] == o && row2[1] == o && row2[2] == o)
{
oWinner = true;
}
else if (row3[0] == o && row3[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[0] == o && row2[1] == o && row3[2] == o)
{
oWinner = true;
}
else if (row1[2] == o && row2[1] == o && row3[0] == o)
{
oWinner = true;
}
}
else
{
Console.SetCursorPosition(0, 8);
Console.WriteLine("You can't do that! Pick another spot!");
Console.WriteLine(" ");
Console.ReadLine();
Console.SetCursorPosition(0, 7);
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
continue;
}
xTurn = true;//o's turn is up, it's now X's turn
}//end try
catch (Exception)
{
//display nothing
}
} while (oTurn == true);//run while it is player o's turn
}//end main while loop. Display win screen
if (xWinner == true)
{
Console.Clear();
PrintCentered(xWins);
}
else if (oWinner == true)
{
Console.Clear();
PrintCentered(oWins);
}
}//end NewGame method
static void PrintCentered(string msg)//Method used to print a string centered on the screen
{
int x = Console.WindowWidth / 2 - msg.Length / 2;
int y = Console.WindowHeight / 2;
Console.SetCursorPosition(x, y);
Console.WriteLine(msg);
Console.SetCursorPosition(0, Console.WindowHeight - 1); //Positions cursor in lower left corner
Console.Write("Press ENTER to return to the main menu..."); //Requests an input from the user, which allows the final purpose line above, to display without closing the console.
Console.ReadLine(); //Receives the input from the user and closes the console.
Console.Clear();
}
static void Main(string[] args)
{
for (int input = 0; input != 5;)
{
Console.SetCursorPosition(0, 0);
Console.WriteLine("Tic Tac Toe");
Console.WriteLine(" Main Menu");
Console.WriteLine("===============");
Console.WriteLine("1) New Game");
Console.WriteLine("2) Load Game");
Console.WriteLine("3) Options");
Console.WriteLine("4) Credits");
Console.WriteLine("5) Quit");
Console.WriteLine("===============");
Console.Write("Make a selection: ");
try
{
input = Convert.ToInt32(Console.ReadLine());
Console.Clear();
switch (input)
{
case 1:
NewGame();
break;
case 2:
Console.WriteLine("Buy the DLC to get this feature!");
Console.ReadLine();
Console.Clear();
break;
case 3:
Console.WriteLine("Buy the DLC to get this feature!");
Console.ReadLine();
Console.Clear();
break;
case 4:
Console.WriteLine("Created by Justin Lynn");
Console.ReadLine();
Console.Clear();
break;
case 5:
//Quit the game
break;
default:
Console.WriteLine("Tic Tac Toe");
Console.WriteLine(" Main Menu");
Console.WriteLine("===============");
Console.WriteLine("1) New Game");
Console.WriteLine("2) Load Game");
Console.WriteLine("3) Options");
Console.WriteLine("4) Credits");
Console.WriteLine("5) Quit");
Console.WriteLine("===============");
Console.Write("Make a selection: ");
Console.WriteLine();
Console.WriteLine("Invalid Input, try again.");
Console.ReadLine();
Console.Clear();
break;
}//end switch
}//end try
catch
{
Console.Write("Invalid Input, try again.");
Console.ReadLine();
Console.Clear();
}//end of try/catch
} //end for
}//end main method
}//end class
}//end namespace
7. By Filip Dobeš
Made by Filip Dobeš. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//select time in second.
namespace TicTacBomb
{
class Manager
{
static void Main(string[] args)
{
int time = Convert.ToInt16(Console.ReadLine());
Bomb fetman = new Bomb(time);
while (fetman.Active) fetman.Tick();
}
}
class Bomb
{
readonly string[,] Decoder = {
{" __ ",
" | |",
" : :",
" | |",
" -- "},
{" ^ ",
" / | ",
" | ",
" | ",
" _ "},
{" __ ",
" |",
" -- ",
" | ",
" -- "},
{" __ ",
" |",
" -- ",
" |",
" -- "},
{" ^ ",
" / | ",
"/__|_",
" | ",
" | "},
{" __ ",
" | ",
" -- ",
" |",
" -- "},
{" __ ",
" | ",
" -- ",
" | |",
" -- "},
{" ___ ",
" /",
" / ",
" / ",
" / "},
{" __ ",
" | |",
" -- ",
" | |",
" -- "},
{" __ ",
" | |",
" -- ",
" |",
" -- "},
{" ",
" ## ",
" ",
" ## ",
" "}
};
private int min1, min2, sec1, sec2;
public bool Active;
public Bomb (int time)
{
int min = time/60;
int sec = time-min*60;
sec2 = sec/10;
sec1 = sec-sec2*10;
min2 = min/10;
min1 = min-min2*10;
Active= true;
}
public void Tick()
{
Drawbomb();
if(sec1--==0)
{
sec1=9;
if(sec2--==0)
{
sec2=9;
if(min1--==0)
{
min1=9;
if(min2--==0) boom ();
}
}
}
}
void Drawbomb()
{
Console.WriteLine("('''''''''''''''''''''''''");
for(int i=0;i<5;i++)
{
Console.Write(Decoder[min2,i]);
Console.Write(Decoder[min1,i]);
Console.Write(Decoder[10,i]);
Console.Write(Decoder[sec2,i]);
Console.WriteLine(Decoder[sec1,i]);
}
}
void boom()
{
Active= false;
Console.WriteLine(" *** ");
Console.WriteLine(" ******* ");
Console.WriteLine(" *****##**** ");
Console.WriteLine(" ***#######** ");
Console.WriteLine(" *#######** ");
Console.WriteLine(" #### ");
Console.WriteLine(" ## ");
Console.WriteLine(" ###### ");
Console.WriteLine(" ############# ");
}
}
}
8. By Matt Cockram
Made by Matt Cockram. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class Program
{
private enum BoardState {
NOUGHTS_WIN, CROSSES_WIN, DRAW }
private static BoardStateGetStateOfBoard(string board)
{ if (IsWin(board, 'X')) { return BoardState.CROSSES_WIN; } else if (IsWin(board, 'O')) { return BoardState.NOUGHTS_WIN; } else { return BoardState.DRAW; } } static void Main(string[] args)
{ // REPLACE STATES WITH ARGS LATER /0/
var states = new string[] { "XXXOO____", "XX_OOOX__", "XXXOXOXOO", "XOOOXXXXO"};
for (int i = 0; i < args.Length; i++) { Console.WriteLine(GetStateOfBoard(args[i])); } } static bool IsWin(string board, char player) { if (board[0] == player && board[1] == player && board[2] == player) { return true; } else if (board[3] == player && board[4] == player && board[5] == player) { return true; } else if (board[6] == player && board[7] == player && board[8] == player) { return true; } else if (board[0] == player && board[3] == player && board[6] == player) { return true; } else if (board[1] == player && board[4] == player && board[7] == player) { return true; } else if (board[2] == player && board[5] == player && board[8] == player) { return true; } else if (board[0] == player && board[4] == player && board[8] == player) { return true; } else if (board[2] == player && board[4] == player && board[6] == player) { return true; } else { return false; } } } }
9. By gil gil
Made by gil gil. Simple C# Tic Tac Toe game. ( Source )
using System;
using System.Collections;
using System.Threading;
namespace ConsoleApp3
{
class Program
{
static bool Player1Turn = true;
static int count = 0;
static int countLine = 27;
static void Main(string[] args)
{
DecorateProgram();
Console.SetCursorPosition(0, 18);
Console.WriteLine("Enter name of player 1 ( X )");
Player p1 = new Player(Console.ReadLine());
Console.WriteLine("Enter name of player 2 ( O )");
Player p2 = new Player(Console.ReadLine());
Console.WriteLine("Enter number of place");
Console.WriteLine(" 1 2 3");
Console.WriteLine(" 4 5 6");
Console.WriteLine(" 7 8 9");
while(count != 9 && !p1.CheckWin() && !p2.CheckWin())
Place(p1,p2);
countLine++;
if(p1.CheckWin())
Console.WriteLine("{0} has won!",p1.name);
else if(p2.CheckWin())
Console.WriteLine("{0} has won!",p2.name);
else
Console.WriteLine("draw");
}
static void DecorateProgram()
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Title = "Tic tac toe";
DrawLines();
}
static void DrawLines()
{
for (int i = 1; i <= 2; i++)
{
for (int j = 0; j < 13; j++)
{
Console.SetCursorPosition(i * 10, j);
Console.Write("*");
}
}
for (int i = 1; i <= 2; i++)
{
for (int j = 0; j < 30; j++)
{
Console.SetCursorPosition(j, i * 4);
Console.Write("*");
}
}
}
static void Place(Player Px,Player Py)
{
int des = 0;
switch(Console.ReadKey(true).Key)
{
case ConsoleKey.D1:
Console.SetCursorPosition(5, 2);
des = 1;
break;
case ConsoleKey.D2:
Console.SetCursorPosition(15, 2);
des = 2;
break;
case ConsoleKey.D3:
Console.SetCursorPosition(25, 2);
des = 3;
break;
case ConsoleKey.D4:
Console.SetCursorPosition(5, 6);
des = 4;
break;
case ConsoleKey.D5:
Console.SetCursorPosition(15, 6);
des = 5;
break;
case ConsoleKey.D6:
Console.SetCursorPosition(25, 6);
des = 6;
break;
case ConsoleKey.D7:
Console.SetCursorPosition(5, 10);
des = 7;
break;
case ConsoleKey.D8:
Console.SetCursorPosition(15, 10);
des = 8;
break;
case ConsoleKey.D9:
Console.SetCursorPosition(25, 10);
des = 9;
break;
default:
Console.WriteLine("Invalid input");
count--;
countLine++;
break;
}
if (Px.CheckExist(des) || Py.CheckExist(des))
{
Console.SetCursorPosition(0, countLine);
Console.WriteLine("Already exists");
count--;
countLine++;
}
else if (Player1Turn && des != 0)
{
Console.Write("X");
Px.UpdateBoxes(des);
Player1Turn = false;
}
else if(!Player1Turn && des != 0)
{
Console.Write("O");
Py.UpdateBoxes(des);
Player1Turn = true;
}
count++;
Console.SetCursorPosition(0, countLine);
}
}
class Player
{
public string name;
int[] Boxes = new int[10];
public Player(string name)
{
this.name = name;
for (int i = 0; i < 10; i++)
{
Boxes[i] = 0;
}
}
public void UpdateBoxes(int des)
{
Boxes[des] = 1;
}
public bool CheckWin()
{
if (Boxes[1] == 1 && Boxes[5] == 1 && Boxes[9] == 1)
return true;
else if (Boxes[1] == 1 && Boxes[2] == 1 && Boxes[3] == 1)
return true;
else if (Boxes[4] == Boxes[5] && Boxes[4] == Boxes[6] && Boxes[4] == 1)
return true;
else if (Boxes[7] == 1 && Boxes[8] == 1 && Boxes[9] == 1)
return true;
else if (Boxes[1] == 1 && Boxes[4] == 1 && Boxes[7] == 1)
return true;
else if (Boxes[2] == 1 && Boxes[5] == 1 && Boxes[9] == 1)
return true;
else if (Boxes[3] == 1 && Boxes[6] == 1 && Boxes[9] == 1)
return true;
else if (Boxes[3] == 1 && Boxes[5] == 1 && Boxes[7] == 1)
return true;
return false;
}
public bool CheckExist(int des)
{
return (Boxes[des] == 1);
}
}
}
10. By Anna Ihorivna Dembytska
Made by Anna Ihorivna Dembytska. ( Source )
using System;
using System.Threading;
namespace SoloLearn
{
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)
{
do
{
Console.Clear();// whenever loop will be again start then screen will be clear
Console.WriteLine("Player1:X and Player2: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");
Board();// calling the board Function
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
Console.Clear();// clearing the console
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
{
Console.WriteLine("Player {0} has won", (player % 2) + 1);
}
else// if flag value is -1 the match will be draw and no one is winner
{
Console.WriteLine("Draw");
}
Console.ReadLine();
}
// 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;
}
}
}
}
11. By Azra Begum
Made by Azra Begum. ( Source )
#include <iostream>
using namespace std;
char square[10] = {'o','1','2','3','4','5','6','7','8','9'}; int checkwin();
void board();
int main()
{
int player = 1,i,choice;
char mark;
do {
board();
player=(player%2)?1:2;
cout << "Player " << player << ", enter a number: ";
cin >> choice;
mark=(player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1') square[1] = mark; else if (choice == 2 && square[2] == '2') square[2] = mark; else if (choice == 3 && square[3] == '3') square[3] = mark; else if (choice == 4 && square[4] == '4') square[4] = mark; else if (choice == 5 && square[5] == '5') square[5] = mark; else if (choice == 6 && square[6] == '6') square[6] = mark; else if (choice == 7 && square[7] == '7') square[7] = mark; else if (choice == 8 && square[8] == '8') square[8] = mark; else if (choice == 9 && square[9] == '9') square[9] = mark; else { cout<<"Invalid move ";
player--;
cin.ignore();
cin.get();
}
i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<"==>\aPlayer "<<--player<<" win ";
else
cout<<"==>\aGame draw";
cin.ignore();
cin.get();
return 0;
}
/********************************************* FUNCTION TO RETURN GAME STATUS 1 FOR GAME IS OVER WITH RESULT -1 FOR GAME IS IN PROGRESS O GAME IS OVER AND NO RESULT **********************************************/
int checkwin()
{
if (square[1] == square[2] && square[2] == square[3]) return 1;
else if (square[4] == square[5] && square[5] == square[6]) return 1;
else if (square[7] == square[8] && square[8] == square[9]) return 1;
else if (square[1] == square[4] && square[4] == square[7]) return 1;
else if (square[2] == square[5] && square[5] == square[8]) return 1;
else if (square[3] == square[6] && square[6] == square[9]) return 1;
else if (square[1] == square[5] && square[5] == square[9]) return 1;
else if (square[3] == square[5] && square[5] == square[7]) return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')
return 0;
else
return -1;
}
/******************************************************************* FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK ********************************************************************/
void board()
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
cout << " | | " << endl << endl;
}
/******************************************************************* END OF PROJECT ********************************************************************/
12. By keivan Ipchi
Made by keivan Ipchi. The game uses AI and runs automatically. It gives different output different times. ( Source )
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics; // For Link Transfer
using System.Threading;
namespace Tic_Tac_Toe
{
public partial class Form1 : Form
{
// Tic Tac Toe Game (AI) (Artificial Intelligence)
byte Count = 1; // Movement Counter
bool CPU_Start = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
// -------------------------------------------------VOID CHECK ()------------------------------------------------------------
void Check()
{
// CPU Moves:
// Attac:
if (button1.Text == "O" && button2.Text == "O" && button3.Text == "")
{
button3.Text = "O"; button3.Enabled = false;
}
else
{
if (button2.Text == "O" && button3.Text == "O" && button1.Text == "")
{
button1.Text = "O"; button1.Enabled = false;
}
else
{
if (button1.Text == "O" && button3.Text == "O" && button2.Text == "")
{
button2.Text = "O"; button2.Enabled = false;
}
else
{
if (button4.Text == "O" && button5.Text == "O" && button6.Text == "")
{
button6.Text = "O"; button6.Enabled = false;
}
else
{
if (button5.Text == "O" && button6.Text == "O" && button4.Text == "")
{
button4.Text = "O"; button4.Enabled = false;
}
else
{
if (button4.Text == "O" && button6.Text == "O" && button5.Text == "")
{
button5.Text = "O"; button5.Enabled = false;
}
else
{
if (button7.Text == "O" && button8.Text == "O" && button9.Text == "")
{
button9.Text = "O"; button9.Enabled = false;
}
else
{
if (button8.Text == "O" && button9.Text == "O" && button7.Text == "")
{
button7.Text = "O"; button7.Enabled = false;
}
else
{
if (button7.Text == "O" && button9.Text == "O" && button8.Text == "")
{
button8.Text = "O"; button8.Enabled = false;
}
else
{
if (button1.Text == "O" && button4.Text == "O" && button7.Text == "")
{
button7.Text = "O"; button7.Enabled = false;
}
else
{
if (button4.Text == "O" && button7.Text == "O" && button1.Text == "")
{
button1.Text = "O"; button1.Enabled = false;
}
else
{
if (button1.Text == "O" && button7.Text == "O" && button4.Text == "")
{
button4.Text = "O"; button4.Enabled = false;
}
else
{
if (button2.Text == "O" && button5.Text == "O" && button8.Text == "")
{
button8.Text = "O"; button8.Enabled = false;
}
else
{
if (button5.Text == "O" && button8.Text == "O" && button2.Text == "")
{
button2.Text = "O"; button2.Enabled = false;
}
else
{
if (button2.Text == "O" && button8.Text == "O" && button5.Text == "")
{
button5.Text = "O"; button5.Enabled = false;
}
else
{
if (button3.Text == "O" && button6.Text == "O" && button9.Text == "")
{
button9.Text = "O"; button9.Enabled = false;
}
else
{
if (button6.Text == "O" && button9.Text == "O" && button3.Text == "")
{
button3.Text = "O"; button3.Enabled = false;
}
else
{
if (button3.Text == "O" && button9.Text == "O" && button6.Text == "")
{
button6.Text = "O"; button6.Enabled = false;
}
else
{
if (button1.Text == "O" && button5.Text == "O" && button9.Text == "")
{
button9.Text = "O"; button9.Enabled = false;
}
else
{
if (button5.Text == "O" && button9.Text == "O" && button1.Text == "")
{
button1.Text = "O"; button1.Enabled = false;
}
else
{
if (button1.Text == "O" && button9.Text == "O" && button5.Text == "")
{
button5.Text = "O"; button5.Enabled = false;
}
else
{
if (button3.Text == "O" && button5.Text == "O" && button7.Text == "")
{
button7.Text = "O"; button7.Enabled = false;
}
else
{
if (button5.Text == "O" && button7.Text == "O" && button3.Text == "")
{
button3.Text = "O"; button3.Enabled = false;
}
else
{
if (button3.Text == "O" && button7.Text == "O" && button5.Text == "")
{
button5.Text = "O"; button5.Enabled = false;
}
// Defencive Action:
else
{
if (button1.Text == "X" && button2.Text == "X" && button3.Text == "")
{
button3.Text = "O"; button3.Enabled = false;
}
else
{
if (button2.Text == "X" && button3.Text == "X" && button1.Text == "")
{
button1.Text = "O"; button1.Enabled = false;
}
else
{
if (button1.Text == "X" && button3.Text == "X" && button2.Text == "")
{
button2.Text = "O"; button2.Enabled = false;
}
else
{
if (button4.Text == "X" && button5.Text == "X" && button6.Text == "")
{
button6.Text = "O"; button6.Enabled = false;
}
else
{
if (button5.Text == "X" && button6.Text == "X" && button4.Text == "")
{
button4.Text = "O"; button4.Enabled = false;
}
else
{
if (button4.Text == "X" && button6.Text == "X" && button5.Text == "")
{
button5.Text = "O"; button5.Enabled = false;
}
else
{
if (button7.Text == "X" && button8.Text == "X" && button9.Text == "")
{
button9.Text = "O"; button9.Enabled = false;
}
else
{
if (button8.Text == "X" && button9.Text == "X" && button7.Text == "")
{
button7.Text = "O"; button7.Enabled = false;
}
else
{
if (button7.Text == "X" && button9.Text == "X" && button8.Text == "")
{
button8.Text = "O"; button8.Enabled = false;
}
else
{
if (button1.Text == "X" && button4.Text == "X" && button7.Text == "")
{
button7.Text = "O"; button7.Enabled = false;
}
else
{
if (button4.Text == "X" && button7.Text == "X" && button1.Text == "")
{
button1.Text = "O"; button1.Enabled = false;
}
else
{
if (button1.Text == "X" && button7.Text == "X" && button4.Text == "")
{
button4.Text = "O"; button4.Enabled = false;
}
else
{
if (button2.Text == "X" && button5.Text == "X" && button8.Text == "")
{
button8.Text = "O"; button8.Enabled = false;
}
else
{
if (button5.Text == "X" && button8.Text == "X" && button2.Text == "")
{
button2.Text = "O"; button2.Enabled = false;
}
else
{
if (button2.Text == "X" && button8.Text == "X" && button5.Text == "")
{
button5.Text = "O"; button5.Enabled = false;
}
else
{
if (button3.Text == "X" && button6.Text == "X" && button9.Text == "")
{
button9.Text = "O"; button9.Enabled = false;
}
else
{
if (button6.Text == "X" && button9.Text == "X" && button3.Text == "")
{
button3.Text = "O"; button3.Enabled = false;
}
else
{
if (button3.Text == "X" && button9.Text == "X" && button6.Text == "")
{
button6.Text = "O"; button6.Enabled = false;
}
else
{
if (button1.Text == "X" && button5.Text == "X" && button9.Text == "")
{
button9.Text = "O"; button9.Enabled = false;
}
else
{
if (button5.Text == "X" && button9.Text == "X" && button1.Text == "")
{
button1.Text = "O"; button1.Enabled = false;
}
else
{
if (button1.Text == "X" && button9.Text == "X" && button5.Text == "")
{
button5.Text = "O"; button5.Enabled = false;
}
else
{
if (button3.Text == "X" && button5.Text == "X" && button7.Text == "")
{
button7.Text = "O"; button7.Enabled = false;
}
else
{
if (button5.Text == "X" && button7.Text == "X" && button3.Text == "")
{
button3.Text = "O"; button3.Enabled = false;
}
else
{
if (button3.Text == "X" && button7.Text == "X" && button5.Text == "")
{
button5.Text = "O"; button5.Enabled = false;
}
else
{
// Other Set Of Moves:
if ((button3.Text == "X" && button4.Text == "X" && button1.Text == "") || (button2.Text == "X" && button7.Text == "X" && button1.Text == ""))
{
button1.Text = "O"; button1.Enabled = false;
}
else
{
if ((button1.Text == "X" && button8.Text == "X" && button7.Text == "") || (button4.Text == "X" && button9.Text == "X" && button7.Text == ""))
{
button7.Text = "O"; button7.Enabled = false;
}
else
{
if ((button1.Text == "X" && button6.Text == "X" && button3.Text == "") || (button2.Text == "X" && button9.Text == "X" && button3.Text == ""))
{
button3.Text = "O"; button3.Enabled = false;
}
else
{
if ((button3.Text == "X" && button8.Text == "X" && button9.Text == "") || (button6.Text == "X" && button7.Text == "X" && button9.Text == ""))
{
button9.Text = "O"; button9.Enabled = false;
}
else
{
// Other Set Of Moves:
if ((button1.Text == "X" && button9.Text == "X") || (button3.Text == "X" && button7.Text == "X"))
{
Random Rand = new Random();
int Random = Rand.Next(0, 4);
switch (Random)
{
case 0: button2.Text = "O"; button2.Enabled = false; break;
case 1: button4.Text = "O"; button4.Enabled = false; break;
case 2: button6.Text = "O"; button6.Enabled = false; break;
case 3: button8.Text = "O"; button8.Enabled = false; break;
}
}
else
{
// Other Set Of Moves:
if (button2.Text == "X" && button4.Text == "X" && button1.Text == "")
{
button1.Text = "O"; button1.Enabled = false;
}
else
{
if (button2.Text == "X" && button6.Text == "X" && button3.Text == "")
{
button3.Text = "O"; button3.Enabled = false;
}
else
{
if (button4.Text == "X" && button8.Text == "X" && button7.Text == "")
{
button7.Text = "O"; button7.Enabled = false;
}
else
{
if (button6.Text == "X" && button8.Text == "X" && button9.Text == "")
{
button9.Text = "O"; button9.Enabled = false;
}
else
{
// Other Set Of Moves:
if (button1.Text == "O" && button6.Text == "O" && button7.Text == "O" && button5.Text == "X" && button4.Text == "X")
{
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button2.Text = "O"; button2.Enabled = false; break;
case 1: button8.Text = "O"; button8.Enabled = false; break;
}
}
else
{
if (button3.Text == "O" && button4.Text == "O" && button9.Text == "O" && button5.Text == "X" && button6.Text == "X")
{
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button2.Text = "O"; button2.Enabled = false; break;
case 1: button8.Text = "O"; button8.Enabled = false; break;
}
}
else
{
if (button2.Text == "O" && button7.Text == "O" && button9.Text == "O" && button5.Text == "X" && button8.Text == "X")
{
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button4.Text = "O"; button4.Enabled = false; break;
case 1: button6.Text = "O"; button6.Enabled = false; break;
}
}
else
{
if (button1.Text == "O" && button3.Text == "O" && button8.Text == "O" && button5.Text == "X" && button2.Text == "X")
{
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button4.Text = "O"; button4.Enabled = false; break;
case 1: button6.Text = "O"; button6.Enabled = false; break;
}
}
else
{
// Other Set Of Moves:
if (button1.Text == "X" && button5.Text == "X" && button9.Text == "O")
{
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button3.Text = "O"; button3.Enabled = false; break;
case 1: button7.Text = "O"; button7.Enabled = false; break;
}
}
else
{
if (button5.Text == "X" && button9.Text == "X" && button1.Text == "O")
{
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button3.Text = "O"; button3.Enabled = false; break;
case 1: button7.Text = "O"; button7.Enabled = false; break;
}
}
else
{
if (button1.Text == "X" && button5.Text == "X" && button9.Text == "O")
{
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button3.Text = "O"; button3.Enabled = false; break;
case 1: button7.Text = "O"; button7.Enabled = false; break;
}
}
else
{
if (button3.Text == "X" && button5.Text == "X" && button7.Text == "O")
{
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button1.Text = "O"; button1.Enabled = false; break;
case 1: button9.Text = "O"; button9.Enabled = false; break;
}
}
else
{
if (button7.Text == "X" && button5.Text == "X" && button3.Text == "O")
{
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button1.Text = "O"; button1.Enabled = false; break;
case 1: button9.Text = "O"; button9.Enabled = false; break;
}
}
else
{
if ((button2.Text == "X" && button5.Text == "O" && button6.Text == "X") || (button8.Text == "X" && button5.Text == "O" && button2.Text == "X"))
{
Random Rand = new Random();
int Random = Rand.Next(0, 4);
switch (Random)
{
case 0: button1.Text = "O"; button1.Enabled = false; break;
case 1: button3.Text = "O"; button3.Enabled = false; break;
case 2: button7.Text = "O"; button7.Enabled = false; break;
case 3: button9.Text = "O"; button9.Enabled = false; break;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
if (Count == 5 && CPU_Start == true)
{
CPU_last_move();
Draw();
}
CPU_Wins(); // Goes To The Fucntion To Check If CPU Wins
} // End Of This Function
// -------------------------------------------------VOID CPU_Wins ()------------------------------------------------------------
void CPU_Wins()
{
// This Fucntion Checks If CPU Wins
bool win = false;
if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O")
{
win = true;
button1.BackColor = Color.Red; button2.BackColor = Color.Red; button3.BackColor = Color.Red;
}
else
{
if (button4.Text == "O" && button5.Text == "O" && button6.Text == "O")
{
win = true;
button4.BackColor = Color.Red; button5.BackColor = Color.Red; button6.BackColor = Color.Red;
}
else
{
if (button7.Text == "O" && button8.Text == "O" && button9.Text == "O")
{
win = true;
button7.BackColor = Color.Red; button8.BackColor = Color.Red; button9.BackColor = Color.Red;
}
else
{
if (button1.Text == "O" && button4.Text == "O" && button7.Text == "O")
{
win = true;
button1.BackColor = Color.Red; button4.BackColor = Color.Red; button7.BackColor = Color.Red;
}
else
{
if (button2.Text == "O" && button5.Text == "O" && button8.Text == "O")
{
win = true;
button2.BackColor = Color.Red; button5.BackColor = Color.Red; button8.BackColor = Color.Red;
}
else
{
if (button3.Text == "O" && button6.Text == "O" && button9.Text == "O")
{
win = true;
button3.BackColor = Color.Red; button6.BackColor = Color.Red; button9.BackColor = Color.Red;
}
else
{
if (button1.Text == "O" && button5.Text == "O" && button9.Text == "O")
{
win = true;
button1.BackColor = Color.Red; button5.BackColor = Color.Red; button9.BackColor = Color.Red;
}
else
{
if (button3.Text == "O" && button5.Text == "O" && button7.Text == "O")
{
win = true;
button3.BackColor = Color.Red; button5.BackColor = Color.Red; button7.BackColor = Color.Red;
}
}
}
}
}
}
}
} // End Of Checking
if (win == true)
{
button1.Enabled = false; button2.Enabled = false; button3.Enabled = false;
button4.Enabled = false; button5.Enabled = false; button6.Enabled = false;
button7.Enabled = false; button8.Enabled = false; button9.Enabled = false;
DialogResult result;
result = MessageBox.Show("You Lost!\nCheck Toturials/Tricks To Ipmrove Your Skills.\n\nWant To Play Again?", "Game Status", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
button1.Text = string.Empty; button1.Enabled = true; button1.BackColor = Color.Empty;
button2.Text = string.Empty; button2.Enabled = true; button2.BackColor = Color.Empty;
button3.Text = string.Empty; button3.Enabled = true; button3.BackColor = Color.Empty;
button4.Text = string.Empty; button4.Enabled = true; button4.BackColor = Color.Empty;
button5.Text = string.Empty; button5.Enabled = true; button5.BackColor = Color.Empty;
button6.Text = string.Empty; button6.Enabled = true; button6.BackColor = Color.Empty;
button7.Text = string.Empty; button7.Enabled = true; button7.BackColor = Color.Empty;
button8.Text = string.Empty; button8.Enabled = true; button8.BackColor = Color.Empty;
button9.Text = string.Empty; button9.Enabled = true; button9.BackColor = Color.Empty;
Count = 1; listBox1.Items.Add("Lost"); button10.Enabled = true; CPU_Start = false;
}
if (result == DialogResult.No)
Application.Exit();
}
} // End Fucntion
// -------------------------------------------------VOID Draw ()------------------------------------------------------------
void Draw()
{
// This Function Is Called If The Game Is A Draw
button1.Enabled = false; button2.Enabled = false; button3.Enabled = false; button1.BackColor = Color.Blue; button2.BackColor = Color.Blue; button3.BackColor = Color.Blue;
button4.Enabled = false; button5.Enabled = false; button6.Enabled = false; button4.BackColor = Color.Blue; button5.BackColor = Color.Blue; button6.BackColor = Color.Blue;
button7.Enabled = false; button8.Enabled = false; button9.Enabled = false; button7.BackColor = Color.Blue; button8.BackColor = Color.Blue; button9.BackColor = Color.Blue;
DialogResult result;
result = MessageBox.Show("Game Draw!\nCheck Toturials/Tricks To Ipmrove Your Skills.\n\nWant To Play Again?", "Draw!", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
button1.Text = string.Empty; button1.Enabled = true; button1.BackColor = Color.Empty;
button2.Text = string.Empty; button2.Enabled = true; button2.BackColor = Color.Empty;
button3.Text = string.Empty; button3.Enabled = true; button3.BackColor = Color.Empty;
button4.Text = string.Empty; button4.Enabled = true; button4.BackColor = Color.Empty;
button5.Text = string.Empty; button5.Enabled = true; button5.BackColor = Color.Empty;
button6.Text = string.Empty; button6.Enabled = true; button6.BackColor = Color.Empty;
button7.Text = string.Empty; button7.Enabled = true; button7.BackColor = Color.Empty;
button8.Text = string.Empty; button8.Enabled = true; button8.BackColor = Color.Empty;
button9.Text = string.Empty; button9.Enabled = true; button9.BackColor = Color.Empty;
Count = 1; listBox1.Items.Add("Draw"); button10.Enabled = true; CPU_Start = false;
}
if (result == DialogResult.No)
Application.Exit();
}
void CPU_last_move()
{
// CPU 4th Move:
if (button1.Enabled == true && button2.Enabled == false && button3.Enabled == false && button4.Enabled == false && button5.Enabled == false && button6.Enabled == false && button7.Enabled == false && button8.Enabled == false && button9.Enabled == false)
{
button1.Text = "O";
}
else
{
if (button1.Enabled == false && button2.Enabled == true && button3.Enabled == false && button4.Enabled == false && button5.Enabled == false && button6.Enabled == false && button7.Enabled == false && button8.Enabled == false && button9.Enabled == false)
{
button2.Text = "O";
}
else
{
if (button1.Enabled == false && button2.Enabled == false && button3.Enabled == true && button4.Enabled == false && button5.Enabled == false && button6.Enabled == false && button7.Enabled == false && button8.Enabled == false && button9.Enabled == false)
{
button3.Text = "O";
}
else
{
if (button1.Enabled == false && button2.Enabled == false && button3.Enabled == false && button4.Enabled == true && button5.Enabled == false && button6.Enabled == false && button7.Enabled == false && button8.Enabled == false && button9.Enabled == false)
{
button4.Text = "O";
}
else
{
if (button1.Enabled == false && button2.Enabled == false && button3.Enabled == false && button4.Enabled == false && button5.Enabled == true && button6.Enabled == false && button7.Enabled == false && button8.Enabled == false && button9.Enabled == false)
{
button5.Text = "O";
}
else
{
if (button1.Enabled == false && button2.Enabled == false && button3.Enabled == false && button4.Enabled == false && button5.Enabled == false && button6.Enabled == true && button7.Enabled == false && button8.Enabled == false && button9.Enabled == false)
{
button6.Text = "O";
}
else
{
if (button1.Enabled == false && button2.Enabled == false && button3.Enabled == false && button4.Enabled == false && button5.Enabled == false && button6.Enabled == false && button7.Enabled == true && button8.Enabled == false && button9.Enabled == false)
{
button7.Text = "O";
}
else
{
if (button1.Enabled == false && button2.Enabled == false && button3.Enabled == false && button4.Enabled == false && button5.Enabled == false && button6.Enabled == false && button7.Enabled == false && button8.Enabled == true && button9.Enabled == false)
{
button8.Text = "O";
}
else
{
if (button1.Enabled == false && button2.Enabled == false && button3.Enabled == false && button4.Enabled == false && button5.Enabled == false && button6.Enabled == false && button7.Enabled == false && button8.Enabled == false && button9.Enabled == true)
{
button9.Text = "O";
}
}
}
}
}
}
}
}
}
// END
}
private void button1_Click(object sender, EventArgs e)
{
if (Count == 1 && CPU_Start == false) // First Move (User)
{
button10.Enabled = false; // Disables "CPU_Start" Button
button1.Text = "X"; button1.Enabled = false;
button5.Text = "O"; button5.Enabled = false;
Count++;
goto b1;
}
if (Count == 1 && CPU_Start == true) // First Move (CPU)
{
button1.Text = "X"; button1.Enabled = false;
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button3.Text = "O"; button3.Enabled = false; break;
case 1: button7.Text = "O"; button7.Enabled = false; break;
}
Count++;
goto b1;
}
if (Count == 2 || Count == 3 || Count == 4)
{
button1.Text = "X"; button1.Enabled = false;
Count++;
Check();
goto b1;
}
if (Count == 5)
{
button1.Text = "X"; button1.Enabled = false;
Draw();
goto b1;
}
b1:;
}
private void button2_Click(object sender, EventArgs e)
{
if (Count == 1 && CPU_Start==false) // First Move (User)
{
button10.Enabled = false; // Disables "CPU_Start" Button
button2.Text = "X"; button2.Enabled = false;
button5.Text = "O"; button5.Enabled = false;
Count++;
goto b2;
}
if (Count == 1 && CPU_Start == true) // First Move (CPU)
{
button2.Text = "X"; button2.Enabled = false;
Random Rand = new Random();
int Random = Rand.Next(0, 4);
switch (Random)
{
case 0: button3.Text = "O"; button3.Enabled = false; break;
case 1: button7.Text = "O"; button7.Enabled = false; break;
case 2: button1.Text = "O"; button1.Enabled = false; break;
case 3: button9.Text = "O"; button9.Enabled = false; break;
}
Count++;
goto b2;
}
if (Count == 2 || Count == 3 || Count == 4)
{
button2.Text = "X"; button2.Enabled = false;
Count++;
Check();
goto b2;
}
if (Count == 5)
{
button2.Text = "X"; button2.Enabled = false;
Draw();
goto b2;
}
b2:;
}
private void button3_Click(object sender, EventArgs e)
{
if (Count == 1 && CPU_Start == false) // First Move (User)
{
button10.Enabled = false; // Disables "CPU_Start" Button
button3.Text = "X"; button3.Enabled = false;
button5.Text = "O"; button5.Enabled = false;
Count++;
goto b3;
}
if (Count == 1 && CPU_Start == true) // First Move (CPU)
{
button3.Text = "X"; button3.Enabled = false;
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button1.Text = "O"; button1.Enabled = false; break;
case 1: button9.Text = "O"; button9.Enabled = false; break;
}
Count++;
goto b3;
}
if (Count == 2 || Count == 3 || Count == 4)
{
button3.Text = "X"; button3.Enabled = false;
Count++;
Check();
goto b3;
}
if (Count == 5)
{
button3.Text = "X"; button3.Enabled = false;
Draw();
goto b3;
}
b3:;
}
private void button4_Click(object sender, EventArgs e)
{
if (Count == 1 && CPU_Start == false) // First Move (User)
{
button10.Enabled = false; // Disables "CPU_Start" Button
button4.Text = "X"; button4.Enabled = false;
button5.Text = "O"; button5.Enabled = false;
Count++;
goto b4;
}
if (Count == 1 && CPU_Start == true) // First Move (CPU)
{
button4.Text = "X"; button4.Enabled = false;
Random Rand = new Random();
int Random = Rand.Next(0, 4);
switch (Random)
{
case 0: button3.Text = "O"; button3.Enabled = false; break;
case 1: button7.Text = "O"; button7.Enabled = false; break;
case 2: button1.Text = "O"; button1.Enabled = false; break;
case 3: button9.Text = "O"; button9.Enabled = false; break;
}
Count++;
goto b4;
}
if (Count == 2 || Count == 3 || Count == 4)
{
button4.Text = "X"; button4.Enabled = false;
Count++;
Check();
goto b4;
}
if (Count == 5)
{
button4.Text = "X"; button4.Enabled = false;
Draw();
goto b4;
}
b4:;
}
private void button5_Click(object sender, EventArgs e)
{
if (Count == 1) // First Move (Client)
{
button5.Text = "X"; button5.Enabled = false;
Random Rand = new Random();
int Random = Rand.Next(0, 4);
switch (Random)
{
case 0: button1.Text = "O"; button1.Enabled = false; break;
case 1: button3.Text = "O"; button3.Enabled = false; break;
case 2: button7.Text = "O"; button7.Enabled = false; break;
case 3: button9.Text = "O"; button9.Enabled = false; break;
}
Count++;
}
// No Further Moves
}
private void button6_Click(object sender, EventArgs e)
{
if (Count == 1 && CPU_Start == false) // First Move (User)
{
button10.Enabled = false; // Disables "CPU_Start" Button
button6.Text = "X"; button6.Enabled = false;
button5.Text = "O"; button5.Enabled = false;
Count++;
goto b6;
}
if (Count == 1 && CPU_Start == true) // First Move (CPU)
{
button6.Text = "X"; button6.Enabled = false;
Random Rand = new Random();
int Random = Rand.Next(0, 4);
switch (Random)
{
case 0: button3.Text = "O"; button3.Enabled = false; break;
case 1: button7.Text = "O"; button7.Enabled = false; break;
case 2: button1.Text = "O"; button1.Enabled = false; break;
case 3: button9.Text = "O"; button9.Enabled = false; break;
}
Count++;
goto b6;
}
if (Count == 2 || Count == 3 || Count == 4)
{
button6.Text = "X"; button6.Enabled = false;
Count++;
Check();
goto b6;
}
if (Count == 5)
{
button6.Text = "X"; button6.Enabled = false;
Draw();
goto b6;
}
b6:;
}
private void button7_Click(object sender, EventArgs e)
{
if (Count == 1 && CPU_Start == false) // First Move (User)
{
button10.Enabled = false; // Disables "CPU_Start" Button
button7.Text = "X"; button7.Enabled = false;
button5.Text = "O"; button5.Enabled = false;
Count++;
goto b7;
}
if (Count == 1 && CPU_Start == true) // First Move (CPU)
{
button7.Text = "X"; button7.Enabled = false;
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button1.Text = "O"; button1.Enabled = false; break;
case 1: button9.Text = "O"; button9.Enabled = false; break;
}
Count++;
goto b7;
}
if (Count == 2 || Count == 3 || Count == 4)
{
button7.Text = "X"; button7.Enabled = false;
Count++;
Check();
goto b7;
}
if (Count == 5)
{
button7.Text = "X"; button7.Enabled = false;
Draw();
goto b7;
}
b7:;
}
private void button8_Click(object sender, EventArgs e)
{
if (Count == 1 && CPU_Start == false) // First Move (User)
{
button10.Enabled = false; // Disables "CPU_Start" Button
button8.Text = "X"; button8.Enabled = false;
button5.Text = "O"; button5.Enabled = false;
Count++;
goto b8;
}
if (Count == 1 && CPU_Start == true) // First Move (CPU)
{
button8.Text = "X"; button8.Enabled = false;
Random Rand = new Random();
int Random = Rand.Next(0, 4);
switch (Random)
{
case 0: button3.Text = "O"; button3.Enabled = false; break;
case 1: button7.Text = "O"; button7.Enabled = false; break;
case 2: button1.Text = "O"; button1.Enabled = false; break;
case 3: button9.Text = "O"; button9.Enabled = false; break;
}
Count++;
goto b8;
}
if (Count == 2 || Count == 3 || Count == 4)
{
button8.Text = "X"; button8.Enabled = false;
Count++;
Check();
goto b8;
}
if (Count == 5)
{
button8.Text = "X"; button8.Enabled = false;
Draw();
goto b8;
}
b8:;
}
private void button9_Click(object sender, EventArgs e)
{
if (Count == 1 && CPU_Start == false) // First Move (User)
{
button10.Enabled = false; // Disables "CPU_Start" Button
button9.Text = "X"; button9.Enabled = false;
button5.Text = "O"; button5.Enabled = false;
Count++;
goto b9;
}
if (Count == 1 && CPU_Start == true) // First Move (CPU)
{
button9.Text = "X"; button9.Enabled = false;
Random Rand = new Random();
int Random = Rand.Next(0, 2);
switch (Random)
{
case 0: button3.Text = "O"; button3.Enabled = false; break;
case 1: button7.Text = "O"; button7.Enabled = false; break;
}
Count++;
goto b9;
}
if (Count == 2 || Count == 3 || Count == 4)
{
button9.Text = "X"; button9.Enabled = false;
Count++;
Check();
goto b9;
}
if (Count == 5)
{
button9.Text = "X"; button9.Enabled = false;
Draw();
goto b9;
}
b9:;
}
private void button10_Click(object sender, EventArgs e)
{
// CPU_Start Entery Point:
button10.Enabled = false; CPU_Start = true;
button5.Text = "O"; button5.Enabled = false;
}
private void button11_Click(object sender, EventArgs e)
{
button1.Text = ""; button1.Enabled = true;
button2.Text = ""; button2.Enabled = true;
button3.Text = ""; button3.Enabled = true;
button4.Text = ""; button4.Enabled = true;
button5.Text = ""; button5.Enabled = true;
button6.Text = ""; button6.Enabled = true;
button7.Text = ""; button7.Enabled = true;
button8.Text = ""; button8.Enabled = true;
button9.Text = ""; button9.Enabled = true;
button10.Enabled = true; CPU_Start = false;
Count = 1;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.wikihow.com/Win-at-Tic-Tac-Toe");
}
}
}
13. By MangaBoy awn
Made by MangaBoy awn. To start either input ‘x’ and a number to go first or just ‘o’ to go second, the rest will be explained in the console. ( Source )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
public static char c1 = ' ',c2 = ' ',c3 = ' ',c4 = ' ',c5 = ' ',c6 = ' ',c7 = ' ',c8 = ' ',c9 = ' ';
public static string line = Console.ReadLine();
public static bool write = true, done = false;
static void Main()
{
if(line[0] == 'o' && line.Length % 2 != 0 && line.Length < 10){
PlayerTurn(line, 'O', 0);
if (!done)
CPTurn(line, 'X', -1);
BoardDraw(c1,c2,c3,c4,c5,c6,c7,c8,c9);
}
if(line[0] == 'x' && line.Length % 2 == 0 && line.Length < 11){
PlayerTurn(line, 'X', -1);
if (!done)
CPTurn(line, 'O', 0);
BoardDraw(c1,c2,c3,c4,c5,c6,c7,c8,c9);
}
}
static void BoardDraw(char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8, char c9)
{
Console.WriteLine("{0}|{1}|{2}", c1,c2,c3);
Console.WriteLine("-----");
Console.Write("{0}|{1}|{2}", c4,c5,c6);
if (write && line.Length != 10)
Console.WriteLine(" Please input: " + line + " and 1-9");
else if (line.Length == 10 && write) Console.WriteLine(" Draw");
else Console.WriteLine();
Console.WriteLine("-----");
Console.WriteLine("{0}|{1}|{2}", c7,c8,c9);
}
static void PlayerTurn(string pos, char xo, int x)
{
string ppos = "";
pos = pos.Substring(1, pos.Length - 1);
for (int i = x; i < pos.Length - 1; i++){
i++;
ppos += pos[i];
}
if (ppos.IndexOf('1') > -1)
c1 = xo;
if (ppos.IndexOf('2') > -1)
c2 = xo;
if (ppos.IndexOf('3') > -1)
c3 = xo;
if (ppos.IndexOf('4') > -1)
c4 = xo;
if (ppos.IndexOf('5') > -1)
c5 = xo;
if (ppos.IndexOf('6') > -1)
c6 = xo;
if (ppos.IndexOf('7') > -1)
c7 = xo;
if (ppos.IndexOf('8') > -1)
c8 = xo;
if (ppos.IndexOf('9') > -1)
c9 = xo;
if((c1 == xo && c2 == xo && c3 == xo) || (c4 == xo && c5 == xo && c6 == xo) || (c7 == xo && c8 == xo && c9 == xo) || (c1 == xo && c4 == xo && c7 == xo) || (c2 == xo && c5 == xo && c8 == xo) || (c3 == xo && c6 == xo && c9 == xo) || (c1 == xo && c5 == xo && c9 == xo) || (c3 == xo && c5 == xo && c7 == xo)){
Console.WriteLine("Player won" + "\n");
write = false;
done = true;
}
if (pos.Length == 9)
done = true;
}
static void CPTurn(string pos, char xo, int x)
{
string cpos = "";
string avaiable = "123456789";
pos = pos.Substring(1, pos.Length - 1);
List<char> lista = avaiable.ToList();
Random ran = new Random();
int posLength = pos.Length;
for (int i = 0; i < posLength; i++) {
lista.RemoveAt(lista.IndexOf(pos[i]));
}
avaiable = string.Join("", lista.ToArray());
if (!done)
pos += avaiable[ran.Next(0, avaiable.Length)];
line += pos[pos.Length - 1];
for (int i = x; i < pos.Length - 1; i++){
i++;
cpos += pos[i];
}
if (cpos.IndexOf('1') > -1)
c1 = xo;
if (cpos.IndexOf('2') > -1)
c2 = xo;
if (cpos.IndexOf('3') > -1)
c3 = xo;
if (cpos.IndexOf('4') > -1)
c4 = xo;
if (cpos.IndexOf('5') > -1)
c5 = xo;
if (cpos.IndexOf('6') > -1)
c6 = xo;
if (cpos.IndexOf('7') > -1)
c7 = xo;
if (cpos.IndexOf('8') > -1)
c8 = xo;
if (cpos.IndexOf('9') > -1)
c9 = xo;
if((c1 == xo && c2 == xo && c3 == xo) || (c4 == xo && c5 == xo && c6 == xo) || (c7 == xo && c8 == xo && c9 == xo) || (c1 == xo && c4 == xo && c7 == xo) || (c2 == xo && c5 == xo && c8 == xo) || (c3 == xo && c6 == xo && c9 == xo) || (c1 == xo && c5 == xo && c9 == xo) || (c3 == xo && c5 == xo && c7 == xo)){
Console.WriteLine("CP won" + "\n");
write = false;
}
}
}
}