```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 ``` ```// Original problem statement @ http://nabetani.sakura.ne.jp/hena/1/ // Input is game steps for tic tac toe // o takes the first move. Moves keep alternating between o and x then onwards // If o or x makes a line of 3 in any direction, it is declared as won // It o or x makes a move over already marked block, it is declared as foul // and opponent is declared as winner // Input is never broken. So you don't need to handle the exception case. // 1 | 2 | 3 // --+---+-- // 4 | 5 | 6 <--------- Board layout // --+---+-- // 7 | 8 | 9 #include #include #include using namespace std; class CTicTacToe { static const signed char BAD_INPUT_ = 'B'; static const signed char CONTINUE_ = 'C'; static const signed char DRAW_GAME_ = 'D'; public: CTicTacToe() : firstMover('o') , curMover('o') //curMover(firstMover) , status(CONTINUE_) , nMoves(0) { for(int i = 0; i < 3; ++i) for(int j = 0; j < 3; ++j) board[i][j] = '.'; } bool isRemaining() const { return getStatus() == CONTINUE_; } signed char getStatus() const { return status; } void playNext(signed char move); private: void play(int r, int c); signed char flipMover(signed char c) const { return 'x' - c + 'o'; } bool isCurWinning() const; bool hasWon() const; private: // Why char? Because it is easy to debug // Tic tac toe is NOT a complex game in terms of space and computation // requirement. // Why explicitly signed? To make flipMover work. signed char firstMover; signed char curMover; signed char board[3][3]; signed char status; int nMoves; }; void CTicTacToe::playNext(signed char move) { assert(nMoves < 9); // Bad input case assert(status == CONTINUE_); // Current status of game if( '1' <= move && move <= '9') { const int n = move - '1'; const int r = n / 3; const int c = n % 3; if( board[r][c] == '.' ) { play(r, c); } else { status = toupper( flipMover( curMover ) ); } } else { status = BAD_INPUT_; } } void CTicTacToe::play(int r, int c) { if(nMoves++ < 4) { board[r][c] = curMover; } else { board[r][c] = curMover; const bool curWon = hasWon(); if(curWon) { status = curMover; } else if(9 == nMoves) { status = DRAW_GAME_; } } curMover = flipMover( curMover ); } // //bool CTicTacToe::isCurWinning() const { // for(int i = 0; i < 3; ++i) { // int nC = 0, nD = 0; // for(int j = 0; j < 3; ++j) { // Check row#i // if(board[i][j] == '.') ++nD; // if(board[i][j] == curMover) ++nC; // } // if(2 == nC && 1 == nD) return true; // // nC = 0, nD = 0; // for(int j = 0; j < 3; ++j) { // Check col#i // if(board[j][i] == '.') ++nD; // if(board[j][i] == curMover) ++nC; // } // if(2 == nC && 1 == nD) return true; // } // // int nC = 0, nD = 0; // for(int j = 0; j < 3; ++j) { // Check Digonal // if(board[j][j] == '.') ++nD; // if(board[j][j] == curMover) ++nC; // } // if(2 == nC && 1 == nD) return true; // nC = 0, nD = 0; // for(int j = 0; j < 3; ++j) { // Check Digonal2 // if(board[j][2-j] == '.') ++nD; // if(board[j][2-j] == curMover) ++nC; // } // if(2 == nC && 1 == nD) return true; // return false; //} bool CTicTacToe::hasWon() const { for(int i = 0; i < 3; ++i) { int nC = 0; for(int j = 0; j < 3; ++j) { // Check row#i if(board[i][j] == curMover) ++nC; } if(3 == nC) return true; nC = 0; for(int j = 0; j < 3; ++j) { // Check col#i if(board[j][i] == curMover) ++nC; } if(3 == nC) return true; } int nC = 0; for(int j = 0; j < 3; ++j) { // Check Digonal if(board[j][j] == curMover) ++nC; } if(3 == nC) return true; nC = 0; for(int j = 0; j < 3; ++j) { // Check Digonal2 if(board[j][2-j] == curMover) ++nC; } if(3 == nC) return true; return false; } void play(const char *moves) { CTicTacToe game; int index = 0; do { //cerr << "Sending move " << moves[index] << endl; game.playNext( moves[index++] ); } while( game.isRemaining() ); cout << "Input: " << moves << endl; switch ( game.getStatus() ) { case 'O': case 'X': cout << "Foul : "; /* Fall-through */ case 'o': case 'x': cout << static_cast( tolower( game.getStatus() ) ) << " won." << endl; break; case 'D': cout << "Draw game." << endl; break; default: cerr << "DEBUG:: " << game.getStatus() << endl; cout << "Broken input" << endl; break; } cout << endl; } int main(int argc, char *const *argv) { for(int i = 1; i < argc; ++i) { play(argv[i]); } if(argc < 2) { cout << "Expecting:: x won." << endl; play("79538246"); cout << "Expecting:: x won." << endl; play("35497162193"); cout << "Expecting:: x won." << endl; play("61978543"); cout << "Expecting:: x won." << endl; play("254961323121"); cout << "Expecting:: x won." << endl; play("6134278187"); cout << "-------------------------------------------------" << endl; cout << "Expecting:: Foul : x won." << endl; play("4319581"); cout << "Expecting:: Foul : x won." << endl; play("9625663381"); cout << "Expecting:: Foul : x won." << endl; play("7975662"); cout << "Expecting:: Foul : x won." << endl; play("2368799597"); cout << "Expecting:: Foul : x won." << endl; play("18652368566"); cout << "-------------------------------------------------" << endl; cout << "Expecting:: o won." << endl; play("965715"); cout << "Expecting:: o won." << endl; play("38745796"); cout << "Expecting:: o won." << endl; play("371929"); cout << "Expecting:: o won." << endl; play("758698769"); cout << "Expecting:: o won." << endl; play("42683953"); cout << "-------------------------------------------------" << endl; cout << "Expecting:: Foul : o won." << endl; play("618843927"); cout << "Expecting:: Foul : o won." << endl; play("36535224"); cout << "Expecting:: Foul : o won." << endl; play("882973"); cout << "Expecting:: Foul : o won." << endl; play("653675681"); cout << "Expecting:: Foul : o won." << endl; play("9729934662"); cout << "-------------------------------------------------" << endl; cout << "Expecting:: Draw Game." << endl; play("972651483927"); cout << "Expecting:: Draw Game." << endl; play("5439126787"); cout << "Expecting:: Draw Game." << endl; play("142583697"); cout << "Expecting:: Draw Game." << endl; play("42198637563"); cout << "Expecting:: Draw Game." << endl; play("657391482"); } return 0; } // EOF ```
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 ``` ```Expecting:: x won. Input: 79538246 x won. Expecting:: x won. Input: 35497162193 x won. Expecting:: x won. Input: 61978543 x won. Expecting:: x won. Input: 254961323121 x won. Expecting:: x won. Input: 6134278187 x won. ------------------------------------------------- Expecting:: Foul : x won. Input: 4319581 Foul : x won. Expecting:: Foul : x won. Input: 9625663381 Foul : x won. Expecting:: Foul : x won. Input: 7975662 Foul : x won. Expecting:: Foul : x won. Input: 2368799597 Foul : x won. Expecting:: Foul : x won. Input: 18652368566 Foul : x won. ------------------------------------------------- Expecting:: o won. Input: 965715 o won. Expecting:: o won. Input: 38745796 o won. Expecting:: o won. Input: 371929 o won. Expecting:: o won. Input: 758698769 o won. Expecting:: o won. Input: 42683953 o won. ------------------------------------------------- Expecting:: Foul : o won. Input: 618843927 Foul : o won. Expecting:: Foul : o won. Input: 36535224 Foul : o won. Expecting:: Foul : o won. Input: 882973 Foul : o won. Expecting:: Foul : o won. Input: 653675681 Foul : o won. Expecting:: Foul : o won. Input: 9729934662 Foul : o won. ------------------------------------------------- Expecting:: Draw Game. Input: 972651483927 Draw game. Expecting:: Draw Game. Input: 5439126787 Draw game. Expecting:: Draw Game. Input: 142583697 Draw game. Expecting:: Draw Game. Input: 42198637563 Draw game. Expecting:: Draw Game. Input: 657391482 Draw game. ```