Advertisement
Guest User

Gato

a guest
Sep 2nd, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int XX;
  6. int YY;
  7. int Winner=2;
  8. int CheckTurn=0;
  9.  
  10. struct Position
  11. {
  12.     int x;
  13.     int y;
  14.    
  15.     void SetPos(int X, int Y)
  16.     {
  17.         this->x = X;
  18.         this->y = Y;
  19.     }
  20.     int GetX()
  21.     {
  22.         return this->x;
  23.     }
  24.    
  25.     int GetY()
  26.     {
  27.         return this->y;
  28.     }
  29. };
  30.  
  31. void Init(char board[3][3])
  32. {
  33.     for(int i = 0; i<3; i++)
  34.         for(int j = 0; j<3; j++)
  35.             board[i][j]=' ';
  36.  
  37. }
  38.  
  39. void PrintBoard(char board[3][3])
  40. {
  41.     for(int i = 0; i<3; i++)
  42.     {
  43.         for(int j = 0; j<3; j++)
  44.             cout << board[i][j] << ' ';
  45.         cout << endl;
  46.     }
  47.            
  48. }
  49.  
  50. void GetPos(Position &pos)
  51. {
  52.     pos.SetPos(XX,YY);
  53. }
  54.  
  55. void ChangeTurn(bool &turn)
  56. {
  57. /* Función encargada de modificar la variable global turno, que indica al programa que
  58. jugador debe marcar la casilla con el símbolo correspondiente en la siguiente jugada*/
  59.     if(CheckTurn%2 == 0)
  60.         turn = false;
  61.     else if(CheckTurn != 0)
  62.         turn = true;
  63. }
  64.  
  65.  
  66. /*
  67. int WhoWins( ) {
  68. Que retorna 1 si gana el primer jugador, 0 si empatan, ­1 si gana el segundo jugador, 2 si
  69. aún no termina la partida.
  70. }
  71. */
  72.  
  73. int main(){
  74. /*El main debe ser capaz de inicializar el tablero, imprimirlo y pedir las coordenadas para
  75. cada turno según corresponda, además debe alertar por pantalla cuando un jugador gane la
  76. partida */
  77.     Position Pos;
  78.     char Board[3][3];
  79.     bool Turn=true;
  80.    
  81.     Init(Board);
  82.    
  83.     while(Winner == 2)
  84.     {
  85.         PrintBoard(Board);
  86.        
  87.         do{
  88.         cin >> XX;}while(XX > 2 && XX <0);
  89.         do{
  90.         cin >> YY;}while(YY > 2 && YY <0);
  91.        
  92.         GetPos(Pos);
  93.        
  94.         ChangeTurn(Turn);
  95.        
  96.         if(Turn == true)
  97.             Board[Pos.GetX()][Pos.GetY()] = 'X';
  98.         else
  99.             Board[Pos.GetX()][Pos.GetY()] = 'O';
  100.        
  101.         CheckTurn++;
  102.     }
  103.    
  104.     system("pause");
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement