2d array output...is odd!

Jul 26, 2012 at 5:16pm
Hi guys, new member. I am in my second term of C++ programming and for one of my projects this term I gotta make a relatively basic tron game.not overly difficult except for a few things. but with that being said one thing is just driving me spider monkey crazy. whenever i run the game, and get into the game loop, my 2d array output appears to be shifting while in the game loop. IE instead of the screen refreshing in a vertical fashion and the 'bikes' staing in position, everything shifts to the right, wraps around the command prompt screen, until the array runs out of memory. Its extremly wierd. below is the code. any feedback sure would be appreciated! Oh, i have a lot of things commented out as a way to troubleshoot. note that most of the things commented out wont be implemented in the final product as I have found other ways to make the game work.

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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>
#include <windows.h>
#include <stdio.h>

using namespace std;
//void gameboard(int& px, int& py, int& cx, int& cy);
void rules ();
void game(int *px, int *py, int *cx, int *cy, bool gameOver,char (&gameboard)[25][80]);
//void game(int& px, int& py, int& cx, int& cy, char& moveN, char& moveS, char& moveE, char& moveW, bool& gameOver,char (&gameboard)[25][80]);
void move1(int *px, int *py, int *cx, int *cy);
//void move2(&movement2);
//void moveNorth(int *px);
void moveSouth(int *px);
void moveEast(int *py);
void moveWest(int *py);
void collision(int* px, int* py, int* cx, int* cy, bool* gameOver);
char gameboard[25][80];
int main(int argc, char *argv[])
{
   bool gameOver=false;
   char moveN='w';
   char moveS='s';
   char moveE='d';
   char moveW='a';
   int px=23;
   int py=79;
   int cx=23;
   int cy=0;
   char menuSelect;
   char playAgain='y';
//   const int column=80;
//   const int row=25;
//   char gameboard[row][column];
//char *pBoard=&gameboard[row][column];
   
   while (playAgain=='y' || playAgain=='Y')
   {
    cout<<"Welcome to Tron, a two player racing game. The object? Survive!."<<endl;
    cout<<"Press R for rules, P to play, or Q to quit"<<endl;
    cin>>menuSelect;
    switch (menuSelect)
           {
           case 'p':
           game(&px,&py,&cx,&cy,gameOver,gameboard);
           break;
           case 'R':
           rules ();
           break;
           case 'Q':
           cout<<"Thanks for playing"<<endl;
           break;
           case 'P':
           game(&px,&py,&cx,&cy,gameOver,gameboard);
           break;
           case 'r':
           rules ();
           break;
           case 'q':
           cout<<"Thanks for playing"<<endl;
           break;
           default:
           cout<<"You entered an invalid menu selection."<<endl;
           break;
           }
    cout<<"Would you like to play again?\n\n";
    cin>>playAgain;
    while (!(playAgain =='y' || playAgain =='Y' || playAgain =='n' || playAgain=='N'))
    {
     cout<<"You have entered an invalid selection\n";
     cout<<"Would you like to play again?\n";
     cin>>playAgain; 
    }      
  }                    
    system("PAUSE");
    return 0;
}



void game(int *px, int *py, int *cx, int *cy, bool gameOver,char (&gameboard)[25][80])
{
  for (int i = 0; i<25; i++)
    {
        for (int j = 0; j<80; j++)
        {
          gameboard[i][j]=' ';
        }
    } 
  cout<<"To begin moving, press the W key to move up, D to move right, A to move left, or S to move down\n\n";
  while(gameOver==false)
  
   {
    move1(px,py,cx,cy);
    for (int i = 0; i<25; i++)
    {
        for (int j = 0; j<80; j++)
        {
          cout<<gameboard[i][j];
          gameboard[*px][*py]='X';
          gameboard[*cx][*cy]='Y';
        }
    }  
    
    Sleep(350);
//    collision(px,py,cx,cy,gameOver);
   }
    
} 
     
     
     
  

void move1(int *px, int *py, int *cx, int *cy)
{
 
 if (kbhit())
 {
  switch (getche())
   {
    case 'w':
    *px=*px-1;
    break;
    case 'W':
    *px=*px-1;;
    break;
    case 'd':
    *py=*py+1;
    break;
    case 'D':
    *py=*py+1;
    break;
    case 's':
    *px=*px+1;
    break;
    case 'S':
    *px=*px+1;
    break;
    case 'a':
    *py=*py-1;
    break;
    case 'A':
    *py=*py-1;
    break;
    default:
    cout<<"You entered an invalid menu selection."<<endl;
    break;
   }
  }
}

//void move2(&movement2)
  //{
     //if (&movement2=='i')
    //   {
     
//} 

//void moveNorth(int *px)
//{
// *px=*px-1;
// 
//} 

//void moveSouth(int *px) 
//{
  
//   *px=*px+1;
  
//}     

//void moveEast(int *py)
//{
//  *py=*py+1;
   
//}   

//void moveWest(int *py)
//{
//  *py=*py-1;
  
//}   


void collision(int* px, int* py, int* cx, int* cy, bool* gameOver)
{
     if (*px>80 || *px<0)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     
     }
     
     if (*py>25|| *py<1)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     }
}     
void rules ()
{
 cout<<"Tron is a contest between you and the machine.\n";
 cout<<"The left line is controlled by the player 1, player 2 controls the right line.\n";
 cout<<"Use the keyboard arrow keys to turn left, right, up or down.\n";
 cout<<"The first player who crashes by touching another line with his own loses.\n";  
 cout<<"To move your bike w,a,s,and d moves the bike north,east,west and south for player one.\n";
 cout<<" for player two, the movement commands are I for north, j for west,e for east and m for south\n";
} 
Jul 26, 2012 at 9:07pm
disregard, problem solved. the issue for those that are interested revolves around the move1 function. needed to add a cout<<endl;. dont quite know why but it fixed it
Topic archived. No new replies allowed.