Beruflich Dokumente
Kultur Dokumente
TECHNOLOGIES
ANSWERS
BLOGS
VIDEOS
INTERVIEWS
BOOKS
NEWS
EVENTS
o CHAPTERS
o ANNUAL CONFERENCE
o AHMEDABAD CONFERENCE
o CHANDIGARH CONFERENCE
o DELHI CONFERENCE
o HYDERABAD CONFERENCE
o MATHURA CONFERENCE
o PUNE CONFERENCE
o STARTUP CONFERENCE
CAREER
JOBS
TRAINING
CONSULTANTS
IDEAS
o MEMBERS
o STORIES
o CERTIFICATIONS
Tic-Tac-Toe Game in C#
Sourabh Somani
Apr 07 2014
288.3k
6
5
linkedI n
What sA pp
o
o
o
TIC_TAC_TOE.rar
Download Free .NET & JAVA Files API
In this article I am showing you how to create a very simple game of Tic-Tac-Toe in a C# console
application.
Introduction
Tic-Tac-Toe is a very simple two player game. So only two players can play at a time. This game is
also known as Noughts and Crosses or Xs and Os game. One player plays with X and the other
player plays with O. In this game we have a board consisting of a 3X3 grid. The number of grids
may be increased.
Game Rules
1. Traditionally the first player plays with "X". So you can decide who wants to go "X" and who
wants go with "O".
2. Only one player can play at a time.
3. If any of the players have filled a square then the other player and the same player cannot
override that square.
4. There are only two conditions that may be match will be draw or may be win.
5. The player that succeeds in placing three respective mark (X or O) in a horizontal, vertical or
diagonal row wins the game.
Winning condition
Whoever places three respective marks (X or O) horizontally vertically or diagonally will be the
winner.
using System;
using System.Threading;
namespace TIC_TAC_TOE
{
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 intchoice; //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;
// 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 me
ssage 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
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(" | | ");
}
else
{
return 0;
}
}
}