你还在为了你的大作业发愁吗,你还在为了及格而努力吗?莫慌!爷替你们完成!!!!
#include <stdio.h>
#define SIZE 15
#define EMPTY 0
#define BLACK 1
#define WHITE 2
int board[SIZE][SIZE] = {0};
int currentPlayer = BLACK;
void drawBoard() {
int i;
for (i = 0; i < SIZE; i++) {
int j=0;
for ( j = 0; j < SIZE; j++) {
if (board[i][j] == EMPTY) {
printf(". ");
} else if (board[i][j] == BLACK) {
printf("X ");
} else if (board[i][j] == WHITE) {
printf("O ");
}
}
printf("\n");
}
printf("\n");
}
int isValidMove(int x, int y) {
return (x >= 0 && x < SIZE && y >= 0 && y < SIZE && board[x][y] == EMPTY);
}
int isWin(int player, int x, int y, int dx, int dy) {
int count = 0;
while (x >= 0 && x < SIZE && y >= 0 && y < SIZE && board[x][y] == player) {
count++;
x += dx;
y += dy;
}
return count >= 5;
}
int checkWin(int player) {
int i=0;
for ( i = 0; i < SIZE; i++) {
int j=0;
for ( j = 0; j < SIZE; j++) {
if (board[i][j] == player) {
if (isWin(player, i, j, 1, 0) || isWin(player, i, j, 0, 1) || isWin(player, i, j, 1, 1) || isWin(player, i, j, 1, -1)) {
return 1;
}
}
}
}
return 0;
}
int main() {
int x, y;
while (1) {
system("cls");
drawBoard();
printf("Player %c's turn (x, y): ", currentPlayer == BLACK ? 'X' : 'O');
scanf("%d %d", &x, &y);
if (isValidMove(x, y)) {
board[x][y] = currentPlayer;
if (checkWin(currentPlayer)) {
printf("Player %c wins!\n", currentPlayer == BLACK ? 'X' : 'O');
break;
}
currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK;
} else {
printf("Invalid move. Please try again.\n");
}
}
return 0;
}