int main() { int matchResult = 0; int **board = createBoard(); printBoard(board); char *currentPlayer = PLAYER_ONE; while(matchResult == 0) { int move[2]; getPlayerMove(board, currentPlayer, move); matchResult = evalBoard(board); if (currentPlayer == PLAYER_ONE) { currentPlayer = PLAYER_TWO; } else { currentPlayer = PLAYER_ONE; } } if (matchResult == PLAYER_ONE_VAL) { printf("Player %s WON!\n", PLAYER_ONE); } if (matchResult == PLAYER_TWO_VAL) { printf("Player %s WON!\n", PLAYER_TWO); } if (matchResult == DRAW) { printf("DRAW!\n"); } // clean up the memory freeBoard(board); return 0; }
/////////////////////////////////////////////////////////////////////////// // Tic-Tac-Toe April 4, 2011 // // // // Author: BJ Peter "Beej" DeLaCruz // // // // Description: A game of tic-tac-toe. Players take turns putting an X // // or an O in a matrix. Whoever fills in all X's or O's in // // a row, column, or diagonal first wins. Game ends when // // there is a tie. Size of matrix is defined by ROW and COL // // in the constants.h header file. ROW and COL must be // // equal to each other. The matrix can be any size, e.g. // // 10 x 10, so feel free to modify the ROW and COL // // constants. Enjoy, and have fun! // // // // Version: 1.1 // // // // Input parameters: None // // // // Returns: -1 if problems were encountered // // 0 on success // // // /////////////////////////////////////////////////////////////////////////// int main(void) { char whoseTurn = 'X'; char didPlayerWin = FALSE; char isTie = FALSE; char** ppcTwoDArray = NULL; TwoDArraySizes* pmsSizes = NULL; pmsSizes = (TwoDArraySizes*) malloc(sizeof(TwoDArraySizes)); if (pmsSizes == NULL) { printf("Memory allocation failure for pmsSizes!\n"); return -1; } pmsSizes->num_rows = MAX_ROW_SIZE; pmsSizes->num_cols = MAX_COL_SIZE; ppcTwoDArray = create2dArray(pmsSizes); if (ppcTwoDArray == NULL) { printf("Memory allocation failure for ppcTwoDArray!\n"); return -1; } printf("\nTic-Tac-Toe\n"); printf("----------------------\n"); printf("How to play:\n"); printf(" > Whoever gets all X's or all O's in a row, column, or diagonal first wins!\n\n"); printf("%c goes first.\n\n", whoseTurn); while (!didPlayerWin && !isTie) { if (getPlayerMove(ppcTwoDArray, &whoseTurn, pmsSizes) < 0) { return -1; } if (checkRowsColumnsAndDiagonals(ppcTwoDArray, &whoseTurn, &didPlayerWin, &isTie, pmsSizes) < 0) { return -1; } if (print2dArray(ppcTwoDArray, pmsSizes) < 0) { return -1; } if (isTie || didPlayerWin) { break; } whoseTurn = (whoseTurn == 'X') ? 'O' : 'X'; } if (didPlayerWin) { printf("%c won!\n\n", whoseTurn); } else { printf("The game is tie.\n\n"); } free2dArray(ppcTwoDArray, pmsSizes); return 0; }
//Calls all other functions in the order that creates a coherent game void playGame(){ char playerMove; int dice1; int dice2; int sum; int playerCount; int choice; while(1) { printf("Welcome to Dice Blackjack!\n"); printf("I am now rolling two dice\n"); dice1 = rollDice(); dice2 = rollDice(); printf("You got a %d and a %d\n",dice1,dice2); sum = dice1 + dice2; printf("Total sum is %d\n",sum); playerCount = (sum + sum); printf("Your total player count is %d\n",playerCount); while(playerMove != 'S'){ playerMove = getPlayerMove(); if(playerMove == 'H') { printf("Rolling two more dice\n"); dice1 = rollDice(); dice2 = rollDice(); printf("You have rolled a %d from the first dice and a %d on the second dice\n",dice1,dice2); choice = getPlayerChoice(); if(playerCount > 21) { printf("You have busted!\n"); break; } else printf("You have a %d player count\n", playerCount); } } //Call the rest of the functions to get the game working //Game logic //Declare the winner of this round printf("Would you like to play again? Y/N:\n"); //If the player enters if(playerMove == 'N')// N, the game ends. exit(0); } }
int getPlayerMove(struct game *state) { int move; printf("Please enter a number between 1 and 9 to choose your move: "); scanf("%d",&move); if(move < 1 || move > 9 || state->board[move - 1] != ' ') { printf("Not a valid move.\n"); return getPlayerMove(state); } return (move - 1); }
void gameloop(struct game *state) { printf("gameloop: current board '%s' with player %c\n",state->board,state->player); displayGame(state); if(gameover(state) == 1) { return; } makePlayerMove(state,getPlayerMove(state)); displayGame(state); if(gameover(state) == 1) { return; } makeComputerMove(state); gameloop(state); }