What Am I Doing rong

closed account (jyU4izwU)
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
208
209
210
211
212
213
214
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
 
void draw(char main[][75], int score);
void reset(char main[][75]);
void move(char main[][75], int &parts, int pastCounter, int past[][2], int &apples, int &score, int &quit);
void check (int &direction);
void directionn(int direction, int &pastCounter, int past[][2]);
void apple (int &apples, char main[][75]);
void quitGame (int score);
 
int main()
{   //past is an array to help direct when the snakes body will go, 
    //did it like this so the behind pieces will follow the snakes head
    int past[1000][2]; //second bracket: 0 is up, 1 is side
    int parts = 3; //number of snake pieces
    char main[23][75];   // 23 down, 73 wide is appropriate screen size
    int pastCounter = 6;  //used in the use of past 
    int direction = 0;
    int apples = 0;
    int score = 0;
    int quit = 0;
    int playAgain = 1;
    srand(time(0));
 
    //SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2);  -- color if wanted
 
    for (int x = 0; x < 1000; x ++) 
    {
        for (int y = 0; y < 2; y ++)
        {
            past [x][y] = 0;
        }
    }
    past[pastCounter][0] = 1; //keep the game from crashing because if you hit the wall it kicks you from the wall
    past[pastCounter][1] = 1;
    while(quit == 0)
    {  //main rotation through all the game's functions.
        draw(main, score);
        check(direction);
        directionn(direction, pastCounter, past);
        reset(main);
        move(main, parts, pastCounter, past, apples, score, quit);
        Sleep(25); //pause to keep the game a normal speed...
        if (apples == 0)
        {  //place more apples only if they are all gone
            apple(apples, main); 
        }
         
    }
    quitGame(score);
    Sleep (10000);
    //getch();
 
}
void draw(char main[][75], int score)
{//draws the screen
    system("cls");
    printf ("Score : %d\n", score);
    for (int x = 0; x < 23; x ++)
    {
        for (int y = 0; y < 75; y ++)
        {
            printf ("%c", main[x][y]);
        }
        printf ("\n");
    }
 
} 
 
void reset(char main[][75])
{//resets the array to keep it from being filled with parts of snake that shouldn't be there after the snake has moved on
    for (int x = 0; x < 23; x++)
        {
            for (int y = 0; y < 75; y++)
            {
                if (main[x][y] == '@')
                {  //doesn't reset the apples
                    main[x][y] == '@';
                }
                else
                {
                    if (x == 0 || x == 22 || y == 0 || y == 74)
                    {//doesnt reset the border
                        main[x][y] = 177;
                    }
                    else
                    {
                        main[x][y] = ' ';
                    }
                }
            }
        }
}
 
void move(char main[][75], int &parts, int pastCounter, int past[][2], int &apples, int &score, int &quit)
{//moves the snake head, and has the body follow it
    if (past[pastCounter][0] == 22 || past[pastCounter][0] == 0)
    {  //if te snake is on the border of the game you loose
        quit = 1;
    }
    if (past[pastCounter][1] == 74 || past[pastCounter][1] == 0)
    {
        quit = 1;
    }
 
    for (int x = 0; x < parts; x++)
    {
        if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == '@')
        {  //if the snake (technically any part of the snake) eats and apple...
            apples--;
            parts++;
            score += 10;
        }
        if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == 'o')
        {  //if the snake eats itself
            quit = 1;
        }
        else
        {  //else the snakes new location is determined
        main[past[pastCounter - x][0]][past[pastCounter - x][1]] = 'o';
        }
    }
}
void check (int &direction)
{//checks if there has been user input
    int key = 0;
    if (kbhit())
    {
        key = -getch();
        switch (key)
        {
        case -72:
            direction = 2;
            break;  //up
        case -77:
            direction = 0;
            break; //right
        case -80:
            direction = 3;
            break; //down
        case -75:
            direction = 1;
            break; //left
        }
    }
         
}
void directionn(int direction, int &pastCounter, int past[][2])
{//based on user input, detects which directing the snake head should be going
    int right;//adding to the array itself did not work, so i worked around it.
    int down;
    right = past[pastCounter][1];
    down = past[pastCounter][0];
    switch (direction)
    {  //directs the snake based on direction
    case 0:
        right ++;
        break;
    case 1:
        right --;
        break;
    case 2:
        down --;
        break;
    case 3:
        down ++;
    }
 
    pastCounter ++;
    past[pastCounter][0] = down;
    past[pastCounter][1] = right;
}
void apple (int &apples, char main[][75])
{//places apples on the screen
    int up = 0;
    int left = 0;
    apples = 3;
 
    for (int x = 0; x < apples; x ++)
    { //places appples, only if theres no border of part of the snake already in the space
        up = (rand() % 22);
        left = (rand() % 74);
 
        if (main[up][left] == 'o')
        {
            apple(apples, main);
        }
        if (main[up][left] == '*')
        {
            apple(apples, main);
        }
        else
        {
            main[up][left] = '@';
        }
    }
}
 
 
 
 
void quitGame (int score)
{//quits and displays score
    int quit = 0;
    system ("cls");
    printf ("GAME OVER!!!!\n\n");
    printf ("You got a score of %d\n", score);
 
 
}
Line 82. Of course it doesn't reset the apples. That statement doesn't do anything.
You want an assignment statement.
1
2
 
main[x][y] = '@';  // Note the single equal 


Line 88. main is an array of signed chars. Signed chars have values from -127 to +127. You're trying to assign a value that's too big (177).

BTW, it's poor practice to name a variable the same as your function (main).

Topic archived. No new replies allowed.