It works but it's not smart. help?

Ok so I am new to this forum and really forums in general, but I needed some help and because I'm not sure how to do something. I am also sorry if this post is in the wrong place, I looked at the forums pages to see if there was already a good posting for this but there are 51 pages of posts. My understanding of programming is very little so examples of code would be helpful.

I would like to add to this a way for it to
1. Scan cells/look for opponents plays & patterns to try to block
2. If there is nothing to block then pick a random open cell.
and just have the program repeat steps 1 & 2.

I have included my code below but when I preview it it doesn't look like code. sorry if that is an issue. I selected the code format at the beginning and end not sure if it worked :(

#include <stdio.h>

// Prototype the functions
int CheckEndOfGame(int * squares);
int ChooseSquare(int * squares);
int MakeMoves(int * ctrMoves, int * playr, int * squares);

int main()
{ /* High Level - play one or more games:
Players make moves until one wins or it's a draw.
Announce the winner (or draw).
Ask player to play again; play game or stop.
*/

// Declare variables
int gameOver = 0; // no, game is not over yet
int playGame = 1; // yes, play the game

do // Play Game(s)
{ // Store all choices in an array of 9 squares (squares[9])
// initialize the array with spaces (space is ASCII 32)
int squares[9] = {32, 32, 32, 32, 32, 32, 32, 32, 32};
// ("X" is ASCII 88)
// ("O" is ASCII 79)
int ctrMoves = 0; // no moves yet
int player = 0; // [[see "if" in MakeMoves]]

do // Make one move
{ gameOver = MakeMoves(&ctrMoves, &player, squares);
}
while (!gameOver);

// draw the board (before declaring a winner), then display
printf("\n");
printf(" %c | %c | %c \n", squares[6], squares[7], squares[8]);
printf("-----------\n");
printf(" %c | %c | %c \n", squares[3], squares[4], squares[5]);
printf("-----------\n");
printf(" %c | %c | %c \n", squares[0], squares[1], squares[2]);
printf("\n");

if (gameOver == 1)
printf("The game ended in a draw.\n");
else if (gameOver == 2)
printf("Congratulations! You won!\n");
else
printf("The computer won.\n");

printf("Play again? (1 is Yes, 0 is No). ");
scanf("%d", &playGame);

} while (playGame == 1);

/*
Wrapup
"Thanks"
clean up

Other
*/
return 0;
}

int MakeMoves(int * ctrMoves, int * playr, int * squares)
{ /* Middle Level - make one move, check for winner:
When X takes a turn, draw the board, ask for chosen square, store it.
When O takes a turn, program chooses square, store it if game not over.
*/

int endTheGame = 0; // no, game has not ended yet
int thisSquare = 0;
int drawTheBoard = 0;

// get a choice (from person or program), then store it in the array
if (*playr == 0)
{ printf("\n");
printf(" %c | %c | %c \n", squares[6], squares[7], squares[8]);
printf("-----------\n");
printf(" %c | %c | %c \n", squares[3], squares[4], squares[5]);
printf("-----------\n");
printf(" %c | %c | %c \n", squares[0], squares[1], squares[2]);
printf("\n");

*playr = 1; // it's now the person's turn

printf("Choose a square (1 - 9). \n");
scanf("%d", &thisSquare);

squares[thisSquare - 1] = 88;
// subtract 1 from thisSquare to get the array subscript
// eg, center square, 5, is square[4]
}
else
{ *playr = 0; // it's now the program's turn

thisSquare = ChooseSquare(squares);
if (thisSquare == 0)
*ctrMoves = 9; // tie - no squares left; game over
else
squares[thisSquare - 1] = 79;
// subtract 1 from thisSquare to get the array subscript
// eg, center square, 5, is square[4]
}

*ctrMoves = *ctrMoves + 1;
if (*ctrMoves > 9)
endTheGame = 1; // tie - no squares left; game over
else if (*ctrMoves >= 5)
endTheGame = CheckForWinner(squares);

return endTheGame;
}

int ChooseSquare(int * squares)
{ /* Low Level (1) - find the best empty square
(eg, squares[4] == 32 means center square is open)
*/

if (squares[4] == 32)
return 5;
else
if (squares[0] == 32)
return 1;
else
if (squares[2] == 32)
return 3;
else
if (squares[6] == 32)
return 7;
else
if (squares[8] == 32)
return 9;
else
if (squares[1] == 32)
return 2;
else
if (squares[3] == 32)
return 4;
else
if (squares[5] == 32)
return 6;
else
if (squares[7] == 32)
return 8;
else
return 0; // tie - no squares left; game over
}

int CheckForWinner(int * squares)
{ /* Low Level (2) - check whether person won, program won, or draw
winners: 1-2-3 | 4-5-6 | 7-8-9 || 1-4-7 | 2-5-8 | 3-6-9 || 1-5-9 | 3-5-7
0 means game not over; 1 means draw; 2 means person won; 3 means program won
*/

if (squares[0] == squares[1] && squares[1] == squares[2] && squares[2] != 32)
{ // top row
if (squares[2] == 88) // X won
return 2;
else
return 3;
}
else if (squares[3] == squares[4] && squares[4] == squares[5] && squares[5] != 32)
{ // middle row
if (squares[5] == 88) // X won
return 2;
else
return 3;
}
else if (squares[6] == squares[7] && squares[7] == squares[8] && squares[8] != 32)
{ // bottom row
if (squares[8] == 88) // X won
return 2;
else
return 3;
}

else if (squares[0] == squares[3] && squares[3] == squares[6] && squares[6] != 32)
{ // left column
if (squares[6] == 88) // X won
return 2;
else
return 3;
}
else if (squares[1] == squares[4] && squares[4] == squares[7] && squares[7] != 32)
{ // middle column
if (squares[7] == 88) // X won
return 2;
else
return 3;
}
else if (squares[2] == squares[5] && squares[5] == squares[8] && squares[8] != 32)
{ // right column
if (squares[8] == 88) // X won
return 2;
else
return 3;
}

else if (squares[0] == squares[4] && squares[4] == squares[8] && squares[8] != 32)
{ // top left to bottom right
if (squares[8] == 88) // X won
return 2;
else
return 3;
}
else if (squares[2] == squares[4] && squares[4] == squares[6] && squares[6] != 32)
{ // top right to bottom left
if (squares[6] == 88) // X won
return 2;
else
return 3;
}
else
return 0; // game not over
}
Work on some formatting and use code tags. I think you tried code tags but failed ;). Posting a formatted version for others' reference...

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>

// Prototype the functions
int CheckEndOfGame(int * squares);
int ChooseSquare(int * squares);
int MakeMoves(int * ctrMoves, int * playr, int * squares);

int main()
{   /* High Level - play one or more games:
    Players make moves until one wins or it's a draw.
    Announce the winner (or draw).
    Ask player to play again; play game or stop.
    */

// Declare variables
    int gameOver	= 0; // no, game is not over yet
    int playGame	= 1; // yes, play the game

    do // Play Game(s)
    {   // Store all choices in an array of 9 squares (squares[9])
// initialize the array with spaces (space is ASCII 32)
        int squares[9] = {32, 32, 32, 32, 32, 32, 32, 32, 32};
//	 ("X" is ASCII 88)
//	 ("O" is ASCII 79)
        int ctrMoves	= 0; // no moves yet
        int player	 = 0; // [[see "if" in MakeMoves]]

        do // Make one move
        {   gameOver = MakeMoves(&ctrMoves, &player, squares);
        }
        while (!gameOver);

// draw the board (before declaring a winner), then display
        printf("\n");
        printf(" %c | %c | %c \n", squares[6], squares[7], squares[8]);
        printf("-----------\n");
        printf(" %c | %c | %c \n", squares[3], squares[4], squares[5]);
        printf("-----------\n");
        printf(" %c | %c | %c \n", squares[0], squares[1], squares[2]);
        printf("\n");

        if (gameOver == 1)
            printf("The game ended in a draw.\n");
        else if (gameOver == 2)
            printf("Congratulations! You won!\n");
        else
            printf("The computer won.\n");

        printf("Play again? (1 is Yes, 0 is No). ");
        scanf("%d", &playGame);

    } while (playGame == 1);

    /*
    Wrapup
    "Thanks"
    clean up

    Other
    */
    return 0;
}

int MakeMoves(int * ctrMoves, int * playr, int * squares)
{   /* Middle Level - make one move, check for winner:
    When X takes a turn, draw the board, ask for chosen square, store it.
    When O takes a turn, program chooses square, store it if game not over.
    */

    int endTheGame	= 0;	 // no, game has not ended yet
    int thisSquare	= 0;
    int drawTheBoard = 0;

// get a choice (from person or program), then store it in the array
    if (*playr == 0)
    {   printf("\n");
        printf(" %c | %c | %c \n", squares[6], squares[7], squares[8]);
        printf("-----------\n");
        printf(" %c | %c | %c \n", squares[3], squares[4], squares[5]);
        printf("-----------\n");
        printf(" %c | %c | %c \n", squares[0], squares[1], squares[2]);
        printf("\n");

        *playr = 1;	 // it's now the person's turn

        printf("Choose a square (1 - 9). \n");
        scanf("%d", &thisSquare);

        squares[thisSquare - 1] = 88;
// subtract 1 from thisSquare to get the array subscript
// eg, center square, 5, is square[4]
    }
    else
    {   *playr = 0;	 // it's now the program's turn

        thisSquare = ChooseSquare(squares);
        if (thisSquare == 0)
            *ctrMoves = 9;	 // tie - no squares left; game over
        else
            squares[thisSquare - 1] = 79;
// subtract 1 from thisSquare to get the array subscript
// eg, center square, 5, is square[4]
    }

    *ctrMoves = *ctrMoves + 1;
    if (*ctrMoves > 9)
        endTheGame = 1;	 // tie - no squares left; game over
    else if (*ctrMoves >= 5)
        endTheGame = CheckForWinner(squares);

    return endTheGame;
}

int ChooseSquare(int * squares)
{   /* Low Level (1) - find the best empty square
    (eg, squares[4] == 32 means center square is open)
    */

    if (squares[4] == 32)
        return 5;
    else if (squares[0] == 32)
        return 1;
    else if (squares[2] == 32)
        return 3;
    else if (squares[6] == 32)
        return 7;
    else if (squares[8] == 32)
        return 9;
    else if (squares[1] == 32)
        return 2;
    else if (squares[3] == 32)
        return 4;
    else if (squares[5] == 32)
        return 6;
    else if (squares[7] == 32)
        return 8;
    else
        return 0;	 // tie - no squares left; game over
}

int CheckForWinner(int * squares)
{   /* Low Level (2) - check whether person won, program won, or draw
    winners: 1-2-3 | 4-5-6 | 7-8-9 || 1-4-7 | 2-5-8 | 3-6-9 || 1-5-9 | 3-5-7
    0 means game not over; 1 means draw; 2 means person won; 3 means program won
    */

    if (squares[0] == squares[1] && squares[1] == squares[2] && squares[2] != 32)
    {   // top row
        if (squares[2] == 88)	// X won
            return 2;
        else
            return 3;
    }
    else if (squares[3] == squares[4] && squares[4] == squares[5] && squares[5] != 32)
    {   // middle row
        if (squares[5] == 88)	// X won
            return 2;
        else
            return 3;
    }
    else if (squares[6] == squares[7] && squares[7] == squares[8] && squares[8] != 32)
    {   // bottom row
        if (squares[8] == 88)	// X won
            return 2;
        else
            return 3;
    }

    else if (squares[0] == squares[3] && squares[3] == squares[6] && squares[6] != 32)
    {   // left column
        if (squares[6] == 88)	// X won
            return 2;
        else
            return 3;
    }
    else if (squares[1] == squares[4] && squares[4] == squares[7] && squares[7] != 32)
    {   // middle column
        if (squares[7] == 88)	// X won
            return 2;
        else
            return 3;
    }
    else if (squares[2] == squares[5] && squares[5] == squares[8] && squares[8] != 32)
    {   // right column
        if (squares[8] == 88)	// X won
            return 2;
        else
            return 3;
    }

    else if (squares[0] == squares[4] && squares[4] == squares[8] && squares[8] != 32)
    {   // top left to bottom right
        if (squares[8] == 88)	// X won
            return 2;
        else
            return 3;
    }
    else if (squares[2] == squares[4] && squares[4] == squares[6] && squares[6] != 32)
    {   // top right to bottom left
        if (squares[6] == 88)	// X won
            return 2;
        else
            return 3;
    }
    else
        return	0;	// game not over
}
Alrighty...

1) This code doesn't compile! CheckForWinner() isn't declared to be used in the scope of MakeMoves().

2) Instead of placing a comment explain what the ASCII value is, just use the ASCII! Use 'X' instead of the value 88.

3) You hold the "cells" in an array. You can use that array and an algorithm that you create to effectively "scan" for what the opponent did and what to do next.

4). CheckForWinner and ChooseSquare have repetitive logic. They can be simplified greatly.

Unfortunately, I'm not going to give you an example as I feel I would be giving away the answer. As a hint though, I would look into rand() for generating a basic random number and the algorithm you'd have to come up with yourself (the fun part ;)).
Thank you for reposting so others can read it. I am still wracking my brain on how to do any of this. Im so new to programming of any kind. :(
Topic archived. No new replies allowed.