Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

As is, the program does not terminate with a tie if all the nine positions on th

ID: 3856197 • Letter: A

Question

As is, the program does not terminate with a tie if all the nine positions on the board are occupied, and no user wins. Also, the program does not prevent a user from overwriting another user’s previous position. Make the necessary changes to correct these two oversights.

#include <iostream>

using namespace std;

void FillArray(char A[3][3]);

void DrawBoard(char A[3][3]);

void UserMove(char A[3][3], int user);

bool GameOver(char A[3][3], int user);

int main(int argc, char **argv)

{

bool flag;

char Array[3][3];

FillArray(Array);

int u = 0;

while (GameOver(Array, u % 2) == false)

{

DrawBoard(Array);

UserMove(Array, u % 2);

u++;

}

return 0;

}

void FillArray(char A[3][3])

{

for (int i = 0; i<3; i++)

{

for (int j = 0; j<3; j++)

A[i][j] = '*';

}

}

void DrawBoard(char A[3][3])

{

for (int i = 0; i<3; i++)

{

cout << " --- --- --- ";

for (int j = 0; j<3; j++)

cout << "| " << A[i][j] << " ";

cout << "| ";

}

cout << " --- --- --- ";

}

void UserMove(char A[3][3], int user)

{

int X, Y;

cout << "Where would you like to place your character?" << endl;

cin >> X >> Y;

if (user == 0)

A[X][Y] = 'X';

else

A[X][Y] = 'O';

}

bool GameOver(char A[3][3], int user)

{

bool flag = 0;

for (int i = 0; i < 3; i++)

{

if ((A[i][0] == A[i][1]) && (A[i][2] == A[i][1]) && (A[i][0] != '*'))

{

if (user == 0)

cout << "User A wins!" << endl;

else if (user == 1)

cout << "User B wins!" << endl;

flag = true;

}

}

for (int j = 0; j < 3; j++)

{

if ((A[0][j] == A[1][j]) && (A[2][j] == A[1][j]) && (A[0][j] != '*'))

{

if (user == 0)

cout << "User A wins!" << endl;

else if (user == 1)

cout << "User B wins!" << endl;

flag = true;

}

}

if ((((A[0][0] == A[1][1]) && (A[1][1] == A[2][2])) || ((A[0][2] == A[1][1]) && (A[1][1] == A[2][0]))) && (A[1][1] != '*'))

{

if (user == 0)

cout << "User A wins!" << endl;

else if (user == 1)

cout << "User B wins!" << endl;

flag = true;

}

return flag;

}

Explanation / Answer

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

#include <iostream>

using namespace std;

void drawBoard(string position[3][3])

{

    cout << "     0    1    2 ";

    cout << "       |    |    ";

    cout << "0   " << position[0][0] << " | " << position[0][1] << " | " << position[0][2] << " ";

    cout << "   ____|____|____ ";

    cout << "       |    |    ";

    cout << "1   " << position[1][0] << " | " << position[1][1] << " | " << position[1][2] << " ";

    cout << "   ____|____|____ ";

    cout << "       |    |    ";

    cout << "2   " << position[2][0] << " | " << position[2][1] << " | " << position[2][2] << " ";

    cout << "       |    |    ";

}

int checkForWinner (string position[3][3])

{

    /// check to see if X won the game

    //horizontal

    if (position[0][0]=="X" && position[0][1]=="X" && position[0][2]=="X")

    return 1;

    if (position[1][0]=="X" && position[1][1]=="X" && position[1][2]=="X")

    return 1;

    if (position[2][0]=="X" && position[2][1]=="X" && position[2][2]=="X")

    return 1;

    //vertical

    if (position[0][0]=="X" && position[1][0]=="X" && position[2][0]=="X")

    return 1;

    if (position[0][1]=="X" && position[1][1]=="X" && position[2][1]=="X")

    return 1;

    if (position[0][2]=="X" && position[1][2]=="X" && position[2][2]=="X")

    return 1;

    //diagonal

    if (position[0][0]=="X" && position[1][1]=="X" && position[2][2]=="X")

    return 1;

    if (position[0][2]=="X" && position[1][1]=="X" && position[2][0]=="X")

    return 1;

    /// check if O won

    //horizontal

    if (position[0][0]=="O" && position[0][1]=="O" && position[0][2]=="O")

    return 2;

    if (position[1][0]=="O" && position[1][1]=="O" && position[1][2]=="O")

    return 2;

    if (position[2][0]=="O" && position[2][1]=="O" && position[2][2]=="O")

    return 2;

    // vertical

    if (position[0][0]=="O" && position[1][0]=="O" && position[2][0]=="O")

    return 2;

    if (position[0][1]=="O" && position[1][1]=="O" && position[2][1]=="O")

    return 2;

    if (position[0][2]=="O" && position[1][2]=="O" && position[2][2]=="O")

    return 2;

    // diagonal

    if (position[0][0]=="O" && position[1][1]=="O" && position[2][2]=="O")

    return 2;

    if (position[0][2]=="O" && position[1][1]=="O" && position[2][0]=="O")

    return 2;

}

bool checkForTie ( string position[3][3])

{

    bool tie;

    int non_empty;

    non_empty=0;

    for (int i=0; i<3; i++)

    {

        for (int j=0; j<3; j++)

        {

            if (position[i][j]!=" ")

            {

                non_empty++;

            }

        }

    }

    if (non_empty==9)

    {

        tie=true;

    }

    else

    {

        tie=false;

    }

    return tie;

}

int main()

{

    bool winner;

    int win_check;

    bool tie_check;

    string position [3][3];

    for (int i=0; i<3; i++)

    {

        for (int j=0; j<3; j++)

        {

            position[i][j]=" ";

        }

    }

    drawBoard(position);

    do

    {

        int i;

        int j;

        cout<<"Player 1, your move. ";

        cout<<"Row? ";

        cin>>i;

        cout<<"Column? ";

        cin>>j;

        position[i][j]="X";

        drawBoard(position);

        win_check=checkForWinner(position);

        if (win_check==1)

        {

            winner=true;

            cout<<"We have a WINNER. Player 1 WON! ";

        }

        else

        {

            winner=false;

        }

        tie_check=checkForTie(position);

        if (tie_check==true)

        {

            cout<<"It's a TIE.";

        }

        cout<<"Player 2, your move. ";

        cout<<"Row?";

        cin>>i;

        cout<<"Column? ";

        cin>>j;

        position[i][j]="O";

        drawBoard(position);

        win_check=checkForWinner(position);

         

        if (win_check==2)

        {

            winner=true;

            cout<<"We have a WINNER. Player 2 WON! ";

        }

        else

        {

            winner=false;

        }

         

        tie_check=checkForTie(position);

         

        if (tie_check==true)

        {

            cout<<"It's a TIE.";

        }

    } while (winner!=true && tie_check!=true);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

#include <iostream>

using namespace std;

void drawBoard(string position[3][3])

{

    cout << "     0    1    2 ";

    cout << "       |    |    ";

    cout << "0   " << position[0][0] << " | " << position[0][1] << " | " << position[0][2] << " ";

    cout << "   ____|____|____ ";

    cout << "       |    |    ";

    cout << "1   " << position[1][0] << " | " << position[1][1] << " | " << position[1][2] << " ";

    cout << "   ____|____|____ ";

    cout << "       |    |    ";

    cout << "2   " << position[2][0] << " | " << position[2][1] << " | " << position[2][2] << " ";

    cout << "       |    |    ";

}

int checkForWinner (string position[3][3])

{

    /// check to see if X won the game

    //horizontal

    if (position[0][0]=="X" && position[0][1]=="X" && position[0][2]=="X")

    return 1;

    if (position[1][0]=="X" && position[1][1]=="X" && position[1][2]=="X")

    return 1;

    if (position[2][0]=="X" && position[2][1]=="X" && position[2][2]=="X")

    return 1;

    //vertical

    if (position[0][0]=="X" && position[1][0]=="X" && position[2][0]=="X")

    return 1;

    if (position[0][1]=="X" && position[1][1]=="X" && position[2][1]=="X")

    return 1;

    if (position[0][2]=="X" && position[1][2]=="X" && position[2][2]=="X")

    return 1;

    //diagonal

    if (position[0][0]=="X" && position[1][1]=="X" && position[2][2]=="X")

    return 1;

    if (position[0][2]=="X" && position[1][1]=="X" && position[2][0]=="X")

    return 1;

    /// check if O won

    //horizontal

    if (position[0][0]=="O" && position[0][1]=="O" && position[0][2]=="O")

    return 2;

    if (position[1][0]=="O" && position[1][1]=="O" && position[1][2]=="O")

    return 2;

    if (position[2][0]=="O" && position[2][1]=="O" && position[2][2]=="O")

    return 2;

    // vertical

    if (position[0][0]=="O" && position[1][0]=="O" && position[2][0]=="O")

    return 2;

    if (position[0][1]=="O" && position[1][1]=="O" && position[2][1]=="O")

    return 2;

    if (position[0][2]=="O" && position[1][2]=="O" && position[2][2]=="O")

    return 2;

    // diagonal

    if (position[0][0]=="O" && position[1][1]=="O" && position[2][2]=="O")

    return 2;

    if (position[0][2]=="O" && position[1][1]=="O" && position[2][0]=="O")

    return 2;

}

bool checkForTie ( string position[3][3])

{

    bool tie;

    int non_empty;

    non_empty=0;

    for (int i=0; i<3; i++)

    {

        for (int j=0; j<3; j++)

        {

            if (position[i][j]!=" ")

            {

                non_empty++;

            }

        }

    }

    if (non_empty==9)

    {

        tie=true;

    }

    else

    {

        tie=false;

    }

    return tie;

}

int main()

{

    bool winner;

    int win_check;

    bool tie_check;

    string position [3][3];

    for (int i=0; i<3; i++)

    {

        for (int j=0; j<3; j++)

        {

            position[i][j]=" ";

        }

    }

    drawBoard(position);

    do

    {

        int i;

        int j;

        cout<<"Player 1, your move. ";

        cout<<"Row? ";

        cin>>i;

        cout<<"Column? ";

        cin>>j;

        position[i][j]="X";

        drawBoard(position);

        win_check=checkForWinner(position);

        if (win_check==1)

        {

            winner=true;

            cout<<"We have a WINNER. Player 1 WON! ";

        }

        else

        {

            winner=false;

        }

        tie_check=checkForTie(position);

        if (tie_check==true)

        {

            cout<<"It's a TIE.";

        }

        cout<<"Player 2, your move. ";

        cout<<"Row?";

        cin>>i;

        cout<<"Column? ";

        cin>>j;

        position[i][j]="O";

        drawBoard(position);

        win_check=checkForWinner(position);

         

        if (win_check==2)

        {

            winner=true;

            cout<<"We have a WINNER. Player 2 WON! ";

        }

        else

        {

            winner=false;

        }

         

        tie_check=checkForTie(position);

         

        if (tie_check==true)

        {

            cout<<"It's a TIE.";

        }

    } while (winner!=true && tie_check!=true);

}