C/C++

Дискусија во форумот 'Програмирање и програмски јазици' започната од Драган, 12 мај 2006.

  1. Dim40

    Dim40

    Член од:
    18 јули 2017
    Мислења:
    32
    Допаѓања:
    7
    Еве од мене Нула Икс Нула игра у C++
    За саат време ја направив,функционална е, ама може да се подобри и тоа како.

    Код:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    //GLOBAL
    
    char grid[3][3]={
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'}
    };
    int width,height;
    bool gameover=true;             //koga e true, programata raboti
    char player;
    unsigned int turn=1;
    int trap=1;
    char rez;
    
    void setup();
    void draw();
    void input();
    void math();
    
    int main()
    {
    start:
    
    setup();
    while (gameover==true){
        draw();
        (turn%2==1)?player='X':player='O';
        input();
        system("cls");
        math();
        turn++;
    }
    
    cout<<"\nRestart(y/n)?";
                cin>>rez;
                if(rez=='y' || rez=='1' || rez=='Y'){
                system ("cls");
                goto start;
                }
    
        return 0;
    }
    
    void setup(){                           //Pocetni vrednosti(SAMO EDNAS)
    char number='1';
    for (int i=0;i<=2;i++){
    for (int j=0;j<=2;j++){
        grid[i][j]=number;
        number++;
        }
    }
    gameover=true;
    turn=1;
    trap=1;
    }
    
    void draw(){                       //Crtanje
    int incrsr1=0,incrsr2=0;       //Ne se potrebni, treba kako vo setup da go napravam ovoj del
    
    cout<<"Tic Tac Toe! \n";
    
    for (width=1;width<=7;width++){
            cout<<"#";
        }
    
        for (height=1;height<=6;height++){
            cout<<"\n";
            if(height==2 || height==4){
                incrsr1=0;
                incrsr2++;
            }
            if(height%2==1){
            for (width=1;width<8;width++){
    
                if(width==2 ||width==4 ||width==6){
                    cout<<grid[incrsr2][incrsr1];
                    incrsr1++;
                }
                else
                    cout<<"#";
    
            }
            }
            else{
    
            for (width=1;width<8;width++){
                    cout<<"#";
    
            }
            }
    }
    
    cout<<"\n";
    (turn%2==1)?cout<<"Player 1's turn (X) \n" : cout<<"Player 2's turn (O)\n";
    
    }
    
    void input(){             //Vnesuvanje
        cout<<"\n";
        char inp;
        cin>>inp;
        switch (inp){
    case '1':
        if(grid[0][0]!='X' && grid[0][0]!='O')
        grid[0][0]=player;
        else
        turn++;
            break;
    case '2':if(grid[0][1]!='X' && grid[0][1]!='O')
        grid[0][1]=player;
        else
        turn++;
        break;
    case '3':if(grid[0][2]!='X' && grid[0][2]!='O')
        grid[0][2]=player;
        else
        turn++;
            break;
    case '4':if(grid[1][0]!='X' && grid[1][0]!='O')
        grid[1][0]=player;
        else
        turn++;
        break;
    case '5':if(grid[1][1]!='X' && grid[1][1]!='O')
        grid[1][1]=player;
        else
        turn++;
            break;
    case '6':if(grid[1][2]!='X' && grid[1][2]!='O')
        grid[1][2]=player;
        else
        turn++;
        break;
    case '7':if(grid[2][0]!='X' && grid[2][0]!='O')
        grid[2][0]=player;
        else
        turn++;
            break;
    case '8':if(grid[2][1]!='X' && grid[2][1]!='O')
        grid[2][1]=player;
        else
        turn++;
        break;
    case '9':if(grid[2][2]!='X' && grid[2][2]!='O')
        grid[2][2]=player;
        else
        turn++;
        break;
        }
    }
    
    void math(){                   //Gleda dali nekoj pobedil ili ima uste potezi
    
    if((grid[0][0]=='X'&&grid[0][1]=='X' &&grid[0][2]=='X') ||
       (grid[1][0]=='X'&&grid[1][1]=='X' &&grid[1][2]=='X') ||
       (grid[2][0]=='X'&&grid[2][1]=='X' &&grid[2][2]=='X') ||
       (grid[0][0]=='X'&&grid[1][0]=='X' &&grid[2][0]=='X') ||
       (grid[0][1]=='X'&&grid[1][1]=='X' &&grid[2][1]=='X') ||
       (grid[0][2]=='X'&&grid[1][2]=='X' &&grid[2][2]=='X') ||
       (grid[0][0]=='X'&&grid[1][1]=='X' &&grid[2][2]=='X') ||
       (grid[0][2]=='X'&&grid[1][1]=='X' &&grid[2][0]=='X')
    ){
        gameover=false;
        draw();
        cout<<"\nPlayer 1 Win!\n";
    
    }
    
    if((grid[0][0]=='O'&&grid[0][1]=='O' &&grid[0][2]=='O') ||
       (grid[1][0]=='O'&&grid[1][1]=='O' &&grid[1][2]=='O') ||
       (grid[2][0]=='O'&&grid[2][1]=='O' &&grid[2][2]=='O') ||
       (grid[0][0]=='O'&&grid[1][0]=='O' &&grid[2][0]=='O') ||
       (grid[0][1]=='O'&&grid[1][1]=='O' &&grid[2][1]=='O') ||
       (grid[0][2]=='O'&&grid[1][2]=='O' &&grid[2][2]=='O') ||
       (grid[0][0]=='O'&&grid[1][1]=='O' &&grid[2][2]=='O') ||
       (grid[0][2]=='O'&&grid[1][1]=='O' &&grid[2][0]=='O')
    ){
        gameover=false;
        draw();
        cout<<"\nPlayer 2 Win!\n";
    }
    
    //proverka dali e TIE
    int trap=0;
    for (int i=0;i<=2;i++){
        for (int j=0;j<=2;j++){
            if (grid[j][i]=='O' || grid[j][i]=='X'){
                trap++;
                }
                }
                }
    if (trap==9){
                gameover=false;
                draw();
                cout<<"\nTIE!\n";
                }
    }
    
     
    Последно уредено: 13 ноември 2017
  2. Oleksandr

    Oleksandr

    Член од:
    10 март 2014
    Мислења:
    684
    Допаѓања:
    597
    Брат пробувам се и сешто едноставно така испадна овде.
     

Сподели преку: