Jump to content

  • Log In with Google      Sign In   
  • Create Account


tic tac toe c++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 mayoscuro   Members   -  Reputation: 105

Like
0Likes
Like

Posted 18 April 2012 - 02:30 PM

Hello,
I'm a beginner, and I want to develop a tic tac toe with variable proportions.
the problem is i dont know how to check correctly who wins (X or O)
If anyone knows the solution... Can you explain me how solve this problem?
Thank you ^^
The complete code is this:


// Tres en raya.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <vector>
#include <conio.h>
typedef std::vector< std::vector< int > > tablero;
//-----------------Funciones----------------------
void jugadores(tablero &tablero);
//char comprobar_ganador(tablero &tablero);
void RecorrerVector(tablero &tablero);
int comprobar(tablero &tablero, int i,int j);
void mostrar_tablero(tablero &tablero);
tablero dimensionar_vector(tablero &tablero);
//int comprobar_ganador_pasodos(tablero &tablero, int &num_x, int &num_o,int &i,int &j);
//int comprobar_ganador_pasotres(int &num_x, int &num_o);
void FinPartida();

void ganaro(int num_o);
void ganarx(int num_x);
int incrementarxo(tablero &tablero,int &x,int &i, int y,int &num_x, int &num_o);
void linea(tablero &tablero,int y);
void mostrar_tablero(tablero &tablero)
{
int i;
int j;
for(i=0; i<tablero.size(); i++) {
   std::cout<<"  "<<"  " <<  static_cast < char >( tablero[ i ][ 0 ] );
   std::cout<<"   |"<<"  " <<  static_cast < char >( tablero[ i ][ 1 ] );
   std::cout<<"   |"<<"  "<<  static_cast < char >( tablero[ i ][ 2 ] );

   if (tablero.size() >= 4)std::cout<<"     |"<<"    "<<  static_cast < char >( tablero[ i ][ 3 ] );
   if (tablero.size() >= 5)std::cout<<"     |"<<"    "<<  static_cast < char >( tablero[ i ][ 4 ] );
   if (tablero.size() >= 6)std::cout<<"     |"<<"    "<<  static_cast < char >( tablero[ i ][ 5 ] );
   if (tablero.size() >= 7)std::cout<<"     |"<<"    "<<  static_cast < char >( tablero[ i ][ 6 ] );
   if (tablero.size() >= 8)std::cout<<"     |"<<"    "<<  static_cast < char >( tablero[ i ][ 7 ] );
   if(i==i) std::cout<<"\n-------------------------------------------------------------------------------"<<std::endl;


}
std::cout<<"\n"<<std::endl;
}
void jugadores(tablero &tablero) {
int x;
int y;
int z=1;

do{
  std::cout<<"es el turno del jugador"<<z<<":"<<std::endl;
  std::cout<<"Introduce las cordenadas y, x del lugar en el que quieras colocar la ficha"<<std::endl;
  std::cin>> x >> y;

  x--;
  y--;

  if( x < 0 || x >= tablero.size() || y < 0 || y >=  tablero.size() ){
   std::cout<<"Las coordenadas que has indicado son erroneas.\n\nvuelbe a intentarlo:"<<std::endl;
   if (z==2){
    z=z-1;
   }
   jugadores(tablero);
  }

  if(tablero[x] [y] == 'X' || tablero [x] [y] == 'O') {
   std::cout<<"Tienes que escribir las cordenadas y, x  del lugar en el que quieras colocar la ficha.\n Vuelbe a intentarlo:"<<std::endl;
   if (z==2){
    z=z-1;
   }
   jugadores(tablero);
  }
  else if(z==1) tablero[x][y] = 'X';
  else if (z==2) tablero[x][y] = 'O';
  //z=z+1;
  mostrar_tablero(tablero);
  linea(tablero,y);
}while(z==1||z==2);
}
//COMPROBACIONES:
void linea(tablero &tablero,int y){
int num_x=0;
int num_o=0;
int i=0;

  for(int x=0; x<tablero.size();x++){
   incrementarxo(tablero,x,y,i,num_x,num_o);
  }
}
int incrementarxo(tablero &tablero,int &x,int &i, int y,int &num_x, int &num_o){

if (tablero[y][x]=='X'){
  num_x++;
  return num_x;

}else if (tablero[y][x]=!'X' && num_x<3)
  num_x=0;
if (tablero[y][x]=='O'){
  num_o++;
  return num_o;

}else if (tablero[y][x]=!'O' && num_o<3)
  num_o=0;
ganarx(num_x);
ganaro(num_o);
}
void ganaro(int num_o){
if(num_o>=3){
  std::cout<<"Ha ganado el jugador 2"<<std::endl;
  FinPartida();
}
}
void ganarx(int num_x){
if (num_x>=3){
  std::cout<<"Ha ganado el jugador 1"<<std::endl;
  FinPartida();
}
}
//FIN COMPROBACIONES
tablero dimensionar_vector(tablero &tablero) {
int tamañovector=0;

tablero;
std::cout<<"Elige las dimensiones del tablero(Maximo 8) Ejemplo: 5 = 5x5"<<std::endl;
std::cin>>tamañovector;
if (tamañovector>=9){
  std::cout<<"Las proporciones dadas son demasiado grandes, el maximo es de 8x8 (8)\nVuelbe a intentarlo:"<<std::endl;
  system("pause");
  dimensionar_vector(tablero);
}
tablero.resize( tamañovector );

for(int i=0; i < tamañovector; ++i) {
  tablero[i].resize(tamañovector);

}

return tablero;
} 
int main()
{
tablero tablero;
char v=' ';
std::cout<<"Tres en raya:\n";
std::cout<<"El jugador1 jugara con X y el jugador2 con O\n\n";

dimensionar_vector(tablero);
std::cout<<"este es el tablero de juego:"<<std::endl;
mostrar_tablero (tablero);
do {

  jugadores(tablero);

} while(v==' ');
std::cin.get();
return 0;
}
void FinPartida(){
int jugarsalir;
std::cout<<"La partida ha finalizado\n\n Pulsa 1 para jugar otra partida, de lo contrario pulsa cualquier otra tecla para salir del programa"<<std::endl;
std::cin>>jugarsalir;
if (jugarsalir == 1){
  system("cls");
  main();
}else
  exit(1);
}

[mod edit by rip-off -- please use code tags when pasting code]

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 10803

Like
0Likes
Like

Posted 18 April 2012 - 02:46 PM

I think you mean "variable size", not "variable proportions". What are the rules of tic-tac-toe on a board other than 3x3?

#3 mayoscuro   Members   -  Reputation: 105

Like
0Likes
Like

Posted 18 April 2012 - 03:07 PM

Yes,your right is variable size.
Sorry I will try to explain this to you , the rules are the same as in a original tic tac toe (3x3)
with the only exeption that you can chose the size(3x3,4x4...8x8).

#4 endless11111   Members   -  Reputation: 135

Like
0Likes
Like

Posted 18 April 2012 - 03:12 PM

is it just me or does the code cut off after the function declarations?

#5 endless11111   Members   -  Reputation: 135

Like
0Likes
Like

Posted 18 April 2012 - 03:30 PM

also i am really sorry i don't speak spanish, but when i made a tictactoe game, to see who won i made a check win function that takes in the tictactoe board as an argument. it checked if each row or column or diagonal row were equal to x or 0, if it was it would return true. this function would check if any of the players, either x or o won, and return true if any rows lined up, and false if none lined up. at the end of each player's turn, after they input a coordinant and it draws the x or o onto the screen, i use the function to see if any of the rows line up, if they do it will return true and i would print out they won to the screen then break out of the main loop.

also i used an multidimensional array for the board, it was a character array so the checkwin function would take in the array and check if a row lined up as either x,o, or none.

#6 Dunge   Members   -  Reputation: 405

Like
0Likes
Like

Posted 18 April 2012 - 03:39 PM

Code look complete here, but you should post it between CODE tags to make it easier to read and keep tabulations. I also don't speech Spanish/Italian/whatever so I just try to deduce what functions name means.

One thing I realized is that you should learn to use loops more. This whole code:

for(i=0; i<tablero.size(); i++) {
std::cout<<" "<<" " << static_cast < char >( tablero[ i ][ 0 ] );
std::cout<<" |"<<" " << static_cast < char >( tablero[ i ][ 1 ] );
std::cout<<" |"<<" "<< static_cast < char >( tablero[ i ][ 2 ] );
if (tablero.size() >= 4)std::cout<<" |"<<" "<< static_cast < char >( tablero[ i ][ 3 ] );
if (tablero.size() >= 5)std::cout<<" |"<<" "<< static_cast < char >( tablero[ i ][ 4 ] );
if (tablero.size() >= 6)std::cout<<" |"<<" "<< static_cast < char >( tablero[ i ][ 5 ] );
if (tablero.size() >= 7)std::cout<<" |"<<" "<< static_cast < char >( tablero[ i ][ 6 ] );
if (tablero.size() >= 8)std::cout<<" |"<<" "<< static_cast < char >( tablero[ i ][ 7 ] );
if(i==i) std::cout<<"\n-------------------------------------------------------------------------------"<<std::endl;
}

Could probably be reduced to
for(i=0; i<tablero.size(); i++) {
	for(j=0; j<tablero.size(); j++) {
		if(j == 0)	  
		    std::cout<<"  " << static_cast < char >( tablero[ i ][ 0 ] );
	    else
		    std::cout<<" | "<< static_cast < char >( tablero[ i ][ j ] );
	}
	std::cout<<"\n-------------------------------------------------------------------------------"<<std::endl;
}

Looking at your actual problem, I think the functions determining if there's a the winner is are ganaro() and ganarx(). Looking at the code, you have a constant there "3". Just replace it by tablero.size();

edit: You also have a few 3 in linea(). Check that out.

#7 Álvaro   Crossbones+   -  Reputation: 10803

Like
0Likes
Like

Posted 18 April 2012 - 05:39 PM

Yes,your right is variable size.
Sorry I will try to explain this to you , the rules are the same as in a original tic tac toe (3x3)
with the only exeption that you can chose the size(3x3,4x4...8x8).


I'll switch to Spanish to see if I can get my answer. Esto que dices de "las reglas son las mismas que en el tres en raya original" no es muy preciso, porque hay varias generalizaiones posibles. Por ejemplo, podría ser que para ganar baste con alinear tres piezas, pero también podría ser que haga falta completar una fila, columna o diagonal. ¿Cuál de las dos es? ¿O quizás es otra generalización que no se me ha ocurrido?

Si quieres aprender a programar, empieza por aprender a expresarte con precisión extrema, porque al programar no te puedes dejar nada sin especificar.

#8 incertia   Crossbones+   -  Reputation: 774

Like
0Likes
Like

Posted 18 April 2012 - 09:08 PM

You will want to be able to use a loop to check to see if there is a winner. Loop through each row and see if the entire row is filled, loop through each column and see if the entire column is filled, and loop through going diagonally and backwards at the same time to check diagonals.
what

#9 mayoscuro   Members   -  Reputation: 105

Like
0Likes
Like

Posted 26 April 2012 - 08:26 AM

Thank you everyone, at the end I solve one part of my problem with thant functions:

void recorrer(juego &tablero,int y,int x,int anteriorx,int anteriory){

	   int num_x=0;
	   int num_o=0;
	   int copiay=y;
	   int copiax=x;


	   //FILAS:
	   for(y ;y<tablero.size();y--){
		   incrementarxo(tablero,x, y,num_x,num_o,anteriorx,anteriory);
	   }
	   num_x=0;
	   num_o=0;
	   y=copiay;
	   for(y ;y>=0 && y<tablero.size();++y){
		   incrementarxo(tablero,x, y,num_x,num_o,anteriorx,anteriory);
	   }
	   //COLUMNAS:
	   num_x=0;
	   num_o=0;
	   x=copiax;
	   y=copiay;
	   for(x ;x<tablero.size() && x>=0;x--){
		   incrementarxo(tablero,x, y,num_x,num_o,anteriorx,anteriory);
	   }
	   num_x=0;
	   num_o=0;
	   x=copiax;
	   y=copiay;
	   for(x ;x>=0 && x<tablero.size();++x){
		   incrementarxo(tablero,x, y,num_x,num_o,anteriorx,anteriory);
	   }
	   //DIAGONALES(Izquierda a derecha):
	   num_x=0;
	   num_o=0;
	   x=copiax;
	   y=copiay;
	   for (x,y; x>=0 && y>=0 || x<tablero.size() && y<tablero.size();x--, y--){
		   incrementarxo(tablero,x, y,num_x,num_o,anteriorx,anteriory);
	   }
	   num_x=0;
	   num_o=0;
	   x=copiax;
	   y=copiay;
		for (x,y; x<tablero.size() && y<tablero.size();x++, y++){
		   incrementarxo(tablero,x, y,num_x,num_o,anteriorx,anteriory);
	   }
	   //DIAGONALES(Derecha a izquierda):
		num_x=0;
	   num_o=0;
	   x=copiax;
	   y=copiay;
	   for (x,y; x<tablero.size() && y>=0;x--, y++){
		   incrementarxo(tablero,x, y,num_x,num_o,anteriorx,anteriory);
	   }
	   num_x=0;
	   num_o=0;
	   x=copiax;
	   y=copiay;
	   for (x,y; x<tablero.size() && y>=0;x++, y--){
		   incrementarxo(tablero,x, y,num_x,num_o,anteriorx,anteriory);
	   }
}

int incrementarxo(juego &tablero,int &x, int &y,int &num_x, int &num_o,int anteriorx, int anteriory){
	   if (tablero[x][y]=='X'){
			 num_x++;
			 ganarx(num_x);
			 return num_x;		  

	   }else if (num_x < 3 && tablero[x][y] != 'X' && x != anteriorx || num_x < 3 && tablero[x][y] != 'X' && y != anteriory)   num_x=0;
	  
	   if (tablero[x][y]=='O'){
			 num_o++;
			 ganaro(num_o);
			 return num_o;			

	   }else if (tablero[x][y]=!'O' && num_o<3)   num_o=0;

	   ganarx(num_x);
	   ganaro(num_o);
}

void ganaro(int num_o){
	   if(num_o>=3){
			 std::cout<<"Ha ganado el jugador 2"<<std::endl;
			 FinPartida();
	   }
}

void ganarx(int num_x){
	   if (num_x>=3){
			 std::cout<<"Ha ganado el jugador 1"<<std::endl;
			 FinPartida();
	   }
}
//FIN COMPROBACIONES

But now I have other problem, I need who one variable save the value of the last move,
I need that because in my code if you have | x | Nothing | x | and you put one X in the midle yo don't
win. That is the only problem.
So I need that variable to make my program who don't delete the X (or O) if x=oldx
The real problem is that variables must be initialized and can not find how to save the previous move.
Have I explained well?
Anyone can help my with that problem?

Thank you, one more time.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS