• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
mayoscuro

tic tac toe c++

8 posts in this topic

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:

[code]

// 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);
}
[/code]

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

Share this post


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

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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:

[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;
}
[/CODE]

Could probably be reduced to
[CODE]
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;
}
[/CODE]

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.
0

Share this post


Link to post
Share on other sites
[quote name='mayoscuro' timestamp='1334783266' post='4932585']
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).
[/quote]

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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Thank you everyone, at the end I solve one part of my problem with thant functions:

[CODE]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[/CODE]

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0