Tuesday 18 January 2011

C Four, Connect Four, miGame

I'm taking a break from miTanks for a while. Here's one of my first ever programs.

I spend alot of my time in lectures at University playing Connect Four on a piece of paper against friends. This is the best paper game we could come up with. Turn based, so could simply pass notes or signal which column to drop in. And there was no need for an eraser; imaging trying to play chess with a pen.

At the time we were learning C programming and had to come up with some simple program to prove we learnt something in the course. Alot of people went with a Facebook similar 'What animal are you?' kind of game. Boring!



It took about 2-3 hours. Remember this was my first C program I created purely by myself, aside from Visual Basic and MicroWorlds (if that even counts).

Code:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
//C(onnect) Four
//Created by: Milk
//Date of final build: 19/10/09
//Height of each line
#define LINE_HEIGHT 6
#define LINE_WIDTH 7
void DrawBoard(int grid[LINE_HEIGHT][LINE_WIDTH]);
int CalculateWin(int grid[LINE_HEIGHT][LINE_WIDTH], int player);
int ValidInput(int input, int grid[LINE_HEIGHT][LINE_WIDTH]);
int main(void)
{
 //Plays connect four
 int grid[LINE_HEIGHT][LINE_WIDTH] = {0};
 int gameOver = 0;
 int input = 0;
 int player = 1;
 char inputValid;
 int i;

 //Draw the intitial board (empty)
 DrawBoard(grid);
 while (gameOver == 0 && (grid[LINE_HEIGHT - 1][0] == 0 || grid[LINE_HEIGHT - 1][1] == 0 || grid[LINE_HEIGHT - 1][2] == 0 || grid[LINE_HEIGHT - 1][3] == 0 || grid[LINE_HEIGHT - 1][4] == 0 || grid[LINE_HEIGHT - 1][5] == 0 || grid[LINE_HEIGHT - 1][6] == 0)) {
  //Get input from user
  input = 0;
  printf("\nWhere would you like to go, player %d?\n", player);
  scanf("%d", &input);
  //Decide if input is a valid move
  inputValid = ValidInput(input, grid);
  //If input is not valid prompt for a valid move
  while (inputValid == 0) {
   printf("Please choose a valid position: ");
   scanf("%d", &input);
   inputValid = ValidInput(input, grid);
  }
  //Add input to grid
  for (i = 0; i < LINE_WIDTH; i++) {
   if (grid[i][input - 1] == 0) {
    grid[i][input - 1] = player;
    break;
   }
  }
  //Draw the board
  DrawBoard(grid);
  //Calculate if there is a win
  gameOver = CalculateWin(grid, player);
  //Set the next players turn
  if (player == 1) {
   player = 2;
  } else {
   player = 1;
  }
 }
 //Print winner
 if (gameOver == 0) {
  printf("\n\nThere are no more available moves!\nIt's a draw\n");
 } else {
  printf("\n\nCongratulations player %d!\nYou won\n", gameOver);
 }
 return 0;
}
void DrawBoard(int grid[LINE_HEIGHT][LINE_WIDTH])
{
 //Draws the board
 int i, j;
 printf("\n\n\n\n\n");
 for (i = LINE_HEIGHT - 1; i >= 0; i--) {
  printf("\nI");
  for (j = 0; j < LINE_WIDTH; j++) {
   if (grid[i][j] == 0) {
    printf("   I");
   } else if (grid[i][j] == 1) {
    printf(" X I");
   } else {
    printf(" O I");
   }
  }
 }
 printf("\n__1___2___3___4___5___6___7__");
 return;
}
int ValidInput(int input, int grid[LINE_HEIGHT][LINE_WIDTH])
{
 //Calculate if input is valid
 if (input < 0 || input > 8 || grid[LINE_HEIGHT - 1][input - 1] != 0) {
  return 0;
 } else {
  return 1;
 }
}
int CalculateWin(int grid[LINE_HEIGHT][LINE_WIDTH], int player)
{
 int i, j;
 //Calculate if there is a win on the horizontal
 for (j = 0; j < LINE_WIDTH - 3; j++) {
  for (i = 0; i < LINE_HEIGHT; i++) {
   if (grid[i][j] == grid[i][j + 1] && grid[i][j] == grid[i][j + 2] && grid[i][j] == grid[i][j + 3] && grid[i][j] != 0) {
    return player;
   }
  }
 }
 //Calculate if there is a vertical win
 for (i = 0; i < LINE_HEIGHT - 3; i++) {
  for (j = 0; j < LINE_WIDTH; j++) {
   if (grid[i][j] == grid[i + 1][j] && grid[i][j] == grid[i + 2][j] && grid[i][j] == grid[i + 3][j] && grid[i][j] != 0) {
    return player;
   }
  }
 }
 //Calculate if there is a diagonal up-right win
 for (i = 0; i < LINE_HEIGHT - 3; i++) {
  for (j = 0; j < LINE_WIDTH - 3; j++) {
   if (grid[i][j] == grid[i + 1][j + 1] && grid[i][j] == grid[i + 2][j + 2] && grid[i][j] == grid[i + 3][j + 3] && grid[i][j] != 0) {
    return player;
   }
  }
 }
 //Calculate if there is a diagonal up-left win
 for (i = 0; i < LINE_HEIGHT - 3; i++) {
  for (j = LINE_WIDTH - 1; j >= 0; j--) {
   if (grid[i][j] == grid[i + 1][j - 1] && grid[i][j] == grid[i + 2][j - 2] && grid[i][j] == grid[i + 3][j - 3] && grid[i][j] != 0) {
    return player;
   }
  }
 }
 //No win
 return 0;
}


When compiled the exe file is only 56.0KiB. So it's pretty much always on my USB drive now.
And here it is:
http://rapidshare.com/files/443141503/Connect4.txt
I don't think rapidshare likes exe files so once you download just change the file extension (txt) to exe, and it will work. Give it a go.

Looking back at this code there is not actually too many things I would do differently. Would've been a nice OO exercise though.

If anyone wants to give me a game just tell me your starting move. Let's play. Lol

Cheers,
Milk

16 comments:

  1. Cood code, might try it out later :) thanks

    ReplyDelete
  2. ahh, connect four. love it :)
    Well, good luck at UNI dude.

    ReplyDelete
  3. i'll start in column 4. :) pretty cool idea, and good code, thanks.

    ReplyDelete
  4. Your on Anonymouse. 3. I hope you check this post again. Haha

    ReplyDelete
  5. This is probably the coolest thing I've seen all day

    ReplyDelete
  6. I remember the first (and only) game I ever created. "Perfection" in VB. I couldn't get it to work.

    ReplyDelete
  7. i'll cover you in 3. and yeah i check blogs i follow at least once a day.

    ReplyDelete
  8. i'll cover your 4. Theres a subscribe by email link down below the comments. Not sure how that works, once a day might made this a very very long game. Haha

    ReplyDelete
  9. lol why play this game this way....

    ReplyDelete
  10. i would subscribe by email but i dont check the email account asociated with my blogger account.

    ReplyDelete
  11. That's Ok then. I'll go another 4. You left me a decent opening just then. Lol

    ReplyDelete
  12. 7, gee, i wonder where you'll go next...

    ReplyDelete