NO

Author Topic: Connect 4  (Read 2544 times)

Brwnman

  • Guest
Connect 4
« on: December 02, 2012, 09:07:51 PM »
Hey can some one help me I'm trying to make my connect 4 game board and I want the tokens to drop down each row but I dont know how plaease help.

Code: [Select]
#include <stdio.h>

void main()
{
  int i = 0;                                   /* Loop counter                         */
  int player = 0;                              /* Player number - 1 or 2               */
  int go = 0;                                  /* Square selection number for turn     */
  int row = 0;                                 /* Row index for a square               */ 
  int column = 0;                              /* Column index for a square            */
  int line = 0;                                /* Row or column index in checking loop */
  int winner = 0;                              /* The winning player                   */
  char board[6][7] =
{                        /* The board                            */
                      {'1','2','3','4','5','6','7'},          /* Initial values are reference numbers */
                      {'1','2','3','4','5','6','7'},          /* used to select a vacant square for   */
                    {'1','2','3','4','5','6','7'},            /* a turn.                              */
{'1','2','3','4','5','6','7'},
{'1','2','3','4','5','6','7'},
{'1','2','3','4','5','6','7'},

      };

   /* The main game loop. The game continues for up to 42 turns */
   /* As long as there is no winner                            */
   for( i = 0; i<42 && winner==0; i++)
   {
      /* Display the board */
    printf("\n\n");
      printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6]);
      printf("|---+---+---+---+---+---+---|\n");
      printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6]);
      printf("|---+---+---+---+---+---+---|\n");
  printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6]);
printf("|---+---+---+---+---+---+---|\n");
printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6]);
printf("|---+---+---+---+---+---+---|\n");
printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6]);
printf("|---+---+---+---+---+---+---|\n");
printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6]);
      player = i%2 + 1;                           /* Select player */
 
      /* Get valid player square selection */
      do
      {
        printf("\nPlayer %d, please enter the number of the square "
        "where you want to place your %c: ", player,(player==1)?'X':'O');
          scanf("%d", &go);

        row = --go/1;                                 /* Get row index of square      */
        column = go%\1;                                /* Get column index of square   */
      }
while(go<0 || go>42 || board[row][column]>'7');


      board[row][column] = (player == 1) ? 'X' : 'O';        /* Insert player symbol   */

      /* Check for a winning line - diagonals first */     
      if((board[5][0] == board[4][1] && board[5][0] == board[3][2] && board[5][0] == board[2][3]) ||
         (board[5][1] == board[4][2] && board[5][1] == board[3][3] && board[5][1] == board[2][4]) ||
(board[5][2] == board[4][3] && board[5][2] == board[3][4] && board[5][2] == board[2][5]) ||
(board[5][3] == board[4][4] && board[5][3] == board[3][5] && board[5][3] == board[2][6]) ||
(board[5][3] == board[4][2] && board[5][3] == board[3][1] && board[5][3] == board[2][0]) ||
(board[5][4] == board[4][3] && board[5][4] == board[3][2] && board[5][4] == board[2][1]) ||
(board[5][5] == board[4][4] && board[5][5] == board[3][3] && board[5][5] == board[2][2]) ||
(board[5][6] == board[4][5] && board[5][6] == board[3][4] && board[5][6] == board[2][3]) ||
(board[4][0] == board[3][1] && board[4][0] == board[2][2] && board[4][0] == board[1][3]) ||
(board[4][1] == board[3][2] && board[4][1] == board[2][3] && board[4][1] == board[1][4]) ||
(board[4][2] == board[3][3] && board[4][2] == board[2][4] && board[4][2] == board[1][5]) ||
(board[4][3] == board[3][4] && board[4][3] == board[2][5] && board[4][3] == board[1][6]) ||
(board[4][3] == board[3][2] && board[4][3] == board[2][1] && board[4][3] == board[1][0]) ||
(board[4][4] == board[3][3] && board[4][4] == board[2][2] && board[4][4] == board[1][1]) ||
(board[4][5] == board[3][4] && board[4][5] == board[2][3] && board[4][5] == board[1][2]) ||
(board[4][6] == board[3][5] && board[4][6] == board[2][4] && board[4][6] == board[1][3]) ||
(board[3][0] == board[2][1] && board[3][0] == board[1][2] && board[3][0] == board[0][3]) ||
(board[3][1] == board[2][2] && board[3][1] == board[1][3] && board[3][1] == board[0][4]) ||
(board[3][2] == board[2][3] && board[3][2] == board[1][4] && board[3][2] == board[0][5]) ||
(board[3][3] == board[2][4] && board[3][3] == board[1][5] && board[3][3] == board[0][6]) ||
(board[3][3] == board[2][2] && board[3][3] == board[1][1] && board[3][3] == board[0][0]) ||
(board[3][4] == board[2][3] && board[3][4] == board[1][2] && board[3][4] == board[0][1]) ||
(board[3][5] == board[4][4] && board[3][5] == board[1][3] && board[3][5] == board[0][2]) ||
(board[3][6] == board[2][5] && board[3][6] == board[1][4] && board[3][6] == board[0][3]))
winner = player;


   }
   /* Game is over so display the final board */
printf("\n\n");
      printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6]);
      printf("|---+---+---+---+---+---+---|\n");
      printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6]);
      printf("|---+---+---+---+---+---+---|\n");
  printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6]);
printf("|---+---+---+---+---+---+---|\n");
printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6]);
printf("|---+---+---+---+---+---+---|\n");
printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6]);
printf("|---+---+---+---+---+---+---|\n");
printf("| %c | %c | %c | %c | %c | %c | %c |\n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6]);
   /* Display result message */
   if(winner == 0)
      printf("\nHow boring, it is a draw\n");
   else
      printf("\nCongratulations, player %d, YOU ARE THE WINNER!\n", winner);
}
« Last Edit: December 02, 2012, 11:04:57 PM by Bitbeisser »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Connect 4
« Reply #1 on: December 02, 2012, 11:18:38 PM »
Well, a couple general comments for a start:

- in general, we don't do someone's homework here in the forum. If you have and issue, please tell us what you have tried and what more exactly your problem is and someone will certainly help you with that particular part of your issue...

- to be able to track down your problem (the integrated debugger is a very good way to follow the flow of code to see if it actuall does what you intend), make your code compile in the first place, without (or at least as little as possible) warning/error messages. The line
Code: [Select]
column = go%\1;                                /* Get column index of square   */certainly does not compile at all. And there are at least a couple of additional warnings you should take care of.

Ralf

PS: I put proper "code" tags around your program in your original post to make it better legible and easier to copy & paste