#include <Wire.h>
//******************************************************************
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
int PuntosX=0;
int PuntosO=0;
const byte FILAS = 4;
const byte COLUMNAS = 4;
char Teclas[FILAS][COLUMNAS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
byte FilasPin[FILAS] = {13,12,14,27};
byte ColumnasPin[COLUMNAS] = {26,25,33,32};
Keypad keypad = Keypad(makeKeymap(Teclas), FilasPin, ColumnasPin, FILAS, COLUMNAS);
// Tabla interna del juego
char posiciones[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
// turnos 0 para O, 1 para X
int jugadorTurno;
int Actual;
int elegido=0;
//*****************************************************************************************************************************************
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const unsigned char logo [] PROGMEM = {
0xff, 0x3f, 0xfc, 0x3f, 0xff, 0xff, 0x03, 0x0f, 0xb9, 0xff, 0xf8, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0x3f, 0xfc, 0x7f, 0xff, 0xfe, 0xc2, 0x1f, 0xa7, 0xfb, 0xf8, 0x00, 0x3f, 0xff, 0xff, 0xff,
0xff, 0x3f, 0xfc, 0x7f, 0xff, 0xfe, 0xc8, 0x3b, 0x8f, 0x03, 0xf8, 0x00, 0x1f, 0xff, 0xff, 0xff,
0xff, 0x3f, 0xf8, 0x7f, 0xff, 0xfe, 0xe0, 0x73, 0xd8, 0x3f, 0xf8, 0x00, 0x0f, 0xff, 0xff, 0xff,
0xff, 0x3f, 0xf0, 0xff, 0xff, 0xfe, 0xf8, 0xf3, 0x30, 0x0f, 0xf8, 0x00, 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xf9, 0xdf, 0xe0, 0x01, 0xff, 0xe0, 0x01, 0xff, 0xff,
0xff, 0xff, 0xff, 0x27, 0xf6, 0xfc, 0xfe, 0x79, 0xef, 0x00, 0x03, 0x7b, 0xf4, 0x00, 0xff, 0xff,
0xff, 0xff, 0xff, 0x6f, 0xf4, 0xf9, 0xff, 0x73, 0xe8, 0x18, 0x07, 0xee, 0xfe, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xff, 0x4f, 0xfa, 0x63, 0xff, 0xa3, 0xe0, 0xc0, 0x1f, 0xff, 0xbe, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xff, 0x0f, 0xfe, 0x27, 0xff, 0xe7, 0x87, 0xe1, 0x0f, 0xff, 0xfe, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xff, 0x8f, 0xff, 0xef, 0xff, 0xe6, 0x2c, 0x09, 0x1f, 0xbb, 0xec, 0x40, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xef, 0x88, 0xe8, 0x04, 0x1f, 0xfe, 0xb9, 0x40, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xc1, 0xe0, 0x30, 0x1f, 0x80, 0x00, 0x80, 0x3f, 0xff,
0xff, 0xff, 0xfd, 0xcf, 0xff, 0xff, 0xff, 0x87, 0xf0, 0x43, 0x1a, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0xff, 0x1f, 0xe0, 0x48, 0x00, 0x0f, 0xe0, 0x00, 0x1f, 0xff,
0xff, 0xff, 0xff, 0xe7, 0xff, 0xff, 0xfe, 0x3f, 0xc0, 0x88, 0x00, 0xff, 0xfc, 0x60, 0x1f, 0xff,
0xff, 0xff, 0xff, 0xe7, 0xff, 0xff, 0xfc, 0x3f, 0xc8, 0x22, 0x43, 0xff, 0xfe, 0x3c, 0x1f, 0xff,
0xff, 0xff, 0xff, 0xb3, 0xff, 0xff, 0xf8, 0xcf, 0x88, 0x24, 0x0f, 0xff, 0xff, 0x1f, 0x8f, 0xff,
0xff, 0xff, 0xfe, 0xf1, 0xdf, 0xff, 0xe0, 0xff, 0x58, 0x88, 0x1f, 0xff, 0xff, 0x1f, 0xc3, 0xff,
0xff, 0xff, 0xff, 0xf0, 0xef, 0xff, 0x91, 0xff, 0x18, 0x24, 0x3f, 0xff, 0xff, 0x9f, 0xe3, 0xff,
0xff, 0xff, 0xff, 0xfc, 0x73, 0xfd, 0x43, 0xfc, 0x98, 0x24, 0x7f, 0xff, 0xff, 0x8f, 0xf1, 0xff,
0xff, 0xff, 0xff, 0xff, 0x04, 0x00, 0x0b, 0xf7, 0x38, 0x08, 0xff, 0xff, 0xff, 0xcf, 0xf8, 0xff,
0xff, 0xff, 0xff, 0xc0, 0x40, 0x00, 0x69, 0x9a, 0x78, 0x11, 0xff, 0xff, 0xff, 0xcf, 0xfc, 0xff,
0xff, 0xff, 0xe1, 0xc0, 0x1e, 0xff, 0xfc, 0xd8, 0xf0, 0xf1, 0xff, 0xff, 0xff, 0xcf, 0xfc, 0x7f,
0xff, 0xff, 0x80, 0x0e, 0x0e, 0x7b, 0xf4, 0x01, 0xc3, 0xa3, 0xff, 0xff, 0xff, 0xcf, 0xfe, 0x7f,
0xff, 0xff, 0x00, 0x30, 0x07, 0x3d, 0xfc, 0x8f, 0x89, 0xe3, 0xff, 0xff, 0xff, 0xcf, 0xfe, 0x7f,
0xff, 0xfe, 0x00, 0x60, 0x03, 0xde, 0xf9, 0xff, 0x00, 0x27, 0xff, 0xff, 0xff, 0xcf, 0xfe, 0x3f,
0xff, 0xfc, 0x00, 0x40, 0x01, 0xee, 0x79, 0xfe, 0x00, 0xe7, 0xbf, 0xff, 0xff, 0xdf, 0xfe, 0x3f,
0xff, 0xf8, 0x01, 0x00, 0x01, 0xf7, 0xcf, 0xfe, 0x1f, 0xe6, 0x7f, 0xff, 0xbf, 0x9f, 0xfe, 0x3f,
0xff, 0xf0, 0x00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0xe1, 0x03,
0xff, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf9, 0xdf, 0x80, 0x20, 0x1b, 0x7b, 0xf9, 0xdf, 0xc7, 0x03,
0xff, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf9, 0x70, 0x01, 0x00, 0x4f, 0x46, 0x69, 0x9f, 0x8e, 0x03,
0xff, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf1, 0x82, 0x40, 0x00, 0x0f, 0x6d, 0x69, 0xfe, 0x1e, 0x03,
0xff, 0x80, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x18, 0x00, 0x00, 0x07, 0x5b, 0x69, 0xfc, 0x3c, 0x83,
0xff, 0x80, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x44, 0x03, 0xf0, 0x17, 0xb0, 0xb1, 0xf8, 0xfc, 0x07,
0xff, 0xc0, 0x00, 0x00, 0x00, 0x3f, 0xe0, 0x40, 0x3f, 0xf8, 0x01, 0x84, 0x83, 0x61, 0xb0, 0x07,
0xff, 0xc0, 0x00, 0x00, 0x00, 0x3f, 0xe0, 0x01, 0xff, 0xfc, 0x44, 0xc2, 0x46, 0x4f, 0x92, 0x07,
0xff, 0xe0, 0x00, 0x00, 0x00, 0x3f, 0xe0, 0x07, 0xff, 0xfe, 0x03, 0x7e, 0xff, 0x8f, 0x90, 0x0f,
0xff, 0xe0, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x0f, 0xff, 0xfe, 0x01, 0xbf, 0xfb, 0x87, 0x20, 0x1f,
0xff, 0xf0, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x31, 0xff, 0xfe, 0xc1, 0xc7, 0xf7, 0x14, 0x20, 0x1f,
0xff, 0xe0, 0x00, 0x00, 0x01, 0xff, 0xe2, 0x6f, 0xff, 0xff, 0x08, 0x02, 0xa2, 0x13, 0xf0, 0x3f,
0xff, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xcf, 0x90, 0xff, 0xff, 0x20, 0x00, 0x00, 0x40, 0x18, 0x7f
};
void setup() {
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
else Serial.println(F("Conexion exitosa!!"));
//**************************************************************************************
display.clearDisplay(); //limpiar pantalla
display.drawBitmap(0,0,logo,128,64,WHITE);
display.display();
delay(2000);
display.clearDisplay();
display.display();
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(" CREDITOS: ");
display.println(" ");
display.println("Osiel Lopez");
display.println("Diego Rodriguez");
display.println("Jaime Lozano");
display.println("Fernando Huerta");
display.println("Natalia Valdez");
display.println(" ");
display.display();
delay(2000);
display.clearDisplay();
display.display();
}
void loop() {
//inicia con la funcion random para determinar el jugador que inicia
if (elegido == 0){
Actual=random(0,2);
jugadorTurno=Actual;
if(jugadorTurno==0){
elegido=1;
display.clearDisplay();
display.display();
display.setTextSize(6);
display.setTextColor(WHITE);
display.setCursor(50,10);
display.println("O");
display.display();
delay(2000);
}
else{
elegido=1;
display.clearDisplay();
display.display();
display.setTextSize(6);
display.setTextColor(WHITE);
display.setCursor(50,10);
display.println("X");
display.display();
delay(2000);
}
}
lineasTablaXO();
char tecla = keypad.getKey();
if (tecla) {
int fil = (tecla - '1') / 3;
int col = (tecla - '1') % 3;
if (posiciones[fil][col] != 'O' && posiciones[fil][col] != 'X') {
if (jugadorTurno == 0) {
posiciones[fil][col] = 'O';
} else {
posiciones[fil][col] = 'X';
}
lineasTablaXO();
if(ganador()){
if (jugadorTurno==0){
delay(50);
PuntosO++;
display.clearDisplay();
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("O Ganador!");
display.setTextSize(1);
display.println(" ");
display.println(" Marcador");
display.println(" O X");
display.print(" ");
display.print(PuntosO);
display.print(" VS ");
display.print(PuntosX);
display.display();
delay(3500);
posiciones[0][0] = '1'; posiciones[0][1] = '2'; posiciones[0][2] = '3';
posiciones[1][0] = '4'; posiciones[1][1] = '5'; posiciones[1][2] = '6';
posiciones[2][0] = '7'; posiciones[2][1] = '8'; posiciones[2][2] = '9';
display.clearDisplay();
display.display();
elegido=0;
delay(50);
} else {
delay(50);
PuntosX++;
display.clearDisplay();
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("X Ganador!");
display.setTextSize(1);
display.println("");
display.println(" Marcador");
display.println(" O X");
display.print(" ");
display.print(PuntosO);
display.print(" VS ");
display.print(PuntosX);
display.display();
delay(3500);
posiciones[0][0] = '1'; posiciones[0][1] = '2'; posiciones[0][2] = '3';
posiciones[1][0] = '4'; posiciones[1][1] = '5'; posiciones[1][2] = '6';
posiciones[2][0] = '7'; posiciones[2][1] = '8'; posiciones[2][2] = '9';
display.clearDisplay();
display.display();
elegido=0;
delay(50);
}
} else
if ( empate()){
delay(50);
display.clearDisplay();
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(" Empate!!");
display.setTextSize(1);
display.println("");
display.println(" Marcador");
display.println(" O X");
display.print(" ");
display.print(PuntosO);
display.print(" VS ");
display.print(PuntosX);
display.display();
delay(3500);
posiciones[0][0] = '1'; posiciones[0][1] = '2'; posiciones[0][2] = '3';
posiciones[1][0] = '4'; posiciones[1][1] = '5'; posiciones[1][2] = '6';
posiciones[2][0] = '7'; posiciones[2][1] = '8'; posiciones[2][2] = '9';
display.clearDisplay();
display.display();
elegido=0;
delay(50);
}else{
jugadorTurno= 1-jugadorTurno;
}
}
}
}
//funcion para dibujar tablero y las X y O
void lineasTablaXO(){
// Lienas
display.clearDisplay();
display.display();
display.drawLine(42,0,42,63, WHITE);
display.drawLine(84,0,84,63, WHITE);
display.drawLine(0,21,127,21, WHITE);
display.drawLine(0,42,127,42, WHITE);
display.display();
// O y X dentro de la tabla
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int x = j * (42)+(21);
int y = i * (21)+(10);
if (posiciones[i][j] == 'O') {
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(x-3, y-5);
display.print("O");
} else if (posiciones[i][j] == 'X') {
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(x-3, y-5);
display.print("X");
}
}
}
display.display();
}
// verifica si hay victoria
bool ganador() {
for (int i = 0; i < 3; i++) {
if (posiciones[i][0] == posiciones[i][1] && posiciones[i][1] == posiciones[i][2]) {
return true;
}
}
for (int j = 0; j < 3; j++) {
if (posiciones[0][j] == posiciones[1][j] && posiciones[1][j] == posiciones[2][j]) {
return true;
}
}
if ((posiciones[0][0] == posiciones[1][1] && posiciones[1][1] == posiciones[2][2]) ||
(posiciones[0][2] == posiciones[1][1] && posiciones[1][1] == posiciones[2][0])) {
return true;
}
return false;
}
// verifica si hay empate
bool empate() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (posiciones[i][j] != 'O' && posiciones[i][j] != 'X') {
return false;
}
}
}
return true;
}