《Cracking the Coding Interview》——第8章:面向对象设计——题目8

2014-04-23 23:49

题目:有个棋牌游戏叫Othello,也叫Reversi。请看游戏规则。中文应该叫黑白棋吧,不常玩儿就是了。

解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的程序来模拟玩游戏的过程。

代码:

  1 // 8.8 Othello game, the rule is balabala. Try to design a class to play the game.

  2 // Here are the rules:

  3 //    1. two players, one black, one white, play alternatively, black first.

  4 //    2. the board is 8x8, two blacks and two whites are placed in the center first.

  5 // 00000000

  6 // 00000000

  7 // 00000000

  8 // 000bw000

  9 // 000wb000

 10 // 00000000

 11 // 00000000

 12 // 00000000

 13 //    3. every time, a player has to place a piece on an empty position, if the eight directions have the player's pieces, then all the opponent's pieces trapped between those pieces and the new piece is flipped to the current player.

 14 //    4. for every move, the player must at least flip one of the opponent's pieces.

 15 //    5. if a player is unable to make a move, the game ends. Whoever has more pieces on the board wins.

 16 #include <cstdio>

 17 #include <vector>

 18 using namespace std;

 19 

 20 class OthelloGame {

 21 public:

 22     OthelloGame() {

 23         board.resize(8);

 24         possible_position.resize(8);

 25         int i, j;

 26         for (i = 0; i < 8; ++i) {

 27             board[i].resize(8);

 28             possible_position[i].resize(8);

 29         }

 30         for (i = 0; i < 8; ++i) {

 31             for (j = 0; j < 8; ++j) {

 32                 board[i][j] = 0;

 33             }

 34         }

 35         board[3][3] = board[4][4] = 1;

 36         board[3][4] = board[4][3] = 2;

 37         winner = 0;

 38         

 39         dd[0][0] = -1;

 40         dd[0][1] = -1;

 41         dd[1][0] = -1;

 42         dd[1][1] = 0;

 43         dd[2][0] = -1;

 44         dd[2][1] = +1;

 45         dd[3][0] = 0;

 46         dd[3][1] = -1;

 47         dd[4][0] = 0;

 48         dd[4][1] = +1;

 49         dd[5][0] = +1;

 50         dd[5][1] = -1;

 51         dd[6][0] = +1;

 52         dd[6][1] = 0;

 53         dd[7][0] = +1;

 54         dd[7][1] = +1;

 55     };

 56     

 57     bool playGame() {

 58         int i, j;

 59         int x, y;

 60         int current_player;

 61         int pc[2];

 62         

 63         current_player = 1;

 64         while (!winner) {

 65             if (!checkPositions(current_player)) {

 66                 pc[0] = pc[1] = 0;

 67                 for (i = 0; i < 8; ++i) {

 68                     for (j = 0; j < 8; ++j) {

 69                         if (board[i][j]) {

 70                             ++pc[board[i][j] - 1];

 71                         }

 72                     }

 73                 }

 74                 winner = (pc[0] > pc[1] ? 1 : pc[0] < pc[1] ? 2 : 0);

 75                 break;

 76             }

 77             while (true) {

 78                 printBoard();

 79                 printf("Player %d please move: ", current_player);

 80                 scanf("%d%d", &x, &y);

 81                 if (inBound(x, y) && possible_position[x][y]) {

 82                     setPiece(x, y, current_player);

 83                     current_player = (current_player == 1) ? 2 : 1;

 84                     break;

 85                 } else {

 86                     printf("Invalid move.\n");

 87                 }

 88             }

 89         }

 90         return 0;

 91     };

 92     

 93     ~OthelloGame() {

 94         int i;

 95         

 96         for (i = 0; i < 8; ++i) {

 97             board[i].clear();

 98             possible_position[i].clear();

 99         }

100         board.clear();

101         possible_position.clear();

102     }

103 private:

104     // 1 for player one, 2 for player two, 0 for empty.

105     vector<vector<int> > board;

106     vector<vector<int> > possible_position;

107     int dd[8][2];

108     int winner;

109     

110     void printBoard() {

111         int i, j;

112         putchar(' ');

113         putchar(' ');

114         for (i = 0; i < 8; ++i) {

115             putchar('0' + i);

116             putchar(' ');

117         }

118         puts("                  \n");

119         for (i = 0; i < 8; ++i) {

120             putchar('0' + i);

121             putchar(' ');

122             for (j = 0; j < 8; ++j) {

123                 putchar('0' + board[i][j]);

124                 putchar(' ');

125             }

126             putchar('\n');

127         }

128     };

129     

130     bool inBound(int x, int y) {

131         return x >= 0 && x <= 7 && y >= 0 && y <= 7;

132     };

133     

134     bool checkPositions(int current_player) {

135         int i, j, k;

136         int x, y;

137         int len;

138         int count;

139         

140         for (i = 0; i < 8; ++i) {

141             for (j = 0; j < 8; ++j) {

142                 possible_position[i][j] = 0;

143             }

144         }

145         count = 0;

146         

147         for (i = 0; i < 8; ++i) {

148             for (j = 0; j < 8; ++j) {

149                 if (board[i][j]) {

150                     continue;

151                 }

152                 for (k = 0; k < 8; ++k) {

153                     len = 1;

154                     while (true) {

155                         x = i + len * dd[k][0];

156                         y = j + len * dd[k][1];

157                         if (!inBound(x, y)) {

158                             break;

159                         }

160                         if (board[x][y] == 0) {

161                             break;

162                         } else if (board[x][y] == current_player) {

163                             if (len > 1) {

164                                 possible_position[i][j] = 1;

165                                 ++count;

166                             }

167                             break;

168                         } else {

169                             ++len;

170                         }

171                     }

172                     if (possible_position[i][j]) {

173                         break;

174                     }

175                 }

176             }

177         }

178         

179         return count > 0;

180     };

181     

182     void setPiece(int x, int y, int current_player) {

183         int xx, yy;

184         int k;

185         int len;

186         

187         board[x][y] = current_player;

188         for (k = 0; k < 8; ++k) {

189             len = 1;

190             while (true) {

191                 xx = x + len * dd[k][0];

192                 yy = y + len * dd[k][1];

193                 if (!inBound(x, y)) {

194                     break;

195                 }

196                 if (board[xx][yy] == 0) {

197                     break;

198                 } else if (board[xx][yy] == current_player) {

199                     while (--len > 0) {

200                         xx = x + len * dd[k][0];

201                         yy = y + len * dd[k][1];

202                         board[xx][yy] = current_player;

203                     }

204                 } else {

205                     ++len;

206                 }

207             }

208         }

209     };

210 };

211 

212 int main()

213 {

214     OthelloGame game;

215     

216     game.playGame();

217     

218     return 0;

219 }

 

你可能感兴趣的:(interview)