int error_check( int x, int y, TicTacToe board1 ) { int err = 0 ; err = board1.checkBoundry( x, y ) ; if( err == 2 ) { cout << "ERROR : The X coordinate you specified is out of the board." << endl ; return 1 ; } if( err == 3 ) { cout << "ERROR : The Y coordinate you specified is out of the board." << endl ; return 2 ; } else { err = board1.checkSpace( x, y ) ; //Check to see if a move is already there if( err == 1 ) { cout << "ERROR : There is already a move there." ; return 3 ; } } return 0 ; }
int main() { TicTacToe g; // creates object g of class TicTacToe g.makeMove(); // invokes function makeMove system("pause"); return 0; } // end main
int main(int argc, char *argv[]) { QApplication a(argc, argv); TicTacToe w; w.show(); return a.exec(); }
int main(int argc, char *argv[]) { Application app(argc, argv); TicTacToe toe; toe.setObjectName("toe"); app.setTicTacToe(&toe); toe.show(); return app.exec(); }
/** * @brief Función para consultar visualmente el estado de una partida * @param ttt Referencia a la partida de la que se obtiene la información * @return Un texto con los jugadores, a quién le toca el siguiente turno, y * una representación en modo texto del estado del tablero, en la que * las posiciones libres están señaladas con '-', las ocupadas por el * jugador 1 con 'X', y las ocupadas por el jugador 2 con 'O' */ string t33_utils::info ( TicTacToe& ttt ) { std::stringstream aux; aux << "Jugador 1 (X): " << ttt.getJugador1 () << ".\t" << "Jugador 2 (O): " << ttt.getJugador2 () << std::endl << "Estado actual del tablero:" << std::endl << info ( ttt.getTablero () ); return ( aux.str () ); }
void manualTesting( NeuralNetAI& ai ) { TicTacToeHuman human( 9, 9, 1, 1 ); TicTacToe game; const int NUM_OF_GAMES = 10; for( int i = 0; i < NUM_OF_GAMES; i++ ) { TicTacToe::Token winner = game.match(ai, human ); verboseEndGame( winner ); } }
int main() { TicTacToe game; char player = 'X'; while(game.DetermineDraw() == false) { game.DrawBoard(); game.GetMove(player); game.TogglePlayer(player); } system("pause"); }
int main() { /// Initialization const unsigned int NUMBER_OF_GAMES = 1000; //-1 to play an infinite number of games const std::string SAVE_FILE_X = "TicTacToeX.save"; const std::string SAVE_FILE_O = "TicTacToeO.save"; const bool VERBOSE_ENABLED = false; // if( VERBOSE_ENABLED ): the stats are calculated for playerO. Use with small NUMBER_OF_GAMES or with a human player TicTacToe game; CaseBasedAI playerX( 9, 9, 1, 1 ); TicTacToeHuman playerO( 9, 9, 1, 1 ); // CaseBasedAI playerO( 9, 9, 1, 1 ); std::cout << "start load" << std::endl; GeneralAI::load< CaseBasedAI >( playerX, SAVE_FILE_X ); // GeneralAI::load< NeuralNetAI >( playerO, SAVE_FILE_O ); // GeneralAI::load< CaseBasedAI >( playerO, SAVE_FILE_O ); std::cout << "load completed" << std::endl; std::cout << "start games" << std::endl; /// Play games for( unsigned int i = 0; i != NUMBER_OF_GAMES; i++ ) { TicTacToe::Token winner; winner = game.match( playerX, playerO ); if( VERBOSE_ENABLED ) { if (!verboseEndGame( winner ) ) // false if must stop for some reason { break; } } } std::cout << "games completed" << std::endl; std::cout << "start save" << std::endl; GeneralAI::save< CaseBasedAI >( playerX, SAVE_FILE_X ); // GeneralAI::save< NeuralNetAI >( playerO, SAVE_FILE_O ); // GeneralAI::save< CaseBasedAI >( playerO, SAVE_FILE_O ); std::cout << "save completed" << std::endl; return 0; }
int main() { bool winner=false;//bool to assist with running the loop TicTacToe grid;//construct grid.print(); //runs the program continuously until someone has won or game is tied while(winner==false) { grid.play_by_user(); grid.print(); //interprets the return value of win_check to see if anyone has won or not if (grid.win_check()=='U') { cout<<"Congratulation! You won!"; winner=true; } if (grid.win_check()=='N') { cout<<"Game is over with no winner!"<<endl; winner=true; } //if the user has won the the computer continues to play if(winner==false) { grid.play_by_computer(); grid.print(); //interprets the return value of win_check to see if anyone has won or not if (grid.win_check()=='C') { cout<<"Sorry, you lost!"; winner=true; } if (grid.win_check()=='N') { cout<<"Game is over with no winner!"<<endl; winner=true; } } } system("pause"); return 0; }
int main () { //Create menu variable int menuChoice = PLAY; //Create turn variables TicTacToe game; char turn = PLAYER1; int totalTurns = 0; //Scoreboard tally int xWin = 0; int oWin = 0; int tie = 0; //Clear the Screen clearScreen (); while (menuChoice < QUIT && menuChoice >= PLAY) { //Display Welcome Message menuChoice = welcome (); switch (menuChoice) { case (PLAY): totalTurns = 0; while (!game.checkWinner (PLAYER1) & !game.checkWinner (PLAYER2) & (totalTurns < SIZE * SIZE)) { //Display Board game.printBoard (); //Take a Turn takeTurn (game, turn); totalTurns++; //Switch turns changeTurns (turn); } //Update Scoreboard and Display Victory Message scoreUpdate (turn, totalTurns, xWin, oWin, tie); //Clear the board for new game game.resetBoard (); case (SCORE): displayScore (xWin, oWin, tie); break; case (QUIT): goodbye (); } } return 0; }
void doCompMove( TicTacToe & t, bool firstMove ) { static int gameNum = 0; int bestRow, bestCol, bestVal; if( !firstMove ) t.chooseMove( TicTacToe::COMPUTER, bestRow, bestCol ); else { gameNum++; bestRow = gameNum % 3; bestCol = gameNum / 3; } cout << "Transposition table size is: " << t.getTransSize( ) << endl; cout << "Computer plays: ROW = " << bestRow << " COL = " << bestCol << endl; t.playMove( TicTacToe::COMPUTER, bestRow, bestCol ); }
//actual program int main() { TicTacToe ttt; const char players[2] = { 'X', 'O' }; int player = 1; bool win = false; bool full = false; while (!win && !full) { player = 1 - player; ttt.Display(); ttt.Turn(players[player]); win = ttt.Win(players[player]); full = ttt.Full(); } ttt.Display(); if (win) { std::cout << "\n" << players[player] << " is the winner.\n"; } else { std::cout << "Tie game.\n"; } return 0; }
void playGame( bool compGoesFirst ) { TicTacToe t; char compSide, humanSide; int row, col; if( compGoesFirst ) { compSide = 'x'; humanSide = 'o'; doCompMove( t, true ); } else { compSide = 'o'; humanSide = 'x'; } while( !t.boardIsFull( ) ) { do { printBoard( t, compSide, humanSide ); cout << endl << "Enter row and col (starts at 0): "; cin >> row >> col; } while( !t.playMove( TicTacToe::HUMAN, row, col ) ); cout << endl; printBoard( t, compSide, humanSide ); cout << endl; if( t.isAWin( TicTacToe::HUMAN ) ) { cout << "Human wins!!" << endl; break; } if( t.boardIsFull( ) ) { cout << "Draw!!" << endl; break; } doCompMove( t, false ); if( t.isAWin( TicTacToe::COMPUTER ) ) { cout << "Computer wins!!" << endl; break; } if( t.boardIsFull( ) ) { cout << "Draw!!" << endl; break; } } printBoard( t, compSide, humanSide ); }
// Task: Play TicTacToe virtual double fitnessEval( NeuralNetAI& ai ) { TicTacToe game; double score = 0; const int NUM_OF_GAMES = 10; RandomAI opponent( INPUT_SIZE, OUTPUT_SIZE, INPUT_AMPLITUDE, OUTPUT_AMPLITUDE ); for( int i = 0; i < NUM_OF_GAMES; i++ ) { TicTacToe::Token winner = game.match( ai, opponent ); if( winner == TicTacToe::Token::X ) { score += 1; } else if( winner == TicTacToe::Token::O ) { score -= 1; } } return score; }
void comp_move_easy( int &x, int &y, TicTacToe board1 ) { int err ; srand( time( NULL ) ) ; while( 1 ) { x = 1 + ( rand() % 3 ) ; y = 1 + ( rand() % 3 ) ; err = board1.checkBoundry( x, y ) ; if( err == 0 ) break ; } return ; }
int main(int argc, char const* argv[]) { using namespace std; char answer; cout << "Player 1 [circle] begins, should it be human (enter h) or computer (enter c): "; cin >> answer; while (!(answer == 'h' || answer == 'c')){ cout << "Wrong answer, try again: "; cin >> answer; } Player p1(Player1, (answer == 'c' ? true : false)); cout << "Player 2 [cross], should it be human (enter h) or computer (enter c): "; cin >> answer; while (!(answer == 'h' || answer == 'c')){ cout << "Wrong answer, try again: "; cin >> answer; } Player p2(Player2, (answer == 'c' ? true : false)); TicTacToe game; while (!game.getStatus()){ // until nobody won and there are still empty fields game.displayBoard(); p1.move(game); if (game.getStatus() != None) // if sb. won or there is a draw break; game.displayBoard(); p2.move(game); if (game.getStatus() != None) // if sb. won or there is a draw break; } game.displayBoard(); switch (game.getStatus()) { case Player1: cout << "Player 1 won!\n"; break; case Player2: cout << "Player 2 won!\n"; break; default: cout << "Draw!\n"; break; } return 0; }
void takeTurn (TicTacToe& game, char turn) { bool mark = false; int row; int col; //Ask player to make a move and check if they can go there while (!mark) { cout << "\n\n"; cout << turn << ": Where would you like to go? (Row Column): "; cin >> row >> col; if (game.placeMark (row, col, turn)) mark = true; else cout << "That is an invalid move, try again please. \n"; } }
int main() { TicTacToe t; // Movement variables uint8_t x, y; // BOOST Random generation support //boost::mt19937 gen; // Get current microseconds struct timeval tv; gettimeofday(&tv, NULL); boost::mt19937 gen(tv.tv_usec); boost::uniform_int<> dist(1, 3); boost::variate_generator<boost::mt19937&, boost::uniform_int<> > roll(gen, dist); while(1) { // Until we finish a game while(false == t.finished()) { do { // Move with PRNG x = roll(); y = roll(); // Keep rolling until a proper space is found if(t.occupied(x, y)) { continue; } // Make a move } while(!t.move(x,y)); // A move succeeded! // Print the board t.printBoard(); } // Game is finished. Who won? t.printResult(); // Continue? std::cout << "Press Enter to play again!\n"; // Get a single character from the keyboard // If it's not a newline, break out of the while loop if(std::cin.get() != '\n') break; // Otherwise, clear the board and continue to the next game t.clearBoard(); } return EXIT_SUCCESS; }
// From here downward, we have non-class functions to play the game. void printBoard( const TicTacToe & obj, char compSide, char humanSide ) { const matrix<int> & b = obj.getBoard( ); cout << "---" << endl; for( int row = 0; row < 3; row++ ) { for( int col = 0; col < 3; col++ ) if( b[ row ][ col ] == TicTacToe::COMPUTER ) cout << compSide; else if( b[ row ][ col ] == TicTacToe::HUMAN ) cout << humanSide; else cout << ' '; cout << endl; } cout << "---" << endl; }
void Player::move(TicTacToe & game) { using namespace std; if (!artificial) { int x = 0, y = 0; do { cout << "Enter position [row column] to which you want to insert a " << (id == Player1 ? "circle" : "cross" ) << ": "; cout << "X coordinate: "; while (!(cin >> x)) { cout << "Wrong x coordinate, try again: "; cin.clear(); while (cin.get() != '\n') {continue;} } cout << "Y coordinate: "; while (!(cin >> y)) { cout << "Wrong y coordinate, try again: "; cin.clear(); while (cin.get() != '\n') {continue;} } #ifndef _NDEBUG std::cout << "\nx = " << x << " y = " << y << std::endl; #endif } while (!game.set(x, y, id) && (std::cout << "Wrong coordinates\n")); }
int main() { int userSelectedNumber; TicTacToe board = TicTacToe(); bool status = false; while (!status) { cout << "Player1(o): Enter a number between 1 and 9." << endl; cin >> userSelectedNumber; board.GetMove(userSelectedNumber, 'o'); board.PrintBoard(); if (board.DetermineWinner('o')) { cout << "Player1(o) has won the game!!" << endl; status = true; continue; } cout << "Player2(x): Enter a number between 1 and 9." << endl; cin >> userSelectedNumber; board.GetMove(userSelectedNumber, 'x'); board.PrintBoard(); if (board.DetermineWinner('x')) { cout << "Player2(x) has won the game!!" << endl; status = true; continue; } } system("pause"); return 0; }
int main() { TicTacToe g; // creates object g of class TicTacToe g.numberPlayers(); // invokes function makeMove } // end main
int main() { TicTacToe t; // Movement variables unsigned int x, y; std::stringstream ss; std::string input; // BOOST Random generation support // Seed with current microseconds struct timeval tv; gettimeofday(&tv, NULL); boost::mt19937 gen(tv.tv_usec); boost::uniform_int<> dist(1, 3); boost::variate_generator<boost::mt19937&, boost::uniform_int<> > roll(gen, dist); while(1) { // Until we finish a game std::cout << "To move, put row (1-3) then column (1-3) like so: 1 3" << std::endl; std::cout << "Then press Enter." << std::endl; while(false == t.finished()) { t.printBoard(); do { if('X' == t.getCurrentPlayer()) { std::cout << t.getCurrentPlayer() << "'s move: "; // Read X and Y from command line while(getline(std::cin, input)) { // Clear error state of string stream ss.clear(); // Clear the old string ss.str(""); // Parse the x and y params from the line ss << input; if(ss >> x >> y) { // Proper input if(!t.isInBounds(x, y)) { std::cout << "Space was out of bounds. Try again." << std::endl << t.getCurrentPlayer() << "'s move: "; continue; } if(t.occupied(x, y)) { std::cout << "Space was already occupied. Try again." << std::endl << t.getCurrentPlayer() << "'s move: "; continue; } else { // Wonderful. Make the move. break; } } else { // Bad input std::cout << "Bad input. Try again." << std::endl; std::cout << t.getCurrentPlayer() << "'s move: "; } } // Make sure we haven't received an EOF if(std::cin.eof()) { // Exit std::cout << "\nExiting..." << std::endl; exit(EXIT_SUCCESS); } // Make a move } else { std::cout << t.getCurrentPlayer() << "'s move" << std::endl; // Query PRNG until we get an unoccupied space do { x = roll(); y = roll(); } while(t.occupied(x,y)); // Make a move } }
int main() { TicTacToe game; game.play(); return 0; }
void main(void) { TicTacToe game; auto playAgain = true; TicTac anyWinnerYet = TicTac::NO_WINNER_YET; TicTac playerTurn; while (playAgain == true) { if (anyWinnerYet == TicTac::PLAYER_O_WIN) { std::cout << "Player O won the last game, and will go first!" << std::endl; playerTurn = TicTac::PLAYER_O_TURN; anyWinnerYet = TicTac::NO_WINNER_YET; } else if (anyWinnerYet == TicTac::PLAYER_X_WIN) { std::cout << "Player X won the last game, and will go first!" << std::endl; playerTurn = TicTac::PLAYER_X_TURN; anyWinnerYet = TicTac::NO_WINNER_YET; } else { playerTurn = game.beginGame(); anyWinnerYet = TicTac::NO_WINNER_YET; } while (anyWinnerYet == TicTac::NO_WINNER_YET) { while (playerTurn == TicTac::PLAYER_X_TURN) { std::cout << "It is now Player X's turn!" << std::endl; anyWinnerYet = game.takeTurn(TicTac::PLAYER_X_TURN); playerTurn = TicTac::PLAYER_O_TURN; } if (anyWinnerYet != TicTac::NO_WINNER_YET) { break; } while (playerTurn == TicTac::PLAYER_O_TURN) { std::cout << "It is now Player O's turn!" << std::endl; anyWinnerYet = game.takeTurn(TicTac::PLAYER_O_TURN); playerTurn = TicTac::PLAYER_X_TURN; } } game.winnaWinnaChickenDinna(anyWinnerYet); playAgain = game.playAgainFunction(); } std::cout << "You Have Chosen to end the game." << std::endl; std::cout << "Thanks for Playing!" << std::endl; system("PAUSE"); }