4x4 Tic Tac Toe game in C# Console application

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, I need some help with my tic tac toe game. I just need a clue about how to make this game so that when you get 3 in a diagonal you win.
<br/>
I know that it is a 4x4 board but you still win with 3 in a row,column and diagonal.
In your answers could you please include some note explaining what the parts of the code you sent does please, Im a student and I need to improve my understanding of everything, Think of me as not knowing anything when you reply.
Thanks to those helpful MSDN user in advance
namespace tic_tac_toe<br/>
{<br/>
class Program<br/>
{ <br/>
// Skeleton program code for the AQA Comp1 Summer 2010 examination
<br/>
// this code should be used in conjunction with the Preliminary
<br/>
// materials written by the AQA COMP1 Programmer Team developed in
<br/>
// the Visual Studio C# Express 2008 programming environment<br/>
<br/>
public static char[,] Board = new char[5, 5];<br/>
public static string PlayerOneName;<br/>
public static string PlayerTwoName;<br/>
public static float PlayerOneScore;<br/>
public static float PlayerTwoScore;<br/>
public static int XCoord;<br/>
public static int YCoord;<br/>
public static bool ValidMove;<br/>
public static int NoOfMoves;<br/>
public static bool GameHasBeenWon;<br/>
public static bool GameHasBeenDrawn;<br/>
public static char CurrentSymbol;<br/>
public static char StartSymbol;<br/>
public static char PlayerOneSymbol;<br/>
public static char PlayerTwoSymbol;<br/>
public static char Answer;<br/>
<br/>
static void Main(string[] args)<br/>
{<br/>
Console.WriteLine("What is the name of player one? ");<br/>
PlayerOneName = Console.ReadLine();<br/>
Console.WriteLine("What is the name of player two? ");<br/>
PlayerTwoName = Console.ReadLine();<br/>
Console.WriteLine();<br/>
PlayerOneScore = 0;<br/>
PlayerTwoScore = 0;<br/>
do // Choose player ones symbol<br/>
{<br/>
Console.WriteLine(PlayerOneName + " what symbol do you wish to use, X or O?");<br/>
PlayerOneSymbol = char.Parse(Console.ReadLine());<br/>
Console.WriteLine();<br/>
if (PlayerOneSymbol != X && PlayerOneSymbol != O)<br/>
{<br/>
Console.WriteLine("Symbol to play must be uppercase X or O");<br/>
Console.WriteLine();<br/>
}<br/>
} while (PlayerOneSymbol != X && PlayerOneSymbol != O);<br/>
if (PlayerOneSymbol == X)<br/>
PlayerTwoSymbol = O;<br/>
else<br/>
PlayerTwoSymbol = X;<br/>
StartSymbol = GetWhoStarts();<br/>
do // Play a game<br/>
{<br/>
NoOfMoves = 0;<br/>
GameHasBeenDrawn = false;<br/>
GameHasBeenWon = false;<br/>
ClearBoard(ref Board);//<br/>
Console.WriteLine();<br/>
DisplayBoard(Board);<br/>
if (StartSymbol == PlayerOneSymbol)<br/>
Console.WriteLine(PlayerOneName + " starts playing " + StartSymbol.ToString());<br/>
else<br/>
Console.WriteLine(PlayerTwoName + " starts playing " + StartSymbol.ToString());<br/>
Console.WriteLine();<br/>
CurrentSymbol = StartSymbol;<br/>
do // Play until a player wins or the game is drawn<br/>
{<br/>
do // Get a valid move<br/>
{<br/>
GetMoveCoordinates(ref XCoord, ref YCoord);<br/>
ValidMove = CheckValidMove(XCoord, YCoord, Board);<br/>
if (!ValidMove)<br/>
Console.WriteLine("Coordinates invalid, please try again");<br/>
} while (!ValidMove);<br/>
Board[XCoord, YCoord] = CurrentSymbol;<br/>
DisplayBoard(Board);<br/>
GameHasBeenWon = CheckXOrOHasWon(Board);<br/>
NoOfMoves++;<br/>
if (!GameHasBeenWon)<br/>
{<br/>
if (NoOfMoves == 16) // Check if maximum number of<br/>
// allowed moves has been reached<br/>
GameHasBeenDrawn = true;<br/>
else<br/>
{<br/>
if (CurrentSymbol == X)<br/>
CurrentSymbol = O;<br/>
else<br/>
CurrentSymbol = X;<br/>
}<br/>
}<br/>
} while (!GameHasBeenWon && !GameHasBeenDrawn);<br/>
if (GameHasBeenWon) // Update scores and display results<br/>
{<br/>
if (PlayerOneSymbol == CurrentSymbol)<br/>
{<br/>
Console.WriteLine(PlayerOneName + " congratulations you win!");<br/>
PlayerOneScore++;<br/>
}<br/>
else<br/>
{<br/>
Console.WriteLine(PlayerTwoName + " congratulations you win!");<br/>
PlayerTwoScore++;<br/>
}<br/>
}<br/>
else<br/>
Console.WriteLine("A draw this time!");<br/>
Console.WriteLine();<br/>
Console.WriteLine(PlayerOneName + ", your score is: " + PlayerOneScore.ToString());<br/>
Console.WriteLine(PlayerTwoName + ", your score is: " + PlayerTwoScore.ToString());<br/>
Console.WriteLine();<br/>
if (StartSymbol == PlayerOneSymbol)<br/>
StartSymbol = PlayerTwoSymbol;<br/>
else<br/>
StartSymbol = PlayerOneSymbol;<br/>
Console.WriteLine("Another game Y/N? ");<br/>
Answer = char.Parse(Console.ReadLine());<br/>
} while (Answer != N && Answer != n);<br/>
} // end Main<br/>
<br/>
public static void DisplayBoard(char[,] Board)<br/>
{<br/>
int Row;<br/>
int Column;<br/>
<br/>
Console.WriteLine(" 1 2 3 4 ");<br/>
Console.WriteLine("--+---------");<br/>
for (Row = 1; Row <= 4; Row++)<br/>
{<br/>
Console.Write((Row).ToString() + " ");<br/>
for (Column = 1; Column <= 4; Column++)<br/>
{<br/>
Console.Write(Board[Column, Row] + " ");<br/>
}<br/>
Console.WriteLine();<br/>
}<br/>
Console.WriteLine();<br/>
} // end DisplayBoard<br/>
<br/>
public static void ClearBoard(ref char[,] Board)<br/>
{<br/>
int Row;<br/>
int Column;<br/>
<br/>
for (Row = 1; Row <= 4; Row++)<br/>
{<br/>
for (Column = 1; Column <= 4; Column++)<br/>
{<br/>
Board[Row, Column] = ;<br/>
}<br/>
}<br/>
} // end ClearBoard<br/>
<br/>
public static void GetMoveCoordinates(ref int XCoordinate, ref int YCoordinate)<br/>
{<br/>
Console.Write("Enter x coordinate: ");<br/>
XCoordinate = int.Parse(Console.ReadLine());<br/>
Console.Write("Enter y coordinate: ");<br/>
YCoordinate = int.Parse(Console.ReadLine());<br/>
Console.WriteLine();<br/>
} // end of GetMoveCoordinates<br/>
<br/>
public static bool CheckValidMove(int XCoordinate, int YCoordinate, char[,] Board)<br/>
{<br/>
bool ValidMove = true;<br/>
<br/>
if (XCoordinate < 1 || XCoordinate > 4) // Check X coordinate is valid<br/>
ValidMove = false;<br/>
if (YCoordinate < 1 || YCoordinate > 4) // Check Y coordinate is valid<br/>
ValidMove = false;<br/>
if (Board[XCoordinate, YCoordinate] != char.Parse (" "))<br/>
ValidMove = false;<br/>
return ValidMove;<br/>
} // end CheckValidMove<br/>
<br/>
public static bool CheckXOrOHasWon(char[,] Board)<br/>
{<br/>
bool XOrOHasWon;<br/>
int Row;<br/>
int Column;<br/>
<br/>
XOrOHasWon = false;<br/>
for (Column = 1; Column <= 4; Column++)<br/>
{<br/>
if (Board[Column, 1] == Board[Column, 2]<br/>
&& Board[Column, 2] == Board[Column, 3]<br/>
&& Board[Column, 2] != )<br/>
XOrOHasWon = true;<br/>
}<br/>
for (Row = 1; Row <= 4; Row++)<br/>
{<br/>
if (Board[1, Row] == Board[2, Row]<br/>
&& Board[2, Row] == Board[3, Row]<br/>
&& Board[2, Row] != )<br/>
XOrOHasWon = true;<br/>
}<br/>
<br/>
return XOrOHasWon;<br/>
} // end of CheckXOrOHasWon<br/>
<br/>
public static char GetWhoStarts()<br/>
{<br/>
char WhoStarts;<br/>
Random objRandom = new Random();<br/>
int RandomNo = objRandom.Next(100);<br/>
if (RandomNo % 2 == 0)<br/>
WhoStarts = X;<br/>
else<br/>
WhoStarts = O;<br/>
return WhoStarts;<br/>
} // end of GetWhoStarts<br/>
}<br/>
} <hr class="sig Im super noob when it comes to programming, Ill love it if you can be as simple as you can or add comment to each section of your answer when answering my questions :D

View the full article
 

Similar threads

P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
Back
Top