Posts

Showing posts with the label OOD

746. Design Tic-Tac-Toe

Code ( Language :C++) ( Judger :ip-172-31-5-19) Edit //OOD design //注意throw exception的操作。 class GameEndException { public : string what () { return "Game is ended!" ; } } gameEndException; class AlreadyTakenException { public : string what () { return "Already taken!" ; } } alreadyTakenException; class TicTacToe { private : //std::vector<vector<char>> board(3, vector<char>(3); char board[ 3 ][ 3 ]; char curPlayer; bool gameEnd; public : /** Initialize your data structure here. */ TicTacToe() { initialize(); } void initialize () { for ( int i = 0 ; i < 3 ; i++){ for ( int j = 0 ; j < 3 ; j++){ board[i][j] = '-' ; } } curPlayer = 'X' ; gameEnd = false ; } void changePlayer () { if (curPlayer == 'X' ){ ...

496. Toy Factory

class Toy { public : virtual void talk () const = 0 ; }; class Dog : public Toy { //继承 // Write your code here void talk () const { cout << "Wow" << endl ; //多态 } }; class Cat : public Toy { // Write your code here void talk () const { cout << "Meow" << endl ; } }; class ToyFactory { public : /** * @param type a string * @return Get object of the type */ Toy* getToy ( string & type) { // Write your code here if (type == "Dog" ){ return new Dog(); } if (type == "Cat" ){ return new Cat(); } return NULL ; } };