1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
bool game(int iRows, int iColumns, char cBoard[8][7], int iPick1, int iPick2, char cP1, char cP2)
{
for (int iCount=0; iCount<iRows; iCount++)
{
for (int iCountB=0; iCountB<iColumns; iCountB++)
{
if (cBoard[iCount][iCountB]==cP1 && cBoard[iCount][iCountB+1]==cP1 && cBoard[iCount][iCountB+2]==cP1 && cBoard[iCount][iCountB+3]==cP1 && iCountB<5)
{
cout << "Player 1 Wins! '-'"; //jugador 1 gana por linea horizontal
return true;
}
if (cBoard[iCount][iCountB]==cP2 && cBoard[iCount][iCountB+1]==cP2 && cBoard[iCount][iCountB+2]==cP2 && cBoard[iCount][iCountB+3]==cP2 && iCountB<5)
{
cout << "Player 2 Wins! '-'"; //jugador 2 gana por linea horizontal
return true;
}
if (cBoard[iCount][iCountB]==cP1 && cBoard[iCount+1][iCountB]==cP1 && cBoard[iCount+2][iCountB]==cP1 && cBoard[iCount+3][iCountB]==cP1)
{
cout << "Player 1 Wins! '|'"; //jugador 1 gana por linea vertical
return true;
}
if (cBoard[iCount][iCountB]==cP2 && cBoard[iCount+1][iCountB]==cP2 && cBoard[iCount+2][iCountB]==cP2 && cBoard[iCount+3][iCountB]==cP2)
{
cout << "Player 2 Wins! '|'"; //jugador 2 gana por linea vertical
return true;
}
if (cBoard[iCount][iCountB]==cP1 && cBoard[iCount+1][iCountB+1]==cP1 && cBoard[iCount+2][iCountB+2]==cP1 && cBoard[iCount+3][iCountB+3]==cP1 && iCount>3)
{
cout << "Player 1 Wins! '/alreves'"; //jugador 1 gana por linea diagonal "\"
return true;
}
if (cBoard[iCount][iCountB]==cP2 && cBoard[iCount+1][iCountB+1]==cP2 && cBoard[iCount+2][iCountB+2]==cP2 && cBoard[iCount+3][iCountB+3]==cP2 && iCount>3)
{
cout << "Player 2 Wins! '/alreves'"; //jugador 2 gana por linea diagonal "\"
return true;
}
if (cBoard[iCount][iCountB]==cP1 && cBoard[iCount-1][iCountB+1]==cP1 && cBoard[iCount-2][iCountB+2]==cP1 && cBoard[iCount-3][iCountB+3]==cP1 && iCountB<5 && iCount<5)
{
cout << "Player 1 Wins! '/'"; //jugador 1 gana por linea diagonal "/"
return true;
}
if (cBoard[iCount][iCountB]==cP2 && cBoard[iCount-1][iCountB+1]==cP2 && cBoard[iCount-2][iCountB+2]==cP2 && cBoard[iCount-3][iCountB+3]==cP2 && iCountB<5 && iCount<5)
{
cout << "Player 2 Wins! '/'"; //jugador 2 gana por linea diagonal "/"
return true;
}
}
}
return false;
}
void imprimirtabla(int iRows, int iColumns, char cBoard[8][7]) //imprime la tabla
{
for (int iCount=0; iCount<iRows; iCount++)
{
for (int iCountB=0; iCountB<iColumns; iCountB++)
{
cout << cBoard[iCount][iCountB] << "\t";
}
cout << endl;
}
}
void turnojugador1(int iRows, int iColumns, char cBoard[8][7], int iPick1, int iPick2, char cP1, char cP2)
{
iRows=8;
cout << "Player 1's turn" << endl;
cin >> iPick1;
while (iPick1<1 || iPick1>7)
{
cout << "No es valido el numero, porfavor escoge otro" << endl; //turno de jugador 1
cin >>iPick1;
}
iPick1-=1;
if(cBoard[iRows][iPick1]!='-')
{
while (cBoard[iRows][iPick1]!='-')
{
iRows--;
}
cBoard[iRows][iPick1]=cP1;
}
}
void turnojugador2(int iRows, int iColumns, char cBoard[8][7], int iPick1, int iPick2, char cP1, char cP2)
{
iRows=8;
cout << "Player 2's turn" << endl;
cin >> iPick2;
while (iPick2<1 || iPick2>7)
{
cout << "No es valido el numero, porfavor escoge otro" << endl; //turno de jugador 2
cin >>iPick2;
}
iPick2-=1;
if(cBoard[iRows][iPick2]!='-')
{
while (cBoard[iRows][iPick2]!='-')
{
iRows--;
}
cBoard[iRows][iPick2]=cP2;
}
}
int main()
{
char cBoard[8][7], cP1='X', cP2='O', cChoice;
bool bGame;
int iRows=8, iColumns=7, iPick1, iPick2;
for (int iCount=0; iCount<iRows; iCount++) // declarar los numeros y las lineas de la mesa
{
for (int iCountB=0; iCountB<iColumns; iCountB++)
{
if (cBoard[iCount][iCountB]==cBoard[0][0])
{
cBoard[iCount][iCountB]='1';
}
else if (cBoard[iCount][iCountB]==cBoard[0][1])
{
cBoard[iCount][iCountB]='2';
}
else if (cBoard[iCount][iCountB]==cBoard[0][2])
{
cBoard[iCount][iCountB]='3';
}
else if (cBoard[iCount][iCountB]==cBoard[0][3])
{
cBoard[iCount][iCountB]='4';
}
else if (cBoard[iCount][iCountB]==cBoard[0][4])
{
cBoard[iCount][iCountB]='5';
}
else if (cBoard[iCount][iCountB]==cBoard[0][5])
{
cBoard[iCount][iCountB]='6';
}
else if (cBoard[iCount][iCountB]==cBoard[0][6])
{
cBoard[iCount][iCountB]='7';
}
else
{
cBoard[iCount][iCountB]='-';
}
}
}
imprimirtabla(iRows, iColumns, cBoard);
while (game(iRows, iColumns, cBoard, iPick1, iPick2, cP1, cP2) == false)
{
turnojugador1(iRows, iColumns, cBoard, iPick1, iPick2, cP1, cP2); //turno del jugador 1
imprimirtabla(iRows, iColumns, cBoard); //imprime la tabla
bGame=game(iRows, iColumns, cBoard, iPick1, iPick2, cP1, cP2); //checa si ya se acabo el juego
if(bGame==true)
break;
turnojugador2(iRows, iColumns, cBoard, iPick1, iPick2, cP1, cP2); //turno del jugador 2
imprimirtabla(iRows, iColumns, cBoard); //imprime la tabla
bGame=game(iRows, iColumns, cBoard, iPick1, iPick2, cP1, cP2); //checa si ya se acabo el juego
if(bGame==true)
break;
}
}
|