int Agent<Reversi>::minMaxAlphaBeta(Reversi& currentGame, unsigned int depth, int alpha, int beta) { if(depth >= maxDepth || currentGame.actionBegin() == currentGame.actionEnd()) { return heuristic(currentGame); } Reversi tempGame; int score, actionCounter = 0, bestAction = 0; if(depth%2 == 0) { for(Action iter = currentGame.actionBegin(); iter != currentGame.actionEnd(); iter++) { tempGame = currentGame; tempGame.play(iter); score = minMaxAlphaBeta(tempGame,depth+1,alpha,beta); if(score > alpha) { bestAction = actionCounter; alpha = score; } if(alpha>=beta) { bestAction = actionCounter; return alpha; } actionCounter++; } if(depth == 0) { return bestAction; } else { return alpha; } } else { for(Action iter = currentGame.actionBegin(); iter != currentGame.actionEnd(); iter++) { tempGame = currentGame; tempGame.play(iter); score = minMaxAlphaBeta(tempGame,depth+1,alpha,beta); if(score < beta) { bestAction = actionCounter; beta = score; } if(alpha>=beta) { bestAction = actionCounter; return beta; } actionCounter++; } if(depth == 0) { return bestAction; } else { return beta; } } }
void testCallByValue2(Reversi reversi1, Reversi reversi2){ cout << "__________CALL BY VALUE TEST FUNCTION____________\n"; cout << "-----GAME1------" << endl << reversi1 << endl; cout << "-----GAME2------" << endl << reversi2 << endl; if(reversi1.compareGames(reversi2)) cout << "GAME 2 IS BETTER THAN GAME 1\n"; else cout << "GAME 1 IS BETTER THAN GAME 2\n"; }
void testCallByReference2(Reversi &reversi){ int coorX, coorY; int size=6; cout << "__________CALL BY REFERENCE TEST FUNCTION____________\n"; cout << "-----GAME1------" << endl << reversi << endl; reversi.resizeTheBoard(size); cout << "The board resizing to " << size << "x" << size << " board." << endl; cout << reversi << endl; cout << "Please enter correct moves for good testing.\n"; reversi.getCoordinates(coorX, coorY); reversi.play(coorX, coorY); cout << reversi << endl; ++reversi; cout << reversi << endl; reversi.resizeTheBoard(4, 4); cout << "The board resizing to 4x4 board." << endl; reversi.printBoard(); reversi.getCoordinates(coorX, coorY); reversi.play(coorX, coorY); cout << reversi << endl; reversi++; cout << reversi << endl; }
/// Prints out a menu of the games available. int main() { int selection=0; const int numberOfGames = 4; string menu[] = { "1) Checkers", "2) ConnectFour", "3) Snakes and Ladders", "4) Reversi" }; // Flush the screen and set the foreground color to violet. cout << CLEAR FVIOLET; // Print out ASCII art of the product name. cout << " ____ _ _ " << " __ \n" << " / ___|___ _ __ ___ _ __ ___ _ __ __| (_)_ _ _ __ ___ " << " ___ / _|\n" << " | | / _ \\| '_ ` _ \\| '_ \\ / _ \\ '_ \\ / _` | | | | | '_ ` " << "_ \\ / _ \\| |_ \n" << " | |__| (_) | | | | | | |_) | __/ | | | (_| | | |_| | | | | | | " << "| (_) | _|\n" << " \\____\\___/|_| |_| |_| .__/ \\___|_| |_|\\__,_|_|\\__,_|_| |_|" << " |_| \\___/|_| \n" << " |_| " << " \n" << " _ _ " << " \n" << " | |__ ___ __ _ _ __ __| | __ _ __ _ _ __ ___ ___ " << " ___ \n" << " | '_ \\ / _ \\ / _` | '__/ _` | / _` |/ _` | '_ ` _ \\ / " << " _\\/ __|\n" << " | |_) | (_) | (_| | | | (_| | | (_| | (_| | | | | | | __/" << "\\__ \\\n" << " |_.__/ \\___/ \\__,_|_| \\__,_| \\__, |\\__,_|_| |_| |_|" << "\\___||___/\n" << " |___/\n"; // Switch the forground color to red. cout << RESET << FRED "\n"; // Create a border. const int borderWitdh = 80; border(borderWitdh); cout << RESET "\n"; // Print out the titles of each game. for(int i=0; i<numberOfGames; i++) { cout << menu[i] << "\n"; } // Create a closing border. cout << FRED "\n"; border(borderWitdh); cout << RESET; // Prompt and read in the users selection. cout << "\nInsert the number of the game you wish to play: \n"; while(true) { cin >> selection; cin.clear(); cin.ignore(1000,'\n'); // Check that input is a numeric value. if(cin.fail()) { cout << "\nYou entered a non numeric value, try again\n"; continue; } if(selection < 1 || selection > 4) { cout << "\nYou didn't enter a valid number, please enter a number in the " << "range 1-4\n"; continue; } switch(selection) { case 1: { Checkers checkers = Checkers(); checkers.start(); return 0; } case 2: { ConnectFour connectFour = ConnectFour(); connectFour.start(); return 0; } case 3: { SnakesAndLadders snakesAndLadders = SnakesAndLadders(); snakesAndLadders.start(); return 0; } case 4: { Reversi reversi = Reversi(); reversi.start(); return 0; } } } return 0; }
int main() { //Oyun Basi Mesaji cout << "Hello Player." << endl << "Welcome to Reversi." << endl << endl << "There is no limit for the Size of Board." << endl << "But Size of Board can be pozitif number." << endl << endl; //Gecerli bir size of board degeri girilmezse oyun biter. Reversi firstSimple; Reversi secondSimple; Reversi thirdSimple; Reversi fourthSimple; Reversi fifthSimple; //5 oyunda sira ile hamle yapilir while( !firstSimple.endGame() || !secondSimple.endGame() || !thirdSimple.endGame() || !fourthSimple.endGame() || !fifthSimple.endGame() ) { firstSimple.playGame(); cout << "******************************" << endl; secondSimple.playGame(); cout << "******************************" << endl; thirdSimple.playGame(); cout << "******************************" << endl; fourthSimple.playGame(); cout << "******************************" << endl; fifthSimple.playGame(); cout << "******************************" << endl; } //Playera ait olan hucre sayisi her obje icin yazdirilir cout << "Score Tables: " << endl << "First game: " << firstSimple.getPlayerScore() << endl << "Second game: " << secondSimple.getPlayerScore() << endl << "Third game: " << thirdSimple.getPlayerScore() << endl << "Fourth game: " << fourthSimple.getPlayerScore() << endl << "Fifth game: " << fifthSimple.getPlayerScore() << endl << endl; //isFirstGameIsBetter fonksiyonu cagrilir //eger isFirstGameIsBetter fonksiyonunu cagiran objenin skoru //parametre olarak alinan objenin skorundan daha iyi ise 1, //daha iyi degil ise 0 return edilir cout << endl << endl << "Which One Is Better:" << endl << endl << "First game vs Second game: " << firstSimple.isFirstGameIsBetter(secondSimple) << endl << "First game vs Third game: " << firstSimple.isFirstGameIsBetter(thirdSimple) << endl << "First game vs Fourth game: " << firstSimple.isFirstGameIsBetter(fourthSimple) << endl << "First game vs Fifth game: " << firstSimple.isFirstGameIsBetter(fifthSimple); cout << endl << endl << "All Living Cells Number: " << Reversi::getLivingCellsNumber(); return 0; }
void Server::play_game(int sock){ Reversi game; AI ai; char user_color='u'; char ai_color='u'; //u for unknown char game_type='u'; // ==r for random ==e,m,h for easy/medium/hard ==u for unknown string ai_move; //AI will be passed the entire board so it can play the game out as needed and return its current move. socket_write(sock,"WELCOME\r\n"); //cout << "wrote to socket\n"; string s,upper_s; while(true){ // read incoming command from client // socket_write(sock,"> "); s=socket_read(sock); //cout << "Read String: '" << s << "'\n"; s=remove_beginning_whitespace(s); s=remove_newlines(s); //cout << "'" << s << "'\n"; upper_s=capitalize(s); //cout << "Received: '" << upper_s << "'\n"; if(upper_s=="EXIT") break; else if(upper_s=="WHITE"){ if(game.set_current_player('w')) { user_color = 'w'; ai_color = 'b'; socket_write(sock,"OK\n"); } else socket_write(sock,"ILLEGAL\n"); } else if(upper_s=="BLACK"){ if(game.set_current_player('b')) { user_color = 'b'; ai_color = 'w'; socket_write(sock,"OK\n"); } else socket_write(sock,"ILLEGAL\n"); } else if(upper_s=="DISPLAY") { string send_string = "OK\n\n"; send_string += game.get_state_string(); socket_write(sock, send_string); } else if(upper_s =="EASY" || upper_s=="MEDIUM" || upper_s=="HARD" || upper_s=="RANDOM_GAME") { if(upper_s=="EASY") game_type='e'; else if(upper_s=="MEDIUM") game_type=='m'; else if(upper_s=="HARD") game_type='h'; else game_type='r'; socket_write(sock,"OK\n"); } else if(upper_s=="UNDO") { //if(game_type=='u' || user_color=='u' || ai_color=='u'){ unknown so don't allow move...} if(game.undo()) socket_write(sock,"OK\n"); else socket_write(sock,"ILLEGAL\n"); } else if(upper_s=="HUMAN-AI"){ socket_write(sock,"OK\n"); } else if(upper_s.substr(0,5)=="AI-AI"){ bool error=false; int AI_sock; string move, opponent_move; string opponent_move_partial; size_t found_index; upper_s = upper_s.substr(6); //This will remove "AI-AI " string server = get_word(upper_s); int port = atoi(get_word(upper_s).c_str()); string difficulty1 = get_word(upper_s); string difficulty2 = upper_s; struct sockaddr_in sin; memset(&sin,0,sizeof(sin)); sin.sin_family=AF_INET; sin.sin_port=port; string host=server; //cout << "'" << port << "' '" << server << "'\n"; if(struct hostent * phe=gethostbyname(host.c_str())){ memcpy(&sin.sin_addr, phe->h_addr, phe->h_length); } else if ((sin.sin_addr.s_addr=inet_addr(host.c_str())) == INADDR_NONE){ error=true; } AI_sock=socket(AF_INET, SOCK_STREAM,0); if(!AI_sock){ error=true; } if(connect(AI_sock, (struct sockaddr *)&sin, sizeof(sin))){ error=true; } string s; if(error){ socket_write(sock,"ILLEGAL\n;Command is valid, but unable to connect with specified server\n"); } else{ socket_write(sock,"OK\n"); socket_write(sock,socket_read(AI_sock)); socket_write(AI_sock,"HUMAN-AI"); socket_write(sock,"HUMAN-AI\n"); socket_write(sock,socket_read(AI_sock)); socket_write(AI_sock,"WHITE"); socket_write(sock,"WHITE\n"); socket_write(sock,socket_read(AI_sock)); game.set_current_player('w'); if(difficulty2=="EASY"){ socket_write(AI_sock,"EASY"); socket_write(sock,"EASY\n"); } else if(difficulty2 == "MEDIUM"){ socket_write(AI_sock,"MEDIUM"); socket_write(sock,"MEDIUM\n"); } else{ socket_write(AI_sock,"HARD"); socket_write(sock,"HARD\n"); } socket_write(sock,socket_read(AI_sock)); while(!game.is_game_over()){ if(difficulty1=="EASY"){ move=ai.get_move(game,AI::EASY); } else if(difficulty1=="MEDIUM"){ move=ai.get_move(game,AI::MEDIUM); } else{ move=ai.get_move(game,AI::HARD); } game.make_move(move); socket_write(AI_sock,move); socket_write(sock,move + "\n"); opponent_move=socket_read(AI_sock); //Note string parsing will have to be done here. found_index = opponent_move.find_first_of("abcdefghABCDEFGH"); //Regex not implemented in gcc4.7 so find move letter while (found_index != string::npos){ //Check if following or previous char is move integer if(opponent_move[found_index+1]=='1' || opponent_move[found_index+1]=='2' || opponent_move[found_index+1]=='3' || opponent_move[found_index+1]=='4' || opponent_move[found_index+1]=='5' || opponent_move[found_index+1]=='6' || opponent_move[found_index+1]=='7' || opponent_move[found_index+1]=='8'){ opponent_move_partial=opponent_move[found_index]; opponent_move_partial+=opponent_move[found_index+1]; } else if(opponent_move[found_index-1]=='1' || opponent_move[found_index-1]=='2' || opponent_move[found_index-1]=='3' || opponent_move[found_index-1]=='4' || opponent_move[found_index-1]=='5' || opponent_move[found_index-1]=='6' || opponent_move[found_index-1]=='7' || opponent_move[found_index-1]=='8'){ opponent_move_partial=opponent_move[found_index-1]; opponent_move_partial+=opponent_move[found_index]; } game.make_move(opponent_move_partial); //make move found_index = opponent_move.find_first_of("abcdefghABCDEFGH",found_index+1); } socket_write(sock,opponent_move + "\n"); } socket_write(sock,"\n"); } } else if(s[0]==';') ;// do nothing? else if(game.make_move(s)) { string send_string = "OK\n"; //if(game_type=='u' || user_color=='u' || ai_color=='u'){ unknown so don't allow move...} if(!game.is_game_over()) { if(game.get_num_moves() > 0) { send_string += "\n"; if(game_type=='e'){ ai_move=ai.get_move(game,AI::EASY); game.make_move(ai_move); } else if(game_type=='m'){ ai_move=ai.get_move(game,AI::MEDIUM); game.make_move(ai_move); } else if(game_type=='h'){ ai_move=ai.get_move(game,AI::HARD); game.make_move(ai_move); } else{ game.make_random_move(); } send_string += game.get_previous_move() + "\n"; send_string += game.get_state_string(); while(game.get_current_player() == ai_color){ send_string += "\n"; if(game_type=='e'){ ai_move=ai.get_move(game,AI::EASY); game.make_move(ai_move); } else if(game_type=='m'){ ai_move=ai.get_move(game,AI::MEDIUM); game.make_move(ai_move); } else if(game_type=='h'){ ai_move=ai.get_move(game,AI::HARD); game.make_move(ai_move); } else{ game.make_random_move(); } send_string += game.get_previous_move() + "\n"; send_string += game.get_state_string(); } } else{ send_string += "\n"; send_string += game.get_previous_move() + "\n"; send_string += game.get_state_string(); } } socket_write(sock, send_string); } else{ socket_write(sock,"ILLEGAL\n"); //cout << "move is... " << s << '\n'; } } close(sock); exit(0); }
void Agent<Reversi>::playerMove(Action& a, Reversi& currentGame) { vector<typename Reversi::Action> actions(currentGame.actionBegin(), currentGame.actionEnd()); a = actions[minMaxAlphaBeta(currentGame,0,-999999,999999)]; }
int Agent<Reversi>::heuristic(Reversi& currentGame) { int score = 0; // corners for(unsigned int i=0; i < currentGame.boardSize; i+=currentGame.boardSize-1) { for(unsigned int j=0; j < currentGame.boardSize; j+=currentGame.boardSize-1) { if(currentGame.getSquare(i, j) == thePlayer) { score += 5; } else if(currentGame.getSquare(i, j) != 2) { score -= 5; } } } // edges for(unsigned int i=0; i < currentGame.boardSize; i++) { if(currentGame.getSquare(i,0) == thePlayer) score++; if(currentGame.getSquare(i,currentGame.boardSize-1) == thePlayer) score++; if(currentGame.getSquare(0,i) == thePlayer) score++; if(currentGame.getSquare(currentGame.boardSize-1,i) == thePlayer) score++; } // near corners if(currentGame.getSquare(0,1) == thePlayer && currentGame.getSquare(0,0) == 2) score -= 5; if(currentGame.getSquare(1,1) == thePlayer && currentGame.getSquare(0,0) == 2) score -= 5; if(currentGame.getSquare(1,0) == thePlayer && currentGame.getSquare(0,0) == 2) score -= 5; if(currentGame.getSquare(0,currentGame.boardSize-2) == thePlayer && currentGame.getSquare(0,currentGame.boardSize-1) == 2) score -= 5; if(currentGame.getSquare(1,currentGame.boardSize-2) == thePlayer && currentGame.getSquare(0,currentGame.boardSize-1) == 2) score -= 5; if(currentGame.getSquare(1,currentGame.boardSize-1) == thePlayer && currentGame.getSquare(0,currentGame.boardSize-1) == 2) score -= 5; if(currentGame.getSquare(currentGame.boardSize-2,0) == thePlayer && currentGame.getSquare(currentGame.boardSize-1,0) == 2) score -= 5; if(currentGame.getSquare(currentGame.boardSize-2,1) == thePlayer && currentGame.getSquare(currentGame.boardSize-1,0) == 2) score -= 5; if(currentGame.getSquare(currentGame.boardSize-1,currentGame.boardSize-1) == thePlayer && currentGame.getSquare(currentGame.boardSize-1,0) == 2) score -= 5; if(currentGame.getSquare(currentGame.boardSize-2,currentGame.boardSize-2) == thePlayer && currentGame.getSquare(currentGame.boardSize-1,currentGame.boardSize-1) == 2) score -= 5; if(currentGame.getSquare(currentGame.boardSize-2,currentGame.boardSize-1) == thePlayer && currentGame.getSquare(currentGame.boardSize-1,currentGame.boardSize-1) == 2) score -= 5; if(currentGame.getSquare(currentGame.boardSize-1,currentGame.boardSize-2) == thePlayer && currentGame.getSquare(currentGame.boardSize-1,currentGame.boardSize-1) == 2) score -= 5; if(thePlayer) return -1.0*currentGame.score() + score; else return currentGame.score() + score; }
int main(int argc, char** argv) { Reversi game1("sixsix.txt"); Reversi game2; Reversi game3(8, 10); Reversi game4(20, 30); Reversi game5("tenten.txt"); Reversi game6("ab.txt"); cout<<"Welcome game of reversi"<<endl; cout<<"I load 5 reversi game from files and new created objects."<<endl; cout<<"Lets play games respectively"<<endl; cout << "------------------" << endl; cout << "Now you play game1" << endl; cout << "------------------" << endl; game1.playGame(); cout << "------------------" << endl; cout << "Now you play game2" << endl; cout << "------------------" << endl; game2.playGame(); cout << "------------------" << endl; cout << "Now you play game3" << endl; cout << "------------------" << endl; game3.playGame(); cout << "------------------" << endl; cout << "Now you play game4" << endl; cout << "------------------" << endl; game4.playGame(); cout << "------------------" << endl; cout << "Now you play game5" << endl; cout << "------------------" << endl; game5.playGame(); bool compare = true; do { cout<<"-----\n Game1 \n -----\n"; game1.displayBoard(); cout<<"-----\n Game2 \n -----\n"; game2.displayBoard(); cout<<"-----\n Game3 \n -----\n"; game3.displayBoard(); cout<<"-----\n Game4 \n -----\n"; game4.displayBoard(); cout<<"-----\n Game5 \n -----\n"; game5.displayBoard(); Reversi *first, *second; cout << "Wanna compare games which you played (y):"; char x; cin>>x; if (x == 'Y' || x == 'y') { string name1, name2; cout << "Give name of first game :"; cin>>name1; cout << "Give name of second game :"; cin>>name2; if(name1=="game1") {first = &game1;} else if(name1=="game2") {first = &game2;} else if(name1=="game3") {first = &game3;} else if(name1=="game4") {first = &game4;} else if(name1=="game5") {first = &game5;} else cout << "Wrong game name1"; if(name2=="game1") {second = &game1;} else if(name2=="game2") {second = &game2;} else if(name2=="game3") {second = &game3;} else if(name2=="game4") {second = &game4;} else if(name2=="game5") {second = &game5;} else cout << "Wrong game name2"; }else compare=false; bool isFirstBig=false; if(compare){ isFirstBig=first->compare(*second); } cout<<"First >= Second : "<<isFirstBig<<endl; char c; cout<<"Wanna write best game to file:(y)"; cin>>c; if(c=='y' || c=='Y'){ Reversi *wFile; if(isFirstBig) wFile=first; else wFile=second; wFile->writeBoard("BestCompared.txt"); } } while (compare);
void Server::play_game(int sock){ Reversi game; char user_color; char ai_color; socket_write(sock,"WELCOME\r\n"); //cout << "wrote to socket\n"; string s,upper_s; while(true){ // read incoming command from client // socket_write(sock,"> "); s=socket_read(sock); //cout << "Read String: '" << s << "'\n"; s=remove_beginning_whitespace(s); s=remove_newlines(s); //cout << "'" << s << "'\n"; upper_s=capitalize(s); //cout << "Received: '" << upper_s << "'\n"; // for now, we ignore all difficulty commands if(upper_s=="EXIT") break; else if(upper_s=="WHITE"){ if(game.set_current_player('w')) { user_color = 'w'; ai_color = 'b'; socket_write(sock,"OK\n"); } else socket_write(sock,"ILLEGAL\n"); } else if(upper_s=="BLACK"){ if(game.set_current_player('b')) { user_color = 'b'; ai_color = 'w'; socket_write(sock,"OK\n"); } else socket_write(sock,"ILLEGAL\n"); } else if(upper_s=="DISPLAY") { string send_string = "OK\n\n"; send_string += game.get_state_string(); socket_write(sock, send_string); } /*else if(upper_s =="EASY" || upper_s=="MEDIUM" || upper_s=="HARD") socket_write(sock,"Set difficulty!\n");*/ else if(upper_s=="UNDO") { if(game.undo()) socket_write(sock,"OK\n"); else socket_write(sock,"ILLEGAL\n"); } /*else if(upper_s=="HUMAN-AI") socket_write(sock,"Human game!\n");*/ /*else if(upper_s.substr(0,5)=="AI-AI"){ upper_s = upper_s.substr(6); //This will remove "AI-AI " string server = get_word(upper_s); int port = atoi(get_word(upper_s).c_str()); string difficulty1 = get_word(upper_s); string difficulty2 = upper_s; string return_str = "AI-AI game on " + server + "on port "to_string(port); return_str += " as " + difficulty1 + " vs " + difficulty2 + "\n"; socket_write(sock, return_str); }*/ else if(s[0]==';') ;// do nothing? else if(game.make_move(s)) { string send_string = "OK\n"; if(!game.is_game_over()) { if(game.get_num_moves() > 0) { send_string += "\n"; game.make_random_move(); send_string += game.get_previous_move() + "\n"; send_string += game.get_state_string(); while(game.get_current_player() == ai_color){ send_string += "\n"; game.make_random_move(); send_string += game.get_previous_move() + "\n"; send_string += game.get_state_string(); } } else{ send_string += "\n"; send_string += game.get_previous_move() + "\n"; send_string += game.get_state_string(); } } socket_write(sock, send_string); } else{ socket_write(sock,"ILLEGAL\n"); //cout << "move is... " << s << '\n'; } } close(sock); exit(0); }
void testCallByValue1(Reversi reversi){ cout << "__________CALL BY VALUE TEST FUNCTION____________\n"; reversi.playGame(); }