Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- char** game;
- void print_game(void);
- int check_winner(void) {
- // check vertical
- for (int row = 6; row > 0; row--) {
- for (int col = 6; col > 0; col--) {
- if (game[row][col] == '1') {
- if (game[row-1][col] == '1') {
- if (game[row-2][col] == '1') {
- if (game[row-3][col] == '1') {
- return 1;
- }
- }
- }
- }
- if (game[row][col] == '2') {
- if (game[row-1][col] == '2') {
- if (game[row-2][col] == '2') {
- if (game[row-3][col] == '2') {
- return 2;
- }
- }
- }
- }
- }
- }
- // check horizontal
- for (int row = 6; row > 0; row--) {
- for (int col = 6; col > 0; col--) {
- if (game[row][col] == '1') {
- if (game[row][col-1] == '1') {
- if (game[row][col-2] == '1') {
- if (game[row][col-3] == '1') {
- return 1;
- }
- }
- }
- }
- if (game[row][col] == '2') {
- if (game[row][col-1] == '2') {
- if (game[row][col-2] == '2') {
- if (game[row][col-3] == '2') {
- return 2;
- }
- }
- }
- }
- }
- }
- // check diagonal
- for (int row = 6; row > 0; row--) {
- for (int col = 6; col > 0; col--) {
- if (game[row][col] == '1') {
- if (game[row-1][col-1] == '1') {
- if (game[row-2][col-2] == '1') {
- if (game[row-3][col-3] == '1') {
- return 1;
- }
- }
- }
- }
- if (game[row][col] == '2') {
- if (game[row-1][col-1] == '2') {
- if (game[row-2][col-2] == '2') {
- if (game[row-3][col-3] == '2') {
- return 2;
- }
- }
- }
- }
- }
- }
- return 0;
- }
- int make_move(int move, int turn) {
- for (int row = 0; row < 7; row++) {
- /* Theres already another counter in this column */
- if (game[row][move] == '1' || game[row][move] == '2') {
- /* Check its not the top row */
- if (row == 0) {
- printf("Column full, try again\n");
- return 0;
- /* Otherwise add it to the column */
- } else {
- if (!turn) {
- game[row-1][move] = '2';
- } else {
- game[row-1][move] = '1';
- }
- break;
- }
- /* Empty cell, but not the bottom row */
- } else if ((game[row][move] == ' ') && (row != 6)) {
- continue;
- /* Empty cell and also the bottom row */
- } else {
- if (!turn) {
- game[row][move] = '2';
- } else {
- game[row][move] = '1';
- }
- break;
- }
- }
- if (check_winner() == 1) {
- print_game();
- printf("Player 1 wins!\n");
- exit(0);
- } else if (check_winner() == 2){
- print_game();
- printf("Player 2 wins!\n");
- exit(0);
- } else {
- print_game();
- }
- return 1;
- }
- void play_game(void) {
- char buffer[256];
- int move;
- int turn = 0;
- while (1) {
- if (!turn) {
- printf("Player 1> ");
- turn = 1;
- } else {
- printf("Player 2> ");
- turn = 0;
- }
- fflush(stdout);
- if (fgets(buffer, 254, stdin) != NULL) {
- if (buffer[0] == '\n') {
- turn = !turn;
- continue;
- }
- if (isdigit(buffer[0]) && buffer[1] == '\n' && buffer[2] == '\0') {
- if (buffer[0] <= '6' && buffer[0] >= '0') {
- move = (int)strtol(buffer, NULL, 10);
- if (!make_move(move, turn)) {
- turn = !turn;
- }
- } else {
- printf("Invalid input\n");
- turn = !turn;
- }
- } else {
- printf("Invalid input\n");
- turn = !turn;
- }
- } else {
- if (feof(stdin)) {
- fprintf(stdout, "Player %d quit\n", turn);
- exit(1);
- }
- }
- }
- }
- void print_game(void) {
- printf("|0|1|2|3|4|5|6|\n");
- for (int i = 0; i < 7; i++) {
- printf("|%c|%c|%c|%c|%c|%c|%c|\n", game[i][0],
- game[i][1],
- game[i][2],
- game[i][3],
- game[i][4],
- game[i][5],
- game[i][6]);
- }
- printf("|-------------|\n");
- }
- void setup_game(void) {
- for (int i = 0; i < 7; i++) {
- for (int j = 0; j < 7; j++) {
- game[i][j] = ' ';
- }
- }
- }
- int main(int argc, const char * argv[]) {
- game = malloc(sizeof(char*) * 7);
- for (int i = 0; i < 7; i++) {
- game[i] = malloc(sizeof(char) * 7);
- }
- setup_game();
- print_game();
- play_game();
- for (int i = 0; i < 7; i++) {
- free(game[i]);
- }
- free(game);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement