Guest User

othello.c

a guest
Sep 26th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.19 KB | None | 0 0
  1. /*
  2.  ============================================================================
  3.  Name        : Othello.c
  4.  Author      : Chris Cirefice
  5.  Version     : 1.0
  6.  Description : Othello (Reversi) in C
  7.  ============================================================================
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "othello_engine.h"
  14.  
  15. int main(int numArgs, const char *args[]) {
  16.  
  17.     setvbuf(stdout, NULL, 0, _IONBF);
  18.  
  19.     if (numArgs != 5) {
  20.         printf("Invalid number of arguments. Use the following command form:\n");
  21.         printf("othello board_size start_player disc_color\n");
  22.         printf("Where:\nboard_size is an even number between 6 and 10 (inclusive)\nstart_player is 1 or 2\ndisc_color is 'B' ('b') or 'W' ('w')");
  23.         return EXIT_FAILURE;
  24.     }
  25.     else if (strcmp(args[1], "othello") != 0) {
  26.         printf("Please start the command using the keyword 'othello'");
  27.         return EXIT_FAILURE;
  28.     }
  29.     else if (atoi(args[2]) < 6 || atoi(args[2]) > 10 || atoi(args[2]) % 2 != 0) {
  30.         printf("board_size must be an even number between 6 and 10");
  31.         return EXIT_FAILURE;
  32.     }
  33.     else if (atoi(args[3]) < 1 || atoi(args[3]) > 2) {
  34.         printf("start_player must be 1 or 2");
  35.         return EXIT_FAILURE;
  36.     }
  37.     else if (args[4][0] != 'B' && args[4][0] != 'b' && args[4][0] != 'W' && args[4][0] != 'w') {
  38.         printf("disc_color must be 'B', 'b', 'W', or 'w'");
  39.         return EXIT_FAILURE;
  40.     }
  41.  
  42.     // --------------------------- End checking for bad commands --------------------------- //
  43.  
  44.     // -------------------------- Initialize the Othello routine --------------------------- //
  45.  
  46.     // Create the board
  47.     int size = atoi(args[2]);
  48.     char board[size][size];
  49.     char playerColor = args[4][0];
  50.  
  51.     // Initialize all elements to 'e'
  52.     for (int i = 0; i < size; i ++) {
  53.         for (int j = 0; j < size; j ++) {
  54.             board[i][j] = 'e';
  55.         }
  56.     }
  57.  
  58.     // Set starting black pieces ('B')
  59.     if (playerColor == 'B') {
  60.         board[(size/2)-1][(size/2)-1] = 'B';
  61.         board[size/2][size/2] = 'B';
  62.     }
  63.     else {
  64.         board[(size/2)-1][(size/2)-1] = 'W';
  65.         board[size/2][size/2] = 'W';
  66.     }
  67.  
  68.     // Set starting white pieces ('W')
  69.     if (playerColor == 'B') {
  70.         board[(size/2)-1][size/2] = 'W';
  71.         board[size/2][(size/2)-1] = 'W';
  72.     }
  73.     else {
  74.         board[(size/2)-1][size/2] = 'B';
  75.         board[size/2][(size/2)-1] = 'B';
  76.     }
  77.  
  78.     // Display game start message
  79.     if (playerColor == 'B' || playerColor == 'b')
  80.         printf("Othello game with a %dx%d board - Player %d with black disc to start\n\n", size, size, atoi(args[3]));
  81.     else
  82.         printf("Othello game with a %dx%d board - Player %d with white disc to start\n\n", size, size, atoi(args[3]));
  83.  
  84.     // --------------------------- Start the game through user input --------------------------- //
  85.  
  86.     // Assign game mechanic variables
  87.     int currentPlayer = atoi(args[3]);
  88.     int boardFull = 0;
  89.     int endOfGame = 0;
  90.  
  91.     displayBoard(size, board);
  92.  
  93.     // Start the game
  94.     do {
  95.         // Assign default values for the turn
  96.         boardFull = boardIsFull(size, board);
  97.  
  98.         // If board is full, break out and tally the score!
  99.         if (boardFull) break;
  100.  
  101.         // Check valid moves for the current player
  102.         Coordinate *validMovesForTurn = movesAvailable(playerColor, size, board);
  103.         Coordinate *root = validMovesForTurn; // Keep a start-of-list placeholder
  104.         //validMovesForTurn = validMovesForTurn->next; // Skip first junk value
  105.  
  106.         // If the current player has no moves, check the other player
  107.         if (validMovesForTurn == NULL) {
  108.             printf("Player %d has no moves... Swapping to other player\n\n", currentPlayer);
  109.             swapPlayer(&currentPlayer, &playerColor);
  110.             // If the other player has no valid moves, mark the game as over
  111.             // Otherwise, continue normal play
  112.             validMovesForTurn = movesAvailable(playerColor, size, board);
  113.             validMovesForTurn = validMovesForTurn->next; // Skip first junk value
  114.             if (validMovesForTurn == NULL) {
  115.                 printf("Player %d also has no moves... I guess that means game over!!!\n\n", currentPlayer);
  116.                 endOfGame = 1;
  117.             }
  118.         }
  119.  
  120.         if (endOfGame) break;
  121.  
  122.         Coordinate desiredLocation;
  123.         validMovesForTurn = validMovesForTurn->next; // Skip first trash element (-1, -1)
  124.  
  125.         // Keep checking for valid input/re-prompt on invalid
  126.         do {
  127.             printf("\nHint - your valid moves are: \n\n");
  128.             while (validMovesForTurn != NULL) {
  129.                 printf("{%d, %d} ", validMovesForTurn->x, validMovesForTurn->y);
  130.                 validMovesForTurn = validMovesForTurn->next;
  131.             }
  132.             printf("\n\n");
  133.             // Reset at root (but skip first element again because it's garbage
  134.             validMovesForTurn = root->next;
  135.             desiredLocation = promptPlayer(currentPlayer);
  136.         } while (!isValidMove(desiredLocation, validMovesForTurn));
  137.  
  138.         // Flip disks at coordinate desiredLocation here
  139.         flipDiscs(desiredLocation, playerColor, size, board);
  140.  
  141.         // Display the board after the turn has been taken
  142.         printf("\n\n");
  143.         displayBoard(size, board);
  144.  
  145.         // Swap player (end current player's turn)
  146.         swapPlayer(&currentPlayer, &playerColor);
  147.         freeList(validMovesForTurn);
  148.     }
  149.     while (!boardFull);
  150.  
  151.     Score score = tallyScore(size, board);
  152.     printf("**********Results**********\n\n");
  153.     printf("The score was %d - %d (B - W)", score.b, score.w);
  154.  
  155.     // TODO: Add out-of-bounds check to validMoves function before actually creating those nodes (off-by-1 error)
  156.     return EXIT_SUCCESS;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment