#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // Display type and constructor
int coordinates[8][8][2] = {
{{2, 7}, {10, 7}, {18, 7}, {26, 7}, {34, 7}, {42, 7}, {50, 7}, {58, 7}},
{{2, 14}, {10, 14}, {18, 14}, {26, 14}, {34, 14}, {42, 14}, {50, 14}, {58, 14}},
{{2, 23}, {10, 23}, {18, 23}, {26, 23}, {34, 23}, {42, 23}, {50, 23}, {58, 23}},
{{2, 31}, {10, 31}, {18, 31}, {26, 31}, {34, 31}, {42, 31}, {50, 31}, {58, 31}},
{{2, 39}, {10, 39}, {18, 39}, {26, 39}, {34, 39}, {42, 39}, {50, 39}, {58, 39}},
{{2, 47}, {10, 47}, {18, 47}, {26, 47}, {34, 47}, {42, 47}, {50, 47}, {58, 47}},
{{2, 55}, {10, 55}, {18, 55}, {26, 55}, {34, 55}, {42, 55}, {50, 55}, {58, 55}},
{{2, 63}, {10, 63}, {18, 63}, {26, 63}, {34, 63}, {42, 63}, {50, 63}, {58, 63}}
};
void calculateDiagonalMoves(int initial_x, int initial_y, int dx, int dy) {
int x = initial_x;
int y = initial_y;
Serial.print("Initial coordinates: (");
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.println(")");
while (x >= 0 && x <= 7 && y >= 0 && y <= 7) {
Serial.print("Current coordinates: (");
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.println(")");
x += dx;
y += dy;
}
Serial.print("Final coordinates: (");
Serial.print(x - dx); // Adjust to print the last valid coordinates
Serial.print(", ");
Serial.print(y - dy);
Serial.println(")");
if (x < 0 || x > 7 || y < 0 || y > 7) {
Serial.println("Out of bounds! Calculation stopped.");
}
}
void calculateBishopMoves(int initial_x, int initial_y) {
// Call calculateDiagonalMoves for each direction
calculateDiagonalMoves(initial_x, initial_y, 1, -1); // xadd_yminus
calculateDiagonalMoves(initial_x, initial_y, -1, -1); // xminus_yminus
calculateDiagonalMoves(initial_x, initial_y, 1, 1); // xadd_yadd
calculateDiagonalMoves(initial_x, initial_y, -1, 1); // xminus_yadd
}
void calculateKnightMoves(int initial_x1, int initial_y1) {
// Define all possible knight move combinations
int knightMoves[8][2] = {
{2, -1}, {2, 1}, // x2+ y- and x2+ y+
{-2, -1}, {-2, 1}, // x2- y- and x2- y+
{1, -2}, {1, 2}, // y2+ x- and y2+ x+
{-1, -2}, {-1, 2} // y2- x- and y2- x+
};
Serial.print("Initial coordinates: (");
Serial.print(initial_x1);
Serial.print(", ");
Serial.print(initial_y1);
Serial.println(")");
// Iterate through each possible move
for (int i = 0; i < 8; ++i) {
int dx = knightMoves[i][0];
int dy = knightMoves[i][1];
int x = initial_x1 + dx;
int y = initial_y1 + dy;
// Check if the new position is within the chessboard boundaries (0 to 7)
if (x >= 0 && x <= 7 && y >= 0 && y <= 7) {
Serial.print("Move ");
Serial.print(i + 1); // Display move number
Serial.print(": (");
Serial.print(dx > 0 ? "+" : ""); // Print + sign for positive dx
Serial.print(dx);
Serial.print(", ");
Serial.print(dy > 0 ? "+" : ""); // Print + sign for positive dy
Serial.print(dy);
Serial.print(") -> (");
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.println(")");
}
}
}
void calculateRookMoves(int initial_x2, int initial_y2) {
// Define all possible rook move directions (x+, x-, y+, y-)
int rookMoves[4][2] = {
{1, 0}, {-1, 0}, // x+ and x-
{0, 1}, {0, -1} // y+ and y-
};
Serial.print("Initial coordinates: (");
Serial.print(initial_x2);
Serial.print(", ");
Serial.print(initial_y2);
Serial.println(")");
// Iterate through each possible move direction
for (int i = 0; i < 4; ++i) {
int dx = rookMoves[i][0];
int dy = rookMoves[i][1];
int x2 = initial_x2 + dx;
int y2 = initial_y2 + dy;
// Continue moving in the same direction until out of bounds (0 to 7)
while (x2 >= 0 && x2 <= 7 && y2 >= 0 && y2 <= 7) {
Serial.print("Move ");
Serial.print(i + 1); // Display move number
Serial.print(": (");
Serial.print(dx > 0 ? "+" : ""); // Print + sign for positive dx
Serial.print(dx);
Serial.print(", ");
Serial.print(dy > 0 ? "+" : ""); // Print + sign for positive dy
Serial.print(dy);
Serial.print(") -> (");
Serial.print(x2);
Serial.print(", ");
Serial.print(y2);
Serial.println(")");
x2 += dx;
y2 += dy;
}
}
}
void calculateQueenMoves(int initial_x3, int initial_y3) {
// Calculate rook-like moves
calculateRookMoves(initial_x3, initial_y3);
// Calculate bishop-like moves
calculateBishopMoves(initial_x3, initial_y3);
}
void calculateKingMoves(int initial_x4, int initial_y4) {
// Define all possible king move combinations
int kingMoves[8][2] = {
{1, 0}, {-1, 0}, // x+ y0 and x- y0
{0, 1}, {0, -1}, // y+ x0 and y- x0
{1, 1}, {1, -1}, // x+ y+ and x+ y-
{-1, 1}, {-1, -1} // x- y+ and x- y-
};
Serial.print("Initial coordinates: (");
Serial.print(initial_x4);
Serial.print(", ");
Serial.print(initial_y4);
Serial.println(")");
// Iterate through each possible move
for (int i = 0; i < 8; ++i) {
int dx = kingMoves[i][0];
int dy = kingMoves[i][1];
int x = initial_x4 + dx;
int y = initial_y4 + dy;
// Check if the new position is within the chessboard boundaries (0 to 7)
if (x >= 0 && x <= 7 && y >= 0 && y <= 7) {
Serial.print("Move ");
Serial.print(i + 1); // Display move number
Serial.print(": (");
Serial.print(dx > 0 ? "+" : ""); // Print + sign for positive dx
Serial.print(dx);
Serial.print(", ");
Serial.print(dy > 0 ? "+" : ""); // Print + sign for positive dy
Serial.print(dy);
Serial.print(") -> (");
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.println(")");
}
}
}
void setup() {
Serial.begin(9600); // Initialize serial communication
int initial_x = 4; // Example initial x coordinate bishop
int initial_y = 3; // Example initial y coordinate bishop
int initial_x1 = 5; // Example initial x coordinate knight
int initial_y1 = 3; // Example initial y coordinate knight
int initial_x2 = 2; // Example initial x coordinate rook
int initial_y2 = 3; // Example initial y coordinate rook
int initial_x3 = 3; // Example initial x coordinate queen
int initial_y3 = 4; // Example initial y coordinate queen
int initial_x4 = 2; // Example initial x coordinate king
int initial_y4 = 4; // Example initial y coordinate king
calculateKnightMoves(initial_x1, initial_y1);
calculateBishopMoves(initial_x, initial_y);
calculateRookMoves(initial_x2, initial_y2);
calculateQueenMoves(initial_x3, initial_y3);
calculateKingMoves(initial_x4, initial_y4);
}
void loop() {
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
u8g2.drawBox(0, 0, 8, 8);
u8g2.drawBox(16, 0, 8, 8);
u8g2.drawBox(32, 0, 8, 8);
u8g2.drawBox(48, 0, 8, 8);
u8g2.drawBox(8, 8, 8, 8);
u8g2.drawBox(24, 8, 8, 8);
u8g2.drawBox(40, 8, 8, 8);
u8g2.drawBox(56, 8, 8, 8);
u8g2.drawBox(0, 16, 8, 8);
u8g2.drawBox(16, 16, 8, 8);
u8g2.drawBox(32, 16, 8, 8);
u8g2.drawBox(48, 16, 8, 8);
u8g2.drawBox(8, 24, 8, 8);
u8g2.drawBox(24, 24, 8, 8);
u8g2.drawBox(40, 24, 8, 8);
u8g2.drawBox(56, 24, 8, 8);
u8g2.drawBox(0, 32, 8, 8);
u8g2.drawBox(16, 32, 8, 8);
u8g2.drawBox(32, 32, 8, 8);
u8g2.drawBox(48, 32, 8, 8);
u8g2.drawBox(8, 40, 8, 8);
u8g2.drawBox(24, 40, 8, 8);
u8g2.drawBox(40, 40, 8, 8);
u8g2.drawBox(56, 40, 8, 8);
u8g2.drawBox(0, 48, 8, 8);
u8g2.drawBox(16, 48, 8, 8);
u8g2.drawBox(32, 48, 8, 8);
u8g2.drawBox(48, 48, 8, 8);
u8g2.drawBox(8, 56, 8, 8);
u8g2.drawBox(24, 56, 8, 8);
u8g2.drawBox(40, 56, 8, 8);
u8g2.drawBox(56, 56, 8, 8);
u8g2.drawLine(64, -1, 64, 64);
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_5x7_tr);
u8g2.drawStr(67, 15, "P,U =Pawn");
u8g2.drawStr(67, 7, "K,O =King");
u8g2.setDrawColor(2);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(10, 55, "U");
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_5x7_tr);
u8g2.drawStr(67, 31, "F,Q =Queen");
u8g2.setDrawColor(2);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(2, 55, "U");
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_5x7_tr);
u8g2.drawStr(67, 39, "C,B =Bishop");
u8g2.setDrawColor(2);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(18, 55, "U");
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_5x7_tr);
u8g2.drawStr(67, 47, "I,H =Knight");
u8g2.setDrawColor(2);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(34, 55, "U");
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_5x7_tr);
u8g2.drawStr(67, 23, "S,R =Rook");
u8g2.setDrawColor(2);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(42, 55, "U");
u8g2.drawStr(50, 55, "U");
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(67, 55, "P,R,H,B,K,Q");
u8g2.drawStr(67, 62, "U,S,I,C,F,O ");
u8g2.setDrawColor(2);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(26, 55, "U");
u8g2.drawStr(58, 55, "U");
u8g2.setDrawColor(1);
u8g2.drawFrame(113, 57, 8, 5);
u8g2.drawBox(113, 50, 8, 5);
u8g2.setDrawColor(1);
u8g2.sendBuffer();
}