//====================================================
//
// 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 above 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!
//
//=====================================================
PImage king ;
int maxi = 8; // PARITY error = even/odd
int rsize = 60;
int[][] board = new int[maxi][maxi]; // default filled with 0's
void setup()
{
size(maxi*rsize+100,maxi*rsize+100);
king = loadImage("king.gif");
}
void draw()
{
background(255);
drawSquares();
showKings();
image(king,maxi*rsize+25,0,rsize*0.8,rsize*0.8);
}
void drawSquares()
{
int bw = -1;
for(int row = 0; row < maxi; row = row+1) // 6 = MAGIC NUMBER
{
// every number needs a name
for(int col = 0; col < maxi; col = col+1)
{
bw = - bw; // toggle between 1 and -1
if(bw == -1)
{ fill(255,0,0); }
else
{ fill(0,255,0); }
int x = rsize*col;
int y = rsize*row;
rect(x,y,rsize,rsize);
}
if(maxi % 2 == 0)
{ bw = - bw; }
}
}
void showKings()
{
for(int row = 0; row < maxi; row = row+1)
{ for(int col = 0; col < maxi ; col = col+1)
{
if(board[row][col] == 1)
{
image(king,col*rsize+rsize*0.1,row*rsize+rsize*0.1,rsize*0.8,rsize*0.8);
}
}
}
}
void mouseClicked()
{
if( mouseX <= maxi*rsize)
{
println(mouseX + " " + mouseY);
int row = mouseY / rsize; // DIGITIZES the mouse position
int col = mouseX / rsize; // integer division 590/100 = 5 TRUNCATES the decimal
println(row + " " + col);
board[row][col] = 1;
}
else
{
computerChooses();
}
}
void computerChooses()
{
int crow = int( random(0,maxi-1) );
int ccol = int( random(0,maxi-1) );
board[crow][ccol] = 1;
}