有一个N by N tic tac toe board,有2名玩家。这个游戏与普通的tic tac toe不同,因为游戏只在棋盘完全填满时结束。连续三个获得一分,连续四个获得两分,连续五个获得三分,等等。 "连续"可以指水平,垂直或对角线。给定一个完成的董事会,尽可能有效地确定获胜者得分最多的赢家。
这是我完成的代码。我想知道是否有更好的方法来做到这一点。只需更改“N' N'到顶部的全局变量中的数字,您应该可以使用任何板尺寸运行它。
const int boardSize = N;
//used for calculating all points except for diagonals with a negative slope
void counter(vector<<vector>> &ticTacToe, int &score1, int &score2, int wid, bool horz, bool diag) {
int count = 0, hei = 0;
if (horz != diag)////allows us to traverse the board vertically instead of horizontally
swap(wid, hei);
while (hei < boardSize && wid < boardSize) {
while (hei < boardSize && wid < boardSize && ticTacToe[wid][hei]) {
count++;
if (horz) {
wid++;
if (diag)//if traversing diagonally, need to increment both width & height
hei++;
}
else {
hei++;
if (diag)//if traversing diagonally, need to increment both width & height
wid++;
}
}
if (count > 2) {
score1 += (count - 2);
}
count = 0;
//only difference from above is ticTacToe[wid][hei]
while (hei < boardSize && wid < boardSize && !ticTacToe[wid][hei]) {
count++;
if (horz) {
wid++;
if (diag)
hei++;
}
else {
hei++;
if (diag)
wid++;
}
}
if (count > 2) {
score2 += (count - 2);
}
count = 0;
}
if (horz !& diag) { //should change?
swap(wid, hei);
}
}
//used for calculating diagonal points with a diagonal slope
void negSlopeDiagonal(vector<vector<int>> &ticTacToe, int &score1, int &score2, int wid, bool horz) {
int count = 0, hei = boardSize - 1;
if (!horz) {
hei -= wid;
wid = 0;
}
while (wid < boardSize && hei >= 0) {
while (wid < boardSize && hei >= 0 && ticTacToe[wid][hei]) {
count++;
hei--;
wid++;
}
if (count > 2) {
score1 += (count - 2);
}
count = 0;
while (wid < boardSize && hei >= 0 && !ticTacToe[wid][hei]) {
count++;
hei--;
wid++;
}
if (count > 2) {
score_P2 += (count - 2);
}
count = 0;
}
}
bool winner(vector<vector<int>> ticTacToe) {
int score_P1 = 0, score_P2 = 0;
//count points vertically
for (int wid = 0; wid < boardSize; wid++) {
counter(ticTacToe, score_P1, score_P2, wid, false, false);
}
//count points horizontally
for (int wid = 0; wid < boardSize; wid++) {
counter(ticTacToe, score_P1, score_P2, wid, true, false);
}
//counts diagonal points sloping up going vertical
for (int wid = 0; wid < boardSize; wid++) {
counter(ticTacToe, score_P1, score_P2, wid, false, true);
}
//counts diagonal points sloping up going horizontal
for (int wid = 1; wid < boardSize; wid++) {
counter(ticTacToe, score_P1, score_P2, wid, true, true);
}
//counts diagonal points sloping down going horizonatal
for (int wid = 0; wid < boardSize; wid++) {
negSlopeDiagonal(ticTacToe, score_P1, score_P2, wid, true);
}
//counts diagonal points sloping down going vertical
for (int wid = 1; wid < boardSize; wid++) {
negSlopeDiagonal(ticTacToe, score_P1, score_P2, wid, false);
}
cout << "P1: " << score_P1 << endl << "P2: " << score_P2 << endl;
return (score_P1 > score_P2);
}
void printBoard(const vector<vector<int>> gameboard) {
assert(gameboard.size() == boardSize && gameboard[0].size() == boardSize);
for (int j = boardSize - 1; j >= 0; j--) {
for (int i = 0; i < boardSize; i++) {
cout << gameboard[i][j] << " ";
}
cout << endl;
}
}
int main() {
vector<int> empt; //empty vector for initialization
vector<vector<int>> board(boardSize, empt);
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
board[i].push_back((bool)(rand() % 2));
}
}
printBoard(board);
if(winner(board)) {
cout << "P1 wins!\n";
}
else {
cout << "P2 wins!\n";
}
return 0;
}
答案 0 :(得分:2)
您可以使用方向矢量来遍历电路板。例如:
text.setKeyListener((KeyListener) textView.getTag());
通过这种方式,您可以轻松处理一个或两个循环中的所有情况。