Tic Tac Toe

I wrote Tic Tac Toe as a first attempt at coding a game. I chose Tic Tac Toe because of the film War Games.

Here is an example of the AI code used when the AI must play a blocking move to prevent the player playing a winning piece.

public static void BlockingMove(ObservableCollection gameBoard, string playerPiece, string aiPiece, int firstSquare, int secondSquare, int thirdSquare, out bool blockSuccess)
{
blockSuccess = false;
bool firstPiece = gameBoard[firstSquare].pieceContent == playerPiece ? true : false;
bool secondPiece = gameBoard[secondSquare].pieceContent == playerPiece ? true : false;
bool thirdPiece = gameBoard[thirdSquare].pieceContent == playerPiece ? true : false;

if (firstPiece == true && secondPiece == true)
{
if (gameBoard[thirdSquare].pieceContent == " ")
{
gameBoard[thirdSquare].pieceContent = aiPiece;
blockSuccess = true;
return;
}
}

if (firstPiece == true && thirdPiece == true)
{
if (gameBoard[secondSquare].pieceContent == " ")
{
gameBoard[secondSquare].pieceContent = aiPiece;
blockSuccess = true;
return;
}
}

if (secondPiece == true && thirdPiece == true)
{
if (gameBoard[firstSquare].pieceContent == " ")
{
gameBoard[firstSquare].pieceContent = aiPiece;
blockSuccess = true;
return;
}
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s