bool action(struct GameBoard board, int columnNum, bool isItX) { if (columnNum != 1 && columnNum != 2 && columnNum != 3 && columnNum != 4 && columnNum != 5 && columnNum != 6 && columnNum != 7) { printf("That's not a valid move. Please enter a column 1-7, or q to quit.\n"); return false; } bool changed = false; int column = columnNum-1; if (board.board[0][column] == 6) { printf("That column is full! Try again.\n"); return changed; } // if the invisible top layer says it's full int row = 6 - board.board[0][column]; if (board.isX) { board.board[row][column] = 1; } // placing the piece! else board.board[row][column] = 2; board.board[0][column]++; switchIsX(board); // DO WE CALL IT ON BOARD? changed = true; board.over = checkOver(board, row, column, board.board[row][column]); // REMOVE FINAL ARGUMENT PROBABLY printBoard(board, changed); if (board.over) return changed; if ((board.aiOn == true) && (changed)) aiTurn(board); return changed; }
/** * Runs all main game functions */ int runGame(ticTacToeBoard* selector, unsigned short int* corners, unsigned short int* sides, unsigned short int center, char* charPtr, unsigned short int* intPtr) { unsigned short int aiPlay; unsigned short int win = 0; //Checks if AI is playing and sets respective character value if(intPtr[1] == 1) aiPlay = 1; else aiPlay=0; if(aiPlay == 1){ // Sets user and AI character values if(charPtr[41] == 'X') charPtr[42] = 'O'; else charPtr[42]= 'X'; } switch(aiPlay) { //Runs game without AI case 0: while(win == 0) { // Runs user turn and returns a win of 0 or 1 win = userTurn(selector, win, charPtr, aiPlay); // Increases turn by 1 intPtr[0] = intPtr[0] + 1; } break; // Runs game with AI case 1: while(win == 0) { if(charPtr[42] == charPtr[40]) { // Runs AI turn win = aiTurn(selector, corners, sides, center, win, charPtr, intPtr); // Increases turn by 1 intPtr[0] = intPtr[0] + 1; if(win == 1) break; } win = userTurn(selector, win, charPtr, aiPlay); // Increases turn by 1 intPtr[0] = intPtr[0] + 1; } break; default: displayError(); break; } // Print board to console renderBoard(charPtr, win); return 0; }