// SCORE FOUR! main program // // ECE 2036 // Fall 2023 // Prof. Yoder // // Goals: // 1. Provide a fun application of C++ to solving a problem from Game Theory // 2. Demonstrate implementation of the minimax algorithm, as presented in class. // 3. Demonstrate parallelization using native C++ threads, as presented in class. // // Notes: // 1. Default search depth is 1. // Larger search depths may be entered as the 2nd argument on the command line. // Search depths artificially limited to the range [1,9] to avoid long delays // waiting for the computer to move. This range is easily expanded for use on // faster computers. // 2. Compile this together with GameBoard.cc using highest optimization level. // 3. Minimax algorithm implemented in GameBoard.cc, parallelized using native threads // and future objects. #include #include #include #include "GameBoard.h" #include "mydefs.h" using namespace std; // Infer requested search depth from the command line. int main(int argc, char **argv) { GameBoard board; Move moveIt; int result, searchDepth; int r,c; // Assign the search depth, keeping within reasonable bounds (1 - 9). switch(argc) { case 1: searchDepth = 1; break; default: searchDepth = MIN( atoi(argv[1]), 9); searchDepth = MAX(searchDepth,1); break; } cout << "Starting the game with search depth " << searchDepth << endl; while (board.movesLeft()) { // Get valid move from human, and update board do { cout << "Enter a move: " ; cin >> r >> c; } while (!board.isValid(r,c)); board.updateBoard(r,c,1); board.displayBoard(); // Check for human win result = board.checkWinner(); if (result == 1) { cout << "You win." << endl; return(0); } // Let's time the computer player. cout << "Looking for best computer move..." << flush; auto start = chrono::high_resolution_clock::now(); // Find best computer move moveIt = board.findBestMove(searchDepth,-1); auto stop = chrono::high_resolution_clock::now(); // Report the computer's move. cout << " and it is ( " <(stop-start); cout << "That took me " << elapsedTime.count() << " milliseconds. " << endl; board.updateBoard(moveIt.row, moveIt.col, -1); board.displayBoard(); // Check for computer win. result = board.checkWinner(); if (result == -1) { cout << "I win." << endl; return(0); } } cout << endl << "It is a tie game." << endl; }