Thread: hey . problam with my c code

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    Wink hey . problam with my c code

    i'll try to write reversi game.
    when i check the program most of functions wroks properly. but when i input test i find some problems. anybody can hrlp me to find the problam? thenks very.
    thets my code.

    Code:
    #include
    <stdio.h>
    
    #define
     size 8
    
    #define
     WHITE  'W'
    
    #define
     NONE   ' '
    
    #define
     BLACK  'B'
    
    
    void
     initBoard(char board[size][size]);
    
    void
     printBoard(char board[size][size],int player);
    
    void
     flip(int x1,int y1,int x2,int y2,char board[size][size]);
    
    int
     hasLegitMove(int player,char board[size][size]);
    
    int
     isLegitMove(int player,int x, int y,char board[size][size] ,int makeTheMove);
    
    int
     checkIfLegitDirectionAndColorAndFlipIfNeeded(int player,int i, int j,char board[size][size],int iDir,int jDir,int colorAndflipWhenPossible);
    
    
    void
     main()
    
    {
    
    	
    char board[size][size]={"0"};
    
    	
    int player=1, player2=2;
    
    	
    int  i,j;
    
    	
    int winnera=0,winnerb=0;
    
    	
    int testerA=1,testerB=1;
    
    		initBoard(board);
    
    	
    while(hasLegitMove(player,board)){
    
    		printBoard(board,player);
    
    		scanf(
    "%d",&j);
    
    		scanf(
    "%d",&i);
    
    		testerA=1,testerB=1;
    
    		i=i-1;j=j-1;
    
    		
    
    			
    if(i+1>size || j+1>size || i+1<0 ||j+1<0)
    
    			{
    
    				printf(
    "There is no such square please select a different square \n");
    
    				testerA=0;
    
    			}
    
    			
    if(board[i][j]!=NONE &&testerA)
    
    			{
    
    			printf(
    "This square is already full please select a different square\n");
    
    			testerB=0;
    
    			}
    
    			
    if(testerA && testerB)
    
    			{
    
    			testerA=1;
    
    			testerB=1;
    
    			
    if(isLegitMove(player,i,j,board,0)){
    
    				isLegitMove(player,i,j,board,1);
    
    				
    if(player==1)
    
    					player=2;
    
    				
    else
    
    					player=1;
    
    			}	
    
    			
    else
    
    			{printf(
    "This move is not legitimate\n"); }
    
    			
    if(hasLegitMove(player,board)==0)
    
    				{
    
    					
    if(player==1)
    
    						player2=2;
    
    					
    else
    
    						player2=1;
    
    				}
    
    			}
    
    	}
    
    
    	
    for( j=0;i<size;j++)
    
    		
    for( i=0;j<size;i++)
    
    		{
    
    			
    if(board[i][j]==WHITE)
    
    				winnera++;
    
    			
    else
    
    				winnerb++;
    
    		}
    
    		printf(
    "\nPlayer 1 has %d squares\n",winnera);
    
    			printf(
    "Player 2 has %d squares\n",winnerb);
    
    		
    if(winnera>winnerb)
    
    			printf(
    "player 1 wins !!!!\n");
    
    			
    if(winnerb>winnera)
    
    				printf(
    "player 2 wins !!!!\n");
    
    			
    if(winnerb==winnera)
    
    			printf(
    "Its a tie !!!!!!\n");
    
    		}
    
    
     
    
     
    
    void
     initBoard(char board[size][size])
    
    {
    
    	
    int i,j;
    
    	
    for( i=0;i<size;i++)
    
    		
    for( j=0;j<size;j++)
    
    		{
    
    			board[i][j]= NONE;
    
    		}
    
        	board[size/2-1][size/2-1]= WHITE;
    
    		board[size/2][size/2]= WHITE;
    
    		board[size/2-1][size/2]= BLACK;
    
    		board[size/2][size/2-1]= BLACK;
    
    }
    
    
     
    
     
    
    void
     printBoard(char board[size][size], int player)
    
    {
    
    	
    int i,i2, j;
    
    	printf(
    "player %ds turn\n \n",player);
    
    	printf(
    "  ");
    
    	
    for ( i=1; i < size+1; i++ ) printf("%d|", i);
    
    	printf(
    "\n");
    
    	
    
    	printf(
    "  ");
    
    	
    for ( i=0; i < size-1; i++ ) { printf("--"); }
    
    	printf(
    "--\n");
    
    	
    
    	
    for ( i=0; i < size; i++ ) {
    
    		i2=i+1;
    
    		printf(
    "%d|", i2);
    
    		
    for ( j=0; j < size; j++ ) {
    
    			
    switch ( board[j][i] ) {
    
    				
    case BLACK: printf("B");
    
    				            
    break;
    
    				
    case WHITE: printf("W");
    
    				            
    break;
    
    				
    case NONE : printf(" ");
    
    				            
    break;
    
    			}
    
    			printf(
    "|");
    
    		}
    
    		printf(
    "\n");
    
    		
    if ( i < size ) {
    
    			printf(
    "  ");
    
    			
    for ( j=0; j < size-1; j++ ) {
    
    				printf(
    "--");
    
    			}
    
    			printf(
    "--\n");
    
    		}
    
    	}
    
    
     
    
     
    
    }
    
    
    void
     flip(int x1,int y1,int x2,int y2,char board[size][size]){
    
    	
    int x,y,i;
    
    	
    char changer=board[x1][y1];
    
    
    	
    //down
    
    	
    if(y1==y2 && x1>x2)
    
    	{
    
    		
    for(x=0;x<size;x++)
    
    			
    for(y=0;y<size;y++)
    
    				
    if(y==y1 && x>x2 && x<x1)
    
    					board[x][y]=changer;
    
    	}
    
    	
    //up
    
    	
    if(y1==y2 && x2>x1)
    
    	{
    
    		
    for(x=0;x<size;x++)
    
    			
    for(y=0;y<size;y++)
    
    				
    if(y==y1 && x<x2 && x>x1)
    
    					board[x][y]=changer;
    
    	}
    
    	
    //left
    
    	
    if(x1==x2 && y1>y2)
    
    	{
    
    		
    for(x=0;x<size;x++)
    
    			
    for(y=0;y<size;y++)
    
    				
    if(x==x1 && y>y2 && y<y1)
    
    					board[x][y]=changer;
    
    	}
    
    	
    //right
    
    	
    if(x1==x2 && y2>y1)
    
    	{
    
    		
    for(x=0;x<size;x++)
    
    			
    for(y=0;y<size;y++)
    
    				
    if(x==x1 && y>y1 && y<y2)
    
    					board[x][y]=changer;
    
    	}
    
    	
    //left up
    
    	
    if(x1>x2 && y1>y2)
    
    	{
    
    		
    for(i=1;x2+i<x1;i++)
    
    			board[x2+i][y2+i]=changer;
    
    	}
    
    	
    //right down
    
    	
    if(x2>x1 && y2>y1)
    
    	{
    
    		
    for(i=1;x1+i<x2;i++)
    
    			board[x1+i][y1+i]=changer;
    
    	}
    
    	
    //left down
    
    	
    if(x2>x1 && y1>y2)
    
    	{
    
    		
    for(i=1;x1+i<x2;i++)
    
    			board[x2-i][y2+i]=changer;
    
    	}
    
    	
    //right up
    
    	
    if(x1>x2 && y2>y1)
    
    	{
    
    		
    for(i=1;x2+i<x1;i++)
    
    			board[x1-i][y1+i]=changer;
    
    	}
    
    }
    
    
     
    
    int
     checkIfLegitDirectionAndColorAndFlipIfNeeded(int player,int i, int j,char board[size][size],int iDir,int jDir,int colorAndflipWhenPossible)
    
    {
    
    	
    char playerdefine;
    
    	
    int k;
    
    	
    int l;
    
    	
    if(player==1)
    
    		playerdefine=WHITE;
    
    	
    if(player==2)
    
    		playerdefine=BLACK;
    
    	k=i+iDir;
    
    	l=j+jDir;
    
    	
    if(board[k][l]==playerdefine)
    
    		
    return 0;
    
    	
    if(k>=0 && k<size && l>=0 && l<size)
    
    	{
    if(player==1)
    
    		
    while(board[k][l]==BLACK)
    
    		{
    
    			k+=iDir;
    
    			l+=jDir;
    
    			
    if(board[k][l]==WHITE){
    
    			
    if(colorAndflipWhenPossible==1)
    
    			{
    
    				board[i][j]=playerdefine;
    
    				flip(i,j,k,l,board);
    
    			}
    
    			
    return 1;
    
    			}
    
    	}
    
    	
    if(player==2)
    
    	   
    while(board[k][l]==WHITE)
    
    	{
    
    
    			k=k+iDir;
    
    			l=l+jDir;
    
    			
    if(board[k][l]==BLACK){
    
    			
    if(colorAndflipWhenPossible==1)
    
    			{
    
    				board[i][j]=playerdefine;
    
    				flip(i,j,k,l,board);
    
    			}
    
    			
    return 1;
    
    			}
    
    	   } 
    return 0;}
    
    	   
    return 0;
    
    }
    
    
    int
     isLegitMove(int player,int x, int y,char board[size][size] ,int makeTheMove)
    
    {
    
    	
    int countLegit=0,i,j;
    
    	
    if(board[x][y]!=NONE)
    
    		
    return 0;
    
    	
    for(i=-1;i<=1;i++)
    
    		
    for(j=-1;j<=1;j++)
    
    			
    if(i!=0 || j!=0){
    
    				countLegit+=checkIfLegitDirectionAndColorAndFlipIfNeeded(player,x,y,board,i,j,makeTheMove);}
    
    
    	
    return countLegit;
    
    }
    
    
    int
     hasLegitMove(int player,char board[size][size]){
    
    	
    int i,j;
    
    		
    for(i=0;i<size;i++)
    
    		
    for(j=0;j<size;j++)
    
    			
    if(board[i][j]==NONE && isLegitMove(player,i,j,board,0))
    
    				
    return 1;
    
    			
    
    				
    return 0;
    
    }
    
    
     
    
     
    
     
    
     
    

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    Talking poor commenting

    first of all i think your program does not have an official documentation at the top that explains to whole function of the program i think if you can include that then debugging would be easier on our part. im just a newbie in this so hope we can start from there.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    thets what i will nedd to do:

    Assignment 3 – functions

    In this assignment you will program a game called reversi.

    Game's description:

    Reversi is an 8X8 squares board game for 2 players. Each player has a color. Player 1 plays

    the white color - W and player 2 plays the Black color – B.

    The game's initial state is:

    In each turn a player has to put his color on an empty square. The player has to choose the

    square so that after putting his color on the square, there will be at least 1 square with the

    other color between this square and another square of the players color in a horizontal,

    vertical or diagonal way. For example in the initial state player 1 plays the W color. For

    example in the next board, the black letters represent the current board state and the red

    letters represent the squares that it is possible to color (the Ws are relevant if this is the W

    turn and the Bs are relevant if the this is the B turn).

    1 2 3 4 5 6 7 8

    1

    2

    3

    4

    5

    6

    7

    8

    W

    W

    B

    B

    1 2 3 4 5 6 7 8

    1

    2

    3

    4

    5

    6

    7

    8

    B

    W

    B

    B

    B

    B

    W

    W

    W

    B

    B

    B

    After the player put his color on a legitimate square, all the other player's squares that are

    between the current played square and another square of that player will change their color

    to the player's color.

    For example in the next board if player W will put his color on square 2,2 the board state will

    change like this:

    Notice that more than 1 row/column/diagonal may change. In this case the B's in 3 ,2 and

    4,2 changed to W as well as the Bs in 3,3 and 4,4.

    It is not possible for a player to put his color on a non-legitimate square (a square that will

    not cause any change of colors) .

    If a player doesn't have any legitimate move on the board, his turn will be skipped and it will

    be the other player's turn. The game ends when none of the players has a legitimate move.

    When the game ends, the player that has more squares in his color wins the game.

    Clarifications:

    This is not a legitimate move because we need all Bs between the Ws:

    1 2 3 4 5 6 7 8

    1

    2

    3

    4

    5

    6

    7

    8

    B

    W

    B

    B

    B

    B

    W

    B

    B

    1 2 3 4 5 6 7 8

    1

    2

    3

    4

    5

    6

    7

    8

    W

    W

    B

    B

    B

    W

    W

    W

    W

    W

    1 2 3 4 5 6 7 8

    1

    2

    3

    4

    5

    6

    7

    8

    W

    B

    W

    B

    the B in 3,3 will be colored because it is between the W In this case
    only in 4,4 and the new

    W in 2,2.

    Assignment:

    You will need to program the reversi game:

    The board starts in the initialize state shown above.

    Player 1 is the first player and he plays the Ws. After player 1 plays, it is player's 2 turn etc.

    Each turn the sentence: "Player Xs turn" will be printed according to the player who is

    supposed to play and under it the updated board will be printed.

    The player will enter two numbers (with a space between) the first is the row number and

    the second is the column number.

    If a player attempts to put his color on a non-empty square the message: "This square is

    already full please select a different square" will be printed and it will be the same player's

    turn again.

    If a player attempts to put his color on a square not between 1 and 8 the message: "There is

    no such square please select a different square" will be printed and it will be the same

    player's turn again.

    If a player attempts to put his color on a non-legitimate square the message: "This move is

    not legitimate" will be printed and it will be the same player's turn again.

    If a player has currently no legitimate moves, the message: "Player X currently has no

    moves, player %d plays again" and the turn will move to the other player.

    If both players have no legitimate moves, the game ends and the winner is announced by

    the messages:

    "Player 1 has X squares"

    "Player 2 has Y squares"

    "Player X wins!!!!" (According to who has more squares)

    If both players has the same number of square, the last message will be replaced with: "Its a

    tie!!!! ".

    1 2 3 4 5 6 7 8

    1

    2

    3

    4

    5

    6

    7

    8

    W

    B

    W

    B

    W

    1 2 3 4 5 6 7 8

    1

    2

    3

    4

    5

    6

    7

    8

    W

    W

    W

    B

    W

    After printing the message the program will finish.

    If a player wants to terminate the game he may enter 100 100. The message "The game was

    terminated" will be printed and the game ends.

    After printing the message the program will finish.

    You
    MUST implement at least these Functions:

    void
    initBoard(char board[8][8])

    initializes the board to the initialize state.

    void
    printBoard(char board[8][8],int player)

    prints the board.

    void
    flip(int x1,int y1,int x2,int y2,char board[8][8])

    receives 2 coordinates (x1,y1 and x2,y2) on the board and change the color of the squares

    between them to their color for example if we get 3,4 and 3,8 and those squares are in color

    W, the function will change the color of 3,5 3,6 and 3,7 to W also.

    In this case x is the row number and y is the column number.

    int
    hasLegitMove(int player,char board[8][8])

    Checks if the given player currently has any legitimate move on the board and returns 1 if he

    has and 0 if he hasn't.

    int
    isLegitMove(int player,int x, int y,char board[8][8] ,int makeTheMove)

    checks if a given move by a given player is legitimate. If makeTheMove = 1 and this is a

    legitimate move, it also makes this move meaning that after this function is finished in case

    it is a legitimate move and makeTheMove = 1, the player's color is set on this square and ALL

    squares needed to be changed on the board are changed)

    In this case x is the row number and y is the column number.

    int
    checkIfLegitDirectionAndColorAndFlipIfNeeded(int player,int i, int j,char

    board[8][8],
    int iDir,int jDir,int colorAndflipWhenPossible)

    this function is similar to "isLegitMove" but it checks a specific direction from the square (i,j).

    it receives i,j and a direction. The direction is given by iDir and jDir in this way:

    Up - left diagonal : iDir = -1, jDir = -1.

    Up : iDir = -1, jDir = 0.

    Up – Right diagonal : iDir = -1, jDir = 1.

    Left : iDir = 0, jDir = -1.

    Right : iDir = 0, jDir = 1.

    Down – Left diagonal : iDir = 1,jDir - -1.

    Down : iDir = 1, jDir = 0.

    Down – right diagonal: iDir = 1, jDir = 1.

    If this is a legitimate direction (it will cause a sequence of the other color between i,j and

    another square of the player's color in this direction) and colorAndflipWhenPossible = 1, the

    function colors i,j with the right color and changes all the needed colors on this direction.

    In this case i is the row number and j is the column number.

    Advice 1:

    It is recommended to implement more functions (whenever is needed).

    Advice 2:

    It will be more simple to reach the end (and check your program in the cases of ending the

    game for example when 1 or two players don’t have any more moves etc.)of the game by

    playing on a smaller board.

    To do so you can define "size" with the command: #define size 8 (you can put it right after

    the #include …) and in all your functions replace the number 8 with size (this means that in

    some cases the number 7 will be replaced with size -1 and in some cases the number 4 will

    be replaced with size/2). Then you will be able to check your program on a smaller board by

    changing the #define size 8 to #define size 4 or #define size 3.
    It is important to remember

    to change it back to 8 before submition!
    .

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, natyshai! Also a big welcome to Nyah Check, who is almost new!

    I have never played reversi, but I see that your code is very hard to study, because of the formatting and indentation of it, on the forum. Due to time constraints, code like that tends to get few responses.

    In your editor, try setting your options to use "spaces only", and set your indentations to 3 to 5 spaces. More than 5 spaces and you can easily run out of room in a long line of code - especially if it also has comments after it.

    If you can be more specific about WHAT functions aren't working (and what functions ARE working), that would help a lot.

    If you need to make a board display on the forum, put it between code tags, or the forum software will make it unreadable!
    Last edited by Adak; 12-12-2011 at 03:30 AM.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    thenks about the comment. i will try next time.
    thets the problam , i'm not find where the problem. when i try to play in debug. every thing look fine. cut somthing is wrong ther. maybe i main or checkIfLegitDirectionAndColorAndFlipIfNeeded functions. about reversi . thets reversi Reversi - Wikipedia, the free encyclopedia

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    look

    a pic on of the problams

    the right W not need to colored in B, everythinghey . problam with my c code-test-jpg else is ok.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you using two functions to flip the markers? Looks like you're using
    checkIfLegitDirectionAndColorAndFlipIfNeeded()

    and

    flip()

    also.

    If you put a getchar() around both those two functions, and run it, you should be able to tell which function has the bug in it.

    What do they call the marker in Reversi, anyway?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Nyah Check View Post
    first of all i think your program does not have an official documentation at the top that explains to whole function of the program i think if you can include that then debugging would be easier on our part. im just a newbie in this so hope we can start from there.
    First the problem is explained... it's Reversi ... also known as Othello ... which is a very common board game played world wide by children and adults alike... At one time it was so popular a computer version was even included in early versions of Windows.

    You can play it online... HERE

    The rules are very simple... you must add pieces to trap one or more of your opponent's men between your new piece and one of your existing pieces in a straight line. Lines can be veritcal, horizontal or on any diagonal. Placing a piece can trap existing pieces in more than one direction at a time. The trapped pieces flip over and become yours. The winner has the most pieces when the board is full.

    So just the word "reversi" was enough to describe the problem.

    Now the OP's code formatting is a whole other story. It's such a nightmare I'm not even going to try studying it to see what's wrong...
    Last edited by CommonTater; 12-12-2011 at 10:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. please any one can solve my pragma problam
    By nkrao123@gmail. in forum C Programming
    Replies: 5
    Last Post: 10-12-2009, 12:47 AM
  3. please solve my #pragma pack problam
    By nkrao123@gmail. in forum C Programming
    Replies: 2
    Last Post: 10-10-2009, 03:22 AM
  4. Problam with a global class variable
    By WebmasterMattD in forum C++ Programming
    Replies: 11
    Last Post: 01-21-2003, 04:38 AM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM