Thread: Take a look at this...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    2

    Unhappy Take a look at this...

    hi! i'm having a hard time dealing with the source code of my friend's homework. the game basically gives the player the option of whether he/she wants to play with the computer or with another opponent. the problem is i don't know how to formulate the code for the compuetr to generate a move and where to put it in the program source code. please

    here's the code...

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <graphics.h>
    #include <dos.h>
    
    #define cursor_LEFT   75
    #define cursor_RIGHT  77
    #define ENTER         13
    #define False         0
    #define player_ONE    1
    #define player_TWO    2
    #define player_THREE  3
    #define player1_ONE   4
    #define max_ROW       8
    #define board_FULL    64
    #define ESC           27
    
    int board[8][8];
    int col = 0;
    int row = 0;
    int limit = 0;
    int max_x, max_y;
    int player_WHO;
    char player_COLOR;
    int max_CHIPS = 0;
    int x,ctr,i;
    
    void objective(void);
    void menu(void);
    void two_players(void);
    void three_players(void);
    void initialize(void);
    void square_board(void);
    void player_1(void);
    void player_2(void);
    void player_3(void);
    void player1(void);
    int option(void);
    int option2(void);
    int input_data(void);
    int input_data2(void);
    void check(void);
    int full(void);
    void chips(void);
    void chips2(void);
    void player_wins(void);
    void player_draw(void);
    void close(void);
    
    void main(void)
    {
     objective();
     close();
    }
    
    void objective(void)
    {
     int ctr;
     int ctrx,ctry;
     textmode(C80);
     textcolor(YELLOW);
     textbackground(BLACK);
     clrscr();
     window(20,6,60,14);
     textbackground(BLUE);
     clrscr();
     gotoxy(2,1);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     gotoxy(2,9);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     for(ctr=1;ctr<=8;ctr++)
     {
     gotoxy(2,ctr);printf("º");gotoxy(40,ctr);printf("º");
     }
     gotoxy(2,1);printf("É");gotoxy(40,1);printf("»");
     gotoxy(2,9);printf("È");gotoxy(40,9);printf("¼");
     textcolor(YELLOW+BLINK);
     gotoxy(13,3);printf("**HOW TO PLAY**");
     textcolor(YELLOW);
     delay(180000);
     gotoxy(7,5);printf("Use the arrow keys to move");
     gotoxy(7,6);printf("the chips from left to right.");
     gotoxy(7,7);printf("Press ENTER to drop the chips.");
     sleep(4);
     clrscr();
     gotoxy(2,1);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     gotoxy(2,9);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     for(ctr=1;ctr<=8;ctr++)
     {
     gotoxy(2,ctr);printf("º");gotoxy(40,ctr);printf("º");
     }
     gotoxy(2,1);printf("É");gotoxy(40,1);printf("»");
     gotoxy(2,9);printf("È");gotoxy(40,9);printf("¼");
     textcolor(YELLOW+BLINK);
     gotoxy(12,2);printf("<<<GAME OBJECTIVE>>>");
     textcolor(YELLOW);
     delay(18000);
     gotoxy(8,4);printf("To be declared as winner,");
     gotoxy(8,5);printf("the payer must form a line of");
     gotoxy(8,6);printf("four chips either vertical,");
     gotoxy(8,7);printf("horizontal or diagonal.");
     getch();
     menu();
    }
    
    void menu(void)
    {
     int ctr;
     char menu_choice ;
     start:
     textmode(C80);
     textcolor(YELLOW);
     textbackground(BLACK);
     clrscr();
     window(20,6,60,14);
     textbackground(BLUE);
     clrscr();
     gotoxy(2,1);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     gotoxy(2,9);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     for(ctr=1;ctr<=8;ctr++)
     {
      gotoxy(2,ctr);printf("º");gotoxy(40,ctr);printf("º");
     }
     gotoxy(2,1);printf("É");gotoxy(40,1);printf("»");
     gotoxy(2,9);printf("È");gotoxy(40,9);printf("¼");
     gotoxy(11,3);printf("@@@  GAME TYPE  @@@");
     gotoxy(14,5);printf("[A] Versus Computer");
     gotoxy(14,6);printf("[B] 2 players");
     gotoxy(14,7);printf("[X] Exit");
     gotoxy(15,8);printf("Choice [ ]");gotoxy(23,8);
     menu_choice=getch();
     switch(toupper(menu_choice))
     {
      case 'A': three_players();
    	    break;
      case 'B': two_players();
    	    break;
      case 'X': close();
    	    break;
      default: goto start;
     }
    }
    
    void three_players(void)
    {
     initialize();
     for(;;)
     {
      player_3();
      player1();
     }
    }
    
    void two_players(void)
    {
     initialize();
     for(;;)
     {
      player_1();
      player_2();
     }
    }
    
    void initialize(void)
    {
     int driver;
     int mode;
     driver=VGA;
     mode=VGAHI;
     detectgraph(&driver,&mode);
     initgraph(&driver,&mode, "");
     setbkcolor(0);
     max_x=getmaxx();
     max_y=getmaxy();
     square_board();
    }
    
    void square_board(void)
    {
     int i;
     int ctrx,ctry;
     int x=(6*max_x)/20;
     int y=(4*max_y)/20;
     setfillstyle(9,9);
     bar((5*max_x)/20,(2.3*max_y)/20,(17*max_x)/20,(18.5*max_y)/20);
     for(ctrx=1;ctrx<=8;ctrx++)
     {
      for(ctry=1;ctry<=8;ctry++)
      {
       setfillstyle(1,0);
       fillellipse(x,y,18,18);
       y+=45;
       delay(1800);
      }
      x+=45;
      y=(4*max_y)/20;
     }
    }
    
    void player_3(void)
    {
     player_WHO==player_THREE;
     player_COLOR=CYAN;
     select:
      option2();
      input_data2();
      if(limit==max_ROW)
      {
    	limit=0;
    	goto select;
      }
     chips();
     check();
    }
    
    void player1(void)
    {
     player_WHO==player1_ONE;
     player_COLOR=BLUE;
     select:
      option();
      input_data();
      if(limit==max_ROW)
      {
    	limit=0;
    	goto select;
      }
     chips();
     check();
    }
    
    void player_1(void)
    {
     player_WHO=player_ONE;
     player_COLOR=RED;
     select:
      option();
      input_data();
      if(limit==max_ROW)
      {
       limit=0;
       goto select;
      }
     chips();
     check();
    }
    
    void player_2(void)
    {
     player_WHO=player_TWO;
     player_COLOR=YELLOW;
     select:
      option();
      input_data();
      if(limit==max_ROW)
      {
       limit=0;
       goto select;
      }
     chips();
     check();
    }
    
    int option(void)
    {
     int options=8;
     register int i=0;
     int x=0;
     int key_press=0;
     for(;;)
     {
      if(key_press!=ENTER)
      {
       x=i*45;
       setviewport((4*max_x)/20,(0.3*max_y)/20,(17*max_x)/20,(2*max_y)/20,1);
       setfillstyle(9,player_COLOR);
       clearviewport();
       fillellipse((2*max_x/20)+x,15,15,15);
      }
      key_press=getch();
      switch(key_press)
      {
       case cursor_LEFT: i==0?i=(options-1):i--;
    		     break;
       case cursor_RIGHT: i==(options-1)?i=0:i++;
    		      break;
       case ENTER: col=(i);
    	       return col;
       case ESC:exit();
    	    break;
      }
     }
    }
    
    int option2(void)
    {
     int options=0;
     register int i=0;
     int x=0;
     for(options=0;options<8;options++)
     {
      chips();
      x=i*45;
      setviewport((4*max_x)/20,(0.3*max_y)/20,(17*max_x)/20,(2*max_y)/20,1);
      setfillstyle(9,player_COLOR);
      clearviewport();
      /*fillellipse((2*max_x/20)+x,15,15,15);*/
      /*return x;*/
      /*options++;*/
      player1();
     }
    }
    
    int input_data(void)
    {
     row=0;
     while((board[row][col]==player_ONE)||(board[row][col]==player_TWO))
     {
      row++;
      if(row==max_ROW)
      {
       limit=max_ROW;
       break;
      }
     }
     if(row!=max_ROW)
     {
      board[row][col]=player_WHO;
     }
     return limit;
    }
    
    int input_data2(void)
    {
     int x=row;
     int y=col;
     row=0; col=0;
     while((board[row][col]==player_THREE)||(board[row][col]==player1_ONE))
     {
      x++;y++;
      /*row++;*/
      if(row==max_ROW)
      {
       limit=max_ROW;
       break;
      }
      if(row!=max_ROW)
      {
       board[row][col]=player_WHO;
      }
      return limit;
     }
    }
    
    void chips(void)
    {
     int sec_y=(row*45);
     int sec_x=(col*45);
     int x=((5*max_x)/20)+sec_x;
     int y=((17.15*max_y)/20)-sec_y;
     setviewport(32,0,max_x,max_y,1);
     setfillstyle(9,player_COLOR);
     fillellipse(x,y,15,15);
    }
    
    /*void chips2(void)
    {
     int i=0;
     int sec_y=(row*45);
     int sec_x=(col*45);
     for(sec_x=0;sec_x!=NULL;sec_x++)
     {
     int x=((5*max_x)/20+sec_x;
     int y=((17.15*max_y)/20)-sec_y;
     setviewport(32,0,max_x,max_y,1);
     setfillstyle(9,player_COLOR);
     fillellipse(x,y,15,15);
     }
    }*/
    
    void check(void)
    {
     int x=0;
     int y=0;
     int ctr=0;
     x=row;y=col;
    /* Vertical Checking */
     if(row==3||row==4||row==5||row==6||row==7)
     {
      ctr=0;
      while(board[x][y]==player_WHO)
      {
       x--;ctr++;
       if(ctr==4)
       {
        player_wins();
       }
      }
     }
    /* Horizontal Checking */
     if(col==0||col==1||col==2||col==3||col==4)
     {
      x=row;y=col;ctr=0;
      while(board[x][y]==player_WHO)
      {
       y++;ctr++;
       if(ctr==4)
       {
        player_wins();
       }
      }
     }
     if(col==1||col==2||col==3||col==4||col==5)
     {
      x=row;y=col-1;ctr=0;
      while(board[x][y]==player_WHO)
      {
       y++;ctr++;
       if(ctr==4)
       {
        player_wins();
       }
      }
     }
     if(col==3||col==4||col==5||col==6||col==7)
     {
      x=row;y=col;ctr=0;
      while(board[x][y]==player_WHO)
      {
       y--;ctr++;
       if(ctr==4)
       {
        player_wins();
       }
      }
     }
     if(col==2||col==3||col==4||col==5||col==6)
     {
      x=row;y=col+1;ctr=0;
      while(board[x][y]==player_WHO)
      {
       y--;ctr++;
       if(ctr==4)
       {
        player_wins();
       }
      }
     }
    /* Diagonal Checking */
     if(row==0||row==1||row==2||row==3||row==4)
     {
      if(col==0||col==1||col==2||col==3||col==4)
      {
       x=row;y=col;ctr=0;
       while(board[x][y]==player_WHO)
       {
        x++;y++;ctr++;
        if(ctr==4)
        {
         player_wins();
        }
       }
      }
     }
     if(row==1||row==2||row==3||row==4||row==5)
     {
      if(col==1||col==2||col==3||col==4||col==5)
      {
       x=row-1;y=col-1;ctr=0;
       while(board[x][y]==player_WHO)
       {
        x++;y++;ctr++;
        if(ctr==4)
        {
         player_wins();
    	 }
       }
      }
     }
     if(row==3||row==4||row==5||row==6||row==7)
     {
      if(col==0||col==1||col==2||col==3||col==4)
      {
       x=row;y=col;ctr=0;
       while(board[x][y]==player_WHO)
       {
        x--;y++;ctr++;
        if(ctr==4)
        {
         player_wins();
        }
       }
      }
     }
     if(row==2||row==3||row==4||row==5||row==6)
     {
      if(col==1||col==2||col==3||col==4||col==5)
      {
       x=row+1;y=col-1;ctr=0;
       while(board[x][y]==player_WHO)
       {
        x--;y++;ctr++;
        if(ctr==4)
        {
         player_wins();
        }
       }
      }
     }
     if(row==0||row==1||row==2||row==3||row==4)
     {
      if(col==3||col==4||col==5||col==6||col==7)
      {
       x=row;y=col;ctr=0;
       while(board[x][y]==player_WHO)
       {
        x++;y--;ctr++;
        if(ctr==4)
        {
         player_wins();
        }
       }
      }
     }
     if(row==1||row==2||row==3||row==4||row==5)
     {
      if(col==2||col==3||col==4||col==5||col==6)
      {
       x=row-1;y=col+1;ctr=0;
       while(board[x][y]==player_WHO)
       {
        x++;y--;ctr++;
        if(ctr==4)
        {
         player_wins();
        }
       }
      }
     }
     if(row==3||row==4||row==5||row==6||row==7)
     {
      if(col==3||col==4||col==5||col==6||col==7)
      {
       x=row;y=col;ctr=0;
       while(board[x][y]==player_WHO)
       {
        x--;y--;ctr++;
        if(ctr==4)
        {
         player_wins();
        }
       }
      }
     }
     if(row==2||row==3||row==4||row==5||row==6)
     {
      if(col==2||col==3||col==4||col==5||col==6)
      {
       x=row+1;y=col+1;ctr=0;
       while(board[x][y]==player_WHO)
       {
        x--;y--;ctr++;
        if(ctr==4)
        {
         player_wins();
        }
       }
      }
     }
     full();
    }
    
    int full(void)
    {
     int x=0;
     int y=0;
     int ctr=0;
     for(x=0;x<=7;x++)
     {
      for(y=0;y<=7;y++)
      {
       if(board[x][y]!=0)
       {
        ctr++;
        if(ctr==board_FULL)
        {
         player_draw();
         break;
        }
       }
      }
     }
    }
    
    void player_wins(void)
    {
     int ctr;
     sleep(3);
     closegraph();
     textmode(C80);
     textcolor(YELLOW);
     textbackground(BLACK);
     clrscr();
     window(20,6,60,14);
     textbackground(BLUE);
     clrscr();
     gotoxy(2,1);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     gotoxy(2,9);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     for(ctr=1;ctr<=8;ctr++)
     {
      gotoxy(2,ctr);printf("º");gotoxy(40,ctr);printf("º");
     }
     gotoxy(2,1);printf("É");gotoxy(40,1);printf("»");
     gotoxy(2,9);printf("È");gotoxy(40,9);printf("¼");
     if(player_WHO==player_ONE)
     {
      gotoxy(12,4);printf("PLAYER 1 WINS!!!");
      gotoxy(13,6);printf("Press enter to continue...");
      getch();
     }
     if(player_WHO==player_TWO)
     {
      gotoxy(12,4);printf("PLAYER 2 WINS!!!");
      gotoxy(13,6);printf("Press enter to continue...");
      getch();
     }
     if(player_WHO==player1_ONE)
     {
      gotoxy(12,4);printf("PLAYER  1 WINS!!!");
      gotoxy(13,6);printf("Press enter to continue...");
      getch();
     }
     if(player_WHO==player_THREE)
     {
      gotoxy(12,4);printf("COMPUTER WINS!!!");
      gotoxy(11,4);printf("NICE GAME...");
      gotoxy(13,6);printf("Press enter to continue...");
      getch();
     }
     menu();
    }
    
    void player_draw(void)
    {
     int ctr;
     sleep(1);
     closegraph();
     textmode(C80);
     textcolor(YELLOW);
     textbackground(BLACK);
     clrscr();
     window(20,6,60,14);
     textbackground(BLUE);
     clrscr();
     gotoxy(2,1);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     gotoxy(2,9);printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
     for(ctr=1;ctr<=8;ctr++)
     {
      gotoxy(2,ctr);printf("º");gotoxy(40,ctr);printf("º");
     }
     gotoxy(2,1);printf("É");gotoxy(40,1);printf("»");
     gotoxy(2,9);printf("È");gotoxy(40,9);printf("¼");
     gotoxy(17,5);printf("IT'S A TIE!!!");
     getch();
     menu();
    }
    
    void close(void)
    {
     textmode(C80);
     textcolor(BLUE);
     textbackground(BLACK);
     clrscr();
     window(0,0,80,25);
     textbackground(BLACK);
     clrscr();
     textcolor(BLUE);
     printf("Thank you for playing...");
     getch();
     exit(1);
    }

  2. #2
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    I think the first thing you should tell your friend is to "use comments"
    "Assumptions are the mother of all **** ups!"

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    2
    the only problem in her program is the generation of computer move that i don't know how to put into codes that would make it run. please help me...

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    try to avoid the goto statements.
    they are known to be responsible for the dark age of the "spaghetti code".

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    ^
    ^
    ^
    HAHAHAHAHHAHAHAHAHAH

  6. #6
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    ^
    ^
    ^
    its true

Popular pages Recent additions subscribe to a feed