Creo no se ve la imagen pero este es el link; https://gyazo.com/98c6482f27ed83e14ea4cc5cb7c6a5dd
Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.
Mostrar Mensajes Menúvoid List::editByValue(string oldValue, string newValue)
{
Nodo *aux;
aux=first;
bool found=false;
if(first != NULL){
while(aux != NULL && found != true){cout<<aux->element<<endl;
cout<<oldValue<<endl;
if( aux->element.compare(oldValue)== 0){
aux->element= newValue;
found = true;
}
aux= aux->next;
cout<<aux->next;
}
if(!found){
cout<<"\t There is not an element with that name."<<endl;
}
}else{
cout<<"\t The list is empty."<<endl;
}
}
#include <iostream>
using namespace std;
class Nodo{
public:
string element;
Nodo *next;
Nodo(string element){this->element=element;next=NULL;}
string getElement(){return element;}
};
class List{
private:
Nodo *first= NULL;
Nodo *last = NULL;
public:
void insertFirst(string element);
void insertLast(string element);
void insertByPos(string element, int pos);
void deleteFirst();
void deleteLast();
void deleteByPos(int pos);
void destroy();
void show();
void editByPos(string value,int pos);
void editByValue(string value, string newValue);
bool isEmpty(Nodo *first);
int listSize();
};
void List::insertFirst(string element)
{
Nodo *temp = new Nodo(element);
temp->next=NULL;
if(isEmpty(first))
{
first=temp;
}
else{
temp->next=first;
first=temp;
}
}
void List::insertLast(string element)
{
Nodo *temp = new Nodo(element);
temp->next=NULL;
if(isEmpty(first)){
first=last=temp;
}
else{
Nodo *aux;
aux = first;
while (aux->next != NULL)
{
aux = aux->next;
}
aux->next=temp;
cout<<"\tElement "<<element<<" was inserted at the end successfully."<<endl;}
}
int List::listSize()
{
int c= 0;
Nodo *aux;
aux=first;
while(aux != NULL){
aux = aux->next;
c++;
}
return c;
}
void List::insertByPos(string element, int pos)
{
Nodo *temp = new Nodo(element);
temp->next=NULL;
if(isEmpty(first))
{
first=temp;
cout<<"\tYou added the first element to the list successfully."<<endl;
}
else if (pos==1){
insertFirst(element);
}
else if (pos == (listSize()+1)){
insertLast(element);
}
else if(pos>1 && pos< (listSize() + 1))
{
Nodo *aux, *aux2;
aux=first;
for (int i=1;i<pos;i++)
{
aux2 = aux;
aux = aux->next;
}
aux2->next=temp;
temp->next = aux->next;
delete(aux);
cout<<"\tThe element: "<<element<<" was inserted successfully on the position: "<<pos<<"."<<endl;
}
else if(pos > (listSize()+1) or pos<0){
cout<<"\tError: The position that you are trying to use is invalid."<<endl;
cout<<"\tThe list only have '"<<listSize()<<"' elements. Try with one that is on range."<<endl;
}
}
void List::deleteFirst()
{
if(isEmpty(first)){
cout<<"\tThe list is empty, you can not delete elements."<<endl;
}
else{
if(first==last)
{
first=last=NULL;
cout<<"\tThe first element was removed successfully."<<endl;
}
else
{
Nodo *aux=first;
first=first->next;
delete aux;
cout<<"\tThe first element was removed successfully."<<endl;
}
}
}
void List::deleteLast()
{
if(isEmpty(first)){
cout<<"\tThe list is empty, you can not delete elements."<<endl;}
else{
Nodo *aux=first;
while(aux!=NULL)
{
if(first==last)
{
first=last=NULL;
cout<<"\tThe last element was removed successfully."<<endl;
}
else if(aux->next==last)
{
Nodo *temp = last;
last=aux;
last->next=NULL;
delete temp;
cout<<"\tThe last element was removed successfully."<<endl;
}
aux=aux->next;
}
}
}
void List::deleteByPos(int pos)
{
if(isEmpty(first)){
cout<<"\tYou can not delete anything.The list is empty."<<endl;
}
else if (pos==1){
deleteFirst();
}
else if(pos==listSize()){
deleteLast();
}
else if(pos>1 && pos< (listSize() + 1)){
Nodo *aux, *aux2;
aux=first;
for (int i=1;i<pos;i++)
{
aux2 = aux;
aux = aux->next;
}
aux2->next=aux->next;
delete(aux);
cout<<"\tThe element was removed successfully of the list."<<endl;
}
else if(pos > (listSize()+1) or pos<0){
cout<<"\tError: The position that you are trying to use is invalid."<<endl;
cout<<"\tThe list only have '"<<listSize()<<"' elements. Try with one that is on range."<<endl;
}
}
void List::destroy()
{
Nodo *aux;
while(first != NULL)
{
aux=first;
first = first->next;
delete(aux);
}
cout<<"\tThe list was destroyed..."<<endl;
}
void List::show()
{
if(isEmpty(first)){
cout<<"The list is Empty."<<endl;}
else{
Nodo *aux;
aux = first;
while(aux != NULL)
{
cout<<aux->element<<endl;
aux= aux->next;
}
}
}
void List::editByPos(string value,int pos)
{
if(isEmpty(first)){
cout<<"\tYou can not modify anything.The list is empty."<<endl;
}
else if (pos==1){
Nodo *temp = new Nodo(value);
temp->next=first->next;
first=temp;
cout<<"The first element is now called '"<<value<<"'."<<endl;
}
else if(pos>0 && pos <= (listSize())){
Nodo *temp= new Nodo(value);
temp->next=NULL;
Nodo *aux, *aux2;
aux=first;
for (int i=1;i<pos;i++)
{
aux2 = aux;
aux = aux->next;
}
aux2->next=temp;
temp->next=aux->next;
delete(aux);
cout<<"\tThe element was modified successfully."<<endl;
}
else if(pos > (listSize()) or pos<=0){
cout<<"\tError: The position that you are trying to use is invalid."<<endl;
cout<<"\tThe list only have '"<<listSize()<<"' elements. Try with one that is on range."<<endl;
}
}
void List::editByValue(string value, string newValue)
{
//Primer intento
Nodo *aux = new Nodo(value);
aux=first;
bool found=false;
if(first != NULL){
while(aux != NULL && found != true){string temp= aux->element;
if( temp.compare(value)== 0){
aux->element= newValue;
found = true;
}
aux=aux->next;
}
if(!found){
cout<<"\t There is not an element with that name."<<endl;
}
}else{
cout<<"\t The list is empty."<<endl;
}
}
/* Segundo intento con una logica similar pero diferente codigo
*Nodo *temp = new Nodo(newValue);
if(isEmpty(first)){
cout<<"\tYou can not modify anything.The list is empty."<<endl;
}
Nodo *aux,*aux2;
Nodo *var = first;
do{
if(var->element.compare(value) != 0)
{
var=var->next;
}}while(var->next != NULL);
var->element = newValue; */
/* Tercer intento, tampoco funciona
Nodo *temp = new Nodo(newValue);
if(isEmpty(first)){
cout<<"\tYou can not modify anything.The list is empty."<<endl;
}
Nodo *aux,*aux2;
Nodo *var = first;
do{
if(var-> element == value)
{
aux2=aux;
aux=aux->next;
temp->next=aux->next;
aux2->next=temp;
delete(aux);}
} */
bool List::isEmpty(Nodo *first)
{
return(first == NULL)? true : false;
}
int main ()
{
List a;
int option;
int x=0;
string name;
int op;
int opp;
while(x==0){
cout<<"What do you wanna do? 1) Add 2) Delete 3) Show 4)Edit 5)Exit"<<endl;
cin>>option;
switch(option)
{
case 1:{
cout<<"How do you want to add? \n 1)At the beginning \n 2)At the end \n 3)In 'n' position "<<endl;
cin>>op;
cout<<"Give me a name to add to the queue:"<<endl;
cout<<" "; cin>>name;
cout<<endl;
if(op==1){
a.insertFirst(name);}
else if (op==2){
a.insertLast(name);}
else if(op==3){
int num;
cout<<"In which position do you want to insert the node?"<<endl;
cin>>num;
a.insertByPos(name,num);}
}break;
case 2:{
int op,pos;
cout<<"How do you want to remove?: \n 1)At the beginning \n 2)At the end \n 3)In 'n' position \n 4)Delete all the list"<<endl;
cin>>op;
if(op==1){
a.deleteFirst();}
else if (op==2){
a.deleteLast();}
else if(op==3){
int pos=0;
cout<<"In which position do you want to remove the element?"<<endl;
cin>>pos;
a.deleteByPos(pos);}
else if(op==4){
a.destroy();}
}break;
case 3:{
a.show();
}break;
case 4:{
int opt;
string value,newName;
cout<<"Do you want to: \n 1)Edit by position \n 2)Edit by value \n "<<endl;
cin>>opt;
cout<<"What's the new value?"<<endl;
cin>>value;
if(opt==1){
int pos;
cout<<"In which position do you want to edit?"<<endl;
cin>>pos;
a.editByPos(value,pos);}
else if (opt==2){
cout<<"For which value do you want to change?"<<endl;
cin>>newName;
a.editByValue(value, newName);}
}break;
case 5:
x=1;
}
}
return 0;
}
#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);
};
#endif // JUGADORFACIL_H
#include "JugadorFacil.h"
JugadorFacil::JugadorFacil()
{
//ctor
}
int JugadorFacil::ejecutar()
{
//reseteo
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;//guarda la variable del jugador
int ficha2 = 0;//para las funciones
int fichasPuestas = 0;
bool juegoTerminado = false;//se cambia a true cuando todas las fichas del tablero se ponen o gana un jugador
char jugador = 15;//empezara con el jugador 1 por eso es igual a 15 y luego se ira al 2
while(!juegoTerminado){//el ciclo no se termina hasta que el juego se temrina
if(ficha2 != -1){//revisa si hay error cuando se pone la ficha
if(jugador == 15){//cuando el jugador uno pone se cambia automaticamente al 2 al declarar jugador = 254
cout<<"Jugador 1: Donde quieres poner tu ficha?";
jugador = 254;//cambio de ficha del jugador
}
else{
jugador = 15;//cambio de ficha del jugador
}
}
while(true){
if(jugador == 15){//el turno de la maquina
ficha = rand()%7+1; //pondra valores aleatorios del 1-7
break;//se rompe el ciclo entonces puede continuar a la evaluacion
}
if(fichasPuestas == 42) break;
cin>>ficha;//esta variable guarda lo que el usuario dice
ficha--;//elimino uno porque el usuario elige entre 1 y 7 y los arreglos empiezan en 0
if(ficha <=6 && ficha>= 0) break;//aqui comprueba si el valor es valido si no lo es le dira que ponga un valor valido
else cout<< "\nPor favor, ingresa un valor entre 1 y 7 :";
if (cin.fail()) //esto es si ingresa un valor que no es, lo guarda en otra variable para evitar el error y preguntar por un numero valido.
{ //
cin.clear(); //
char c; //
cin>>c; //en esta variable guarda el caracter no valido
}
}
if(fichasPuestas == 42) break;//comprueba si esta lleno
ficha2 = fichas(ficha,jugador);//evalua
if(ficha2 == -1) cout<<"La columna esta llena...\nPorfavor ingresa otro numero entre 1 y 7:";//si la columna esta llena te manda el mensaje
else{
juegoTerminado = revisar(ficha2,ficha);//si el lugar esta vacio entonces coloca la ficha donde va
fichasPuestas ++;
system("cls");//esto solo limpia la pantalla
mostrar();//actualiza el tablero
}
}
system("cls");
if(fichasPuestas == 42){//si todo el tablero esta lleno y no hubo un ganador declara empate
cout<<"No hay ganador!\n";
system("pause");
return 0;
}
if(jugador == 15)//si la variable es igual a 15(codigo ascii) gana el jugador 2
cout<<"El jugador 2 gano!!!\n";
else cout<<"El jugador 1 gano!!!\n";//de otra manera gana el 1
system("pause");//esto pausa para que se pueda ver quien gano
return 0;
}
void JugadorFacil::mostrar()
{
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++);//abajo
for(i = a -1;tablero[i][b] == jugador && i >= 0;i--,vertical++);//arrina
if(vertical >= 4)return true;
//revisando horizontales
for(ii = b -1;tablero[a][ii] == jugador && ii >= 0;ii--,horizontal++);//izquierda
for(ii = b +1;tablero[a][ii] == jugador && ii <= 6;ii++,horizontal++);//derecha
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 --);//arriba e izquierda
for(i = a +1, ii = b+1;tablero[i][ii] == jugador && i<=5 && ii <=6;diagonal1 ++, i ++, ii ++);//abajo y derecha
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 ++);//arriba y derecha
for(i = a +1, ii= b -1;tablero[i][ii] == jugador && i<=5 && ii >=0; diagonal2 ++, i ++, ii --);//arriba e izquierda
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;
}
}
#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;
}
}