Advertisement
Guest User

Untitled

a guest
May 4th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define NOBODY 0
  4. #define X 1
  5. #define O 2
  6.  
  7. char playerCharacters[3] = {'-', 'X', 'O'};
  8.  
  9. int takenSpots;
  10.  
  11. int checkVict(int board[3][3]);
  12.  
  13. void putPiece(int playerID, int board[3][3]);
  14.  
  15. void showBoard(int board[3][3]);
  16.  
  17. int main()
  18. {
  19.   int playerTurn, coordX, coordY;
  20.   int board[3][3];
  21.  
  22.   playerTurn = 1;
  23.   takenSpots = 0;
  24.  
  25.   for (coordX = 0; coordX < 3; coordX++)
  26.     for (coordY = 0; coordY < 3; coordY++)
  27.       board[coordX][coordY] = 0;
  28.  
  29.   while( (checkVict(board) ) == NOBODY && takenSpots < 9)
  30.   {
  31.     showBoard(board);
  32.     putPiece(playerTurn, board);
  33.  
  34.     if (playerTurn == 1)
  35.       playerTurn = 2;
  36.     else
  37.       playerTurn = 1;
  38.   }
  39.  
  40.   showBoard(board);
  41.   printf("El ganador es el jugador NΒΊ%c!!!\n", playerCharacters[checkVict(board)]);
  42. }
  43.  
  44. /*
  45. * Checks for a player victory
  46. *
  47. * Param board: int array that is the game board
  48. * Returns: Winner of the game: 1 - player 1; 2 - player 2; 0 - none;
  49. **/
  50. int checkVict(int board[3][3])
  51. {
  52.   int x, y;
  53.  
  54.   for (x=0; x<3; x++)
  55.     if (board[x][0] == board[x][1] && board[x][1] == board[x][2])
  56.       if (board[x][0] != NOBODY)
  57.         return board[x][0];
  58.  
  59.   for (y=0; y<3; y++)
  60.     if (board[0][y] == board[1][y] && board[1][y] == board[2][y])
  61.       if (board[0][y] != NOBODY)
  62.         return board[0][y];
  63.  
  64.   if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
  65.     if (board[0][0] != NOBODY)
  66.       return board[0][0];
  67.  
  68.   if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
  69.     if (board[2][0] != NOBODY)
  70.       return board[0][2];
  71.  
  72.   return NOBODY;
  73. }
  74.  
  75. /*
  76. * Puts a piece on the board
  77. *
  78. * Param board: int array that is the game board
  79. * Param coordX & coordY: coordenates in the game board where the player puts the
  80. *                        piece.
  81. * Param playerID: int ID for the player 0 = none; 1 = player 1; 2 = player 2.
  82. * Side effects: Sets the board in the specified location to the player ID
  83. **/
  84. void putPiece(int playerID, int board[3][3])
  85. {
  86.   int coordX;
  87.   int coordY;
  88.  
  89.   printf("It's player %d's turn, please enter x and y for your piece.\n", playerID);
  90.   scanf("%d %d", &coordX, &coordY);
  91.  
  92.   while (coordX > 3 || coordX < 0){
  93.     printf("Invaid value please re enter X\n");
  94.     scanf("%d", &coordX);
  95.   }
  96.  
  97.   while (coordY > 3 || coordY < 0){
  98.     printf("Invaid value please re enter Y\n");
  99.     scanf("%d", &coordY);
  100.   }
  101.  
  102.   coordX--; coordY--;
  103.  
  104.   if (takenSpots < 9)
  105.   {
  106.     if (board[coordX][coordY] == 0)
  107.     {
  108.       board[coordX][coordY] = playerID;
  109.       takenSpots++;
  110.     }
  111.     else
  112.     {
  113.       printf("Tha place is used, give another coordenate!\n");
  114.       putPiece(playerID, board);
  115.     }
  116.   }
  117. }
  118.  
  119. void showBoard(int board[3][3])
  120. {
  121.   int x, y;
  122.   for (x = 0; x<3; x++)
  123.     for (y=0; y<3; y++)
  124.     {
  125.       printf("%c", playerCharacters[ board[x][y]] );
  126.       if (y == 2)
  127.         printf("\n");
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement