Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct Tile {
- int x, y;
- };
- struct Game {
- int size;
- char **board;
- Tile white;
- bool turn; // 0 - blue, 1 - green
- };
- bool isOutOfBounds(int x, int y, int size);
- bool checkWin(Game g);
- int main() {
- Game g;
- g.size = 5;
- g.turn = 0;
- g.white.x = g.white.y = g.size / 2;
- g.board = new char*[g.size];
- for (int i = 0; i < g.size; i++) {
- g.board[i] = new char[g.size];
- for (int j = 0; j < g.size; j++) {
- if (i % 2 == 0)
- g.board[i][j] = j % 2 ? 'B' : 'G';
- if (i % 2)
- g.board[i][j] = j % 2 ? 'G' : 'B';
- }
- }
- g.board[g.white.x][g.white.y] = 'W';
- for (int i = 0; i < g.size; i++) {
- for (int j = 0; j < g.size; j++) {
- printf("%c ", g.board[i][j]);
- }
- printf("\n");
- }
- printf("%d\n", checkWin(g));
- return 0;
- }
- bool isOutOfBounds(int x, int y, int size) {
- return x < 0 || x >= size || y < 0 || y >= size;
- }
- bool checkWin(Game g) {
- int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
- int l, c;
- char charToLookFor = g.turn ? 'G' : 'B';
- bool foundChar = false;
- for (int i = 0; i < 4; i++) {
- l = g.white.x + dx[i];
- c = g.white.y + dy[i];
- if (!isOutOfBounds(l, c, g.size) && g.board[l][c] == charToLookFor) {
- foundChar = true;
- }
- }
- if (!foundChar)
- return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment