Thread: 5 In a Row Game

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    5 In a Row Game

    Hi guys,
    Ive got to do this, this week. http://www.olympiad.org.uk/papers/2004/bio/q2.html

    I'm not to sure on what to do but I've been looking at arrays and come up with this so far.

    I use arrays to create a 7x6 board and then if statements to determine where the pieces go.

    Is this the right way to do it before I start coding?

    Thanks

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It is impossible to determine if you are on the right track or not, from that brief description.
    Got any code to post yet?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Hi,
    I'll get some code written now. For a 7x6 board is this the right code?

    Code:
    int boardarry[7][6];

    Also is it four spaces i need to indent my code with?


    Thanks

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I used to use 2 spaces but more and more I'm liking 4. Any is better than none.

    If you want to simplify posting code here make sure you specify in your IDE to insert spaces instead of tabs. Tabs are not universal in that they depend on the settings of the application using the tab. Hence they are much different within this edit window than they are in your environment edit window.
    Last edited by VirtualAce; 12-08-2007 at 09:50 AM.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    The code is right. Note that, usually, the way C represent a 2D array, it should be

    Code:
    type arrayName[NB_OF_ROWS][NB_OF_COLS];
    But this doesn't really matter, the only think who matters is to stick with your representation; ie accessing the elements always the same way.

    By the way, iMalc, your avatar is showing heapsort (after a couple of elements sorted), right ? I want my cookie.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I like tabs. They're easier to work with. One tab for each indent, not 4 spaces.
    You can delete one tab and insert one tab. You can insert a tab and convert into 4 spaces, but then you have to delete 4 times too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Thanks for the replies guys,

    Im trying to compile the solution provided to see how it works but I get errors. I think its because im using Dev C++ and maybe its compiling C++ and not C does anyone know if theres a way to change it to compile C?

    This is the solution they provide

    Code:
    /*****************************************************************************
    
      Copyright (c) British Informatics Olympiad, 2004
    
      BIO 2004 Question 2 "Four in a line"
      Question by Richard Forster
      Example solution by Antony Rix.
    
      This program may be freely copied by persons or organisations
      involved in the British Informatics Olympiad or the International
      Olympiad in Informatics, on condition that no changes are made and
      this notice is not altered or removed. Distribution for profit is
      forbidden unless permission is first obtained in writing from the BIO.
    
      This program is for educational purposes only and comes with no
      warranty, implied or otherwise, as to its fitness for any purpose.
    
      http://www.olympiad.org.uk/
    
    *****************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    /* The player whose turn it is to play next */
    int Player;
    
    /* The number of moves made */
    int Moves;
    
    /* The board.  0 represents no piece, 1 player 1's piece, 2 player 2's piece. */
    int Board[7][7];
    /* Note that as this is C, all array indices count from zero rather than 1. */
    
    /* Initialise the board to all 0's */
    void InitBoard( void ) {
    	int i, j;
    	for( i = 0; i < 6; i++ ) for( j = 0; j < 7; j++ ) Board[i][j] = 0;
    	Moves = 0;
    }
    
    /* Show the board in the required format */
    void ShowBoard( void ) {
    	int i, j;
    	for( i = 0; i < 6; i++ ) {
    		for( j = 0; j < 7; j++ ) 
    			if( Board[i][j] == 0 ) printf("-");
    			else if( Board[i][j] == 1 ) printf("*");
    			else if( Board[i][j] == 2 ) printf("o");
    		printf("\n");
    	}
    }
    
    /* Test whether the current move wins the game */
    int WinsGame( int i0, int j0 ) {
    	int i, j;
    
    	/* Test horizontal lines of 4 pieces including the current one */
    	i = i0;
    	j = j0-3; if( j < 0 ) j = 0;
    	while( (j <= j0) && (j <= 3) ) {
    		if( (Board[i][j] == Board[i0][j0]) &&
    			(Board[i][j+1] == Board[i0][j0]) &&
    			(Board[i][j+2] == Board[i0][j0]) &&
    			(Board[i][j+3] == Board[i0][j0]) )
    			return 1;
    
    		/* Not won - try the next starting position */
    		j++;
    	}
    
    	/* Test vertical lines of 4 pieces including the current one */
    	j = j0;
    	i = i0-3; if( i < 0 ) i = 0;
    	while( (i <= i0) && (i <= 2) ) {
    		if( (Board[i][j] == Board[i0][j0]) &&
    			(Board[i+1][j] == Board[i0][j0]) &&
    			(Board[i+2][j] == Board[i0][j0]) &&
    			(Board[i+3][j] == Board[i0][j0]) )
    			return 1;
    
    		/* Not won - try the next starting position */
    		i++;
    	}
    
    	/* Test \-diagonal lines of 4 pieces including the current one */
    	j = j0-3; i = i0-3;
    	while( (j < 0) || (i < 0) ) { j++; i++; };
    	while( (i <= i0) && (i <= 2) && (j <= j0) && (j <= 3) ) {
    		if( (Board[i][j] == Board[i0][j0]) &&
    			(Board[i+1][j+1] == Board[i0][j0]) &&
    			(Board[i+2][j+2] == Board[i0][j0]) &&
    			(Board[i+3][j+3] == Board[i0][j0]) )
    			return 1;
    
    		/* Not won - try the next starting position */
    		j++; i++;
    	}
    
    	/* Test /-diagonal lines of 4 pieces including the current one */
    	j = j0+3; i = i0-3;
    	while( (j > 6) || (i < 0) ) { j--; i++; };
    	while( (i <= i0) && (i <= 2) && (j >= j0) && (j >= 3) ) {
    		if( (Board[i][j] == Board[i0][j0]) &&
    			(Board[i+1][j-1] == Board[i0][j0]) &&
    			(Board[i+2][j-2] == Board[i0][j0]) &&
    			(Board[i+3][j-3] == Board[i0][j0]) )
    			return 1;
    
    		/* Not won - try the next starting position */
    		j--; i++;
    	}
    
    	/* No, this has not won the game */
    	return 0;
    }
    
    /* Apply the rules to make the next move, and exit if the game is over */
    void DoMove( void ) {
    	int i, j;
    
    	/* Rule 1 - if we can win immediately, do so. */
    	for( j = 0; j < 7; j++ ) {
    		if( Board[0][j] == 0 ) {
    			/* Find the first place that we can go */
    			i = 0;
    			while( (i < 6) && (Board[i][j] == 0) ) i++;
    			i--;
    
    			/* See if going here wins */
    			Board[i][j] = Player;
    			if( WinsGame(i, j) ) {
    				ShowBoard();
    				printf("Player %d wins\n", Player);
    				exit(0);
    			} else
    				/* Erase this move and try the next */
    				Board[i][j] = 0;
    		}
    	}
    
    	/* Rule 2 - if the other player can win immediately, go there. */
    	for( j = 0; j < 7; j++ ) if( Board[0][j] == 0 ) {
    		/* Find the first place that we can go */
    		i = 0;
    		while( (i < 6) && (Board[i][j] == 0) ) i++;
    		i--;
    
    		/* See if going here would win for the other player */
    		Board[i][j] = 3-Player;
    		if( WinsGame(i, j) ) {
    			/* We go there */
    			Board[i][j] = Player;
    			return;
    		} else
    			/* Erase this move and try the next */
    			Board[i][j] = 0;
    	}
    
    	/* Rule 3 - go in first available space. */
    	for( j = 0; j < 7; j++ ) if( Board[0][j] == 0 ) {
    		/* Find the first place that we can go */
    		i = 0;
    		while( (i < 6) && (Board[i][j] == 0) ) i++;
    		i--;
    
    		Board[i][j] = Player;
    		return;
    	}
    }
    
    /* Function to solve Q2a */
    void Q2a( void ) {
    	char c;
    	int n, count, i, j;
    	Player = 1;
    	InitBoard();
    
    	/* Play the initial sequence of moves */
    
    	/* Read the number of initial moves */
    	scanf("%d", &n);
    
    	/* Do the initial moves */
    	for( count = 0; count < n; count++ ) {
    		/* Get the column for the next move */
    		scanf("%d", &j);
    		/* Reduce by 1 since we're counting from 0, but the question counts from 1 */
    		j--;
    
    		/* Find the next position we can go */
    		i = 0;
    		while( (i < 6) && (Board[i][j] == 0) ) i++;
    		i--;
    
    		Board[i][j] = Player;
    		Player = 3-Player;
    		Moves++;
    	}
    	ShowBoard();
    
    	/* The main loop where we get an instruction and process it */
    	while( Moves < 42 ) {
    		scanf("%c", &c);
    		if( (c == 'n') || (c == 'N') ) {
    			/* Play the next move */
    			DoMove();
    			ShowBoard();
    			Moves++;
    			Player = 3-Player;
    		} else if( (c == 'r') || (c == 'R') ) {
    			while( Moves < 42 ) {
    				DoMove();
    				Moves++;
    				Player = 3-Player;
    			}
    		}
    	}
    
    	/* Neither player has won and the board is now full. */
    	ShowBoard();
    	printf("Draw\n");
    	scanf("%c", &c);
    }
    
    /* Count of the number of combinations for Q2d */
    long Npatterns;
    
    /* Recursive helper function to count the number of combinations for Q2d */
    void TryPos( int Column, int Remaining ) {
    	int This;
    	if( Column == 8 ) {
    		if( Remaining == 0 ) {
    			/* This is a valid pattern with all 21 pieces placed, so add to the count */
    			Npatterns++;
    		}
    	} else {
    		/* Try to place between 0 and 6 pieces in Column, then recurse to try the next */
    		This = 0;
    		while( (This <= 6) && (This <= Remaining) ) {
    			TryPos(Column+1, Remaining-This);
    			This++;
    		}
    	}
    }
    
    /* Function to solve Q2d */
    void Q2d( void ) {
    	Npatterns = 0;
    	TryPos(1,21);
    	printf("Number of distinct patterns for Q2d is %d\n", Npatterns);
    }
    
    /* Main function called from the command-line */
    void main( void ) {
    	Q2a();		/* Edit this line to Q2d() to solve 2d */
    }
    /* End of program */

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Dev Cpp works on C programs, it doesn't matter.

    the return type of main is int not void.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It compiles as a C program here (almost)
    - main returns int
    - the printf in Q2d() uses the wrong format

    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `Q2d':
    foo.c:251: warning: int format, long int arg (arg 2)
    foo.c: At top level:
    foo.c:255: warning: return type of 'main' is not `int'
    > Im trying to compile the solution provided to see how it works but I get errors.
    Post them if you don't understand them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    I cant compile it either if I change them to main.

  11. #11
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Are you naming the source file Something.c or are you naming it Something.cpp?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > I cant compile it either if I change them to main.
    Lemme guess, some linker error message about unable to find winmain() ?

    If this is the case, you've created a GUI project where you should have created a console project.

    Like I said, if you post actual error messages, you'll get a more accurate and speedy response.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tetris
    By estoyperdida in forum C Programming
    Replies: 8
    Last Post: 01-30-2008, 01:02 PM
  2. Passing a 2d array by Reference
    By loko in forum C Programming
    Replies: 8
    Last Post: 07-23-2005, 06:19 AM
  3. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  4. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  5. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM