//====================================================
//
// KINGS is a puzzle based on chess.  In chess, it is never permitted
// to have two kings standing next to each other.  There must be at
// least one empty space between them.
//
// The puzzle involves placing many kings on the board, but legally
// with space between them.  There are several puzzles possible:
//
//  Maximum : what is the maximum number of kings that can
//      be placed on the board?
//
//  Minimum : what is the minimum number of kings which can "fill up"
//      the board, so that no more can be added legally?
//
//  Two player game:
//      Two players take turns placing kings on the board legally,
//      until the board is "full".  The last player to place a king wins.
//
// The program below is a VERY SIMPLE version that keeps track of where
// the kings have been placed.  It doesn't know any rules, so it
// will allow kings to be placed illegally.  It also allows a king
// to be placed where another is already standing.
//
// The purpose of this assignment is to get some practice programming
// 2-dimensional arrays and GRID oriented positioning.
// Do each of the following:
//
// 1 - Play the game several times with a partner - take turns clicking.
//     Try to figure out a reliable winning strategy.
//
// 2 - Find out what happens if you click on a space outside the board.
//     Try to prevent this by checking mouseX and mouseY to make
//     sure they are below 500 before playing the man.
//
// 3 - Make all the necessary changes so that the board
//     is 6 rows by 6 columns.
//
// 4 - Try to add a button to force the computer to choose a
//     random space and play in that position.
//
// 5 - Make the squares smaller - 60 pixels wide, 60 pixels high.
//     All the clicking should still work properly.
//     You will need to make the KING images smaller.
//
// 6 - Change the board to 8x8, like a normal chess board.
//     Make sure all the squares still work properly.
//
// 7 - Now add commands that PREVENT illegal moves:
//     - clicking off the sides is not permited but
//       should not cause an error (no red error messages)
//     - clicking on a square that already has a KING
//       should output a warning - "Don't click occupied squares"
//     - clicking on a square NEXT TO a KING should
//       be rejected with a warning - "That move is illegal"
//       This is a very tricky problem.
//
// 8 - The board is FULL if ALL SQUARES ARE ILLEGAL MOVES.
//     Write a method that decides whether the board is full.
//
// 9 - Change the program so that it keeps track of who
//     is making the next move, so that the computer AUTOMATICALLY
//     plays when it is the computer's turn.
//
// 10 - The computer can play PERFECTLY by following a simple
//     strategy for winning.  Change the computer's random
//     moves to intelligent moves - this is REALLY DIFFICULT!
//
//================================================


int[][] board = new int[5][5]; // default filled with 0's

void setup()
{
   size(600,600);
   textFont(createFont("AndaleMono-32.vlw",64));
}

void draw()
{
   background(0);
   drawSquares();
   showKings();
}

void drawSquares()
{
   int bw = -1;
   for(int row = 0; row < 5; row = row+1)
   {
      for(int col = 0; col < 5; col = col+1)
      {
         bw = - bw;  // toggle between 1 and -1
         if(bw == -1)
         { fill(255,0,0); }
         else
         { fill(0,255,0); }
         int x = 100*col;
         int y = 100*row;
         rect(x,y,100,100);
      }
   } 
}

void showKings()
{
   for(int row = 0; row < 5; row = row+1)
   {  for(int col = 0; col < 5; col = col+1)
      {
         if(board[row][col] == 1)
         {
            fill(0,0,0);
            stroke(0,0,0);
            text("K",col*100+25,row*100+75);
         }
      }
   }
}

void mouseClicked()
{
   int row = mouseY / 100;
   int col = mouseX / 100;
   board[row][col] = 1; 
}