1
井字得獎有由N個井字板的N,用2名選手。本場比賽是正常井字不同,因爲比賽只結束時,董事會完全填補,一個點是在一排一排一排授予三,二分數是四,三五年,和等等。 「連續」可以表示水平,垂直或對角線。給定一個完成董事會,確定由誰儘可能高效具有最高分的獲得者。上的N×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;
}