int main() { int matchResult = 0; int **board = createBoard(); printBoard(board); char *currentPlayer = PLAYER_ONE; while(matchResult == 0) { int move[2]; getPlayerMove(board, currentPlayer, move); matchResult = evalBoard(board); if (currentPlayer == PLAYER_ONE) { currentPlayer = PLAYER_TWO; } else { currentPlayer = PLAYER_ONE; } } if (matchResult == PLAYER_ONE_VAL) { printf("Player %s WON!\n", PLAYER_ONE); } if (matchResult == PLAYER_TWO_VAL) { printf("Player %s WON!\n", PLAYER_TWO); } if (matchResult == DRAW) { printf("DRAW!\n"); } // clean up the memory freeBoard(board); return 0; }
int main(int argc, char ** argv) { if (argc != 4) { fprintf(stderr,"Usage: minesweeper width height numMines\n"); return EXIT_FAILURE; } int width = atoi(argv[1]); int height = atoi(argv[2]); int numMines = atoi(argv[3]); if (width <= 0 || height <= 0 || numMines <= 0) { fprintf(stderr, "Width, height, and numMines must all be positive ints\n"); return EXIT_FAILURE; } char * line = NULL; size_t linesz = 0; do { board_t * b = makeBoard (width, height, numMines); int gameOver = 0; while (!gameOver) { gameOver = playTurn(b, &line, &linesz); } freeBoard(b); do { printf("Do you want to play again?\n"); } while(getline(&line, &linesz, stdin) == -1); } while(line[0] == 'Y' || line[0] == 'y'); free(line); return EXIT_SUCCESS; }
int calcDistance(int xPos1, int yPos1, int xPos2, int yPos2, World* world) { char** tempBoard = copyBoard(world->gameBoard); int distanceToDest = UNREACHABLE_DISTANCE; // unreachable int isNodeValid = 0; // set the cheese square as a wall in the temp board, because the cat can't move through cheese tempBoard[world->cheeseYPos][world->cheeseXPos] = WALL_SQUARE; Node* node = NULL; Node* node1 = (Node*)malloc(sizeof(Node)); if (node1 == NULL) { perror("ERROR: malloc failed\n"); exit(1); } node1->xPos = xPos1; node1->yPos = yPos1; node1->distance = 0; int isFoundDest = 0; ListRef queue = newList(node1); ListRef queueHead = queue; while (queue != NULL && !isEmpty(queue) && !isFoundDest) { node = (Node*)headData(queue); tempBoard[node->yPos][node->xPos] = WALL_SQUARE; Direction direction; for (direction = 0; direction < NUM_DIRECTIONS; direction++) { Node* movedNode = (Node*)malloc(sizeof(Node)); if (movedNode == NULL) { perror("ERROR: malloc failed\n"); exit(1); } movedNode->xPos = node->xPos; movedNode->yPos = node->yPos; movedNode->distance = node->distance; isNodeValid = moveNode(node->xPos, node->yPos, tempBoard, direction, movedNode); if (isNodeValid == 1) { if (isClose(movedNode->xPos,movedNode->yPos, xPos2, yPos2)) { distanceToDest = movedNode->distance; isFoundDest = 1; free(movedNode); break; } tempBoard[movedNode->yPos][movedNode->xPos] = WALL_SQUARE; append(queue, movedNode); } } queue = tail(queue); } freeBoard(tempBoard); destroyList(queueHead, free); return distanceToDest; }
int main(int argc, char* argv[]) { int width, height; int** board; FILE* file; struct Queue* changeQueue; if (argc!=2) { printf("the right usage is: life <board file>\n"); return WRONG_ARGUMENTS; } /*open board file*/ file = fopen(argv[1], "r"); if (file==NULL) { printf("error in openning the board file\n"); return FILE_OPEN_ERROR; } board = readBoard(file, &width, &height); /*close board file*/ if (fclose(file) != 0) { free(board); return FILE_CLOSE_ERROR; printf("error in closing the board file\n"); } if (board==NULL) { return FILE_FORMAT_ERROR; } changeQueue = newQueue(); /*print initial state*/ system("cls");/*clear the screen*/ printBoard(board, width, height); Sleep(2000); /*game math*/ while (1) { calcChange(board, width, height, changeQueue); flip(board,changeQueue); system("cls");/*clear the screen*/ printBoard(board, width, height); Sleep(1500); } /*doesn't really get here because it's an infinte loop*/ deleteQueue(changeQueue); freeBoard(board,height); return 0; }
void freeStack(stack *s) { board *next, *b = s->top; while(b) { next = b->next; freeBoard(b); b = next; } free(s); }
void Play(tGame * game) { int legal; tScan scan; tCommand command; command.undo.can_undo = FALSE; command.undo.undo_error = FALSE; command.undo.lastboard.rows = game->visualboard.rows; command.undo.lastboard.columns = game->visualboard.columns; CreateBoard(&command.undo.lastboard);; do { PrintAll(game, &command); do { if ((legal = InputCommand(&scan))) { if((legal = LegalCommand(&scan, &command))) if (command.command_ref < 5) /*All commands but quit or undo*/ legal = LegalParams(game, &command, &scan); } if (!legal) printf("%s%s%s\n", KERR, COMMAND_ERR, KDEF); } while (!legal); ExecCommand(game, &command); CheckGameState(game); } while(game->gamestate == GAMESTATE_DEFAULT); PrintResult(game); freeBoard(game->hiddenboard.board, game->hiddenboard.rows); freeBoard(game->visualboard.board, game->hiddenboard.rows); return; }
/* *Basic: adds unqiue boards to linear linked list queue */ void generateUniqueBoardWithMove(BoardNode currentBoard, int rowMove, int colMove, int currRow, int currCol) { //!Create a blank board, copy parent board to it, make the move, check if it //!is unique, add it to queue BoardNode generatedBoard; if((generatedBoard = compBoardWithList(makeMove(copyParentToChild(currentBoard,createBoard(currentBoard)),rowMove,colMove,currRow,currCol,DELETE))) != NULL) { addToQueue(generatedBoard); checkTarget(generatedBoard); } else { freeBoard(generatedBoard); } }
int main(void) { char **board = createBoard(); /* Begin main game */ coord currentPos; char currentPlayer = crossChar; int i; for(i=0; gameOver(board) == FALSE; i++) { if(i == 0) { printBoard(board, stdout); printf("It's %c's turn.\nChoose co-ordinates: ", currentPlayer); } else { puts("Incorrect input, please try again"); } currentPos = getInput(stdin); if(currentPos.row == -1) continue; if( placeTurn(board, currentPos, currentPlayer) == FALSE ) continue; if(currentPlayer == crossChar) currentPlayer = naughtChar; else currentPlayer = crossChar; i = -1; } if(currentPlayer == crossChar) currentPlayer = naughtChar; else currentPlayer = crossChar; if(gameOver(board) == win) printf("Game over. %c wins.\n", currentPlayer); else puts("Game over. Draw.\n"); printBoard(board, stdout); freeBoard(board); return EXIT_SUCCESS; }
int main(int argc, char **argv){ #ifdef DEBUG DEBUG_FILE = fopen("debug.txt", "w"); #endif initscr(); keypad(stdscr, TRUE); noecho(); clear(); start_color(); init_pair(COL_BLUE, COLOR_BLUE, COLOR_BLACK); init_pair(COL_YELLOW, COLOR_YELLOW, COLOR_BLACK); init_pair(COL_RED, COLOR_RED, COLOR_BLACK); init_pair(COL_WHITE, COLOR_WHITE, COLOR_BLACK); init_pair(COL_GREEN, COLOR_GREEN, COLOR_BLACK); init_pair(COL_CYAN, COLOR_CYAN, COLOR_BLACK); WINDOWMAX_Y = getmaxy(stdscr); WINDOWMAX_X = getmaxx(stdscr); INPUTMODE = INM_NORMAL; dprintf("Window size is %ix%i\n", WINDOWMAX_X, WINDOWMAX_Y); USECOLOR = 1; GCRUNFREQ = 50; CONSIZE = WINDOWMAX_Y - CHUNKSIZE - 5; const char *PATTERN = NULL; const char *BOARDNAME = NULL; int getoptval; while((getoptval = getopt(argc, argv, "cg:p:n:s:")) != -1){ switch(getoptval) { //Disable color case 'c': USECOLOR = 0; break; case 'p': PATTERN = optarg; break; case 'n': BOARDNAME = optarg; break; case 'g': GCRUNFREQ = atoi(optarg); break; case 's': CONSIZE = atoi(optarg); break; } } CONSIZE = clamp(CONSIZE, 5, 15); consoleBuffer = calloc(CONSIZE * GSTRINGSIZE, 1); cprintf("Welcome to the game of life! Press space/zxc to begin!"); cprintf("Use arrows/wasd to move. Press q to quit."); struct board *b; if(PATTERN){ b = readNewBoard(PATTERN); if(BOARDNAME){ setBoardName(b, BOARDNAME); } }else{ b = createBoard(BOARDNAME); initializeBoard(b); } //START TESTS /*#define on(x, y) curChunk(b)->board[at(x, y)] = 1*/ /*on(1, 1);*/ /*on(2, 2);*/ /*on(3, 2);*/ /*on(2, 3);*/ /*on(3, 1);*/ /*goto CLEANUP;*/ //END TESTS while(1){ erase(); drawBoard(b); inputRenderer(); consoleRenderer(); refresh(); if(!input(b)) goto CLEANUP; } CLEANUP: #ifdef DEBUG fclose(DEBUG_FILE); #endif free(consoleBuffer); freeBoard(b); endwin(); printf("Program complete"); return 0; }