elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Rompecabezas de Bitcoin, Medio millón USD en premios


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Turnos en C++
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Turnos en C++  (Leído 4,505 veces)
BrendiisFox

Desconectado Desconectado

Mensajes: 13


Ver Perfil
Turnos en C++
« en: 27 Septiembre 2015, 04:00 am »

Buenas Noches,
Estoy realizando en xo o tic tac toe en c++. Mi programa funciona bien hasta ahora pero mi unico problema es al intercambiar los jugadores. Si alguien me puede señalar el error se los agradeceria mucho

les dejo el codigo:


Código
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <windows.h>
  7.  
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int auxiliar;
  13. bool turno ;
  14. class Nodo{
  15. public:
  16. char valor;
  17.    int Index;
  18.    Nodo *sigPtr;
  19.    Nodo *antrPtr;
  20.    Nodo *arrPtr;
  21.    Nodo *abaPtr;
  22.  
  23. Nodo(char val){
  24. valor=' ';
  25.        //indice a -1
  26.        Index = val;
  27.        //se le mandara al nodo el numero de indice
  28.        sigPtr = 0;
  29.        antrPtr = 0;
  30.        arrPtr = 0;
  31.        abaPtr = 0;
  32.   }
  33. };
  34.  
  35. class Lista{
  36. private:
  37.  
  38. public:
  39.    Nodo *PrimeroPtr;
  40.    Nodo *UltimoPtr;
  41.    int tamano;
  42.  
  43. Lista(){
  44. PrimeroPtr=UltimoPtr=0;
  45.        tamano=0;
  46.    }
  47.  
  48. bool IsEmpty(){
  49.        return PrimeroPtr==0;
  50.     }
  51.  
  52. void InsertAtEndDbl(int val){
  53. Nodo *nuevoPtr=new Nodo(val);
  54.  
  55. if (IsEmpty()){
  56. PrimeroPtr = UltimoPtr = nuevoPtr;
  57. }else{
  58. UltimoPtr->sigPtr = nuevoPtr;
  59.     nuevoPtr->antrPtr = UltimoPtr;
  60.       UltimoPtr = nuevoPtr;
  61.       tamano++;
  62. }
  63. }
  64.  
  65. void createListX0(){
  66. for(int i=1; i<=9; i++){
  67. InsertAtEndDbl(i);
  68. }
  69.  
  70. Nodo * anttemp = PrimeroPtr;
  71.    Nodo *actual = PrimeroPtr;
  72.  
  73. for(int j=1; j<=9; j++){
  74. actual = actual->sigPtr;
  75.  
  76. if(j>2&&j<9){
  77. anttemp->abaPtr = actual;
  78.            actual->arrPtr = anttemp;
  79.            anttemp = anttemp->sigPtr;        
  80.        }                
  81.    }  
  82. }
  83.  
  84. void Imprimir(){
  85. cout<<" "<<endl;
  86. if(IsEmpty()){
  87.        cout<<"Vacio"<<endl;
  88.    }else{
  89. Nodo *Actual=PrimeroPtr;
  90.        cout<<"["<<Actual->valor<<"] ";
  91.        while(Actual->sigPtr!=0){
  92.            Actual=Actual->sigPtr;
  93.            cout<<"["<<Actual->valor<<"] ";
  94.            if(Actual->Index==3 && Actual->Index<=9 || Actual->Index==6 && Actual->Index<=9){
  95.                cout<<endl;
  96.            }
  97.        }
  98.    }
  99. }
  100.  
  101. void LlenarJugador1(int posicion){
  102. char simbolo = 'X';
  103. Nodo *actual=PrimeroPtr;
  104. int auxiliar = 0;
  105.  
  106. if(posicion == 1){
  107. actual->valor = simbolo;
  108. auxiliar++;
  109. }
  110. else if(posicion == 2){
  111. actual->sigPtr->valor = simbolo;
  112. auxiliar++;
  113. }
  114. else if(posicion == 3){
  115. actual->sigPtr->sigPtr->valor = simbolo;
  116. auxiliar++;
  117. }
  118. else if(posicion == 4){
  119. actual->sigPtr->sigPtr->sigPtr->valor = simbolo;
  120. auxiliar++;
  121. }
  122. else if(posicion == 5){
  123. actual->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  124. auxiliar++;
  125. }
  126. else if(posicion == 6){
  127. actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  128. auxiliar++;
  129. }
  130. else if(posicion == 7){
  131. actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  132. auxiliar++;
  133. }
  134. else if(posicion == 8){
  135. actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  136. auxiliar++;
  137. }
  138. else{
  139. actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  140. auxiliar++;
  141. }
  142. turno == false;
  143. }  
  144.  
  145. void LlenarJugador2(int posicion){
  146. char simbolo = '0';
  147. Nodo *actual=PrimeroPtr;
  148. int auxiliar = 0;
  149.  
  150. if(posicion == 1){
  151. actual->valor = simbolo;
  152. auxiliar++;
  153. }
  154. else if(posicion == 2){
  155. actual->sigPtr->valor = simbolo;
  156. auxiliar++;
  157. }
  158. else if(posicion == 3){
  159. actual->sigPtr->sigPtr->valor = simbolo;
  160. auxiliar++;
  161. }
  162. else if(posicion == 4){
  163. actual->sigPtr->sigPtr->sigPtr->valor = simbolo;
  164. auxiliar++;
  165. }
  166. else if(posicion == 5){
  167. actual->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  168. auxiliar++;
  169. }
  170. else if(posicion == 6){
  171. actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  172. auxiliar++;
  173. }
  174. else if(posicion == 7){
  175. actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  176. auxiliar++;
  177. }
  178. else if(posicion == 8){
  179. actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  180. auxiliar++;
  181. }
  182. else{
  183. actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor = simbolo;
  184. auxiliar++;
  185. }
  186. turno == true;
  187. }  
  188.  
  189. void Gane(){
  190. Nodo *actual=PrimeroPtr;
  191.  
  192. //GANE HORIZONTAL
  193. if (actual->valor == 'X' && actual->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->valor == 'X') {
  194. cout<<"Gano de manera Horizontal"<<endl;
  195. auxiliar =9;
  196. system("PAUSE");
  197. }
  198. if(actual->valor=='0' && actual->sigPtr->valor == '0' && actual->sigPtr->sigPtr->valor=='0')
  199. {
  200. cout<<"Gano de manera Horizontal"<<endl;
  201. auxiliar =9;
  202. system("PAUSE");
  203. }
  204. if(actual->sigPtr->sigPtr->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor == 'X'){
  205. cout<<"Gano de manera Horizontal"<<endl;
  206. }
  207. if(actual->sigPtr->sigPtr->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor == '0'){
  208. cout<<"Gano de manera Horizontal"<<endl;
  209. auxiliar =9;
  210. system("PAUSE");
  211. }
  212. if(actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr-> valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor == 'X' &&  actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor =='X'){
  213. cout<<"Gano de manera Horizontal"<<endl;
  214. auxiliar =9;
  215. system("PAUSE");
  216. }
  217. if(actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr-> valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor == '0' &&  actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor =='0'){
  218. cout<<"Gano de manera Horizontal"<<endl;
  219. auxiliar =9;
  220. system("PAUSE");
  221. }
  222.  
  223.  
  224. //GANE DIAGONAL
  225. if(actual->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor =='X'){
  226. cout<<"Gano de manera Diagonal"<<endl;
  227. auxiliar =9;
  228. system("PAUSE");
  229. }
  230. if(actual->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor =='0'){
  231. cout<<"Gano de manera Diagonal"<<endl;
  232. system("PAUSE");
  233. }
  234. if(actual->sigPtr->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor == 'X'){
  235. cout<<"Gano de manera Diagonal";
  236. auxiliar = 9;
  237. system("PAUSE");
  238. }
  239. if(actual->sigPtr->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor == '0'){
  240. cout<<"Gano de manera Diagonal"<<endl;
  241. auxiliar =9;
  242. system("PAUSE");
  243. }
  244.  
  245. //GANE VERTICAL
  246. if(actual->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr-> valor == 'X'){
  247. cout<<"Gano de manera vertical"<<endl;
  248. auxiliar =9;
  249. system("PAUSE");
  250. }
  251. if(actual->valor == '0' && actual->sigPtr->sigPtr->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr-> valor == '0'){
  252. cout<<"Gano de manera vertical"<<endl;
  253. auxiliar =9;
  254. system("PAUSE");
  255. }
  256. if(actual->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr-> valor == 'X'){
  257. cout<<"Gano de manera vertical"<<endl;
  258. auxiliar =9;
  259. system("PAUSE");
  260. }
  261. if(actual->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr-> valor == '0'){
  262. cout<<"Gano de manera vertical"<<endl;
  263. auxiliar =9;
  264. system("PAUSE");
  265. }
  266. if(actual->sigPtr->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor == 'X' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr-> valor == 'X'){
  267. cout<<"Gano de manera vertical"<<endl;
  268. auxiliar =9;
  269. system("PAUSE");
  270. }
  271. if(actual->sigPtr->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->valor == '0' && actual->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr->sigPtr-> valor == '0'){
  272. cout<<"Gano de manera vertical"<<endl;
  273. auxiliar =9;
  274. system("PAUSE");
  275. }
  276. }
  277.  
  278.  
  279. friend ostream &operator<<(ostream &os, Lista &l){
  280.    if(l.IsEmpty()){
  281.        return os;
  282.        Nodo *actPtr=l.PrimeroPtr;
  283.        cout<<actPtr->valor<<endl;
  284.        while(actPtr->sigPtr!=0){
  285.            actPtr=actPtr->sigPtr;
  286.            cout<<"->"<<actPtr->valor;
  287.        }
  288.        return os;
  289.    };
  290. }
  291. };
  292.  
  293. int main(){
  294. system("CLS");
  295. system("Color 8F");
  296. bool seguir = true;
  297.  
  298. string opcion;
  299. int posicion;
  300.  
  301. Lista l=Lista();
  302. l.createListX0();
  303. cout<<"--------------------------------- Tic Tac Toe ---------------------------------"<<endl;
  304. while(auxiliar<=3){
  305. if(turno == true){
  306. cout<<"Ingrese posicion: ";
  307. cin>>posicion;
  308. l.LlenarJugador1(posicion);
  309. l.Imprimir();
  310. l.Gane();
  311. auxiliar++;
  312. cout<<"\n";
  313. cout<<"\n";
  314. }
  315. else if(turno == false){
  316. cout<<"Ingrese posicion: ";
  317. cin>>posicion;
  318. l.LlenarJugador2(posicion);
  319. l.Imprimir();
  320. l.Gane();
  321. auxiliar++;
  322. cout<<"\n";
  323. cout<<"\n";
  324.  
  325. system("PAUSE");
  326. }
  327. }
  328. return 0;
  329. }
  330.  


En línea

ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: Turnos en C++
« Respuesta #1 en: 27 Septiembre 2015, 12:04 pm »

Muther of god. Esas largas cadenas de if de llenarJugadorX, los puedes (debes) hacer con un for o con un while.

Por lo demás, no sé cual es el error. ¿Es un error sintáctico, lógico? ¿Qué problema tienes al cambiar los turnos? Como dato, y por legibilidad, el cambio de turno (turno = false/true) lo deberías hacer en el main.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
hacer el cambio de turnos automatico con un timer en una empresa
Programación Visual Basic
audioslave1401 3 2,817 Último mensaje 17 Diciembre 2009, 23:36 pm
por Angeldj27
Ayuda para establecer un sistema de turnos en un juego
Java
aahjnnot 3 9,545 Último mensaje 9 Junio 2011, 06:15 am
por dakomt
sistema de turnos php
PHP
kakashi20 2 3,276 Último mensaje 28 Junio 2013, 18:34 pm
por kakashi20
¿Ordenar turnos por velocidad? « 1 2 »
Scripting
Tachikomaia 13 10,910 Último mensaje 13 Agosto 2021, 02:11 am
por Tachikomaia
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines