Exemple #1
0
int main(int argc, char* argv[]) {
   // C++11 introduces the keyword 'auto' to use "type inference" to select
   // the type of a variable automatically when it is declared.

   auto i = 10; // i is of type int, because the rhs is int.
   auto d = 10.0; // d is of type double

   // For primitive types, this doesn't make much sense to use, so DON'T GO 
   // OVERBOARD and make all variables auto. For one, you must initialize an 
   // auto variable when declared.

   // auto unknown; <---- what type is this variable? Not allowed.

   // Second, sometimes the inferred type is misleading...
   auto s = "Hello, world!";
   // Can you guess what type s is?

   // Third, can't construct objects on the stack as easily...
   // Suppose I want to replace "Rational r(1, 3); with auto.
   // auto r(1,3); won't compile, why?




   // So when is auto useful?
   // If the return type of a function is obvious, but tedious to use.
   // Imagine I'm in your Othello project.

   OthelloBoard board;
   const vector<OthelloMove*> *history = board.GetMoveHistory(); // ugly!
   auto hist = board.GetMoveHistory(); // pretty!


   // For the same reason, auto is useful with iterators.
   for (auto itr = hist->rbegin(); itr != hist->rend(); itr++) {
      // printing in reverse order, C++11 style!
   }

}
Exemple #2
0
int main(int argc, char* argv[]) {
   
   // Initialization
   OthelloBoard board; // the state of the game board
   OthelloView v(&board); // a View for outputting the board via operator<<
   string userInput; // a string to hold the user's command choice
   vector<OthelloMove *> possMoves; // a holder for possible moves


   do {
      
      string command1, command2;
      //Clearing possible moves and freeing memory
      for (OthelloMove *move : possMoves) {
         delete move;
      }
      possMoves.clear();

      istringstream iss;
      bool isValidMove = false;
      cout << v << endl;
      
      board.GetPossibleMoves(&possMoves);
      
      cout << "Possible moves for " << ((board.GetNextPlayer() == -1) ? "White: " : "Black: ") << endl;
      for (OthelloMove *move : possMoves) {
         cout << (string) *move << " ";
      }
      cout << endl;

      cout << "Enter a command" << endl;
   
      getline(cin, userInput);
      iss.str(userInput);
      iss >> command1;

      if (command1 == "move") {
         
         iss >> command2;
         
         OthelloMove *m = board.CreateMove();
         
         try {
            
            *m = command2;
         
            for (OthelloMove *move : possMoves) {
               if (*m == *move) {
                  isValidMove = true;
               }
            }
            
            if (isValidMove){
               board.ApplyMove(m);
            }
            else {
               cout << "Not a valid move." << endl;
               delete m;
               continue;
            }
         }
         catch (OthelloException e) {
            cout << e.what() << endl;
            delete m;
         }
         
      }
      else if (command1 == "undo") {
         int times;
         
         iss >> times;
         
         for (int i = times; i > 0; i --) {
            if (board.GetMoveHistory()->size() == 0)
               break;
            else
               board.UndoLastMove();
         }
      }
Exemple #3
0
int main(int argc, char* argv[]){
	//Welcome message
	cout << "Welcome to Othello!" << endl;
	//OthelloBoard class object
	OthelloBoard board;
	//OthelloView class object
	OthelloView view(&board);
	//Vector move pointer
	vector<OthelloMove *> move;
	//User input
	string input;

	while (!(board.IsFinished())) {
		//Display current board
		cout << view << endl;
		//Get all possible moves
		board.GetPossibleMoves(&move);
		//Possible moves variable
		string possibleMoves;

		for(OthelloMove *mov: move){
			//Retrieving all possible moves
			possibleMoves += *mov;
			//Removing all possible moves from the heap
			delete mov;

		}
		//Clear all move vector pointers
		move.clear();

		if (board.GetNextPlayer() == board.BLACK) {
			//Black's move
			cout << "\nBlack's turn" << endl;

		}

		else {
			//White's move
			cout << "\nWhite's turn" << endl;

		}
		//Display all possible moves
		cout << "Possible moves:\n" << possibleMoves << endl;

		cout << "Enter a command: ";
		//Get user input
		getline(cin, input);

		cout << endl;
		//Command input
		string choice;
		//new move position
		int position = board.EMPTY;

		for (int index = board.EMPTY; index < input.length(); index++){

			if (input[index] == ' ') {
				//Retrieving new move position
				position = index + 1;

				break;

			}
			//Retrieving command input
			choice += input[index];

		}

		if (choice == "move") {
			//Creating new move
			OthelloMove *newMove = board.CreateMove();

			try {
				//Retrieving new move
				(*newMove) = input.substr(position);

			}

			catch(OthelloException &error) {
				//Invalid new move
				cout << error.what() << endl;

				delete newMove;

			}
			//Get all new possible moves
			board.GetPossibleMoves(&move);

			bool equal = false;

			for (vector<OthelloMove *>::iterator itr = move.begin();
				itr != move.end(); ++itr){
				//Store all possible new moves
				OthelloMove *temp = *itr;

				if (*newMove == *temp && !equal) {
					//Apply new move onto the board
					board.ApplyMove(newMove);
					//Valid new move
					equal = true;

				}
				//Removing all possible moves on the heap
				delete temp;
			}

			if (!equal) {

				cout << "\nInvalid input" << endl;

				//Removing invalid new move
				delete newMove;

			}
			//Clear all move vector pointers
			move.clear();

		}


		else if (choice == "undo") {
			//Number of undoes
			int number = stoi(input.substr(position));

			if (number > board.GetMoveCount()){
				//Max number of undoes
				number = board.GetMoveCount();

			}

			while(number > board.EMPTY) {
				//Undo latest move
				board.UndoLastMove();
				//Next undo move
				number--;

			}
		}

		else if (choice == "showValue") {
			//Display current board value
			cout << "Board Value: " << board.GetValue() << endl;

		}

		else if (choice == "showHistory") {
			//History of all moves apply
			const vector<OthelloMove *> *mov = board.GetMoveHistory();
			//Number moves applied to the board
			int index = board.GetMoveCount();

			for (vector<OthelloMove *>::const_reverse_iterator temp =
				mov->crbegin(); temp != mov -> crend(); ++temp){
				//Getting latest move's history
				OthelloMove *m = *temp;

				if (index % (board.BLACK + board.BLACK) == 0) {
					//White history movement
					cout << "White: " << (string) *m << endl;
				}

				else {
					//Black history movement
					cout << "Black: " << (string) *m << endl;

				}
				//Next player's history movement
				index--;
			}

		}

		else {
			//Exit the game
			cout << "Thank you for playing" << endl;

			break;

		}

	}

	if (board.GetValue() > board.EMPTY) {
		//Black won the game
		cout << "\nBlack wins!" << endl;

	}

	else if (board.GetValue() < board.EMPTY){
		//White won the game
		cout << "\nWhite wins!" << endl;
	}

	else {
		//Tie game
		cout << "\nIt's a tie!" << endl;

	}

}