#ifndef JUGADORFACIL_H
#define JUGADORFACIL_H
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <ctime>
#include <string.h>
using namespace std;
class JugadorFacil
{
private:
char tablero[6][7];
public:
JugadorFacil();
int ejecutar();
bool revisar(int a, int b);
void mostrar();
int fichas(int b, char jugador);
};
#include "JugadorFacil.h"
JugadorFacil::JugadorFacil()
{
//ctor
}
int JugadorFacil::ejecutar()
{
for(int a =0;a <= 5; a++){
for(int b = 0; b<=6; b++)
tablero[a][b] = ' ';
}
//Pongo el =' ' para dejar los espacios en blanco en el tablero
mostrar();
int ficha;//Will house user row choice
int ficha2 = 0;//will hold drop value
int fichasPuestas = 0;//Number of peices dropped so can end game if a draw
bool juegoTerminado = false;//Will be changed to true when game is won and will exit while loop
char jugador = 15;//start as player 2 will change back 2 player 1
while(!juegoTerminado){//will stop when game is won, ! means NOT makes the oppisite be checked
if(ficha2 != -1){//check if there was a error in the last drop
if(jugador == 15){//if player 2 lasted dropped a piece so its player 1s turn
cout<<"Jugador 1: Donde quieres poner tu ficha?";
jugador = 254;//char of players piece
}
else{
cout<<"Jugador 2: Donde quieres poner tu ficha?";
jugador = 15;//char of player piece
}
}
while(true){//will run untill 'break;'
if(fichasPuestas == 42) break;//if draw
cin>>ficha;//get user input
ficha--;//take off 1 to account for arrays starting at 0 not 1
if(ficha <=6 && ficha>= 0) break;//if within valid range stop loop
else cout<< "\nPor favor, ingresa un valor entre 1 y 7 :";//ask for input and loop again
if (cin.fail()) //catch a non number
{ //
cin.clear(); //Stops cin trying to put its value in to hold
char c; //Try entering a non number without this, 2 see what this does
cin>>c; //
} //Catch a non number
}
if(fichasPuestas == 42) break;//if draw
ficha2 = fichas(ficha,jugador);//drop the player store the row in hold2
if(ficha2 == -1) cout<<"La columna esta llena...\nPorfavor ingresa otro numero entre 1 y 7:";//if error -1 row is full
else{
juegoTerminado = revisar(ficha2,ficha);//check if game is run
fichasPuestas ++;//another character has been succesfully placed
system("cls");//This clears the screen works with windows, not nesscery to run game
mostrar();//displayed updated board
}
}
system("cls");//this clears the screen
if(fichasPuestas == 42){//if draw
cout<<"No hay ganador!\n";
system("pause");
return 0;
}
if(jugador == 15)//if won by player 2
cout<<"El jugador 2 gano!!!\n";
else cout<<"El jugador 1 gano!!!\n";//Else won by player 1
system("pause");//pauses before exit so players can see who won, works with windows
return 0;//Exit application
}
void JugadorFacil::mostrar() // Draw board
{
cout<<" 1 2 3 4 5 6 7\n";
for(int a = 0; a<= 5; a++)
{
for(int b =0; b <= 6; b++) cout<<char(218)<<char(196)<<char(191)<<" ";
cout<<'\n';
for(int b =0; b <= 6; b++) cout<<char(179)<<tablero[a][b]<<char(179)<<" ";
cout<<'\n';
for(int b =0; b <= 6; b++) cout<<char(192)<<char(196)<<char(217)<<" ";
cout<<'\n';
}
}
bool JugadorFacil::revisar(int a, int b)
{
int vertical = 1;
int horizontal = 1;
int diagonal1 = 1;
int diagonal2 = 1;
char jugador = tablero[a][b];
int i; //vertical
int ii;//horizontal
//revisando verticales
for(i = a +1;tablero[i][b] == jugador && i <= 5;i++,vertical++);//Check down
for(i = a -1;tablero[i][b] == jugador && i >= 0;i--,vertical++);//Check up
if(vertical >= 4)return true;
//revisando horizontales
for(ii = b -1;tablero[a][ii] == jugador && ii >= 0;ii--,horizontal++);//Check left
for(ii = b +1;tablero[a][ii] == jugador && ii <= 6;ii++,horizontal++);//Check right
if(horizontal >= 4) return true;
//revisando diagonal 1
for(i = a -1, ii= b -1;tablero[i][ii] == jugador && i>=0 && ii >=0; diagonal1 ++, i --, ii --);//up and left
for(i = a +1, ii = b+1;tablero[i][ii] == jugador && i<=5 && ii <=6;diagonal1 ++, i ++, ii ++);//down and right
if(diagonal1 >= 4) return true;
//revisando diagonal 2
for(i = a -1, ii= b +1;tablero[i][ii] == jugador && i>=0 && ii <= 6; diagonal2 ++, i --, ii ++);//up and right
for(i = a +1, ii= b -1;tablero[i][ii] == jugador && i<=5 && ii >=0; diagonal2 ++, i ++, ii --);//up and left
if(diagonal2 >= 4) return true;
return false;
}
int JugadorFacil::fichas(int b, char jugador)
{
if(b >=0 && b<= 6)
{
if(tablero[0][b] == ' '){
int i;
for(i = 0;tablero[i][b] == ' ';i++)
if(i == 5)
{tablero[i][b] = jugador;
return i;}
i--;
tablero[i][b] =jugador;
return i;
}
else{
return -1;
}
}
else{
return -1;
}
}