Skip to content

Commit 864041a

Browse files
committed
fix: range conditions
1 parent cf77d5a commit 864041a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/play/tic-tac-toe/AI/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void aiReadRows(AI *ai, Board *board, const int winThis, const int tieThis) {
4949
int result;
5050

5151
for (i = 0; i < boardRows; i++) {
52-
result = multiplyBoardRow(board, i, boardCols - 1);
52+
result = multiplyBoardRow(board, i, boardCols);
5353

5454
if (result == winThis) {
5555
ai->__winningMoves += 1;
@@ -71,7 +71,7 @@ void aiReadCols(AI *ai, Board *board, const int winThis, const int tieThis) {
7171
int result;
7272

7373
for (i = 0; i < boardCols; i++) {
74-
result = multiplyBoardCol(board, i, boardRows - 1);
74+
result = multiplyBoardCol(board, i, boardRows);
7575

7676
if (result == winThis) {
7777
ai->__winningMoves += 1;

src/play/tic-tac-toe/board/main.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ int multiplyBoardCol(const Board *board, const size_t targetCol, const size_t st
9191
if (stopAtRow < 0 || stopAtRow > board->__rows) return 0;
9292
if (targetCol < 0 || targetCol > board->__cols) return 0;
9393

94-
for (i = 0; i < stopAtRow; i++) result *= board->__array2D[i][targetCol];
94+
for (i = 0; i < stopAtRow; i++) {
95+
result *= board->__array2D[i][targetCol];
96+
};
9597

9698
return result;
9799
}
@@ -103,7 +105,9 @@ int multiplyBoardMainDiagonal(const Board *board) {
103105

104106
if (board->__rows != board->__cols) return 0;
105107

106-
for (i = 0; i < board->__rows; i++) result *= board->__array2D[i][i];
108+
for (i = 0; i < board->__rows; i++) {
109+
result *= board->__array2D[i][i];
110+
};
107111

108112
return result;
109113
}
@@ -116,7 +120,9 @@ int multiplyBoardRow(const Board *board, const size_t targetRow, const size_t st
116120
if (targetRow < 0 || targetRow > board->__rows) return 0;
117121
if (stopAtCol < 0 || stopAtCol > board->__cols) return 0;
118122

119-
for (i = 0; i < stopAtCol; i++) result *= board->__array2D[targetRow][i];
123+
for (i = 0; i < stopAtCol; i++) {
124+
result *= board->__array2D[targetRow][i];
125+
};
120126

121127
return result;
122128
}

0 commit comments

Comments
 (0)