Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I made a Tic Tac Toe game, but I think there's a lot of hard-coding and useless variables. Can you check it out and tell me what I can improve on? It works, but it has a couple of bugs.

#include <iostream>
#include <stdlib.h> 
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<iomanip>
using namespace std;
bool check_right_diagonal(char **arr, int ver_s, int hor_s, bool &win_p1, bool &win_p2, int i, bool &func_worked){
    for (i = 0; i < ver_s; i++){
        for (static int k = ver_s-1; k >= i||k<i ; k--){
            if (arr[i][k] == '_'){
                func_worked = false;
                win_p1 = false;
                win_p2 = false;
                break;
            }
            if (arr[i][k] == 'X'){
                if (win_p2){
                    win_p1 = false;
                    win_p2 = false;
                    break;
                }
                win_p1 = true;
                win_p2 = false;
                break;
            }
            else if (arr[i][k] == 'O'){
                if (win_p1){
                    win_p1 = false;
                    win_p2 = false;
                    break;
                }
                win_p2 = true;
                win_p1 = false;
                break;
            }

        }


    }
    if (win_p1){
        cout << "P1__WON!" << endl;
        func_worked = true;
        return -1;
    }
    else if (win_p2){
        cout << "P2_WON!" << endl;
        func_worked = true;
        return -1;
    }

}
bool check_left_diagonal(char **arr, int ver_s, int hor_s, bool &win_p1, bool &win_p2, int i,bool &func_worked){
    for (i = 0; i < ver_s; i++){
        for (static int k = 0; k < i + 1; k++){
            if (arr[i][k] == '_'){
                func_worked = false;
                win_p1 = false;
                win_p2 = false;
                break;
            }
            if (arr[i][k] == 'X'){
                if (win_p2){
                    win_p1 = false;
                    win_p2 = false;
                    break;
                }
                win_p1 = true;
                win_p2 = false;
            }
            else if (arr[i][k] == 'O'){
                if (win_p1){
                    win_p1 = false;
                    win_p2 = false;
                    break;
                }
                win_p2 = true;
                win_p1 = false;
            }

        }


    }
    if (win_p1){
        cout << "P1__WON!" << endl;
        func_worked = true;
        return -1;
    }
    else if (win_p2){
        cout << "P2_WON!" << endl;
        func_worked = true;
        return -1;
    }

}
bool check_verticalANDhorizontal_win(char **arr, int ver_s, int hor_s, char p1_m, char p2_m, bool &win_p1, bool &win_p2, int i, int j,bool &func_worked){

        for (i = 0; i < ver_s; i++){
            for (j = 0; j < hor_s; j++){
                if (arr[i][j] == '_'){
                    func_worked = false;
                    win_p1 = false;
                    win_p2 = false;
                    break;
                }
                if (arr[i][j] == p1_m){
                        if (win_p2 ){
                        func_worked = false;
                        win_p1 = false;
                        win_p2 = false;
                        break;
                    }
                    win_p1 = true;
                    win_p2 = false;
                }
                else if (arr[i][j] == p2_m ){
                    if (arr[i][j] == '_')break;
                    if (win_p1){
                        func_worked = false;
                        win_p1 = false;
                        win_p2 = false;
                        break;
                    }
                    win_p1 = false;
                    win_p2 = true;
                }



            }
            if (win_p1 || win_p2 && j == hor_s)break;
        }
        if (win_p1){

            cout << "P1__WON!" << endl;
            func_worked = true;
            return -1;
        }
        else if (win_p2){
            func_worked = true;
            cout << "P2_WON!" << endl;
            return -1;
        }
        func_worked = false;
        return 0;

}
void draw_field(char **arr,int size){
    for (int i = 0; i < size; i++)
        cout<<setw(4)<< i;
    cout << endl;
    for (int i = 0; i < size; i++){
        cout << i<<setw(3);
        for (int j = 0; j < size; j++){     
            cout << arr[i][j]<<"   ";
        }
        cout << endl;
    }
}

bool check_occupied_pos(int p_i,int p_j,char **arr,char p1_mark,char p2_mark){
    if (arr[p_i][p_j] == p1_mark || arr[p_i][p_j] == p2_mark){
        cout << "Teritorry is occupied"<< endl;
        return false;
    }
    else if (arr[p_i][p_j] == p2_mark || arr[p_i][p_j] == p1_mark){
        cout << "Teritorry is occupied"<< endl;
        return false;
    }
    else
        return true;

}
void TIC_TAC_TOE_PRINT(){
    char mass[9][74] = {
        {"TTTTTTTT  IIII  CCCCCC  TTTTTTT  AAAAAA  CCCCCC  TTTTTTT  OOOOOO  EEEEEEE"},
        {"TTTTTTTT  IIII  CCCCCC  TTTTTTT  AAAAAA  CCCCCC  TTTTTTT  OOOOOO  EEEEEEE" },
        {"  TTT           CC        TTT    AA  AA  CC        TTT    OO  OO  EE     "},
        {"  TTT      II   CC        TTT    AA  AA  CC        TTT    OO  OO  EEEEE  "},
        {"  TTT      II   CC        TTT    AA  AA  CC        TTT    OO  OO  EEEEE  "},
        {"  TTT      II   CC        TTT    AAAAAA  CC        TTT    OO  OO  EE     "},
        {"  TTT      II   CCCCCC    TTT    AA  AA  CCCCCC    TTT    OOOOOO  EEEEEEE"},
        {"  TTT      II   CCCCCC    TTT    AA  AA  CCCCCC    TTT    OOOOOO  EEEEEEE"}
    };
    for (int i = 0; i < 9; i++){
        for (int j = 0; j < 74; j++){
            cout <<mass[i][j];
        }
        cout << endl;
    }

}
void WinGame(char **arr,int ver_s,int hor_s, char p1_m,char p2_m,bool &GAME_START){
    bool win_p1 = false;
    bool win_p2 = false;
    bool func_worked = false;
    int j = 0;
    int i = 0;

    check_verticalANDhorizontal_win(arr, ver_s, hor_s, p1_m, p2_m, win_p1, win_p2, i, j,func_worked);
    if (!func_worked)
    check_verticalANDhorizontal_win(arr, ver_s, hor_s, p1_m, p2_m, win_p1, win_p2, j, i,func_worked);
    if (!func_worked){
    check_left_diagonal(arr, ver_s, hor_s, win_p1, win_p2, i,func_worked);
    }
    if (!func_worked){
        check_right_diagonal(arr, ver_s, hor_s, win_p1, win_p2, i, func_worked);
    }
    if (win_p1 && !win_p2 || win_p1 && win_p2)
        GAME_START = false;
    else
        GAME_START = true;



}
int main()
{
    char p1_mark = 'X'; char p2_mark = 'O';

    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    TIC_TAC_TOE_PRINT();
    SetConsoleTextAttribute(h, FOREGROUND_BLUE | FOREGROUND_INTENSITY); 
    //Initialization-------------------------------
    int ver_s =  3;
    int hor_s = 3;
    bool GAME_START = TRUE;
    int field_size = ver_s*hor_s;
    int *fields = new int[field_size];
    for (int i = 0; i < field_size; i++){
        fields[i] = 0;
        fields[i] = fields[i] + i;

    }

    char **GameField = new char*[ver_s];
    for (int i = 0; i < ver_s; ++i)
        GameField[i] = new char[hor_s];

    for (int i = 0; i < ver_s; i++){
        for (int j = 0; j < hor_s; j++){
            GameField[i][j] ='_' ;

        }


    }
    draw_field(GameField, ver_s);

    //end of initialization-------------------------Beginning of tic tac toe game logic
    //int P1_random_num = 4;
    //int P2_random_num = 3;
    bool p2_try = false;
    bool p1_try = true;
    int p1_move_choice_i = 0;
    int p1_move_choice_j = 0;

    int p2_move_choice_i = 0;
    int p2_move_choice_j = 0;
        while (GAME_START){
            if (p1_try){
                cout << "Player_1,enter two numbers(position) of where to place" << endl;
                cin >> p1_move_choice_i;
                cin >> p1_move_choice_j;
                if (check_occupied_pos(p1_move_choice_i, p1_move_choice_j, GameField, p1_mark, p2_mark)){
                    GameField[p1_move_choice_i][p1_move_choice_j] = NULL;
                    GameField[p1_move_choice_i][p1_move_choice_j] = 'X';
                    draw_field(GameField, ver_s);
                    p1_try = false;
                    p2_try = true;
                }
                else continue;
            }

            else if (p2_try){
                cout << "Player_2, enter two numbers(position) of where to place" << endl;
                cin >> p2_move_choice_i;
                cin >> p2_move_choice_j;
                if (check_occupied_pos(p2_move_choice_i, p2_move_choice_j, GameField, p1_mark, p2_mark)){
                    GameField[p2_move_choice_i][p2_move_choice_j] = NULL;
                    GameField[p2_move_choice_i][p2_move_choice_j] = 'O';
                    draw_field(GameField, ver_s);
                    p2_try = false;
                    p1_try = true;
                }
                else continue;
            }
            WinGame(GameField, ver_s, hor_s, p1_mark, p2_mark, GAME_START);


        }



}
share|improve this question
    
Did you try to get rid of the hard coded/useless variables yourself? Any particular troubles you encountered that we can help you with? –  Emily L. Aug 27 at 13:45
    
If I got rid of the hard coded variables myself, would my question exist??? I posted this question to get a review of it, that someone could improve it and tell me how –  Mandruk Aug 27 at 14:01
    
I asked for things that you had problems with when trying to get rid of them, so that I could better explain to you how to do it. –  Emily L. Aug 27 at 14:58
    
maybe lack of exp) –  Mandruk Aug 27 at 15:02
    
@Mandruk Use the @ when taling with users, it notifies them. –  Caridorc Aug 27 at 15:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.