jeudi 7 juillet 2022

Failing Tictactoe in c++

I'm a beginner in c++, i'm trying to make a tictactoe. My program fails at the part acfter i enter input. When I enter an input, there is no next action from the program like i expected it to ("unvalid", check if win or loose, ask for input again). It only shows blank. like below: after I enter "1", nothing happens, and I can keep enter more input. terminal example photo

I know it is a simple activity but I just ca't figure it out ToT. Thank you for helping!

//tictactoe 
#include <iostream>
#include <vector>
using namespace std;

//declared variables
vector<char> out = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int in = 2;
char player_out;
bool loose = false;
char x;
bool filled = false;


bool end(){ //won yet?
    bool loose = false;
    //horizontal
    if (out[1] == out[2] && out[3] == out[2]){
        loose = true;
    }
    else if (out[4] == out[5] && out[6] == out[5]){
        loose = true;
    }
    else if (out[7] == out[8] && out[9] == out[8]){
        loose = true; 
    }

    //vertical
    else if (out[1] == out[4] && out[7] == out[1]){
        loose = true;
    }
    else if (out[2] == out[5] && out[8] == out[2]){
        loose = true;
    }
    else if (out[3] == out[6] && out[9] == out[3]){
        loose = true;
    }
    else if (out[1] == out[5] && out[9] == out[5]){
        loose = true;
    }
    else if (out[3] == out[5] && out[7] == out[5]){
        loose = true;
    }
    else{
        loose = false;
    }
    return loose;
}

void game_start_display(){ //display the board
    cout << "TIC TAC TOE\n";
cout << "      |      |      \n";
cout << "  " << out[1] << "   |  " << out[2] << "   |  " << out[3] << "    \n";
cout << "______|______|______\n";
cout << "      |      |      \n";
cout << "  " << out[4] << "   |  " << out[5] << "   |  " << out[6] << "    \n";
cout << "______|______|______\n";
cout << "      |      |      \n";
cout << "  " << out[7] << "   |  " << out[8] << "   |  " << out[9] << "    \n";
cout << "      |      |      \n\n";
}
int change_player(){ //take turn 1st and 2nd player
    if (in == 1){
        in++;
    }
    else{
        in--;
    }
    return in;
}

bool filled_f() { //check if the spot is filled
    if (out[x] != 'X' and out[x] != 'O'){
        filled = true;
        out[x] = player_out; //fill the input into the spot
    }
    else if (out[x] == 'X' or out[x] == 'O')
        cout << "The square has already been used!\n";
        filled = false;

    return filled;
}

char player_out_f(){ //change output sign for each players (X, O)
    if (in == 1){
        player_out = 'X';
    }
    else if (in == 2){
        player_out = 'O';
    }
    return player_out;

}

void c_player_display(){ //tell players to enter a number
        cout << "Player " << in << "'s turn, please enter a number:\n";
    }

int main(){


//intro
    int loose = false;
    game_start_display();




    while(loose == false){  //when the game is still happening
        change_player(); //change player (player start is set 2 so this comes first and change it to 1)
        player_out_f(); //change player output sign (X, O)
        c_player_display(); //print the line to ask for input
    
        

        while(filled == false){ //when the there is no input yet (the spot is not filled)
            cin >> x; // input
            if (x > 0 && x < 10){ //check if input is in range 1-9
                filled_f(); //check if the spot is occupied
            }
            else if(x < 0 && x > 10) { //if input is out of range
                cout << "Invalid! Enter again!\n"; 
                filled = false; //repeat the asking input circle (the while)
            }

        }
    
        game_start_display(); //output the board again with new char (X or O)
        end(); //check if anyone's won yet, if no, repeat the circle

}
    cout << "Player " << in << " won! GG";
}   

Aucun commentaire:

Enregistrer un commentaire