• Log in or sign up for an account here: https://www.sythe.org/login

my first programme :) [connect 4]

  • Thread starter Thread starter mog13
  • Start date Start date
M

mog13

Guest
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
 
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;
       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

Good effort for a first program.. wherabouts did you learn?
 
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;
       }
 
Good effort for a first program.. wherabouts did you learn?

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.


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.

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
 
i suppose a loop that carrys on untill a integer below 6 is found would work ?
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.
 
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.

ohh yer XD badly thought out on my part, thankyou very much for youre code correction, i have edited my programme with it.
 
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 :)
 
Runescape Gamblingrunescape gambling
Back
Top