Move AIPlayer::alpha_beta(GameBoard* game, int lookahead, int alpha, int beta, int player) { vector<Move> all_moves = get_all_moves(game, player); //cout << "Moves are: " << endl; //cout << m.x +1 << ", " << m.y + 1 << " Score: " << m.score << endl; if (all_moves.empty()) return Move(-1, -1, maximizing_player(player) * 35); if (lookahead == 0) { //return heuristic value of previous player if (!is_maximizing(player)) { Move temp = *(std::max_element(all_moves.begin(), all_moves.end(), [](Move m1, Move m2){return m1.score < m2.score;})); //cout << "return: "<< temp.x +1 << ", " << temp.y + 1 << " Score: " << temp.score << endl; return temp; } else { Move temp = *(min_element(all_moves.begin(), all_moves.end(), [](Move m1, Move m2){return m1.score < m2.score;})); //cout << "return: "<< temp.x +1 << ", " << temp.y + 1 << " Score: " << temp.score << endl; return temp; } } Move return_move; if (is_maximizing(player)) { for (auto m : all_moves) { GameBoard temp_board(game); temp_board.make_move(m.x, m.y, player); Move temp_move = alpha_beta(&temp_board, lookahead - 1, alpha, beta, other_player(player)); if (temp_move.score > alpha) { return_move = m; alpha = temp_move.score; } if (alpha >= beta) { break; } } return return_move; } else { for (auto m : all_moves) { GameBoard temp_board(game); temp_board.make_move(m.x, m.y, player); Move temp_move = alpha_beta(&temp_board, lookahead - 1, alpha, beta, other_player(player)); if (temp_move.score < beta) { beta = temp_move.score; return_move = m; } if (alpha >= beta) { break; } } return return_move; } }
Move AIPlayer_fork::alpha_beta(GameBoard* game, int lookahead, int alpha, int beta, int player) { concurrent_vector<Move> all_moves = concurrent_get_all_moves(game, player); //cout << "Moves are: " << endl; //cout << m.x +1 << ", " << m.y + 1 << " Score: " << m.score << endl; if (all_moves.empty()) return Move(-1, -1, maximizing_player(player) * 35); if (lookahead == 0) { //return heuristic value of previous player if (!is_maximizing(player)) { Move temp = *(std::max_element(all_moves.begin(), all_moves.end(), [](Move m1, Move m2) { return m1.score < m2.score; })); //cout << "return: "<< temp.x +1 << ", " << temp.y + 1 << " Score: " << temp.score << endl; return temp; } else { Move temp = *(std::min_element(all_moves.begin(), all_moves.end(), [](Move m1, Move m2) { return m1.score < m2.score; })); //cout << "return: "<< temp.x +1 << ", " << temp.y + 1 << " Score: " << temp.score << endl; return temp; } } Move return_move; if (is_maximizing(player)) { task_group g; for (unsigned int i = 0; i < all_moves.size(); i++) { g.run([=, &all_moves, &return_move, &alpha, & beta] { GameBoard temp_board(game); temp_board.make_move(all_moves[i].x, all_moves[i].y, player); Move temp_move; temp_move = alpha_beta(&temp_board, lookahead - 1, alpha, beta, other_player(player)); if (temp_move.score > alpha) { return_move = all_moves[i]; alpha = temp_move.score; } if (alpha >= beta) { //g.cancel(); } }); } g.wait(); return return_move; } else { task_group g; for (unsigned int i = 0; i < all_moves.size(); i++) { g.run([=, &all_moves, &return_move, &alpha, &beta] { GameBoard temp_board(game); temp_board.make_move(all_moves[i].x, all_moves[i].y, player); Move temp_move; temp_move = alpha_beta(&temp_board, lookahead - 1, alpha, beta, other_player(player)); if (temp_move.score < beta) { beta = temp_move.score; return_move = all_moves[i]; } if (alpha >= beta) { //g.cancel(); } }); } g.wait(); return return_move; } }
void MazeBoard::initialize(int _board_size) { board_size = _board_size; // adding board_size to class variable /* initialize random seed: */ srand(time(NULL)); std::vector<int> ratio (3, 0); // ratio between counts of Stone types board.resize(board_size * board_size); // adding corners to the board (4x L) board[INDEX(1, 1 )] = Stone(L, 1); board[INDEX(1, board_size)] = Stone(L, 2); board[INDEX(board_size, 1 )] = Stone(L, 0); board[INDEX(board_size, board_size)] = Stone(L, 3); ratio[L] += 4; // adding T stones (odd x and odd y coordinates - but not corners) // count is ((N/2+1)*(N/2+1)-4) for (int x = 1; x <= board_size; x+=2) { for (int y = 1; y <= board_size; y+=2) { if (is_corner(x, y, board_size)) continue; board[INDEX(x,y)] = Stone(T, rand()%4); if (x == 1) (board[INDEX(x,y)]).rotation = 0; // first row else if (x == board_size) (board[INDEX(x,y)]).rotation = 2; // last row else if (y == 1) (board[INDEX(x,y)]).rotation = 3; // first column else if (y == board_size) (board[INDEX(x,y)]).rotation = 1; // last column (ratio[T])++; //std::cout << "ratio.. I: " << ratio[I] << " L: " << ratio[L] << " T: " << ratio[T] << std::endl; } } // placing remaining stones (with random shape and rotation) // it is trying to keep ratio 1:1:1 int remaining_stones_cnt = (board_size/2)*board_size + (board_size/2+1)*(board_size/2); std::vector<Stone> temp_board (remaining_stones_cnt); StoneType min_ratio; // stone type with the smallest count of stones for (int x = 0; x < remaining_stones_cnt; x++) { min_ratio = minimum(ratio); temp_board[x].type = min_ratio; temp_board[x].rotation = rand() % 4; (ratio[min_ratio])++; //std::cout << "ratio.. I: " << ratio[I] << " L: " << ratio[L] << " T: " << ratio[T] << std::endl; } // shuffle remaining stones unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); auto engine = std::default_random_engine{seed}; std::shuffle(std::begin(temp_board), std::end(temp_board), engine); // adding remaining stones to the even rows (N/2)*N for (int x = 2; x <= board_size; x+=2) { for (int y = 1; y <= board_size; y++) { board[INDEX(x,y)] = temp_board.back(); temp_board.pop_back(); } } // adding remaining stones to the odd rows, where column is even ((N/2+1)*(N/2)) for (int x = 1; x <= board_size; x+=2) { for (int y = 2; y <= board_size; y+=2) { board[INDEX(x,y)] = temp_board.back(); temp_board.pop_back(); } } free_stone.type = minimum(ratio); }
void Projector::ctProjection(std::string ctFilePath, int startPoint, int xDim, int yDim, int zDim, bool PNG) { SimpleViewer viewer; CTObject ctObject(ctFilePath, xDim, yDim, zDim, startPoint); if(PNG) ctObject.readDataPNG(); else ctObject.readData(); libfreenect2::FrameMap frames; libfreenect2::Registration* registration = new libfreenect2::Registration(_dev->getIrCameraParams(), _dev->getColorCameraParams()); libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4); bool shutdown = false; cv::Mat board(480, 640, CV_8UC4, cv::Scalar::all(0)); //cv::namedWindow("CTviewer", CV_WINDOW_NORMAL); //cv::moveWindow("CTviewer", 00, 0); //cv::setWindowProperty("CTviewer", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); { viewer.setSize(480, 640); // TO-DO change resolution viewer.initialize(); libfreenect2::Frame b(640, 480, 4); b.data = board.data; viewer.addFrame("RGB", &b); shutdown = shutdown || viewer.render(); } libfreenect2::Frame *rgb; libfreenect2::Frame *depth; (_listener)->waitForNewFrame(frames); rgb = frames[libfreenect2::Frame::Color]; depth = frames[libfreenect2::Frame::Depth]; registration->undistortDepth(depth, &undistorted); while (!shutdown) { std::thread *nextFrame = new std::thread[1]; /*(_listener)->waitForNewFrame(frames); rgb = frames[libfreenect2::Frame::Color]; depth = frames[libfreenect2::Frame::Depth]; registration->undistortDepth(depth, &undistorted);*/ //registration->apply(rgb, depth, &undistorted, ®istered, true, NULL, NULL); //cv::Mat frame = frameToMat("registered", ®istered); cv::Mat depthFrame;// = frameToMat("depth", depth); //cv::Mat board(424, 512, CV_8UC4, cv::Scalar::all(0)); board = cv::Mat(480, 640, CV_8UC4, cv::Scalar::all(0)); //depthFrame *= 0.001; cv::Mat temp_board(480, 640, CV_16UC3, cv::Scalar::all(0)); int parts = 128; int jump = 512 / parts; int minX = 999; int maxX = -1; int minY = 999; int maxY = -1; std::thread *tt = new std::thread[parts]; for (int i = 0; i < parts; ++i) { tt[i] = std::thread(tst, registration, &undistorted, ®istered, jump * i, jump + jump * i, &board, &ctObject, right, up, _mr, _mt, _cam, _pro, &depthFrame, &temp_board, &minX, &maxX, &minY, &maxY); } for (int i = 0; i < parts; ++i) tt[i].join(); //_listener->release(frames); nextFrame[0] = std::thread(loadNextFrame, _listener, &frames, rgb, depth, registration, &undistorted); int parts2 = 8; int jump2 = 512 / parts2; for (int ii = 0; ii < 3; ii++) { for (int i = 0; i < parts2; ++i) { tt[i] = std::thread(tt2, &temp_board, jump2 * i, jump2 + jump2 * i, cv::Size(3, 3), minX, maxX, minY, maxY); } for (int i = 0; i < parts2; ++i) tt[i].join(); } //for (int i = 0; i < 3; i++) //temp_board = pseudoBoxFilter(temp_board, temp_board, cv::Size(3, 3)); //temp_board = pseudoBoxFilter(temp_board, temp_board, cv::Size(3, 3)); //temp_board = pseudoBoxFilter(temp_board, temp_board, cv::Size(7, 7)); //temp_board = pseudoBoxFilter(temp_board, temp_board, cv::Size(7, 7)); //cv::medianBlur(temp_board, temp_board, 5); // TO-DO check if needed //cv::GaussianBlur(temp_board, temp_board, cv::Size(3, 3), 0, 1); //int parts3 = 16; //int jump3 = 480 / parts3; //std::thread *tt2 = new std::thread[parts]; //for (int i = 0; i < parts3; ++i) { // tt2[i] = std::thread(tt3, &board, &temp_board, &ctObject, jump3 * i, jump3 + jump3 * i); // //std::cout << jump3 * i << " " << jump3 + jump3 * i << "\n"; //} //for (int i = 0; i < parts; ++i) // tt2[i].join(); //std::cout << "zuoooooo\n"; /*for (int i = 0; i < 480; i++) { for (int j = 0; j < 640; j++) { cv::Mat ROI = board(cv::Rect(j, i, 1, 1)); cv::Vec3s v = temp_board.at<cv::Vec3s>(i, j); unsigned char value = ctObject.at(v[0], v[1], v[2]); ROI.setTo(cv::Scalar(value, value, value, 100)); } }*/ //cv::imshow("CTviewer", board); { libfreenect2::Frame b(640, 480, 4); b.data = board.data; viewer.addFrame("RGB", &b); shutdown = shutdown || viewer.render(); } //_listener->release(frames); nextFrame[0].join(); delete[] tt; delete[] nextFrame; //delete[] tt2; /*{ int op = cv::waitKey(1); if (op == 100 || (char)(op) == 'd') right -= 1; if (op == 115 || (char)(op) == 's') up += 1; if (op == 97 || (char)(op) == 'a') right += 1; if (op == 119 || (char)(op) == 'w') up -= 1; if (op == 1113997 || op == 1048586 || op == 1048608 || op == 10 || op == 32) { std::cout << "right = " << right << ";\nup = " << up << ";\nrotX = " << rotX << ";\n"; shutdown = true; cv::destroyWindow("CTviewer"); } }*/ } }