my first programme :) [connect 4]

Discussion in 'Programming General' started by mog13, Jul 28, 2008.

my first programme :) [connect 4]
  1. Unread #1 - Jul 28, 2008 at 11:46 AM
  2. mog13
    Referrals:
    0

    mog13 Guest

    my first programme :) [connect 4]

    ok well it isnt my FIRST programme :p but i doubted anyone would care about a "hello world" app, or a 2 number adder. so im counting this as my first, well first finished, programme.

    Code:
       
     #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    string map[10][10]; //[x],[y]
    bool p1 = 1,p2 = 0;
    
    inline int draw_map(void) {
    int i =1,ii=1;
    
    cout << "[1][2][3][4][5][6]\n\n";
    lines: for ( ; i<=6;){
            cout <<"[" << map [i][ii] << "]";
            i++;
            }
    ii++;
    i = 1;
    cout <<"\n";
    if ( ii <=6)goto lines;
    int l;
    for(l=0;l<15;l++)cout << "\n";
    }
    
    inline int clear_map(void){
    int i = 1,ii=0;
          exs: ii ++;
               for(;i<=6;i++) {
                           
                map [i][ii] = "-";
                         
                }
                i = 1;
                if (ii <6) goto exs;
                }
                
    
    
    inline int checkwin(int pox, int poy){
           if ((map[pox+1][poy] == map[pox][poy] && map[pox+2][poy] == map[pox][poy] && map[pox+3][poy] == map[pox][poy])
           or(map[pox][poy+1] == map[pox][poy] && map[pox][poy+2] == map[pox][poy] && map[pox][poy+3] == map[pox][poy])
           or(map[pox-1][poy] == map[pox][poy] && map[pox-2][poy] == map[pox][poy] && map[pox-3][poy] == map[pox][poy])
           or(map[pox+1][poy+1] == map[pox][poy] && map[pox+2][poy+2] == map[pox][poy] && map[pox+3][poy+3] == map[pox][poy])
           or(map[pox-1][poy-1] == map[pox][poy] && map[pox-2][poy-2] == map[pox][poy] && map[pox-3][poy-3] == map[pox][poy])
           or(map[pox+1][poy-1] == map[pox][poy] && map[pox+2][poy-2] == map[pox][poy] && map[pox+3][poy-3] == map[pox][poy])
           or(map[pox-1][poy+1] == map[pox][poy] && map[pox-2][poy+2] == map[pox][poy] && map[pox-3][poy+3] == map[pox][poy])
           or(map[pox-1][poy-1] == map[pox][poy] && map[pox+1][poy+1] == map[pox][poy] && map[pox+2][poy+2] == map[pox][poy])
           or(map[pox-1][poy-1] == map[pox][poy] && map[pox+1][poy+1] == map[pox][poy] && map[pox-2][poy-2] == map[pox][poy])
           or(map[pox-1][poy-1] == map[pox][poy] && map[pox+1][poy+1] == map[pox][poy] && map[pox+2][poy+2] == map[pox][poy])
           or(map[pox-1][poy-1] == map[pox][poy] && map[pox+1][poy-1] == map[pox][poy] && map[pox+2][poy-2] == map[pox][poy])
           or(map[pox+1][poy-1] == map[pox][poy] && map[pox-1][poy+1] == map[pox][poy] && map[pox-2][poy+2] == map[pox][poy])
           or(map[pox+1][poy] == map[pox][poy] && map[pox-1][poy] == map[pox][poy] && map[pox-2][poy] == map[pox][poy])
           or(map[pox-1][poy] == map[pox][poy] && map[pox+1][poy] == map[pox][poy] && map[pox+2][poy] == map[pox][poy])
           or(map[pox-1][poy+1] == map[pox][poy] && map[pox-1][poy-1] == map[pox][poy] && map[pox-2][poy-2] == map[pox][poy])
           or(map[pox-1][poy+1] == map[pox][poy] && map[pox-2][poy+2] == map[pox][poy] && map[pox+1][poy-1] == map[pox][poy])
           or(map[pox+1][poy+1] == map[pox][poy] && map[pox-1][poy-1] == map[pox][poy] && map[pox-2][poy-2] == map[pox][poy])
           or(map[pox-1][poy-1] == map[pox][poy] && map[pox+1][poy+1] == map[pox][poy] && map[pox+2][poy+2] == map[pox][poy])
            ) {
           
          if (p1 == 1) cout << " player 1 wins\n";
           if (p2 == 1) cout << " player 2 wins\n";
           Sleep(8000);
        
         
       exit(0);
           
           }
           
           
    }
    
    inline int anim_move(int pos){
           int i=1;
       
           for (;i<=6;i++){ 
               if (map[pos][i] != "-") break;
               if (map[pos][i]=="-") { 
               if (p1 == 1)map[pos][i] = "X";
               if (p2 == 1)map[pos][i] = "O";
               map[pos][i-1] = "-";
               }
               draw_map();
               Sleep(100);
               }
               checkwin(pos,i-1);
               return 0;
           }
           
    inline int go(){
           int place;
           while(place > 6 || place < 1) {
           cout << "Where would you like to go?" ;
           cin >> place;
           }
    
           anim_move(place);
           return 0;
    }
                
    int main(int argc, char *argv[])
    {
        clear_map();
        draw_map(); 
       for(;;){
    if (p1 = 1){
       cout << "player 1's go\n" ;               
       go();
       p2 = 1;
       p1 = 0;
    }
       if (p2 = 1){
       cout << "player 2's go\n" << p2;                
       go();
       
       p2 = 0;
       p1 = 1;
    }
      
       }    
    }
    
    i know there are thigns i could of probably/definatley done better, like not check for a win every step =/, but its only small so doesnt really effect it :p it would be helpful if you could give CC :) especially a easyer way to check for a win? thanks
     
  3. Unread #2 - Jul 31, 2008 at 9:58 PM
  4. demonavenger
    Joined:
    Feb 25, 2007
    Posts:
    536
    Referrals:
    0
    Sythe Gold:
    0

    demonavenger Forum Addict
    $5 USD Donor

    my first programme :) [connect 4]

    Good effort for a first program.. wherabouts did you learn?
     
  5. Unread #3 - Aug 1, 2008 at 1:52 AM
  6. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    my first programme :) [connect 4]

    I'm tired so I don't feel like going over it all now, but I'll probably have another look soon. It's pretty well done especially since you say you've started recently. I will point out that your go() function should be modified so that the program doesn't get a fatal error on bad input from the user. I'll get back to you with more later.
    Code:
    inline int go(){
           int place;
           while(place > 6 || place < 1) {
           cout << "Where would you like to go?" ;
           cin >> place;
           }
           anim_move(place);
           return 0;
           }
     
  7. Unread #4 - Aug 2, 2008 at 8:38 AM
  8. mog13
    Referrals:
    0

    mog13 Guest

    my first programme :) [connect 4]

    thanks :) umm well a few books and i typed c++ tutorials into google and used some sites there, ive programmed alot in other languages, so am used to it so found it easyer. currently im using "http://lazyfoo.net/SDL_tutorials/index.php" to learn SDL.


    thankyou very much! ohh yer i didnt really notice since i only ever enter good input :p lol shoudl of thought about that. i suppose a loop that carrys on untill a integer below 6 is found would work ? hmm
     
  9. Unread #5 - Aug 3, 2008 at 2:29 PM
  10. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    my first programme :) [connect 4]

    No, because then it would accept input of zero or negative values which would indeed cause an error. My previous post included the correct method to check for valid input.
     
  11. Unread #6 - Aug 3, 2008 at 3:33 PM
  12. mog13
    Referrals:
    0

    mog13 Guest

    my first programme :) [connect 4]

    ohh yer XD badly thought out on my part, thankyou very much for youre code correction, i have edited my programme with it.
     
  13. Unread #7 - Aug 4, 2008 at 10:38 PM
  14. The Supreme Intelligence
    Joined:
    Apr 29, 2007
    Posts:
    738
    Referrals:
    0
    Sythe Gold:
    0

    The Supreme Intelligence Apprentice
    Banned

    my first programme :) [connect 4]

    Well not everyone's first program was a hello world or a plus 2 program. Mine personally was a calculator with all 4 basic functions, using only printf and scanf :)
     
< Starting VB.` | :) >

Users viewing this thread
1 guest


 
 
Adblock breaks this site