//====================================================
// TicTacToe.cpp
//====================================================

#include "DragonFireSDK.h"
//====================================================
// Handle declarations
int HorizontalLine;
int VerticalLine;
int BlankSquare;
int XSquare;
int OSquare;
int Background;
int WinSound;
int MessageFont;
int MessageText;

// Array declarations:
int VerticalLineViews[2];
int HorizontalLineViews[2];
int SquareViews[9];
int SquareVisited[9];
int SquareValue[9];

// Allow Moves determines whether any more moves can be made
bool AllowMoves;

// Constant declatations
const int WinConditions[8][3] =
{
{0, 1, 2},
//Down win conditions
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
//Across win conditions
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
//Diagonal win conditions
{2, 4, 6}
};

// The first player is always 'X'.
char Player = 'X';

//====================================================
// CheckWinCondition checks whether the specified player has met the
// conditions to win the game, as specified in our constant, WinningCombinations.

bool CheckWinCondition(char Player)
{
  
bool PlayerWins;

  
for (int i = 0; i < 8; i++)
  
{
     
// Assume the player wins, until we show otherwise.
      PlayerWins =
true;

     
for (int j = 0; j < 3; j++)
     
{
        
// If the current square contains any value other than the
         // player's mark, he cannot win this
         // permutation of the winning scenarios, so short curcuit the for
         // loop, and evaluate the next WinningCombination.


         PlayerWins = (PlayerWins && (SquareValue[WinConditions[i][j]] == Player));
        
if (!PlayerWins)
        
{
           
break;
        
}
     
}
     
if (PlayerWins)
     
{
        
break;
     
}
  
}

  
// If the player has met a WinningCombination, disallow any further moves.
   AllowMoves = !PlayerWins;
  
return PlayerWins;
}

//====================================================
// OnPlayerMove is called whenever a square is touched. If a move is possible, the player
// whose turn it is has their mark placed in the square.

int OnPlayerMove(int id, int evt, int x, int y)
{
  
if (AllowMoves && !SquareVisited[id])
  
{
      SquareVisited[id] =
true;
     
if (Player == 'X')
     
{
         ViewSetImage(SquareViews[id], XSquare);
         SquareValue[id] =
'X';
         Player =
'O';
        
if (CheckWinCondition('X'))
        
{
           
// Player X wins!
            TextSetText(MessageText,
"X wins! ");
            SoundPlay(WinSound);
        
}
     
}
     
else
     
{
         ViewSetImage(SquareViews[id], OSquare);
         SquareValue[id] =
'O';
         Player =
'X';
        
if (CheckWinCondition('O'))
        
{
           
// Player O wins!
            TextSetText(MessageText,
"O wins! ");
            SoundPlay(WinSound);
        
}
     
}
  
}
return 0;
}

//====================================================
// OnReset is called as a result of pressing the Reset Button.
int OnReset(int id)
{
   Player =
'X';
   AllowMoves =
true;
  
for (int i = 0; i < 9; i++)
  
{
      ViewSetImage(SquareViews[i], BlankSquare);
      SquareVisited[i] =
false;
      SquareValue[i] =
' ';
  
}
   TextSetText(MessageText,
"No winner ");
  
return 0;
}

//====================================================
void LoadImages()
{
  
// Load Images from File
   HorizontalLine = ImageAdd(
"Assets/Images/HorizontalLine.PNG");
   VerticalLine = ImageAdd(
"Assets/Images/VerticalLine.PNG");
   BlankSquare = ImageAdd(
"Assets/Images/BlankSquare.PNG");
   XSquare = ImageAdd(
"Assets/Images/XSquare.PNG");
   OSquare = ImageAdd(
"Assets/Images/OSquare.PNG");
   Background = ImageAdd(
"Assets/Images/Background.PNG");
}

//====================================================
// AppMain intitalizes all the objects in the game, and sets up the playing field.
void AppMain()
{
  
// Initialize Images
   LoadImages();

   ViewAdd(Background,0,0);

  
// Initialize Vertical line Views
   VerticalLineViews[0] = ViewAdd(VerticalLine, 100, 0);
   VerticalLineViews[1] = ViewAdd(VerticalLine, 210, 0);

   // Initialize Horizontal line Views
   HorizontalLineViews[0] = ViewAdd(HorizontalLine, 0, 100);
   HorizontalLineViews[1] = ViewAdd(HorizontalLine, 0, 210);

   // Initialize Sounds
   WinSound = SoundAdd("Assets/Sounds/tada.wav");

   // Initialize Texts:
   MessageFont = FontAdd("Assets/Fonts/Arial24Bold");
   MessageText = TextAdd(100, 325,
"No winner ", MessageFont);

  
// Add the reset button so we can play a new game
   ButtonAdd(
"Assets/Images/ResetButton", 0, 400, OnReset, 0);

   // Sets the AllowMoves variable to true, so that we can actually play the game.
   AllowMoves = true;

   // Initialize the grid with blank squares.
   int i = 0;
  
for (int x = 0; x < 3; x++)
  
{
     
for (int y = 0; y < 3; y++)
     
{
         SquareVisited[i] =
false;
         SquareViews[i] = ViewAdd(
"Assets/Images/BlankSquare.PNG", x * 110, y * 110, OnPlayerMove, i);
         i++;
     
}
  
}
}

//====================================================
// OnTimer is not used in this project.
void OnTimer()
{
//Main loop code goes here.
}

//====================================================