#include <iostream>
using namespace std;
#define BOARD_SIZE 15
void print_board(int board[][BOARD_SIZE]) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
bool is_move_valid(int board[][BOARD_SIZE], int x, int y) {
// check if move is within board bounds
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
return false;
}
// check if spot is already taken
if (board[x][y] != 0) {
return false;
}
return true;
}
int check_win(int board[][BOARD_SIZE], int x, int y) {
int player = board[x][y];
int consecutive = 0;
// check horizontal
for (int i = max(0, x - 4); i <= min(x + 4, BOARD_SIZE - 1); i++) {
if (board[i][y] == player) {
consecutive++;
if (consecutive >= 5) {
return player;
}
}
else {
consecutive = 0;
}
}
// check vertical
consecutive = 0;
for (int j = max(0, y - 4); j <= min(y + 4, BOARD_SIZE - 1); j++) {
if (board[x][j] == player) {
consecutive++;
if (consecutive >= 5) {
return player;
}
}
else {
consecutive = 0;
}
}
// check diagonal-1
consecutive = 0;
for (int i = -4; i <= 4; i++) {
int new_x = x + i;
int new_y = y + i;
if (new_x >= 0 && new_x < BOARD_SIZE && new_y >= 0 && new_y < BOARD_SIZE) {
if (board[new_x][new_y] == player) {
consecutive++;
if (consecutive >= 5) {
return player;
}
}
else {
consecutive = 0;
}
}
}
// check diagonal-2
consecutive = 0;
for (int i = -4; i <= 4; i++) {
int new_x = x + i;
int new_y = y - i;
if (new_x >= 0 && new_x < BOARD_SIZE && new_y >= 0 && new_y < BOARD_SIZE) {
if (board[new_x][new_y] == player) {
consecutive++;
if (consecutive >= 5) {
return player;
}
}
else {
consecutive = 0;
}
}
}
return 0;
}
int main() {
int board[BOARD_SIZE][BOARD_SIZE] = { 0 };
int turn = 1;
int winner = 0;
while (winner == 0) {
cout << "Turn " << turn << endl;
print_board(board);
int x, y;
do {
cout << "Player " << turn << ", enter x and y for your move:\n";
cin >> x >> y;
} while (!is_move_valid(board, x, y));
board[x][y] = turn;
winner = check_win(board, x, y);
turn = 3 - turn; // switch turns between 1 and 2
}
cout << "Player " << winner << " wins!" << endl;
return 0;
}