Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define ROWS 5
- #define COLUMNS 5
- #define marker1 'X'
- #define marker2 '0'
- #define WINNER_1 1
- #define WINNER_2 2
- #define DRAW 3
- #define NO_WINNER 0
- void reset_board(char(*board)[COLUMNS]);
- void draw_board(char(*board)[COLUMNS]);
- //void user_position();
- int check_board_position(int i,int j,char (*board)[COLUMNS]);
- void random_setup(char (*board)[COLUMNS]);
- void humanvshuman(char (*board)[COLUMNS]);
- int check_winner(char (*board)[COLUMNS]);
- void humanvscomputer(int gamemode,char (*board)[COLUMNS]);
- //void check_computer_wins(char (*board)[COLUMNS]);
- //void check_human_wins(char (*board)[COLUMNS]);
- int main(void)
- {
- int game_mode, firstplayer;
- char myboard[ROWS][COLUMNS], player1, player2;
- printf("Select a game mode:\n");
- printf("1 - Human vs computer\n");
- printf("2 - Human vs human\n");
- fflush(stdin);
- scanf("%d", &game_mode);
- if (game_mode==1)
- {
- printf("Would you like to go: \n1 - First \n2 - Second\n");
- fflush(stdin);
- scanf("%d", &firstplayer);
- reset_board(myboard);
- draw_board(myboard);
- if(firstplayer == 1)
- {
- player1 = marker1;
- player2 = marker2;
- humanvscomputer(game_mode,myboard);
- }
- else
- {
- player1 = marker2;
- player2 = marker1;
- }
- }
- if(game_mode ==2)
- {
- do
- {
- humanvshuman(myboard);
- draw_board(myboard);
- check_winner(myboard);
- } while (check_winner(myboard) != -1);
- }
- // if (game_mode == 1)
- // {
- // humanvscomputer(game_mode,myboard);
- // draw_board(myboard);
- // check_winner(myboard);
- }
- void reset_board(char(*board)[COLUMNS])
- {
- int i,j;
- for(i=0; i<ROWS; i++)
- {
- for(j=0; j<COLUMNS; j++)
- {
- board[i][j]=' ';
- }
- }
- }
- void draw_board(char (*board)[COLUMNS])
- {
- int i, j;
- printf(" ");
- for(j=0; j<COLUMNS; j++)
- {
- printf(" %d ", j);
- }
- printf("\n");
- for(i=0; i<ROWS; i++)
- {
- printf(" ");
- for(j=0; j<COLUMNS; j++)
- {
- printf("+---");
- }
- printf("+\n");
- printf("%d ", i);
- for(j=0; j<COLUMNS; j++)
- {
- printf("| %c ", board[i][j]);
- }
- printf("|\n");
- }
- printf(" ");
- for(j=0; j<COLUMNS; j++)
- {
- printf("+---");
- }
- printf("+\n");
- }
- int check_board_position(int i,int j,char (*board)[COLUMNS])
- {
- int n;
- if(board[i][j] != ' ')
- {
- draw_board(board);
- printf("\nInvalid position %d, %d, %c \n", i, j, board[i][j]);
- //humanvshuman(board);
- n=0; // Indicates an invalid position
- }
- else{
- n=1;
- }
- return n;
- }
- void random_setup(char (*myboard)[COLUMNS])
- {
- reset_board(myboard);
- myboard[0][1] = 'X';
- myboard[1][1] = 'X';
- myboard[2][1] = 'X';
- myboard[3][1] = 'O';
- myboard[4][1] = 'O';
- }
- void humanvshuman (char (*board)[COLUMNS])
- {
- int a,b;
- printf("Player 1 enter which row you would like to go in: ");
- fflush(stdin);
- scanf("%d", &a);
- printf("Player 1 enter which column you would like to go in: ");
- fflush(stdin);
- scanf("%d", &b);
- board[a][b]='X';
- check_winner(board);
- int c,d;
- printf("\nPlayer 2 enter which row you would like to go in: ");
- fflush(stdin);
- scanf("%d", &c);
- printf("Player 2 enter which column you would like to go in: ");
- fflush(stdin);
- scanf("%d", &d);
- if (check_board_position(c,d,board))
- {
- board[c][d]='0';
- }
- check_board_position(c,d,board);
- check_winner(board);
- }
- int check_winner(char (*board)[COLUMNS])
- {
- int i, j, nof_markers_1, nof_markers_2, empty_board_positions;
- // Check rows
- for(i=0; i<ROWS; i++)
- {
- nof_markers_1 = nof_markers_2 = 0;
- for(j=0; j<COLUMNS; j++)
- {
- switch(board[i][j])
- {
- case marker1:
- nof_markers_1++;
- break;
- case marker2:
- nof_markers_2++;
- break;
- }
- }
- if(nof_markers_1 == COLUMNS)
- return WINNER_1;
- if(nof_markers_2 == COLUMNS)
- return WINNER_2;
- }
- // Check cols
- for(j=0; j<COLUMNS; j++)
- {
- nof_markers_1 = nof_markers_2 = 0;
- for(i=0; i<ROWS; i++)
- {
- switch(board[i][j])
- {
- case marker1:
- nof_markers_1++;
- break;
- case marker2:
- nof_markers_2++;
- break;
- }
- }
- if(nof_markers_1 == COLUMNS)
- return WINNER_1;
- if(nof_markers_2 == COLUMNS)
- return WINNER_2;
- }
- // Check main diagonal
- nof_markers_1 = nof_markers_2 = 0;
- for(j=0; j<COLUMNS; j++)
- {
- switch(board[j][j])
- {
- case marker1:
- nof_markers_1++;
- break;
- case marker2:
- nof_markers_2++;
- break;
- }
- }
- if(nof_markers_1 == COLUMNS)
- return WINNER_1;
- if(nof_markers_2 == COLUMNS)
- return WINNER_2;
- // Check the other diagonal
- nof_markers_1 = nof_markers_2 = 0;
- for(j=0; j<COLUMNS; j++)
- {
- switch(board[j][COLUMNS-1-j])
- {
- case marker1:
- nof_markers_1++;
- break;
- case marker2:
- nof_markers_2++;
- break;
- }
- }
- if(nof_markers_1 == COLUMNS)
- return WINNER_1;
- if(nof_markers_2 == COLUMNS)
- return WINNER_2;
- // Check if board is full
- empty_board_positions = 0;
- for(i=0; i<ROWS; i++)
- {
- for(j=0; j<COLUMNS; j++)
- {
- if(board[i][j] == ' ')
- {
- empty_board_positions++;
- }
- }
- }
- if(empty_board_positions == 0)
- return DRAW;
- else
- return NO_WINNER; // The game goes on
- }
- void humanvscomputer(int gamemode,char (*board)[COLUMNS])
- {
- int firstplayer;
- firstplayer=gamemode;
- if(firstplayer == 1)
- {
- int a,b;
- printf("Enter which row you would like to go in: ");
- fflush(stdin);
- scanf("%d", &a);
- printf("Enter which column you would like to go in: ");
- fflush(stdin);
- scanf("%d", &b);
- if (check_board_position(a, b, board))
- {
- board[a][b]='X';
- }
- }
- draw_board(board);
- // Computer's turn
- //check_computer_wins(myboard);
- // check_human_wins(myboard);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement