main() { reg char *sp; char *getenv(); int _putchar(), die(); srand(getpid()); /* initialize random sequence */ if (isatty(0)) { gettmode(); if (sp=getenv("TERM")) setterm(sp); signal(SIGINT, die); } else { printf("Need a terminal on %d\n", _tty_ch); exit(1); } _puts(TI); _puts(VS); noecho(); nonl(); tputs(CL, NLINES, _putchar); for (;;) { makeboard(); /* make the board setup */ puton('*'); /* put on '*'s */ puton(' '); /* cover up with ' 's */ } }
main() { srand(getpid()); /* initialize random sequence */ if (!initscr()) { fprintf(stderr, "Could not initialize libcurses.\n"); exit(1); } signal(SIGINT, die); noecho(); nonl(); leaveok(stdscr, TRUE); scrollok(stdscr, FALSE); for (;;) { makeboard(); /* make the board setup */ puton('*'); /* put on '*'s */ puton(' '); /* cover up with ' 's */ } }
void startsingleplayer(){ const int player=ORDER; clearscreen(); moveto(0,0); setbold(true); tprintf("Order & Chaos -- Single player"); setbold(false); Board *board=makeboard(); int curx=0,cury=0; bool aiturn=false; Move mv; int win=-1; while(true){ moveto(2,2); tprintboard(board); redraw(); if(win!=-1)break; if(aiturn){ aiturn=false; moveto(0,N+3); tprintf("Calculating..."); redraw(); mv=calcmove(board,!player); applymove(board,mv); win=checkwin(board); if(win!=-1)break; moveto(0,N+3); tprintf(" "); continue; } moveto(2+2*curx,2+cury); redraw(); int key=tgetkey(); int stone; switch(key){ case 'q': moveto(0,N+3); setbold(true); tprintf("Really quit? [y/N] "); setbold(false); redraw(); key=tgetkey(); if(key=='y'||key=='Y')return; moveto(0,N+3); tprintf(" "); break; case 'h': case KEY_LEFT: if(curx>0)curx--; else bel(); break; case 'j': case KEY_DOWN: if(cury<N-1)cury++; else bel(); break; case 'k': case KEY_UP: if(cury>0)cury--; else bel(); break; case 'l': case KEY_RIGHT: if(curx<N-1)curx++; else bel(); break; case 'x': stone=XX; if(false){ case 'o': stone=OO; } if(!isempty(board,N*cury+curx)){ bel(); break; } mv.pos=N*cury+curx; mv.stone=stone; applymove(board,mv); win=checkwin(board); if(win!=-1)break; aiturn=true; break; default: bel(); break; } } moveto(0,N+3); setbold(true); const char *plstr=win==ORDER?"Order":"Chaos"; if(win==player)tprintf("You (%s) won! Congratulations!",plstr); else tprintf("The AI (%s) won! Better next time...",plstr); setbold(false); redraw(); tgetkey(); }
int main() { int gen = 1; int x = 0; int y; int width = 0; int height = 0; int born = 0; int died = 0; printf("Enter the number of generations: "); scanf("%d", &gen); while (gen < 1) { printf("Please enter a valid generation number: "); scanf("%d", &gen); } printf("Enter the dimensions of the game board: "); scanf("%d %d", &width, &height); if (width > 80 || height > 100) { printf("Please enter new dimensions: "); scanf("%d %d", &width, &height); } //first create array char board[width][height]; //array created with given dimensions //populate gameboard with '-' using loop makeboard((char *)board, width, height); //Loop to grab coordinates while (x != -1) { printf("\nEnter coordinates: "); //can't be bigger than height and width of the board scanf("%d %d", &x, &y); //easier to implement and read when adding * in the main method if (x <= width && y <= height) board[x-1][y-1] = '*'; else printf("Out of bounds"); } printf("Initial State\n"); printboard((char *)board, width, height); //PART TWO - Life and Death //first find cells and use occ if they are occupied. //died counter goes up if parameters are matched //born goes up when every cell is analyzed and occ is 3 for any one of them int genIterator; for (genIterator = 1; genIterator <= gen; genIterator++) { printf("\nGeneration #%d", genIterator); for (x = 0; x < width; x++) { for (y = 0; y < height; y++ ) { if ((board[x][y] == '*') && (2 <= (occ((char*)board, width, height, x, y))) && ((occ((char*)board, width, height, x, y)) <= 3)) { //it gets to live } if ((board[x][y] == '-') && ((occ((char*)board, width, height, x, y)) == 3)) { board[x][y] = '^'; //an organism is marked as born born++; } if ((board[x][y] == '*') && ((2 > (occ((char*)board, width, height, x, y))) || ((occ((char*)board, width, height, x, y)) >= 4))) { //if occ is less than 2 board[x][y] = 'x'; //x,y is now marked for death died++; } } } //go back through and replace the dead and the born with their appropriate symbols for (y = 0; y < height; y++ ) { for (x = 0; x < width; x++) { if (board[x][y] == 'x') { board[x][y] = '-'; } else if(board[x][y] == '^') { board[x][y] = '*'; } } } printf("\nNumber born = %d", born); printf(" Number died = %d \n", died); printboard((char *)board, width, height); died = 0; born = 0; } return 0; }
/* * game: * Play one game up to glimit points. Actually, we only ASK the * player what card to turn. We do a random one, anyway. */ void game(void) { int i, j; bool flag; bool compcrib; makedeck(deck); shuffle(deck); if (gamecount == 0) { flag = TRUE; do { if (!rflag) { /* player cuts deck */ char *foo; /* This is silly, but we should parse user input * even if we're not actually going to use it. */ do { msg(quiet ? "Cut for crib? " : "Cut to see whose crib it is -- low card wins? "); foo = get_line(); if (*foo != '\0' && ((i = atoi(foo)) < 4 || i > 48)) msg("Invalid cut"); else *foo = '\0'; } while (*foo != '\0'); } i = arc4random_uniform(CARDS); /* random cut */ do { /* comp cuts deck */ j = arc4random_uniform(CARDS); } while (j == i); addmsg(quiet ? "You cut " : "You cut the "); msgcard(deck[i], FALSE); endmsg(); addmsg(quiet ? "I cut " : "I cut the "); msgcard(deck[j], FALSE); endmsg(); flag = (deck[i].rank == deck[j].rank); if (flag) { msg(quiet ? "We tied..." : "We tied and have to try again..."); shuffle(deck); continue; } else compcrib = (deck[i].rank > deck[j].rank); } while (flag); do_wait(); clear(); makeboard(); refresh(); } else { makeboard(); refresh(); werase(Tablewin); wrefresh(Tablewin); werase(Compwin); wrefresh(Compwin); msg("Loser (%s) gets first crib", (iwon ? "you" : "me")); compcrib = !iwon; } pscore = cscore = 0; flag = TRUE; do { shuffle(deck); flag = !playhand(compcrib); compcrib = !compcrib; } while (flag); ++gamecount; if (cscore < pscore) { if (glimit - cscore > 60) { msg("YOU DOUBLE SKUNKED ME!"); pgames += 4; } else if (glimit - cscore > 30) { msg("YOU SKUNKED ME!"); pgames += 2; } else { msg("YOU WON!"); ++pgames; } iwon = FALSE; } else { if (glimit - pscore > 60) { msg("I DOUBLE SKUNKED YOU!"); cgames += 4; } else if (glimit - pscore > 30) { msg("I SKUNKED YOU!"); cgames += 2; } else { msg("I WON!"); ++cgames; } iwon = TRUE; } gamescore(); }