Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- char b[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
- int gameStatus;
- int boardSquare;
- int turn;
- int turnNumber;
- /*checks the status of the game and sets gameStatus accordingly (1=winner/gameover, 0=game ongoing)*/
- void
- status()
- {
- if((b[0]!=' ' && b[1]!=' ' && b[2]!=' '&& b[0]==b[1] && b[1]==b[2]),
- (b[3]!=' ' && b[4]!=' ' && b[5]!=' '&& b[3]==b[4] && b[4]==b[5]),
- (b[6]!=' ' && b[7]!=' ' && b[8]!=' '&& b[6]==b[7] && b[7]==b[8]),
- (b[0]!=' ' && b[3]!=' ' && b[6]!=' '&& b[0]==b[3] && b[3]==b[6]),
- (b[1]!=' ' && b[4]!=' ' && b[7]!=' '&& b[1]==b[4] && b[4]==b[7]),
- (b[2]!=' ' && b[5]!=' ' && b[8]!=' '&& b[2]==b[5] && b[5]==b[8]),
- (b[0]!=' ' && b[4]!=' ' && b[8]!=' '&& b[0]==b[4] && b[4]==b[8]),
- (b[2]!=' ' && b[4]!=' ' && b[6]!=' '&& b[2]==b[4] && b[4]==b[6])) {
- gameStatus++;
- }
- }
- /*clears the screen and prints the board.*/
- void
- board()
- {
- system("clear");
- printf(" %c | %c | %c \n-----------\n %c | %c | %c \n-----------\n %c | %c | %c \n",\
- b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8]);
- printf("\ngameStatus: %i\n", gameStatus);
- printf("boardSquare: %i\n", boardSquare);
- printf("turn: %i\n", turn);
- printf("turnNumber: %i\n", turnNumber);
- }
- /*takes an input from the player, checks who's turn it is,
- * and sets the corresponding square to the letter of who's turn it is*/
- void
- boardInput()
- {
- printf("\nSelect square: ");
- scanf("%i", &boardSquare);
- if(turn==0) {
- if(b[boardSquare]==' ') {
- b[boardSquare]='X';
- }
- } else {
- if(b[boardSquare]==' ') {
- b[boardSquare]='O';
- }
- }
- }
- /*Progresses the counter of turns and changes who's turn it is */
- void
- nextTurn()
- {
- if(b[boardSquare]=='X'){
- turn=1;
- turnNumber++;
- } else {
- turn=0;
- turnNumber++;
- }
- }
- int
- main(void)
- {
- while(1) {
- status();
- if(gameStatus==0 && turnNumber==8) {
- system("clear");
- printf("Tie!\n");
- break;
- }
- if(gameStatus==1) {
- system("clear");
- printf("Player wins!");
- break;
- }
- board();
- boardInput();
- nextTurn();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement