746. Design Tic-Tac-Toe

Code(Language:C++) (Judger:ip-172-31-5-19)
//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'){
            curPlayer = 'O'; 
        }
        else{
            curPlayer = 'X'; 
        }
    }
    
    bool isFull(){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                if(board[i][j] == '-'){
                    return false; 
                }
            }
        }
        gameEnd = true; 
        return true; 
    }
    
    bool move(int row, int col) {
        if(gameEnd){
            throw gameEndException; 
        }
        if(board[row][col] != '-'){
            throw alreadyTakenException; 
        }
        if(isFull()){
            printf("it's a draw"); 
        }
        
        board[row][col] = curPlayer; 
        //check row 
        bool win = true; 
        for(int j = 0; j < 3; j++){
            if(board[row][j] != curPlayer){
                win = false; 
                break; 
            }
        }
        if(win){
            gameEnd = true; //不要忘记这里
            return true; 
        }
        //check col 
        win = true; 
         for(int j = 0; j < 3; j++){
            if(board[j][col] != curPlayer){
                win = false; 
                break; 
            }
        }
        if(win){
            gameEnd = true; 
            return true; 
        }
        //check diaganol
        //if(col == row){
            win = true; 
             for(int j = 0; j < 3; j++){
                if(board[j][j] != curPlayer){
                    win = false; 
                    break; 
                }
            }
            if(win){
                gameEnd = true; 
                return true; 
            }
        //}
        //if(row + col == 2){
            win = true; 
             for(int j = 0; j < 3; j++){
                if(board[2 - j][j] != curPlayer){
                    win = false; 
                    break; 
                }
            }
            if(win){
                gameEnd = true; 
                return true; 
            }
        //}
        changePlayer(); 
        return win; 
    }
};

Comments

Popular posts from this blog

1427. Split Array into Fibonacci Sequence

Amazon OA 763. Partition Labels

05/25 周一