-
[C++] Tic Tac Toe
Alright, I need help in fixing this code for a Tic Tac Toe Game.
The problem is, the computer doesn't actually responds well and whenever someone wins, the program exit's itself.
If someone can take a look at it, and try it. I need help finding why is doing that. Ugh, please help.
Code:
# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;
char TABLERO [3][3];
char SIMBOLO;
char COMPUTADORA;
int Movida;
int ContadorTur = 1;
int Ganador;
void setMovida(int Movida, char TIPO);
void inicializarTablero();
void seTranco();
int quienGano();
void jugadaDelaComputadora();
int cojeRANDOMS();
void jugadaDelUsuario();
void printTablero();
bool ESPACIO = true;
bool ESPACIOLIBRE = true;
bool espacioDeVerdadLibre(int EspacioNum);
int main(){
cout << " --------------------------------------" << endl;
cout << " *---Bienvenido al Mega Tic Tac Toe---*" << endl;
cout << " --------------------------------------" << endl;
int ORDEN;
inicializarTablero();
cout << "Favor de entrar el simbolo que desea ser, X o O? "<<endl;
cin >> SIMBOLO;
if (SIMBOLO == 'x' || SIMBOLO == 'X')
{
SIMBOLO = 'X';
COMPUTADORA ='O';
}
else if (SIMBOLO == 'o' || SIMBOLO == 'O')
{
SIMBOLO = 'O';
COMPUTADORA = 'X';
}
else
{
cout << "Bien" << endl;
}
cout << "\nPresiona 1 para ser numero 1 sino presiona 2: ";
cin >> ORDEN;
while (ORDEN !=1 && ORDEN !=2)
{
cout << "\n\nEso no es na, trata otra vez";
cout << "\nPresiona 1 para ser numero 1 sino presiona 2: ";
cin >> ORDEN;
}
cout << "Usuario: " << SIMBOLO << "\tTurno: " << ORDEN << "\nCompu: " << COMPUTADORA << "\tTurno: " << (3 - ORDEN);
printTablero();
if (ORDEN == 1)
{
while (ContadorTur < 10)
{
if ((ContadorTur % 2) == 0)
{
jugadaDelaComputadora();
quienGano();
}
else
{
jugadaDelUsuario();
quienGano();
}
}
}
else if(ORDEN == 2)
{
while (ContadorTur < 10)
{
if ((ContadorTur % 2) == 0)
{
jugadaDelUsuario();
quienGano();
}
else
{
jugadaDelaComputadora();
quienGano();
}
}
}
if (Ganador == 1)
{
cout << "Usuario Gano el juego, WEPA!" << endl;
}
else if (Ganador == 2)
{
cout << "La computadora gano, perdededor!!" << endl;
}
else if (Ganador == 0)
{
cout << "Empate!!" << endl;
}
else
{
cout << " Error" << endl;
}
return 0;
}
void inicializarTablero()
{
TABLERO[0][0] = '1';
TABLERO[0][1] = '2';
TABLERO[0][2] = '3';
TABLERO[1][0] = '4';
TABLERO[1][1] = '5';
TABLERO[1][2] = '6';
TABLERO[2][0] = '7';
TABLERO[2][1] = '8';
TABLERO[2][2] = '9';
}
void printTablero(){
cout << "\n\n\t" << TABLERO[0][0] << "|" << TABLERO[0][1] << "|" << TABLERO[0][2] << endl;
cout << "\t-+-+-" << endl;
cout << "\t" << TABLERO[1][0] << "|" << TABLERO[1][1] << "|" << TABLERO[1][2] << endl;
cout << "\t-+-+-" << endl;
cout << "\t" << TABLERO[2][0] << "|" << TABLERO[2][1] << "|" << TABLERO[2][2] << endl << "\n";
}
void jugadaDelUsuario()
{
ContadorTur++;
cout << "Sigue la leyenda, y entra a donde te quieres mover: ";
cin >> Movida;
ESPACIO = espacioDeVerdadLibre(Movida);
while (Movida != 1 && Movida != 2 && Movida != 3 && Movida != 4 && Movida != 5 && Movida != 6 && Movida != 7 && Movida != 8 && Movida != 9 && ESPACIOLIBRE == false)
{
cout << "\nNo se puede, intenta otra ves: ";
cin >> Movida;
}
setMovida (Movida, SIMBOLO);
Ganador = quienGano();
}
void setMovida (int Movida, char TIPO)
{
switch (Movida)
{
case 1:
TABLERO[0][0] = TIPO;
break;
case 2:
TABLERO[0][1] = TIPO;
break;
case 3:
TABLERO[0][2] = TIPO;
break;
case 4:
TABLERO[1][0] = TIPO;
break;
case 5:
TABLERO[1][1] = TIPO;
break;
case 6:
TABLERO[1][2] = TIPO;
break;
case 7:
TABLERO[2][0] = TIPO;
break;
case 8:
TABLERO[2][1] = TIPO;
break;
case 9:
TABLERO[2][2] = TIPO;
break;
}
}
void jugadaDelaComputadora()
{
int RANDOMSmovida = 1;
ContadorTur++;
while (ESPACIOLIBRE == false)
{
RANDOMSmovida = cojeRANDOMS();
ESPACIOLIBRE = espacioDeVerdadLibre(RANDOMSmovida);
}
setMovida(RANDOMSmovida, COMPUTADORA);
cout << "Movio la Computadora: ";
printTablero();
}
int cojeRANDOMS()
{
srand((unsigned)time(0));
int RANDOMS;
RANDOMS = (rand()%9)+1;
while (1)
{
if (RANDOMS < 1)
{
RANDOMS = (rand()%9)+1;
}
else if (RANDOMS > 9)
{
RANDOMS = (rand()%9)+1;
}
else
{
break;
}
}
return RANDOMS;
}
int quienGano ()
{
if(TABLERO [0][0] == SIMBOLO && TABLERO[0][1] == SIMBOLO && TABLERO[0][2] == SIMBOLO)
{
ContadorTur = 10;
return 1;
}
else if (TABLERO [1][0] == SIMBOLO && TABLERO[1][1] == SIMBOLO && TABLERO[1][2] == SIMBOLO)
{
ContadorTur = 10;
return 1;
}
else if (TABLERO [2][0] == SIMBOLO && TABLERO[2][1] == SIMBOLO && TABLERO[2][2] == SIMBOLO)
{
ContadorTur = 10;
return 1;
}
else if (TABLERO [0][0] == SIMBOLO && TABLERO[1][0] == SIMBOLO && TABLERO[2][0] == SIMBOLO)
{
ContadorTur = 10;
return 1;
}
else if (TABLERO [0][1] == SIMBOLO && TABLERO[1][1] == SIMBOLO && TABLERO[2][1] == SIMBOLO)
{
ContadorTur = 10;
return 1;
}
else if (TABLERO [0][2] == SIMBOLO && TABLERO[1][2] == SIMBOLO && TABLERO[2][2] == SIMBOLO)
{
ContadorTur = 10;
return 1;
}
else if (TABLERO [0][0] == SIMBOLO && TABLERO[1][1] == SIMBOLO && TABLERO[2][2] == SIMBOLO)
{
ContadorTur = 10;
return 1;
}
else if (TABLERO [2][0] == SIMBOLO && TABLERO[1][1] == SIMBOLO && TABLERO[0][2] == SIMBOLO)
{
ContadorTur = 10;
return 1;
}
else if (TABLERO [0][0] == COMPUTADORA && TABLERO[0][1] == COMPUTADORA && TABLERO[0][2] == COMPUTADORA)
{
ContadorTur = 10;
return 2;
}
else if (TABLERO [1][0] == COMPUTADORA && TABLERO[1][1] == COMPUTADORA && TABLERO[1][2] == COMPUTADORA)
{
ContadorTur = 10;
return 2;
}
else if (TABLERO [2][0] == COMPUTADORA && TABLERO[2][1] == COMPUTADORA && TABLERO[2][2] == COMPUTADORA)
{
ContadorTur = 10;
return 2;
}
else if (TABLERO [0][0] == COMPUTADORA && TABLERO[1][0] == COMPUTADORA && TABLERO[2][0] == COMPUTADORA)
{
ContadorTur = 10;
return 2;
}
else if (TABLERO [0][1] == COMPUTADORA && TABLERO[1][1] == COMPUTADORA && TABLERO[2][1] == COMPUTADORA)
{
ContadorTur = 10;
return 2;
}
else if (TABLERO [0][2] == COMPUTADORA && TABLERO[1][2] == COMPUTADORA && TABLERO[2][2] == COMPUTADORA)
{
ContadorTur = 10;
return 2;
}
else if (TABLERO [0][0] == COMPUTADORA && TABLERO[1][1] == COMPUTADORA && TABLERO[2][2] == COMPUTADORA)
{
ContadorTur = 10;
return 2;
}
else if (TABLERO [2][0] == COMPUTADORA && TABLERO[1][1] == COMPUTADORA && TABLERO[0][2] == COMPUTADORA)
{
ContadorTur = 10;
return 2;
}
else
return 0;
}
bool espacioDeVerdadLibre (int EspacioNum)
{
switch(EspacioNum)
{
case 1:
if (TABLERO[0][0] == '1')
return true;
else
return false;
case 2:
if (TABLERO[0][1] == '2')
return true;
else
return false;
case 3:
if (TABLERO[0][2] == '3')
return true;
else
return false;
case 4:
if (TABLERO[1][0] == '4')
return true;
else
return false;
case 5:
if (TABLERO[1][1] == '5')
return true;
else
return false;
case 6:
if (TABLERO[1][2] == '6')
return true;
else
return false;
case 7:
if (TABLERO[2][0] == '7')
return true;
else
return false;
case 8:
if (TABLERO[2][1] == '8')
return true;
else
return false;
case 9:
if (TABLERO[2][2] == '9')
return true;
else
return false;
default:
return false;
}
}
-
Re: [C++] Tic Tac Toe
You need an endless while loop which will be exitted if the user doesn't want to play an Nth game.
For the incorrect responses ^^...its all in the code. I don't understand your code much, its in spanish/portugues. Try to see where it does wrong responses, go through the code and try to allocate the problem.
-
Re: [C++] Tic Tac Toe
I went to the code, and I saw this part.
Code:
void jugadaDelaComputadora() //ComputerPlay
{
int RANDOMSmovida = 1;// //I noticed, this only makes the computer move to the first square in the tic tac toe, but I need to make it random. What should I do?
ContadorTur++;
while (ESPACIOLIBRE == false)
{
RANDOMSmovida = cojeRANDOMS();
ESPACIOLIBRE = espacioDeVerdadLibre(RANDOMSmovida);
}
setMovida(RANDOMSmovida, COMPUTADORA);
cout << "Movio la Computadora: ";
printTablero();
-
Re: [C++] Tic Tac Toe
My suggestion for finding a random computer move:
Code:
vector<int> freeSpaces;
for (int i = 0; i < 9; i++)
{
if (table[i] == FREE)
{
freeSpaces.push_back(i);
}
}
// let's have -1 mean that game has ended
if (freeSpaces.empty()) return -1;
int computerMove = freeSpaces.at( rand() % freeSpaces.size() );
freeSpaces.clear();
-
Re: [C++] Tic Tac Toe
something i learn in class last year. >.< its a 2 player game.
Code:
# include <stdio.h>
char a,b,c,d,e,f,g,h,i;
void main(){
//Selection values.
a = '1';
b = '2';
c = '3';
d = '4';
e = '5';
f = '6';
g = '7';
h = '8';
i = '9';
int turn = 1; // 1 or 2 denote player 1 or player 2
bool gotWinner = false;
char playerMove = '?', player = ' ';
while(!gotWinner){
//char playerMove = '';
//Print grid
printf(" %c | %c | %c \n", a, b, c);
printf("---+---+---\n");
printf(" %c | %c | %c \n", d, e, f);
printf("---+---+---\n");
printf(" %c | %c | %c \n", g, h, i);
//Print player's turn and selection
if(turn == 1){
printf("P1 > Select your moves. Choose 1 to 9.\n");
printf("Your move: ");
scanf("%c", &playerMove);
fflush(stdin);
//Updates moves
if(playerMove == '1'){
a = 'O';
}else if(playerMove == '2'){
b = 'O';
}else if(playerMove == '3'){
c = 'O';
}else if(playerMove == '4'){
d = 'O';
}else if(playerMove == '5'){
e = 'O';
}else if(playerMove == '6'){
f = 'O';
}else if(playerMove == '7'){
g = 'O';
}else if(playerMove == '8'){
h = 'O';
}else if(playerMove == '9'){
i = 'O';
}
}else{ //Player's 2 turn
printf("P2 > Select your moves. Choose 1 to 9.\n");
printf("Your move: ");
scanf("%c", &playerMove);
fflush(stdin);
//Updates moves
if(playerMove == '1'){
a = 'X';
}else if(playerMove == '2'){
b = 'X';
}else if(playerMove == '3'){
c = 'X';
}else if(playerMove == '4'){
d = 'X';
}else if(playerMove == '5'){
e = 'X';
}else if(playerMove == '6'){
f = 'X';
}else if(playerMove == '7'){
g = 'X';
}else if(playerMove == '8'){
h = 'X';
}else if(playerMove == '9'){
i = 'X';
}
}
if(turn == 1){
player = 'O';
turn = 2;
}else{
player = 'X';
turn = 1;
}
//Check winner here!
if(a == player && b == player && c == player){
gotWinner = true;
}else if(d == player && e == player && f == player){
gotWinner = true;
}else if(g == player && h == player && i == player){
gotWinner = true;
}else if(a == player && d == player && g == player){
gotWinner = true;
}else if(b == player && e == player && h == player){
gotWinner = true;
}else if(c == player && f == player && i == player){
gotWinner = true;
}else if(a == player && e == player && i == player){
gotWinner = true;
}else if(c == player && e == player && g == player){
gotWinner = true;
}
if(gotWinner){
if(player == 'O'){
printf("Player 1 WINS!\n");
}else{
printf("Player 2 WINS!\n");
}
}
}
printf("END OF GAME!!! YAHOO!!! CAN BALIK RUMAH....\n");
}
-
Re: [C++] Tic Tac Toe