Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================================
- Name : Othello.c
- Author : Chris Cirefice
- Version : 1.0
- Description : Othello (Reversi) in C
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "othello_engine.h"
- int main(int numArgs, const char *args[]) {
- setvbuf(stdout, NULL, 0, _IONBF);
- if (numArgs != 5) {
- printf("Invalid number of arguments. Use the following command form:\n");
- printf("othello board_size start_player disc_color\n");
- 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')");
- return EXIT_FAILURE;
- }
- else if (strcmp(args[1], "othello") != 0) {
- printf("Please start the command using the keyword 'othello'");
- return EXIT_FAILURE;
- }
- else if (atoi(args[2]) < 6 || atoi(args[2]) > 10 || atoi(args[2]) % 2 != 0) {
- printf("board_size must be an even number between 6 and 10");
- return EXIT_FAILURE;
- }
- else if (atoi(args[3]) < 1 || atoi(args[3]) > 2) {
- printf("start_player must be 1 or 2");
- return EXIT_FAILURE;
- }
- else if (args[4][0] != 'B' && args[4][0] != 'b' && args[4][0] != 'W' && args[4][0] != 'w') {
- printf("disc_color must be 'B', 'b', 'W', or 'w'");
- return EXIT_FAILURE;
- }
- // --------------------------- End checking for bad commands --------------------------- //
- // -------------------------- Initialize the Othello routine --------------------------- //
- // Create the board
- int size = atoi(args[2]);
- char board[size][size];
- char playerColor = args[4][0];
- // Initialize all elements to 'e'
- for (int i = 0; i < size; i ++) {
- for (int j = 0; j < size; j ++) {
- board[i][j] = 'e';
- }
- }
- // Set starting black pieces ('B')
- if (playerColor == 'B') {
- board[(size/2)-1][(size/2)-1] = 'B';
- board[size/2][size/2] = 'B';
- }
- else {
- board[(size/2)-1][(size/2)-1] = 'W';
- board[size/2][size/2] = 'W';
- }
- // Set starting white pieces ('W')
- if (playerColor == 'B') {
- board[(size/2)-1][size/2] = 'W';
- board[size/2][(size/2)-1] = 'W';
- }
- else {
- board[(size/2)-1][size/2] = 'B';
- board[size/2][(size/2)-1] = 'B';
- }
- // Display game start message
- if (playerColor == 'B' || playerColor == 'b')
- printf("Othello game with a %dx%d board - Player %d with black disc to start\n\n", size, size, atoi(args[3]));
- else
- printf("Othello game with a %dx%d board - Player %d with white disc to start\n\n", size, size, atoi(args[3]));
- // --------------------------- Start the game through user input --------------------------- //
- // Assign game mechanic variables
- int currentPlayer = atoi(args[3]);
- int boardFull = 0;
- int endOfGame = 0;
- displayBoard(size, board);
- // Start the game
- do {
- // Assign default values for the turn
- boardFull = boardIsFull(size, board);
- // If board is full, break out and tally the score!
- if (boardFull) break;
- // Check valid moves for the current player
- Coordinate *validMovesForTurn = movesAvailable(playerColor, size, board);
- Coordinate *root = validMovesForTurn; // Keep a start-of-list placeholder
- //validMovesForTurn = validMovesForTurn->next; // Skip first junk value
- // If the current player has no moves, check the other player
- if (validMovesForTurn == NULL) {
- printf("Player %d has no moves... Swapping to other player\n\n", currentPlayer);
- swapPlayer(¤tPlayer, &playerColor);
- // If the other player has no valid moves, mark the game as over
- // Otherwise, continue normal play
- validMovesForTurn = movesAvailable(playerColor, size, board);
- validMovesForTurn = validMovesForTurn->next; // Skip first junk value
- if (validMovesForTurn == NULL) {
- printf("Player %d also has no moves... I guess that means game over!!!\n\n", currentPlayer);
- endOfGame = 1;
- }
- }
- if (endOfGame) break;
- Coordinate desiredLocation;
- validMovesForTurn = validMovesForTurn->next; // Skip first trash element (-1, -1)
- // Keep checking for valid input/re-prompt on invalid
- do {
- printf("\nHint - your valid moves are: \n\n");
- while (validMovesForTurn != NULL) {
- printf("{%d, %d} ", validMovesForTurn->x, validMovesForTurn->y);
- validMovesForTurn = validMovesForTurn->next;
- }
- printf("\n\n");
- // Reset at root (but skip first element again because it's garbage
- validMovesForTurn = root->next;
- desiredLocation = promptPlayer(currentPlayer);
- } while (!isValidMove(desiredLocation, validMovesForTurn));
- // Flip disks at coordinate desiredLocation here
- flipDiscs(desiredLocation, playerColor, size, board);
- // Display the board after the turn has been taken
- printf("\n\n");
- displayBoard(size, board);
- // Swap player (end current player's turn)
- swapPlayer(¤tPlayer, &playerColor);
- freeList(validMovesForTurn);
- }
- while (!boardFull);
- Score score = tallyScore(size, board);
- printf("**********Results**********\n\n");
- printf("The score was %d - %d (B - W)", score.b, score.w);
- // TODO: Add out-of-bounds check to validMoves function before actually creating those nodes (off-by-1 error)
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment