0% found this document useful (0 votes)
4 views2 pages

Graph

Document
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Graph

Document
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#define SIZE 11

typedef enum { EMPTY, BLUE, RED } Player;

typedef struct {
Player board[SIZE][SIZE];
} HexBoard;

void initializeBoard(HexBoard *board) {


for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
board->board[i][j] = EMPTY;
}
void displayBoard(HexBoard *board) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < i; j++) printf(" "); // Indent for hex shape
for (int j = 0; j < SIZE; j++) {
char symbol = '.';
if (board->board[i][j] == BLUE) symbol = 'B';
else if (board->board[i][j] == RED) symbol = 'R';
printf("%c ", symbol);
}
printf("\n");
}
}
int isValidMove(HexBoard *board, int row, int col) {
return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board->board[row]
[col] == EMPTY;
}

void makeMove(HexBoard *board, int row, int col, Player player) {


if (isValidMove(board, row, col)) {
board->board[row][col] = player;
} else {
printf("Invalid move. Try again.\n");
}
}
int checkWin(HexBoard *board, Player player) {
// Use BFS/DFS to check if the player has connected their sides
// This is a placeholder. Full implementation requires graph traversal.
return 0;
}
#include <stdlib.h>
#include <time.h>

void computerMove(HexBoard *board, Player aiPlayer) {


srand(time(NULL));
int row, col;
do {
row = rand() % SIZE;
col = rand() % SIZE;
} while (!isValidMove(board, row, col));
makeMove(board, row, col, aiPlayer);
}
int main() {
HexBoard board;
initializeBoard(&board);

Player currentPlayer = BLUE;


int row, col;

while (1) {
displayBoard(&board);

if (currentPlayer == BLUE) {
printf("Blue's turn. Enter row and column: ");
scanf("%d %d", &row, &col);
if (isValidMove(&board, row, col)) {
makeMove(&board, row, col, BLUE);
if (checkWin(&board, BLUE)) {
printf("Blue wins!\n");
break;
}
currentPlayer = RED;
} else {
printf("Invalid move. Try again.\n");
}
} else {
printf("Red's turn (AI).\n");
computerMove(&board, RED);
if (checkWin(&board, RED)) {
printf("Red wins!\n");
break;
}
currentPlayer = BLUE;
}
}

displayBoard(&board);
return 0;
}

You might also like